如何从 cabal 文件中排除以避免破坏我的包的 GHC 错误?

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

因为 my package will not work with a known issue in GHC 9.0.1, if I want to exclude versions (of what; base?) that have the bug that causes the issue 来自我的 .cabal,我需要在那里指定什么?它与 GHC 的每个其他人一起工作到 7 岁左右。

编辑:就像我发布这个一样,我记得还有另一个技巧可以通过在 cabal 文件中写入来排除 GHC 版本:

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

这不会给出描述性很强的错误,但会阻止 GHC 9.0.1 构建您的包。


有一个 Haskell 维基页面关于哪些基本版本对应于哪些 GHC 版本:https://wiki.haskell.org/Base_package。特别是“9.0.1 (Feb 2021) 4.15.0.0”,因此您可以将 base <4.15.0.0 添加到您的 cabal 文件中以排除 GHC 9.0.1。但这不是万无一失的方法,因为基本版本并不总是随着 GHC 版本的变化而变化。当错误被修复时,它可能不一定伴随着新的 base 版本。

我认为没有办法在你的 cabal 文件中明确排除 GHC 版本,但这也是合理的,因为带有错误的 GHC 版本根本不应该用于编译任何包;用户不应期望 GHC 9.0.1 能够正确编译任何包。我认为库作者可以在文档中的某个地方简单地警告这个特定的错误,如果已知它会导致他们的包出现问题。

另一种选择是在包含错误的模块中使用 CPP 强制编译失败,以检查它是否使用特定 GHC 版本编译,如 , which links to this section in the GHC manual 中所述。

所以您可以在文件顶部添加类似这样的内容:

{-# 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

这至少会阻止任何人使用 GHC 9.0.1 构建该模块,并且会给出合理可读的错误消息:

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 #-}
  | ^