Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding Dependency to stack Project?

Tags:

haskell

After running:

stack new my-project
cd my-project
stack setup
stack build

I would like to add the Conduit library as a dependency.

I edited the generated, via stack new, stack.yaml to have:

extra-deps: 
- conduit-1.2.10

Then, I modified the my-project.cabal from:

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

to:

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

When I try to stack build the following:

$cat app/Main.hs

module Main where

import Conduit
import Lib

main :: IO ()
main = someFunc

It fails:

$stack build
mtl-2.2.1: using precompiled package
primitive-0.6.1.0: using precompiled package
stm-2.4.4.1: using precompiled package
transformers-compat-0.5.1.4: using precompiled package
exceptions-0.8.3: using precompiled package
mmorph-1.0.9: using precompiled package
transformers-base-0.4.4: using precompiled package
monad-control-1.0.1.0: using precompiled package
lifted-base-0.2.3.10: using precompiled package
resourcet-1.1.9: using precompiled package
conduit-1.2.10: configure
conduit-1.2.10: build
conduit-1.2.10: copy/register
my-project-0.1.0.0: configure (lib + exe)
Configuring my-project-0.1.0.0...
my-project-0.1.0.0: build (lib + exe)
Preprocessing library my-project-0.1.0.0...
[1 of 1] Compiling Lib              ( src/Lib.hs, .stack-work/dist/x86_64-osx/Cabal-1.24.2.0/build/Lib.o )
Preprocessing executable 'my-project-exe' for my-project-0.1.0.0...
[1 of 1] Compiling Main             ( app/Main.hs, .stack-work/dist/x86_64-osx/Cabal-1.24.2.0/build/my-project-exe/my-project-exe-tmp/Main.o )

/Users/kevinmeredith/Workspace/conduit_sandbox/my-project/app/Main.hs:3:1: error:
    Failed to load interface for ‘Conduit’
    Use -v to see a list of the files searched for.
Completed 12 action(s).

--  While building package my-project-0.1.0.0 using:
      /Users/kevinmeredith/.stack/setup-exe-cache/x86_64-osx/Cabal-simple_mPHDZzAJ_1.24.2.0_ghc-8.0.2 --builddir=.stack-work/dist/x86_64-osx/Cabal-1.24.2.0 build lib:my-project exe:my-project-exe --ghc-options " -ddump-hi -ddump-to-file"
    Process exited with code: ExitFailure 1

How can I properly add conduit?

When adding a library to a stack project, do I need to edit both the stack.yaml and/or my-project.cabal?

like image 910
Kevin Meredith Avatar asked May 27 '17 16:05

Kevin Meredith


People also ask

Where does stack install packages?

Stack installs the Stackage libraries in ~/. stack and any project libraries or extra dependencies in a . stack-work directory within each project's directory.

Should I use cabal or stack?

AFAIU stack is handy to quickly work on an existing project. If you start something from scratch, you will certainly have to use cabal.

Where does stack install GHC?

You can find these sandboxed GHC installations in the ghc-* directories in the stack path --programs directory. If you would like Stack to use your system GHC installation, use the --system-ghc flag or run stack config set system-ghc --global true to make Stack check your PATH for a suitable GHC by default.

What is stack Ghci?

stack ghci allows you to load components and files of your project into ghci . It uses the same TARGET syntax as stack build , and can also take options like --test , --bench , and --flag . Similarly to stack build , the default is to load up ghci with all libraries and executables in the project.


1 Answers

If you look at the haddocks for conduit, notice the module you want to import is not Conduit, it is Data.Conduit.

The Conduit module comes from the conduit-combinators package. If that is the package you would like to use instead, adjust your cabal file as follows and import Conduit as before:

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

The differences between the packages are summarized below (this is taken from the project's readme).

  • conduit-combinators: provides a large number of common functions built-in
  • conduit: defines the core datatypes and primitive functions
  • conduit-extra: adds support for many common low-level operations

Side note: You don't need to make any changes to your stack.yaml file as both of these packages are available on stackage.

like image 70
Erik Avatar answered Oct 27 '22 20:10

Erik