在 Windows 上使用 Stack 编译测试框架时找不到模块 System.Console.MinTTY.Win32

Could not find module System.Console.MinTTY.Win32 when compiling test-framework with Stack on Windows

尝试在 Windows 上使用 test-framework 编译 Haskell 测试时出现错误。

重现步骤

使用 Stack 创建新库:

$ stack new repro simple-library

然后导航到 repro 目录并通过将 test-framework 添加到 build-depends 来编辑 repro.cabal 文件:

library
  hs-source-dirs:      src
  exposed-modules:     Lib
  build-depends:       base >= 4.7 && < 5,
                       test-framework
  default-language:    Haskell2010

现在尝试编译库:

$ stack build

预期结果

代码编译

实际结果

编译失败并显示此错误消息:

$ stack build
WARNING: Ignoring mintty's bounds on Win32 (>=2.13.1); using Win32-2.6.2.1.
Reason: trusting snapshot over cabal file dependency information.
mintty        > configure
mintty        > Configuring mintty-0.1.3...
mintty        > build
mintty        > Preprocessing library for mintty-0.1.3..
mintty        > Building library for mintty-0.1.3..
mintty        > [1 of 1] Compiling System.Console.MinTTY
mintty        >
mintty        > src\System\Console\MinTTY.hs:31:1: error:
mintty        >     Could not find module `System.Console.MinTTY.Win32'
mintty        >     Use -v (or `:set -v` in ghci) to see a list of the files searched for.
mintty        >    |
mintty        > 31 | import qualified System.Console.MinTTY.Win32 as Win32 (isMinTTY, isMinTTYHandle)
mintty        >    | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
mintty        >
Progress 1/5

--  While building package mintty-0.1.3 (scroll up to its section to see the error) using:
      C:\sr\setup-exe-cache\x86_64-windows\Cabal-simple_Z6RU0evB_3.2.1.0_ghc-8.10.7.exe --builddir=.stack-work\dist4b403a build --ghc-options " -fdiagnostics-color=always"
    Process exited with code: ExitFailure 1

如何解决此错误?

环境

$ stack --version
Version 2.7.3, Git revision 7927a3aec32e2b2e5e4fb5be76d0d50eddcc197f x86_64 hpack-0.34.4

Windows

Edition Windows 10 Pro
Version 21H1
Installed on    ‎14.‎09.‎2020
OS build    19043.1348
Experience  Windows Feature Experience Pack 120.2212.3920.0

我假设,鉴于您发布此问题时使用的是 LTS 18.17。查看那个 LTS,它使用 mintty 0.1.3. Looking in mintty 0.1.3's cabal file shows a special flag that is enabled by default that means that System.Console.MinTTY.Win32 is not included。该 cabal 文件中的注释指出,在使用 Win32 2.13.1.0 或更新版本时应使用该标志。

但是,当我在 Stackage 中查看 LTS 18 的配置时,我可以看到它正在使用 Win32 2.6.2.1,因此应该将该标志设置为 false 才能使该包正常工作。

所以让我们在 Stackage 构建约束中检查一下。我看到 another flag is being set, and it seems to be an old flag that is no longer used (looks like it was used in an older 0.1.2 version)。一定是这个问题。

解决方法:在你的 stack.yaml:

中手动设置标志
flags:
  mintty:
    Win32-2-13-1: false