为什么 `stack build` 改变了我的 .cabal 文件?

Why is `stack build` altering my .cabal file?

我正在尝试构建一个使用 Euterpea.

的项目

运行 stack build 我收到以下错误,提示我需要将 Euterpea 添加到 .cabal 文件的 build-depends 部分。

$ sb
composition-0.1.0.0: build (lib + exe)
Preprocessing library composition-0.1.0.0...
[2 of 2] Compiling Lib              ( src/Lib.hs, .stack-work/dist/x86_64-linux-nix/Cabal-1.24.2.0/build/Lib.o )

/home/matthew/backup/composition/composition/src/Lib.hs:5:1: error:
    Failed to load interface for ‘Euterpea’
    It is a member of the hidden package ‘Euterpea-2.0.4’.
    Perhaps you need to add ‘Euterpea’ to the build-depends in your .cabal file.
    Use -v to see a list of the files searched for.

--  While building package composition-0.1.0.0 using:
      /home/matthew/.stack/setup-exe-cache/x86_64-linux-nix/Cabal-simple_mPHDZzAJ_1.24.2.0_ghc-8.0.2 --builddir=.stack-work/dist/x86_64-linux-nix/Cabal-1.24.2.0 build lib:composition exe:composition-exe --ghc-options " -ddump-hi -ddump-to-file"
    Process exited with code: ExitFailure 1

我在那里添加 Euterpea,然后我的 .cabal 文件的 library 部分如下。

library
  hs-source-dirs:
      src
  build-depends:   base >= 4.7 && < 5
                 , Euterpea
  exposed-modules:
      Lib
  other-modules:
      Paths_composition
  default-language: Haskell2010

然而,当我再次 运行 stack build 时,它给出了同样的错误 - 并将我的 .cabal 文件更改回原来的状态,其中 library 部分看起来像

library
  hs-source-dirs:
      src
  build-depends:
      base >= 4.7 && < 5
  exposed-modules:
      Lib
  other-modules:
      Paths_composition
  default-language: Haskell2010

为什么 stack build 改变了我的 cabal 文件?我以前从未见过这种情况。


旁注: 不确定是否相关,但 .cabal 文件的格式似乎与正常情况不同。与以前的项目一样,我通过 运行ning stack new <project-name> 自动初始化。我不知道我可能做了哪些与以前的项目不同的事情来导致 stack build.

的这种意外行为

确保 package.yaml 存在于项目目录的根目录中。

package.yaml是一种改进cabal语法的新文件格式,由hpack.

转换而来

Stack 对 hpack 的支持与 stack build 命令一样强大,可以使用 hpack 命令自动将 package.yaml 转换为 cabal 文件。

因此,删除 package.yaml 或编辑 package.yaml 以添加 Euterpea 包。 编辑它不会那么困难,因为它的格式是 YAML。

我想补充 YAMAMOTO Yuji's 答案。解决方案是绝对正确的。但我只是想添加一些东西,编辑 package.yaml.

并不难

第 1 步:最棘手的部分是找到正确的包名称

Use Hoogle or Stackage to find the package where the module resides. Read more about how to find package name in this .

步骤 2:现在您必须打开 package.yaml 文件并添加包名称。在您的情况下,在依赖项列表中添加 'Euterpea' 包。

dependencies:
...
- your-package-name

Please note that Euterpea package has to be added in a different way. Please read this post for better understanding.

第三步:在项目根目录下打开project-name.cabal,在build-depends:

下添加需要的包名
library
  hs-source-dirs:
      src

  build-depends:
      base >= 4.7 && < 5
    , your-package-name

  exposed-modules:
      Lib

第4步:发布stack build下载并构建依赖 (或者 stack ghci 如果你打算在 REPL 中使用它)

希望这有效!编码愉快! :)