4
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?

Calvin Cheng
  • 35,640
  • 39
  • 116
  • 167

1 Answers1

4

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
ErikR
  • 51,541
  • 9
  • 73
  • 124
  • Absolutely. I have `cabal.sandbox.config` file present in the directory where I am running my `ghc-mod check` commands. But `ghc-mod` still does not know to check for available packages in my sandbox. – Calvin Cheng Jan 06 '15 at 06:21
  • My experimentation shows that you also need a .cabal file. – ErikR Jan 06 '15 at 06:22
  • How can I generate a `.cabal` file? – Calvin Cheng Jan 06 '15 at 06:23
  • And no, even with my `.cabal` file, `ghc-mod` still does not know where to look for my 3rd party packages in the cabal sandbox. – Calvin Cheng Jan 06 '15 at 06:26
  • Nope. Both `cabal repl` and `cabal run` cannot find the sandbox either... :-( – Calvin Cheng Jan 06 '15 at 06:32
  • But `cabal exec -- runghc hellowai.hs` works (i.e. able to find the sandbox) – Calvin Cheng Jan 06 '15 at 06:33
  • You can always create a symlink for cabal.sandbox.config that points to the real file. – ErikR Jan 06 '15 at 06:43
  • I am not sure I understood what you meant. My project source file `hellowai.hs` is in the same directory as my `cabal.sandbox.conf` file. – Calvin Cheng Jan 06 '15 at 06:45
  • You can also use `cabal sandbox init --path=...` to create a cabal.sandbox.config file which points to an existing .cabal-sandbox directory. – ErikR Jan 06 '15 at 06:45
  • I've updated my answer with a recipe which works for me. Perhaps you can use it to figure out what is different with your setup. – ErikR Jan 06 '15 at 07:00
  • Added an example of where the sandbox dir is _not_ directly above the current working directory and how to configure the cabal.sandbox.config file to point to it. – ErikR Jan 06 '15 at 07:14
  • `cabal` version `1.18.1.4`, `ghc-mod` version `5.2.1.2 compiled by GHC 7.8.3` with a valid project `.cabal` file containing the correct dependencies in `build-depends` solved the problem for me. Thank you for sharing your example! – Calvin Cheng Jan 06 '15 at 08:34