How do I exclude from cabal file to avoid GHC bug that breaks my package?

Viewed 60
1 Answers

Edit: Just as I posted this, I remembered that there is another trick to exclude GHC versions by writing this in a cabal file:

library
    ...
    -- This package does not work with GHC 9.0.1 due to a bug
    -- See: https://github.com/orome/crypto-enigma-hs/issues/35#issuecomment-865260187
    if impl(ghc == 9.0.1)
      buildable: False

This won't give a very descriptive error, but it will prevent GHC 9.0.1 from building your package.


There is a Haskell wiki page about which base versions correspond to which GHC versions: https://wiki.haskell.org/Base_package. Notably "9.0.1 (Feb 2021) 4.15.0.0", so you can add base <4.15.0.0 to your cabal file to exclude GHC 9.0.1. But this is not a foolproof method, because base versions do not always change when the GHC version changes. When the bug is fixed it might not necessarily be accompanied by a new base version.

I don't think there is a way to explicitly exclude GHC versions in your cabal file, but that is also kind of reasonable because versions of GHC with bugs in them should not be used to compile any package at all; users should not expect GHC 9.0.1 to compile any package correctly. I think it is acceptable for library authors to simply warn somewhere in the documentation about this specific bug if it is known to cause issues in their package.

Another option is to enforce compilation failure using CPP in the module containing the bug to check if it is compiled with a specific GHC version as explained in this stackoverflow question, which links to this section in the GHC manual.

So you could add something like this to the top of your file:

{-# LANGUAGE CPP, TemplateHaskell #-}

#if __GLASGOW_HASKELL__==900 && __GLASGOW_HASKELL_PATCHLEVEL1__==1
$(error "This library does not work with GHC 9.0.1 due to a bug\n\
  \      See: https://github.com/orome/crypto-enigma-hs/issues/35#issuecomment-865260187.")
#endif

That will at least prevent anyone from building that module with GHC 9.0.1 and it will give a reasonably readable error message:

CPPTest.hs:1:1: error:
    Exception when trying to run compile-time code:
      This library does not work with GHC 9.0.1 due to a bug
      See: https://github.com/orome/crypto-enigma-hs/issues/35#issuecomment-865260187
CallStack (from HasCallStack):
  error, called at CPPTest.hs:4:1 in main:Main
    Code: error
            "This library does not work with GHC 9.0.1 due to a bug\n      See: https://github.com/orome/crypto-enigma-hs/issues/35#issuecomment-865260187"
  |
1 | {-# LANGUAGE CPP, TemplateHaskell #-}
  | ^
Related