Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 318
Karl Avatar asked Jun 02 '14 18:06

Karl


People also ask

How does Cabal v2-build work with remote package repositories?

If the project contains multiple remote package repositories it will update the index of all of them (e.g. when using overlays). cabal v2-build takes a set of targets and builds them. It automatically handles building and installing any dependencies of these targets. A target can take any of the following forms:

Does Cabal treat executables as dependencies?

Fortunately, starting with Cabal 1.8.0.4, executables can also declare the package that they are in as a dependency, and Cabal will treat them as if they were in another package that depended on the library.

What is the library section for in Cabal?

The library section may also contain build information fields (see the section on build information ). Cabal 2.0 and later support “internal libraries”, which are extra named libraries (as opposed to the usual unnamed library section).

How do I define internal sub-library components in Cabal?

Starting with Cabal 2.0, private internal sub-library components can be defined by setting the name field to a name different from the current package’s name; see section on Internal Libraries for more information. The library section should contain the following fields: A list of modules added by this package.


1 Answers

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.

like image 123
bheklilr Avatar answered Sep 21 '22 01:09

bheklilr