Cabal 库无法导入暴露的模块

Cabal library cannot import exposed modules

我的 foo.cabal 文件如下所示:

library
  exposed-modules:
      Foo.Bar,
      Foo.Something
  hs-source-dirs:      lib
  default-language:    Haskell2010

executable foobar
  main-is:             Main.hs
  other-modules:
      Utils,  -- Local module in ./src
      Foo.Bar

  hs-source-dirs:      src
  default-language:    Haskell2010

根据这里的示例代码: https://www.haskell.org/cabal/users-guide/developing-packages.html#example-a-package-containing-a-library-and-executable-programs

我收到以下错误:

$ cabal build
Resolving dependencies...
Configuring foobar-0.1.0.0...
Preprocessing executable 'foobar' for foobar-0.1.0.0..
cabal: can't find source for Foo/Bar in src, dist/build/foobar/autogen,
dist/build/global-autogen

您必须将库添加到可执行文件的依赖项中。这是你的 cabal 文件的 hpack 版本,我永远记不起 cabal 语法:

name: foo
version: 0.1.0
dependencies:
  - base

source-dirs: lib

# no need for exponsed-modules, hpack does that for you

executable:
  main: Main.hs
  source-dirs: src
  dependencies:
    - base
    - foo

或者只使用 stack new,你会得到一个工作项目。

将行 build-depends: foo 添加到您的可执行文件节以指示可执行文件取决于您正在定义的库。