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

Luc*_*cas 4 haskell haskell-stack

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

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

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

==> app/Baz.hs <==
module Main where
Run Code Online (Sandbox Code Playgroud)

我用这个package.yaml看起来很相似(我)到了一个从链接官方文档的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
Run Code Online (Sandbox Code Playgroud)

但是当我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
Run Code Online (Sandbox Code Playgroud)

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

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

rad*_*row 7

我认为app不应将其视为所有最终应用程序的文件夹的约定。在我的项目中,我总是将这些拆分为foo/Main.hsbar/Main.hsfoobar目标。因此package.yaml应该包含

executables:
  foo:
    main:                Main.hs
    source-dirs:         foo
    dependencies:
    - myproject
  bar:
    main:                Main.hs
    source-dirs:         bar
    dependencies:
    - myproject
Run Code Online (Sandbox Code Playgroud)

并把myproject它们当作图书馆