sol / hpack

hpack: A modern format for Haskell packages
MIT License
624 stars 103 forks source link

Exclude modules from source-dir(s) #409

Closed andreasabel closed 4 years ago

andreasabel commented 4 years ago

Is there a way to exclude files from a source directory?

Something like:

source-dir:
  - LBNF
    exclude: 
    - Skel
    - Test

In my case, BNFC generates a bunch of files from a grammar file, e.g.

LBNF
├── Abs.hs
├── ErrM.hs
├── Lex.x
├── Par.y
├── Print.hs
├── Skel.hs
└── Test.hs

but I want to exclude the modules Skel and Test. The latter is even a main module, so I have to exclude it to not confuse cabal about a second Main.

sol commented 4 years ago

@andreasabel right now, your best bet is to do the following:

library:
  source-dirs: LBNF
  when:
    condition: false
    exposed-modules:
    - Skel
    - Test

With the next release the conditional will not even end up in the generated .cabal file, but it will work with older versions of hpack too.

We might want to add more explicit support for your use case, thinking, not sure yet.

sol commented 4 years ago

Actually, I like the idea of standardizing on the existing approach instead of adding more explicit support for it. Module inference is already complicated. On reason for it is that it tries to handle all the corner cases in a sane way (e.g. modules in conditionals, source directories in conditionals). So we might as well use the current mechanism for maximum profit instead of making it more complicated.

The current approach works because hpack will never infer modules that are mentioned explicitly (within the scope of one section, of course).

Related, if you want to explicitly add modules, that are not in any of your source-dirs, while still retaining module inference, right now this should work:

library:
  source-dirs: src
  when:
    condition: true
    exposed-modules: Foo

Effectively, conditionals that are always true or false can be used to add or remove items from list of inferred modules. @tfausak had the idea to flatten conditionals that are always true. If we standardize on this mechanism to augment the list of inferred modules, it could indeed be nice to do so.