Stack: [Stack User Guide] Stack Build error when adding dependencies

Created on 9 Oct 2016  路  6Comments  路  Source: commercialhaskell/stack

I'm following the examples in the user guide to learn how Stack works. After adding dependencies (updating src/Lib.hs and cabal file) as suggested in the doc, I still get a build error.

Steps to reproduce

a. Modify src/Lib.hs as follows.

{-# LANGUAGE OverloadedStrings #-}
module Lib
    ( someFunc
    ) where

import qualified Data.Text.IO as T

someFunc :: IO ()
someFunc = T.putStrLn "someFunc"

b. Add text to build-depends in Cabal file as follows.

name:                helloworld
version:             0.1.0.0
synopsis:            Initial project template from stack
description:         Please see README.md
homepage:            https://github.com/githubuser/helloworld#readme
license:             BSD3
license-file:        LICENSE
author:              Author name here
maintainer:          [email protected]
copyright:           2016 Author name here
category:            Web
build-type:          Simple
-- extra-source-files:
cabal-version:       >=1.10

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

executable helloworld-exe
  hs-source-dirs:      app
  main-is:             Main.hs
  ghc-options:         -threaded -rtsopts -with-rtsopts=-N
  build-depends:       base
                     , helloworld
                     , text
  default-language:    Haskell2010

test-suite helloworld-test
  type:                exitcode-stdio-1.0
  hs-source-dirs:      test
  main-is:             Spec.hs
  build-depends:       base
                     , helloworld
  ghc-options:         -threaded -rtsopts -with-rtsopts=-N
  default-language:    Haskell2010

source-repository head
  type:     git
  location: https://github.com/githubuser/helloworld

c. Run command stack build

$ stack build
helloworld-0.1.0.0: configure
Configuring helloworld-0.1.0.0...
helloworld-0.1.0.0: build
Preprocessing library helloworld-0.1.0.0...
[1 of 1] Compiling Lib              ( src\Lib.hs, .stack-work\dist\b7fec021\build\Lib.o )

C:\Users\nelson.lin\Source\Haskell\helloworld\src\Lib.hs:6:1: error:
    Failed to load interface for `Data.Text.IO'
    It is a member of the hidden package `text-1.2.2.1'.
    Perhaps you need to add `text' to the build-depends in your .cabal file.
    Use -v to see a list of the files searched for.

--  While building package helloworld-0.1.0.0 using:
      C:\sr\setup-exe-cache\x86_64-windows\setup-Simple-Cabal-1.24.0.0-ghc-8.0.1.exe --builddir=.stack-work\dist\b7fec021 build lib:helloworld exe:helloworld-exe --ghc-options " -ddump-hi -ddump-to-file"
    Process exited with code: ExitFailure 1

d. Run command stack list-dependencies

array 0.5.1.1
base 4.9.0.0
binary 0.8.3.0
bytestring 0.10.8.1
containers 0.5.7.1
deepseq 1.4.2.0
ghc-prim 0.5.0.0
helloworld 0.1.0.0
integer-gmp 1.0.0.1
text 1.2.2.1

Expected

The command stack build should compile the src and linking the dependent packages without error.

Actual

The command stack build fails to load interface for Data.Text.IO even after the text package is included as a dependency.

The stack build verbose output can be found at Gist

Stack version

$ stack --version
Version 1.2.0, Git revision 123819b7d65df2ad7fe63fb5eb39a98536acb5f3 (4055 commits) x86_64 hpack-0.14.0

Method of installation

Windows 10 + Cygwin 2.6.0

awaiting pull request documentation output (UUX) help wanted discuss

Most helpful comment

Thanks for the extensive repro! The problem is that you're importing a module of the text package from Lib.hs, which is part of the library component of the package, while you have included the dependency on text in the executable component, _not_ the library component.

In the case of #2058 the user had already added the text dependency to the build-depends section of the library component but also added the import in Main.hs.

As you are at least the second person who has run into this confusing issue (I think pretty much every Haskell beginner does eventually) we should try to make this part of the guide more clear!

We could also consider parsing these hidden package errors and giving more clear recommendations to the users.

All 6 comments

Thanks for the extensive repro! The problem is that you're importing a module of the text package from Lib.hs, which is part of the library component of the package, while you have included the dependency on text in the executable component, _not_ the library component.

In the case of #2058 the user had already added the text dependency to the build-depends section of the library component but also added the import in Main.hs.

As you are at least the second person who has run into this confusing issue (I think pretty much every Haskell beginner does eventually) we should try to make this part of the guide more clear!

We could also consider parsing these hidden package errors and giving more clear recommendations to the users.

It's working now - thank you :-) As a newbie, I failed to notice that there were separate components like library, executable and test-suite in the cabal file each with its own dependency declaration. The user guide is actually quite clear in hindsight.

Agree, for the uninitiated, it would be nice if the tool gives a bit more info on where to look when getting this error.

I think the ideal next step here would be to change our recommendation text that says Perhaps you need to addtext' to the build-depends in your .cabal file.` Unfortunately, this is coming from ghc and as far as I know, ghc doesn't know about which cabal component it is compiling.

I don't think there is anything we can easily do here except perhaps document it more clearly? People are just going to have to learn this one. @pengo98 Feel free to open a PR adding some documentation to an appropriate place.

So what changes to stack.yaml file should be done to make it work in similar cases?

@ulfryk you must have dependency in build-depends of library section, if you want to use the same dependency in build-depends of executable, even if it is not used in library.

This message should be changed. I lost whole day by this error. :(

@morfinPL thank you very much :)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Toxaris picture Toxaris  路  4Comments

sjakobi picture sjakobi  路  3Comments

cybaj picture cybaj  路  3Comments

domenkozar picture domenkozar  路  3Comments

bitemyapp picture bitemyapp  路  3Comments