如何使用 stack/hpack 定义多个可执行文件/主模块

How to define multiple executables / Main modules with stack/hpack

我使用 stack and a package.yaml file for hpack 来编译我的 haskell 项目。它具有由一个库支持的三个可执行文件。正如人们所期望的那样,可执行文件都定义了一个 Main 模块:

$ head -n1 app/*
==> app/Foo.hs <==
module Main where

==> app/Bar.hs <==
module Main where

==> app/Baz.hs <==
module Main where

我使用这个 package.yaml,它(对我来说)看起来与 the one linked from the official docs for hpack(第三个)非常相似。

name: myproject
dependencies:
  - base
library:
  source-dirs: src
executables:
  foo:
    main: Foo.hs
    source-dirs: app
    dependencies: myproject
  bar:
    main: Bar.hs
    source-dirs: app
    dependencies: myproject
  baz:
    main: Baz.hs
    source-dirs: app
    dependencies: myproject

但是当我 stack build 我得到错误,模块名称不匹配 文件名:

Building all executables for `myproject' once. After a successful build of all of them, only specified executables will be rebuilt.
myproject-0.0.0: build (lib + exe)
Preprocessing library for myproject-0.0.0..
Building library for myproject-0.0.0..
[1 of 2] Compiling Lib              ( src/Lib.hs, .stack-work/dist/x86_64-linux-tinfo6/Cabal-2.2.0.1/build/Lib.o )
[2 of 2] Compiling Paths_myproject  ( .stack-work/dist/x86_64-linux-tinfo6/Cabal-2.2.0.1/build/autogen/Paths_myproject.hs, .stack-work/dist/x86_64-linux-tinfo6/Cabal-2.2.0.1/build/Paths_myproject.o )
Preprocessing executable 'bar' for myproject-0.0.0..
Building executable 'bar' for myproject-0.0.0..

/home/luc/test/app/Baz.hs:1:8: error:
    File name does not match module name:
    Saw: ‘Main’
    Expected: ‘Baz’
  |
1 | module Main where
  |        ^^^^


--  While building package myproject-0.0.0 using:
      /home/luc/.stack/setup-exe-cache/x86_64-linux-tinfo6/Cabal-simple_mPHDZzAJ_2.2.0.1_ghc-8.4.4 --builddir=.stack-work/dist/x86_64-linux-tinfo6/Cabal-2.2.0.1 build lib:myproject exe:bar exe:baz exe:foo --ghc-options " -ddump-hi -ddump-to-file -fdiagnostics-color=always"
    Process exited with code: ExitFailure 1

我的设置与我找到的示例之间的唯一区别是 app/ 中文件的大写字母。事实上,如果我将它们更改为小写(在文件系统和 package.yaml 中),它都会正确构建。

但为什么会这样?记录在哪里?

我认为 app 不应被视为所有最终应用程序的文件夹的约定。在我的项目中,对于 foobar 目标,我总是将它们拆分为 foo/Main.hsbar/Main.hs。因此 package.yaml 应包含

executables:
  foo:
    main:                Main.hs
    source-dirs:         foo
    dependencies:
    - myproject
  bar:
    main:                Main.hs
    source-dirs:         bar
    dependencies:
    - myproject

并将 myproject 视为他们的图书馆