如何将 "containers" 包添加到我的 .cabal 文件中(在编译时不会被堆栈覆盖)?

How do I add the "containers" package to my .cabal file (without getting overwritten by stack at compile time)?

我正在研究练习 Haskell 轨道上的“罗马数字”任务,并遵循了他们的 instructions to installing stack。我正在开发 Fedora 24 box。

只要我使用来自基础的 Haskell 模块,我就没有问题。现在我正在尝试导入 Data.Map 模块。使用 ghci 命令行可以正常工作:

$ ghci
GHCi, version 7.8.4: http://www.haskell.org/ghc/  :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Prelude> import Data.Map
Prelude Data.Map> 

但是,当我尝试使用以下命令从我的 src 文件中导入它时:

import qualified Data.Map as M (foldlWithKey, fromList)

当我尝试 运行 测试时,我 运行 遇到了问题:

$ stack test
roman-numerals-0.0.0: build (lib + test)
Preprocessing library roman-numerals-0.0.0...
[2 of 2] Compiling Roman            (...)
(...) /roman-numerals/src/Roman.hs:3:1: error:
    Failed to load interface for ‘Data.Map’
    It is a member of the hidden package ‘containers-0.5.7.1’.
    Perhaps you need to add ‘containers’ to the build-depends in your .cabal file.
    Use -v to see a list of the files searched for.
Progress: 1/2
(...)

我用谷歌搜索了这个问题并在 the Cabal FAQ at haskell.org 找到了一个简单的解决方案:

What you need to do is to add containers to the build-depends in your .cabal file.

我假设它们指的是我工作目录中的文件 roman-numerals.cabal。内容是:

-- This file has been generated from package.yaml by hpack version 0.14.0.
--
-- see: https://github.com/sol/hpack

name:           roman-numerals
version:        0.0.0
build-type:     Simple
cabal-version:  >= 1.10

library
  hs-source-dirs:
      src
  build-depends:
      base
  exposed-modules:
      Roman
  other-modules:
      Paths_roman_numerals
  default-language: Haskell2010

test-suite test
  type: exitcode-stdio-1.0
  main-is: Tests.hs
  hs-source-dirs:
      test
  build-depends:
      base
    , roman-numerals
    , hspec
  default-language: Haskell2010

我试图将“容器”添加到“库”和“测试套件”部分的构建依赖中,但是当我 运行

$ stack test

错误仍然存​​在,.cabal 文件恢复为上面显示的相同内容。

有什么指点吗?非常感谢!

这暗示了问题所在:

-- This file has been generated from package.yaml by hpack version 0.14.0.
--
-- see: https://github.com/sol/hpack

hpack 是 Haskell 包的另一种基于 YAML 的规范格式,可以用来代替传统的 cabal 格式。然后可以使用 hpack 程序将规范从 hpack 格式转换为 cabal 格式,以便能够与 Haskell 工具链的其余部分集成。

有些 basic support for hpack 是前一段时间添加到堆栈中的。它在当前目录中检查一个名为 package.yaml 的文件,这是 hpack 格式包规范的标准名称,如果存在,则运行 hpack 将其转换为 cabal 文件,然后继续构建像平常一样。这就是在践踏您的 .cabal 文件。

要解决这个问题,可以:

  • 修改package.yaml代替roman-numerals.cabal达到同样的效果
  • 删除 package.yaml 并继续直接使用 roman-numerals.cabal

hpack格式添加依赖的语法为:

dependencies:
  - base
  - containers