Search code examples
haskellcabal

Cabal repl can't :load module in library


Hi I have the following modules in cabal file:

library task1-lib
    exposed-modules:
        Interpreter,
        Parser
    build-depends: 
        base ^>=4.14.3.0,
        containers,
        mtl,
        parsec
    ghc-options: -Wall -Werror -Wcompat -Widentities -Wincomplete-uni-patterns -Wincomplete-record-updates -Wno-unused-top-binds -Wno-orphans -Wno-type-defaults

    hs-source-dirs:   
        task1/lib
    default-language: Haskell2010

executable task1
    main-is:          Main.hs
    ghc-options: -Wall -Werror -Wcompat -Widentities -Wincomplete-uni-patterns -Wincomplete-record-updates -Wno-unused-top-binds -Wno-orphans -Wno-type-defaults

    build-depends:    
        base ^>=4.14.3.0
    hs-source-dirs:   task1
    default-language: Haskell2010

When I do:

cabal repl
...
GHCi, version 8.10.7: https://www.haskell.org/ghc/  :? for help
[1 of 2] Compiling Interpreter      ( task1/lib/Interpreter.hs, interpreted )
[2 of 2] Compiling Parser           ( task1/lib/Parser.hs, interpreted )
Ok, two modules loaded.
> :l Parser

I get the following error:

<no location info>: error: [-Wmissing-home-modules, -Werror=missing-home-modules]
    These modules are needed for compilation but not listed in your .cabal file's other-modules: 
        Interpreter

Question

What to do when I want to load a single file to experiment with the functions from that module in repl?

Also when I try the command cabal repl task1-lib the same happens.

EDIT:

There is a possible workaround to modify the cabal in the following way, by adding the -Wwarn=missing-home-modules after the -Werror. This still applies the -Werror for all warnings instead of the missing-home-modules error that remains a warning and makes it possible to :load a module in repl then.

library task1-lib
    exposed-modules:
        Interpreter,
        Parser
    build-depends: 
        base ^>=4.14.3.0,
        containers,
        mtl,
        parsec
    ghc-options: -Wall -Werror -Wwarn=missing-home-modules -Wcompat -Widentities -Wincomplete-uni-patterns -Wincomplete-record-updates -Wno-unused-top-binds -Wno-orphans -Wno-type-defaults

    hs-source-dirs:   
        task1/lib
    default-language: Haskell2010

executable task1
    main-is:          Main.hs
    ghc-options: -Wall -Werror -Wcompat -Widentities -Wincomplete-uni-patterns -Wincomplete-record-updates -Wno-unused-top-binds -Wno-orphans -Wno-type-defaults

    build-depends:    
        base ^>=4.14.3.0
    hs-source-dirs:   task1
    default-language: Haskell2010

Solution

  • import enables only functions that are exported by the module

    Instead of unsing :load or import, try using :module + *NameOfTheModule.

    According to the docs for :module:

    :module supports the * modifier on modules, which opens the full top-level scope of a module, rather than just its exports.

    This can't be done with ordinary import declarations, which behave much like imports in regular Haskell programs.