GHC 在 cabal 沙箱中找不到模块

GHC cannot find module in cabal sandbox

我在 (X)Ubuntu 15.10 上使用 Haskell 版本 7.8.4,同时通过 apt 安装了 Cabal-Install 1.18。我没有尝试在这台机器上手动安装任何与 Haskell 相关的东西。我设置了一个 cabal 沙箱,获取并安装了一个模块,结果发现 ghc 似乎没有接收到它。 ghc -v 似乎暗示我有两个版本的 cabal 库,它们相互影响。这是如何工作的?

我从一个空目录开始 /tmp/haskell-example

然后我做一个cabal sandbox init

$ cabal sandbox init
Writing a default package environment file to
/tmp/haskell-example/cabal.sandbox.config
Creating a new sandbox at /tmp/haskell-example/.cabal-sandbox

然后我安装 natural-numbers 因为我想在程序中使用 Data.Natural 模块。此操作成功。

$ cabal install natural-numbers
Resolving dependencies...
Notice: installing into a sandbox located at
/tmp/haskell-example/.cabal-sandbox
Configuring natural-numbers-0.1.2.0...
Building natural-numbers-0.1.2.0...
Installed natural-numbers-0.1.2.0

我可以验证 Data.Natural 模块确实已经安装到 cabal 沙箱中。

$ ls /tmp/haskell-example/.cabal-sandbox/lib/x86_64-linux-ghc-7.8.4/natural-numbers-0.1.2.0
 Data
libHSnatural-numbers-0.1.2.0.a
libHSnatural-numbers-0.1.2.0-ghc7.8.4.so
$ ls /tmp/haskell-example/.cabal-sandbox/lib/x86_64-linux-ghc-7.8.4/natural-numbers-0.1.2.0/Data
Natural.dyn_hi
Natural.hi

然后我创建一个简单的 Main.hs 文件导入 Data.Natural 但不使用它。

module Main where

import Data.Natural

main = putStrLn "Hello World"

当我尝试 ghc Main.hs 时,我看到以下内容:

$ ghc Main.hs 
Main.hs:3:8:
    Could not find module ‘Data.Natural’
    Use -v to see a list of the files searched for.

启用详细标志后,似乎我的 cabal 被后来的 cabal 遮蔽了,而后者又反过来遮蔽了早期的 cabal。为什么会这样?

$ ghc -v Main.hs 
Glasgow Haskell Compiler, Version 7.8.4, stage 2 booted by GHC version 7.8.4
Using binary package database: /usr/lib/ghc/package.conf.d/package.cache
hiding package Cabal-1.18.1.5 to avoid conflict with later version Cabal-1.22.1.1
wired-in package ghc-prim mapped to ghc-prim-0.3.1.0-ec14d2f6075975a2ce9112eae431c8e1
wired-in package integer-gmp mapped to integer-gmp-0.5.1.0-de4898ebdc5ab81cedce89121ae9ac84
wired-in package base mapped to base-4.7.0.2-5ef1e7e809bc3b18d74efc783356e209
wired-in package rts mapped to builtin_rts
wired-in package template-haskell mapped to template-haskell-2.9.0.0-c1976a420ad8b9b589eee08844c59ba2
wired-in package dph-seq not found.
wired-in package dph-par not found.
Hsc static flags: 
hiding package Cabal-1.18.1.5 to avoid conflict with later version Cabal-1.22.1.1
wired-in package ghc-prim mapped to ghc-prim-0.3.1.0-ec14d2f6075975a2ce9112eae431c8e1
wired-in package integer-gmp mapped to integer-gmp-0.5.1.0-de4898ebdc5ab81cedce89121ae9ac84
wired-in package base mapped to base-4.7.0.2-5ef1e7e809bc3b18d74efc783356e209
wired-in package rts mapped to builtin_rts
wired-in package template-haskell mapped to template-haskell-2.9.0.0-c1976a420ad8b9b589eee08844c59ba2
wired-in package dph-seq not found.
wired-in package dph-par not found.
*** Chasing dependencies:
Chasing modules from: *Main.hs

Main.hs:3:8:
    Could not find module ‘Data.Natural’
    Locations searched:
    Data/Natural.hs
    Data/Natural.lhs
*** Deleting temp files:
Deleting: 
*** Deleting temp dirs:
Deleting: 

如果您喜欢手动破解,可以将沙箱的 pkg 数据库位置传递给 ghc,例如:

ghc Main.hs -package-db .cabal-sandbox/x86_64-linux-ghc-7.10.2-packages.conf.d/

然而,"normal" 使用沙箱的方法是始终使用 cabal build(或 cabal install 不带参数)进行编译,而不是直接 运行ning ghc。

  1. 运行cabal init,请回答问题
  2. 编辑生成的 foo.cabal 文件(foo 是您的包的名称)。
  3. 运行 cabal buildcabal install - 这将为您 运行 ghc。

编辑 cabal 文件时,请检查您导出的模块(如果它是一个 lib)是否已列出,并且您的主 src 是否正确。还要确保 natural-numbers 等依赖项列在 build-depends: 子句中。