在默认 Haskell Stack 项目中构建多个可执行文件

Building multiple executables in the default Haskell Stack project

我使用默认 stack new 来设置一个项目,该项目将服务器和客户端作为单独的可执行文件。我以似乎正确的方式更改了 package.yaml 文件(截至 2020 年 4 月 21 日“There is no user guide”),并向我的 app 目录添加了一个名为 Client.hs 的新文件.

我收到一条错误消息 "Enabling workaround for Main module 'Main' listed in 'other-modules' illegally!"

如何同时构建客户端和服务器的堆栈?

当我 运行 stack build 我得到:

[... clip ...]
Building executable 'ObjectServer' for ObjectServer-0.1.0.1..
[4 of 4] Compiling Client
Linking .stack-work\distcc6475\build\ObjectServer\ObjectServer.exe ...
Warning: Enabling workaround for Main module 'Main' listed in 'other-modules'
illegally!
Preprocessing executable 'Client' for ObjectServer-0.1.0.1..
Building executable 'Client' for ObjectServer-0.1.0.1..
[3 of 3] Compiling Client

<no location info>: error:
    output was redirected with -o, but no output will be generated
because there is no Main module.


--  While building package ObjectServer-0.1.0.1 using:
      D:\HaskellStack\setup-exe-cache\x86_64-windows\Cabal-simple_Z6RU0evB_3.0.1.0_ghc-8.8.3.exe --builddir=.stack-work\distcc6475 build lib:ObjectServer exe:Client exe:ObjectServer --ghc-options " -fdiagnostics-color=always"
    Process exited with code: ExitFailure 1

package.yaml 的相关部分如下所示:

executables:
  ObjectServer:
    main:                Main.hs
    source-dirs:         app
    ghc-options:
    - -threaded
    - -rtsopts
    - -with-rtsopts=-N
    dependencies:
    - ObjectServer
  Client:
    main:                Client.hs
    source-dirs:         app
    ghc-options:
    - -threaded
    - -rtsopts
    - -with-rtsopts=-N
    dependencies:
    - ObjectServer

这里有两个问题。首先,hpackother-modules的默认值是"all modules in source-dirs except main and modules mentioned in a when clause"。如果查看生成的 .cabal 文件,您会发现由于此默认设置,每个可执行文件都错误地将其他可执行文件的模块包含在其 other-modules 列表中。其次,main 设置给出包含主模块的源文件,但不会将 GHC 期望的模块名称从 Main 更改为其他任何名称。因此,那个 模块 仍然需要命名为 module Main where ...,而不是 module Client where...,除非你也单独添加一个 -main-is Client GHC 选项。

所以,我建议修改 Client.hs 使其成为 Main 模块:

-- in Client.hs
module Main where
...

然后为两个可执行文件显式指定 other-modules: []

executables:
  ObjectServer:
    main:                Main.hs
    other-modules:       []
    source-dirs:         app
    ghc-options:
    - -threaded
    - -rtsopts
    - -with-rtsopts=-N
    dependencies:
    - ObjectServer
  Client:
    main:                Client.hs
    other-modules:       []
    source-dirs:         app
    ghc-options:
    - -threaded
    - -rtsopts
    - -with-rtsopts=-N
    dependencies:
    - ObjectServer

这似乎在我的测试中有效。