Mark Karpov
Mark Karpov

Reputation: 7599

GHCi cannot find modules of my program

I'm working on a project and I'm using Cabal for management. I've specified directory of source files, modules, all the stuff. All my files have the same names as their corresponding modules, case is preserved.

I can do:

$ cabal configure
$ cabal build

without problems.

However, imagine I have a module Module in file Module.hs, and file File.hs in the same directory. Now, when I'm trying to load File.hs from Emacs for testing, I get the following:

____Could not find module ‘Module’
    It is a member of the hidden package ‘ghc-7.8.3’.
    Use -v to see a list of the files searched for.
Failed, modules loaded: none.

Full contents of File.hs:

module File where
import Module

How to make it find files of my project?

Upvotes: 8

Views: 19929

Answers (3)

Julian
Julian

Reputation: 4666

I had the same problem, fixed it, and decided to write about my troubleshooting. This might help new people learning Haskell. Read on.

I was playing around with this example code.

http://zvon.org/other/haskell/Outputdirectory/getCurrentDirectory_f.html

Code:

import Directory 

main = aaa "/tmp/FOO"

aaa ddd = do createDirectory ddd
        setCurrentDirectory ddd
        d <- getCurrentDirectory
        print d
        writeFile "aaa" "HELLO"
        l <- getDirectoryContents d
        print l

I noticed that they are using this package.

https://hackage.haskell.org/package/directory-1.3.6.2/docs/System-Directory.html

So I installed it with this commands:

cabal update
cabal install directory

Compiling the example code with ghc failed with this error message.

    Could not find module `Directory'
    Use -v to see a list of the files searched for.
  |
4 | import Directory
  | ^^^^^^^^^^^^^^^^

I was stuck for a while until I changed the import line to this:

import System.Directory

After this change ghc could compile successfully.

Conclussion: are you sure you are importing properly?

Upvotes: 0

vivian
vivian

Reputation: 1434

You need to tell GHCi where to find your source files. For example, if your project directory is ./foo and you have your source files in ./foo/src you need to say (from your project directory):

:set -isrc

at the command prompt in GHCi. You will then have access to private members in your sourc file loaded with C-c C-l.

You also need to make sure that you haven't cabal installed your package, otherwise the package will be loaded, not the project source files.

Upvotes: 6

James Davies
James Davies

Reputation: 9839

You can launch the REPL via Cabal like so:

# cabal repl

This is the same as running ghci, but will take into account any additional dependencies installed by cabal install your local or sandbox package repository.

Upvotes: 9

Related Questions