简体   繁体   中英

cabal fails to build simple project

I have a simple existing Haskell project that compiles fine with ghc:

$ ghc src/*.hs
[1 of 3] Compiling Address          ( src/Address.hs, src/Address.o )
[2 of 3] Compiling Person           ( src/Person.hs, src/Person.o )
[3 of 3] Compiling Main             ( src/Main.hs, src/Main.o )
Linking src/Main ...

I try to set up a cabal build but can't seem to succeed

$ cabal init --source-dir=src --main-is=src/Main.hs
$ cabal build
Resolving dependencies...
...<omitted for brevity>...
src/Main.hs:1:1: error:
    Could not find module ‘Person’
    Use -v (or `:set -v` in ghci) to see a list of the files searched for.
  |
1 | import Person
  | ^^^^^^^^^^^^^

src/Main.hs:2:1: error:
    Could not find module ‘Address’
    Use -v (or `:set -v` in ghci) to see a list of the files searched for.
  |
2 | import Address
  | ^^^^^^^^^^^^^^

What am I missing?

Your cabal file is the problem. From your description it sounds like you have a file with the lines:

    main-is:          src/Main.hs
    hs-source-dirs:   .

I don't know how this manifests into the issue you post (and I have reproduced) but the normal form is to use the directory field to list the directory and the main-is field for just the file name:

    main-is:          Main.hs
    hs-source-dirs:   src

This works. Also, both these are not conforming to cabal norms. Consider placing the modules under src and the executable Main under a directory named app or my-projects-binary-name if you want to look a little more like a directory structure that many devs have in mind.

It seems that using --application-dir=src instead of --source-dir=src works:

$ cabal init --main-is=Main.hs --application-dir=src
  • Not sure if this is a bug or just confusing flag names (?)
  • Anyway, it looks like no manual editing of the auto-generated cabal file is needed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM