尽管 stack ghci 有效,但由于缺少包而导致堆栈构建失败

Stack build fails due to missing package although stack ghci works

我正在尝试使用堆栈在 Haskell 中构建一个简单的程序。我使用 stack new 创建了一个新项目,然后做了一个 stack setup。模板构建良好。

我想尝试二进制文件解析,所以我导入了 Data.ByteString。我的 build-depends 在 cabal 文件中看起来像这样:

build-depends:     base >= 4.7 && < 5
                 , bytestring >= 0.10.6
                 , binary >= 0.7.5

stack ghci 现在可以了,但是 stack build 还是不开心。 有人可以告诉我我在这里做错了什么吗?

完整的错误信息如下:

test-0.1.0.0: build
Preprocessing library test-0.1.0.0...
In-place registering test-0.1.0.0...
Preprocessing executable 'test-exe' for test-0.1.0.0...

haskell/test/app/Main.hs:4:18:
    Could not find module ‘Data.ByteString’
    It is a member of the hidden package ‘bytestring-0.10.6.0@bytes_6VWy06pWzJq9evDvK2d4w6’.
    Perhaps you need to add ‘bytestring’ to the build-depends in your .cabal file.
    Use -v to see a list of the files searched for.

haskell/test/app/Main.hs:5:8:
    Could not find module ‘Data.Binary.Get’
    It is a member of the hidden package ‘binary-0.7.5.0@binar_3uXFWMoAGBg0xKP9MHKRwi’.
    Perhaps you need to add ‘binary’ to the build-depends in your .cabal file.
    Use -v to see a list of the files searched for.

--  While building package test-0.1.0.0 using:
      .stack/setup-exe-cache/x86_64-osx/setup-Simple-Cabal-1.22.5.0-ghc-7.10.3 --builddir=.stack-work/dist/x86_64-osx/Cabal-1.22.5.0 build lib:test exe:test-exe --ghc-options " -ddump-hi -ddump-to-file"
    Process exited with code: ExitFailure 1

这是我的 app/Main.hs 文件:

module Main where

import Lib
import qualified Data.ByteString as B
import Data.Binary.Get
import Data.Word

main :: IO ()
main =  do
    putStrLn "f"

非常感谢您的帮助。

这可能是因为您将 bytestring 添加到库的 build-depends,而不是可执行文件。避免需要为不同的节重复这些依赖关系的一种选择是使用 hpack 作为包描述格式。