如何添加"容器"打包到我的.cabal文件(在编译时没有被堆栈覆盖)?

时间:2016-10-12 20:24:51

标签: haskell cabal haskell-stack

我正在研究"罗马数字"来自练习Haskell的任务跟踪并跟随他们的instructions to installing stack。我正在使用Fedora 24盒子。

只要我从基地使用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找到了一个简单的解决方案:

  

您需要做的是将容器添加到.cabal文件中的build-depends中。

我假设它们是指我工作目录中的文件roman-numeral.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

我试图添加"容器"构建 - 依赖于"库和#34;和"测试套件"部分,但当我运行

$ stack test

错误仍然存​​在,并且.cabal文件还原为上面显示的相同内容。

任何指针?非常感谢!

1 个答案:

答案 0 :(得分:10)

这暗示了这个问题:

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

hpack是一种替代的,基于YAML的Haskell包规范格式,可以用来代替传统的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