Search code examples
haskellbuildmodulehierarchycabal

Flexibility of the hierarchy of module sources allowed in cabal project


I have a project with source tree:

src/
src/A/
src/A/A.hs
src/B/
src/B/C/
src/B/C/C.hs
...

The two haskell files divide source code into modules:

-- File A/A.hs
module A where
...

and

-- File B/C/C.hs
module B.C where
...

The cabal file contains:

other-modules: A, B.C, ...
hs-source-dirs: src/, src/A/, src/B/, src/B/C/, ...

But while the module A can be easily found, cabal complains about B.C:

cabal: can't find source for B/C in ...

I see no rational explanation why placing a file defining module A under A/A.hs is OK but placing B.C under B/C/C.hs isn't. Is there a workaround other than placing C.hs directly under B (I would like to maintain some separation of sources)?


Solution

  • The reason for the error is that module B.C should be defined in file B/C.hs, not B/C/C.hs (that would be module B.C.C). This error would have appeared if you had only one source dir with one source file, it is not because of the extra parts you have put in.

    Also, the dir that appears in the hs-source-dirs directive should only be the root of the dir tree, so it is doubtful that you need all of the parts that you put in, for instance, src/B/C (which would treat src/B/C as another root.... meaning you can define top level modules in that dir. If you are actually doing that, I would consider this a mistake).

    What you probably want to do is define multiple top level source dirs, like this

    A_src/A.hs
    B_src/B/C.hs
    
    hs-source-dirs: A_src, B_src
    

    Even better, I would suggest you use stack, which allows you to separate different modules completely with their own source dirs, called src, and independent .cabal files, allowing for richer dependencies between each module.