GHC cannot find module in cabal sandbox
Asked Answered
D

1

5

I am using Haskell version 7.8.4 on (X)Ubuntu 15.10, with Cabal-Install 1.18 both installed through apt. I have not attempted to install anything Haskell-related by hand on this machine. I set up a cabal sandbox, fetched and installed a module only to discover that ghc doesn't seem to be picking it up. ghc -v seems to suggest that I have two versions of the cabal library that are mutually shadowing each other. How does this work?

I am starting out with an empty directory /tmp/haskell-example

then I do a cabal sandbox init.

$ cabal sandbox init
Writing a default package environment file to
/tmp/haskell-example/cabal.sandbox.config
Creating a new sandbox at /tmp/haskell-example/.cabal-sandbox

then I install natural-numbers because I want to use the Data.Natural module in a program. This operation succeeds.

$ cabal install natural-numbers
Resolving dependencies...
Notice: installing into a sandbox located at
/tmp/haskell-example/.cabal-sandbox
Configuring natural-numbers-0.1.2.0...
Building natural-numbers-0.1.2.0...
Installed natural-numbers-0.1.2.0

I can verify that the Data.Natural module has indeed been installed to the cabal sandbox.

$ ls /tmp/haskell-example/.cabal-sandbox/lib/x86_64-linux-ghc-7.8.4/natural-numbers-0.1.2.0
 Data
libHSnatural-numbers-0.1.2.0.a
libHSnatural-numbers-0.1.2.0-ghc7.8.4.so
$ ls /tmp/haskell-example/.cabal-sandbox/lib/x86_64-linux-ghc-7.8.4/natural-numbers-0.1.2.0/Data
Natural.dyn_hi
Natural.hi

then I create a simple Main.hs file that imports Data.Natural but does not use it.

module Main where

import Data.Natural

main = putStrLn "Hello World"

When I try to ghc Main.hs I see the following :

$ ghc Main.hs 
Main.hs:3:8:
    Could not find module ‘Data.Natural’
    Use -v to see a list of the files searched for.

With the verbose flag enabled, it seems as if my cabal is getting shadowed by a later cabal, which is in turn shadowing the earlier cabal. Why is this happening?

$ ghc -v Main.hs 
Glasgow Haskell Compiler, Version 7.8.4, stage 2 booted by GHC version 7.8.4
Using binary package database: /usr/lib/ghc/package.conf.d/package.cache
hiding package Cabal-1.18.1.5 to avoid conflict with later version Cabal-1.22.1.1
wired-in package ghc-prim mapped to ghc-prim-0.3.1.0-ec14d2f6075975a2ce9112eae431c8e1
wired-in package integer-gmp mapped to integer-gmp-0.5.1.0-de4898ebdc5ab81cedce89121ae9ac84
wired-in package base mapped to base-4.7.0.2-5ef1e7e809bc3b18d74efc783356e209
wired-in package rts mapped to builtin_rts
wired-in package template-haskell mapped to template-haskell-2.9.0.0-c1976a420ad8b9b589eee08844c59ba2
wired-in package dph-seq not found.
wired-in package dph-par not found.
Hsc static flags: 
hiding package Cabal-1.18.1.5 to avoid conflict with later version Cabal-1.22.1.1
wired-in package ghc-prim mapped to ghc-prim-0.3.1.0-ec14d2f6075975a2ce9112eae431c8e1
wired-in package integer-gmp mapped to integer-gmp-0.5.1.0-de4898ebdc5ab81cedce89121ae9ac84
wired-in package base mapped to base-4.7.0.2-5ef1e7e809bc3b18d74efc783356e209
wired-in package rts mapped to builtin_rts
wired-in package template-haskell mapped to template-haskell-2.9.0.0-c1976a420ad8b9b589eee08844c59ba2
wired-in package dph-seq not found.
wired-in package dph-par not found.
*** Chasing dependencies:
Chasing modules from: *Main.hs

Main.hs:3:8:
    Could not find module ‘Data.Natural’
    Locations searched:
    Data/Natural.hs
    Data/Natural.lhs
*** Deleting temp files:
Deleting: 
*** Deleting temp dirs:
Deleting: 
Dagon answered 7/12, 2015 at 7:32 Comment(3)
did you update your .cabal file?Mastersinger
@Sigrlami I'm afraid I don't understand. I have a directory called $HOME/.cabal and /tmp/haskell-example/.cabal-sandbox, but no .cabal file. What would I need to do to update it?Dagon
When you're using command cabal init inside your project is creating foo.cabal file which describes all needed dependencies, and you need to add them manually. Assuming your project name foo.For example, github.com/sigrlami/pollock/blob/master/pollock.cabalMastersinger
P
7

If you're into manual hacking, you can pass to ghc the sandbox's pkg db location, such as:

ghc Main.hs -package-db .cabal-sandbox/x86_64-linux-ghc-7.10.2-packages.conf.d/

However the "normal" way to use sandboxes is to always compile using cabal build (or cabal install with no parameters) rather than running ghc directly.

  1. Run cabal init, answer questions as you please
  2. Edit the resulting foo.cabal file (foo is the name of your package).
  3. Run cabal build or cabal install - this will run ghc for you.

When editing the cabal file, check that your exported modules (if it's a lib) are listed and that your main src is correct. Also make sure dependencies such as natural-numbers are listed in the build-depends: clause.

Padishah answered 7/12, 2015 at 8:57 Comment(1)
Wait a second. I think I was thinking about cabal completely the wrong way. I was expecting a cabal sandbox to behave like a virtualenv or npm environment and alter behavior of ghc, but it seems like the only similarity is that it redirects where cabal install installs things. This makes so much sense now.Dagon

© 2022 - 2024 — McMap. All rights reserved.