Search code examples
haskellhaskell-stack

How do I disable `-Werror` only when compiling as a library?


I have

ghc-options:
- -Wall
- -Werror

in my package.yaml and it builds fine for GHC 8.6.

But when using the project in a GHC 9 codebase, it errors because of an unnessessary MonadFail import.

How can I change the library such that it won't abort compilation when used in other projects?

I have tried

ghc-options:
  "$everything": -Wwarn

in the downstream (dependent) project, but that doesn't seem to affect it. I expected -Wwarn to override the -Werror since $everything should cover even dependencies.


Solution

  • I believe, it is bad practice to specify -Werror on the library itself. Same goes for other compiler flags, such as optimization for example -O2. Setting -Wall on the other hand is definitely good stuff, plus a few other warning flags of top of my head, eg. -Wincomplete-record-updates, -Wincomplete-uni-patterns, -Wredundant-constraints, etc.

    If you want to turn build warnings into errors while working on the library or in CI, which is a sensible thing to do, then you can enable it in your

    • stack.yaml:
    ghc-options:
      my-library: -Werror
    
    • cabal.project:
    package my-library
      ghc-options: -Werror
    

    That being said you can turn off -Werror for any library downstream by setting -Wwarn for that library in the exact same fashion as above, which will override the original flag.