Search code examples
haskellghc-mod

ghc-mod does not use my cabal sandbox. why?


runghc -package-db=.cabal-sandbox/.cabal-sandbox/x86_64-osx-ghc-7.8.3-packages.conf.d hellowai.hs

Works perfect for me.

Similarly, with

ghci -package-db=.cabal-sandbox/.cabal-sandbox/x86_64-osx-ghc-7.8.3-packages.conf.d 

I am also able to import my cabal-sandbox-installed Wai package in ghci with no issue at all.

But when I ask ghc-mod to validate my haskell source code, via

ghc-mod check --boundary="" -g -package-db=.cabal-sandbox/x86_64-osx-ghc-7.8.3-packages.conf.d hellowai.hs
hellowai.hs:4:8:Could not find module ‘Network.Wai.Handler.Warp’Use -v to see a list of the files searched for.
hellowai.hs:3:8:Could not find module ‘Network.HTTP.Types’Perhaps you meant  Network.HTTP.Base (from HTTP-4000.2.19)  Network.HTTP.Base (needs flag -package HTTP-4000.2.10)  Network.HTTP.Headers (needs flag -package HTTP-4000.2.10)Use -v to see a list of the files searched for.
hellowai.hs:2:8:Could not find module ‘Network.Wai’Perhaps you meant  Network.BSD (needs flag -package network-2.4.2.3)  Network.URI (needs flag -package network-2.4.2.3)  Network.TCP (needs flag -package HTTP-4000.2.10)Use -v to see a list of the files searched for.

It is unable to find my cabal sandbox installed module. Why is that so?


Solution

  • Do you have a cabal.sandbox.config file? And are you using a .cabal file for your project?

    If you have both of these you should be able to use ghc-mod check ... and it will just work.

    Another advantage of using a .cabal file is that you can use cabal repl to invoke ghci and cabal run to invoke runhaskell with the correct command line options.

    Update

    Here is a recipe you can try out to see when ghc-mod can find your cabal sandbox. Perhaps this can help you determine what's different with your set up.

    Start in a clean directory:

    $ mkdir foo
    $ cd foo
    $ cabal sandbox init
    $ cabal get split
    $ cd split-0.2.2
    $ cabal sandbox init --sandbox=../.cabal-sandbox
    

    Edit around line 55 of split.cabal to add heredoc as a dependency.

    Edit src/Data/List/Split.hs to use the module Text.Heredoc:

    {-# LANGUAGE QuasiQuotes #-}
    ...
    import Text.Heredoc
    ...
    foo :: String
    foo = [here|this is a test|]
    

    Make sure heredoc is installed:

    $ cabal install --only-dependencies
    

    Finally this should work:

    $ ghc-mod check ./src/Data/List/Split.hs
    

    And it will still work if you cd into a sub-directory:

    $ cd src
    $ ghc-mod check ./Data/List/Split.hs
    

    However, ghc-mod won't work if you move away split.cabal:

    (back at the top level directory)
    $ mv split.cabal split.cabal-old
    $ ghc-mod check ./src/Data/List/Split.hs
    

    In this case I created the sandbox in a parent directory of our working directory, but things should also work if the initial sandbox was created like this:

    $ mkdir foo
    $ cd foo
    $ mkdir sandbox-dir
    $ cd sandbox-dir
    $ cabal sandbox init
    $ cd ..
    $ cabal get split
    $ cd split-0.2.2
    $ cabal sandbox init --sandbox=../sandbox-dir/.cabal-sandbox