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

时间:2019-01-11 16:45:00

标签: haskell haskell-stack

我使用stackhpackpackage.yaml文件来编译我的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(第三个)链接的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中),则它们都可以正确构建。

但是为什么如此?在哪里记录?

1 个答案:

答案 0 :(得分:2)

我认为app不应被视为所有最终应用程序的文件夹。在我的项目中,我始终将这些目标分为foo/Main.hsbar/Main.hs目标的foobar。因此,package.yaml应该包含

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

并将myproject视为它们的图书馆