Search code examples
haskellghccabalghcicabal-install

Different behavior of cabal repl for library vs. executable


Using cabal repl seems to do nothing at all when used on library projects, but works fine for executable projects. Is this expected behavior that I just don't understand?

If I have a file containing simply

go = putStrLn "test"

and use cabal init with all the defaults (but choose "library" as the type), then running cabal repl just produces the some text about configuring and preprocessing the library and never enters a REPL environment. The exact same steps, but with "executable" selected as the type, puts me right into GHCi as expected.

The code works fine when loaded directly into GHCi.


Solution

  • For cabal repl to load your modules, you have to first name them in code and then specify them in your project's .cabal file as exposed:


    -- MyModule.hs
    module MyModule where
    
    go = putStrLn "test"
    

    -- MyProject.cabal
    name: MyProject
    -- other info ...
    
    library
        exposed-modules: MyModule
        -- other options ...
    

    Then when you run cabal repl, it'll have access to everything in your sandbox (if present) and the exposed modules. It might also work if you specify them as other-modules instead of exposed-modules, but I haven't tried that one out.