Adding Dependency to stack Project? - 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?

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.

Related

Stack auto-removes dependency from .cabal file following 'stack build'

I want to work on a new Haskell project using the threepenny-gui package.
The first thing I did was create a stack project via $ stack new threepennydemo. From here, here, I did the following:
I edited the extra-deps section in my stack.yaml file from:
# extra-deps: []
to
extra-deps:
- threepenny-gui-0.9.0.0
I edited the .cabal file from:
library
exposed-modules:
Lib
other-modules:
Paths_threepennydemo
hs-source-dirs:
src
build-depends:
base >=4.7 && <5
default-language: Haskell2010
executable threepennydemo-exe
main-is: Main.hs
other-modules:
Paths_threepennydemo
hs-source-dirs:
app
ghc-options: -threaded -rtsopts -with-rtsopts=-N
build-depends:
base >=4.7 && <5
, threepennydemo
default-language: Haskell2010
test-suite threepennydemo-test
type: exitcode-stdio-1.0
main-is: Spec.hs
other-modules:
Paths_threepennydemo
hs-source-dirs:
test
ghc-options: -threaded -rtsopts -with-rtsopts=-N
build-depends:
base >=4.7 && <5
, threepennydemo
default-language: Haskell2010
to
library
exposed-modules:
Lib
other-modules:
Paths_threepennydemo
hs-source-dirs:
src
build-depends:
base >=4.7 && <5
, threepenny-gui >= 0.9
default-language: Haskell2010
executable threepennydemo-exe
main-is: Main.hs
other-modules:
Paths_threepennydemo
hs-source-dirs:
app
ghc-options: -threaded -rtsopts -with-rtsopts=-N
build-depends:
base >=4.7 && <5
, threepennydemo
, threepenny-gui >= 0.9
default-language: Haskell2010
test-suite threepennydemo-test
type: exitcode-stdio-1.0
main-is: Spec.hs
other-modules:
Paths_threepennydemo
hs-source-dirs:
test
ghc-options: -threaded -rtsopts -with-rtsopts=-N
build-depends:
base >=4.7 && <5
, threepennydemo
, threepenny-gui >= 0.9
default-language: Haskell2010
I edited the stock /app/Main.hs from:
module Main where
import Lib
main :: IO ()
main = someFunc
to
import Graphics.UI.Threepenny
main :: IO ()
main = do
startGUI defaultConfig showMessage
showMessage :: Window -> UI ()
showMessage window = do
getBody window #+ [string "Hello, world!"]
return ()
I enter the command $ stack build.
From here, I notice two things. The first is that I receive the following error:
Building all executables for `threepennydemo' once. After a successful build of all of them, only specified executables will be rebuilt.
threepennydemo> configure (lib + exe)
Configuring threepennydemo-0.1.0.0...
threepennydemo> build (lib + exe)
Preprocessing library for threepennydemo-0.1.0.0..
Building library for threepennydemo-0.1.0.0..
Preprocessing executable 'threepennydemo-exe' for threepennydemo-0.1.0.0..
Building executable 'threepennydemo-exe' for threepennydemo-0.1.0.0..
[1 of 2] Compiling Main
/Users/my_username/threepennydemo/app/Main.hs:1:1: error:
Could not load module ‘Graphics.UI.Threepenny’
It is a member of the hidden package ‘threepenny-gui-0.9.0.0’.
Perhaps you need to add ‘threepenny-gui’ to the build-depends in your .cabal file.
Use -v (or `:set -v` in ghci) to see a list of the files searched for.
|
1 | import Graphics.UI.Threepenny
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
-- While building package threepennydemo-0.1.0.0 using:
/Users/my_username/.stack/setup-exe-cache/x86_64-osx/Cabal-simple_mPHDZzAJ_3.0.1.0_ghc-8.8.4 --builddir=.stack-work/dist/x86_64-osx/Cabal-3.0.1.0 build lib:threepennydemo exe:threepennydemo-exe --ghc-options " -fdiagnostics-color=always"
Process exited with code: ExitFailure 1
The second is that my .cabal file, edited as mentioned in 2., has automatically removed the edits I made to the file.
What am I missing in order to be able to use a third party library when setting up a new stack project?
My difficulty as the result of some confusion between stack.yaml and package.yaml. The latter being what generates the threepennydemo.cabal file. Hence, dependencies must also be specified to a package.yaml file. Adding this dependency to the package.yaml allowed $ stack build to complete without issue.

Problem loading module Data.Number.CReal in cabal file

I installed a simple proyect with stack, my stack.yaml looks like:
resolver: lts-14.6
packages:
- .
dependencies:
- base (>=3 && <5)
- numbers
and I have a simple code that only calls a function:
module Main where
import Lib
import Data.Number.CReal
main :: IO ()
main = someFunc
and the error is always the same:
numbers-play-0.1.0.0: unregistering (local file changes: numbers-play.cabal)
numbers-play> configure (lib + exe)
Configuring numbers-play-0.1.0.0...
numbers-play> build (lib + exe)
Preprocessing library for numbers-play-0.1.0.0..
Building library for numbers-play-0.1.0.0..
[1 of 2] Compiling Lib
[2 of 2] Compiling Paths_numbers_play
Preprocessing executable 'numbers-play-exe' for numbers-play-0.1.0.0..
Building executable 'numbers-play-exe' for numbers-play-0.1.0.0..
[1 of 2] Compiling Main [Data.Number.CReal changed]
/home/damian/dev/haskell/numbers-play/app/Main.hs:4:1: error:
Could not load module ‘Data.Number.CReal’
It is a member of the hidden package ‘numbers-3000.2.0.2’.
Perhaps you need to add ‘numbers’ to the build-depends in your .cabal file.
Use -v to see a list of the files searched for.
|
4 | import Data.Number.CReal
| ^^^^^^^^^^^^^^^^^^^^^^^^
My cabal file:
cabal-version: 1.12
-- This file has been generated from package.yaml by hpack version 0.31.2.
--
-- see: https://github.com/sol/hpack
--
-- hash: fb0ed9e8eb8062639f1d6a02a65d857d15b3265158925242287d4a8a885f8381
name: numbers-play
version: 0.1.0.0
description: Please see the README on GitHub at <https://github.com/githubuser/numbers-play#readme>
homepage: https://github.com/githubuser/numbers-play#readme
bug-reports: https://github.com/githubuser/numbers-play/issues
author: Author name here
maintainer: example#example.com
copyright: 2019 Author name here
license: BSD3
license-file: LICENSE
build-type: Simple
extra-source-files:
README.md
ChangeLog.md
source-repository head
type: git
location: https://github.com/githubuser/numbers-play
library
exposed-modules:
Lib
other-modules:
Paths_numbers_play
hs-source-dirs:
src
build-depends:
base >=4.7 && <5
default-language: Haskell2010
executable numbers-play-exe
main-is: Main.hs
other-modules:
Paths_numbers_play
hs-source-dirs:
app
ghc-options: -threaded -rtsopts -with-rtsopts=-N
build-depends:
base >=4.7 && <5
, numbers-play
default-language: Haskell2010
test-suite numbers-play-test
type: exitcode-stdio-1.0
main-is: Spec.hs
other-modules:
Paths_numbers_play
hs-source-dirs:
test
ghc-options: -threaded -rtsopts -with-rtsopts=-N
build-depends:
base >=4.7 && <5
, numbers-play
default-language: Haskell2010
How can I import it properly?
The dependencies field, which specifies the numbers dependency, should be in your package.yaml file (which Stack uses to generate the cabal file), and not in stack.yaml (which is for Stack-specific configuration, such as choosing the resolver).

Haskell stack ignores -Wall -Werror cabal ghc-options flags when building

I would like to always use the "-Wall -Werror" options when building with stack (executing stack build) but adding these flags to ghc-options in package.yaml does nothing. I would also like to avoid passing the --pedantic flag to stack build. Here's the config files:
package.yaml
...
executables:
XYZ-exe:
main: Main.hs
source-dirs: app
ghc-options:
- -Wall
- -Werror
- -threaded
- -rtsopts
- -with-rtsopts=-N
dependencies:
- XYZ
...
XYZ.cabal
...
executable XYZ-exe
main-is: Main.hs
hs-source-dirs:
app
ghc-options: -Wall -Werror -threaded -rtsopts -with-rtsopts=-N
...
The "-Wall -Werror" flags are specified in ghc-options but as-if ignored when building. This is the output for stack build:
stack build
Building all executables for `XYZ' once. After a successful build of all of
them, only specified executables will be rebuilt.
XYZ-0.1.0.0: configure (lib + exe)
Configuring XYZ-0.1.0.0...
XYZ-0.1.0.0: build (lib + exe)
Preprocessing library for XYZ-0.1.0.0..
Building library for XYZ-0.1.0.0..
[ 1 of 105] Compiling Data.List.Extras ( src\Data\List\Extras.hs, .stack-
work\dist\e626a42b\build\Data\List\Extras.o )
... the rest is omitted, all succeed ...
And here's the output for stack build --pedantic
stack build --pedantic
Building all executables for `HStat' once. After a successful build of all of them, only specified executables will be rebuilt.
HStat-0.1.0.0: configure (lib + exe)
Configuring HStat-0.1.0.0...
HStat-0.1.0.0: build (lib + exe)
Preprocessing library for HStat-0.1.0.0..
Building library for HStat-0.1.0.0..
[ 1 of 105] Compiling Data.List.Extras ( src\Data\List\Extras.hs, .stack-work\dist\e626a42b\build\Data\List\Extras.o )
src\Data\List\Extras.hs:4:1: error: [-Wunused-imports, -Werror=unused-imports]
The import of ‘Data.Maybe’ is redundant
except perhaps to import instances from ‘Data.Maybe’
To import instances alone, use: import Data.Maybe()
|
4 | import Data.Maybe
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
This works as expected - src\Data\List\Extras.hs indeed does have an unused Data.Maybe import. What am I doing wrong?
The ghc-options flags had to be separately defined in the library part of package.yaml:
library:
source-dirs: src
ghc-options:
- -Wall
- -Werror
- -fwarn-incomplete-uni-patterns
Doing that solved the issue.

Two executables in one cabal file; stack build does not recognize them

I'm trying to make 2 executables "project". All duplicates of this question did not help me - their answers don't fix my problem. I have .cabal file like this:
name: int-tests
version: 0.1.0.0
synopsis: Integration Tests Suite
description: Integration Tests Suite
license: AllRightsReserved
author: Author name here
maintainer: example#example.com
copyright: 2018 Author name here
build-type: Custom
extra-source-files: README.md
cabal-version: >=1.10
library
hs-source-dirs: common
exposed-modules: Common
build-depends: base
, text
, aeson
, network-uri
default-language: Haskell2010
ghc-options: -Wall -Werror
executable api-tests-exe
hs-source-dirs: api
main-is: Main.hs
ghc-options: -threaded -rtsopts -with-rtsopts=-N -Wall -Werror
build-depends: base
, hspec
, QuickCheck
default-language: Haskell2010
executable e2e-tests-exe
hs-source-dirs: e2e
main-is: Main.hs
ghc-options: -threaded -rtsopts -with-rtsopts=-N -Wall -Werror
build-depends: base
, hspec
, QuickCheck
default-language: Haskell2010
and when I call stack ide targets I don't see these 2 targets. So, stack build api-tests and stack build e2e-tests don't work too.
How can I create 2 targets' project for stack? I tried also package.yaml but result is the same. Stack version is 1.9.1. I have folders tree like:
api/
...
e2e/
...
where are Main.hs files with content like:
module Main (main) where
main :: IO ()
main = print "Hello"
Also I tried option -main-is Main but without success.
Error looks like:
Error: While constructing the build plan, the following exceptions were encountered:
Unknown package: api-tests
AFAIK, stack build always builds all your targets. But if you want to be run just one executable, you'll need the full name including the -exe. So, stack exec api-tests-exe and stack exec e2e-tests-exe.
But what you really want to do is make these test targets: https://www.haskell.org/cabal/users-guide/developing-packages.html#test-suites
Problem was in stack.yaml file, I had to add '.' folder to "packages:" section.

Cabal fails to build detailed-0.9 test suite

I'm trying to build a package with a single test suite:
name: test
version: 0.1.0.0
build-type: Simple
cabal-version: >=1.10
library
-- exposed-modules:
build-depends: base
hs-source-dirs: src
default-language: Haskell2010
Test-Suite tests
type: detailed-0.9
test-module: Tests
build-depends: base, Cabal
hs-source-dirs: tests
default-language: Haskell2010
Here's what happens:
PS C:\temporary\test> cabal configure --enable-tests
Resolving dependencies...
Configuring test-0.1.0.0...
PS C:\temporary\test> cabal build
Building test-0.1.0.0...
Preprocessing library test-0.1.0.0...
In-place registering test-0.1.0.0...
Preprocessing test suite 'tests' for test-0.1.0.0...
[1 of 1] Compiling Tests ( tests\Tests.hs, dist\build\Tests.o )
C:\Program Files\Haskell Platform\2014.2.0.0\mingw\bin\ar.exe: dist\build\Tests.o: No such file or directory
PS C:\temporary\test>
I'm using Windows 8.1 64-bit, with Haskell Platform 2014.2.0.0. I have installed MSYS, and the only MinGW on the path is GHC's. Also, all git binaries are after MSYS and MinGW on the path so they do not conflict. Any advice is appreciated.
Update: It appears that building tests works fine when using type: exitcode-stdio-1.0.

Resources