How to make Stack only build one executable? stack build :exe1 doesn't work? - haskell

I have multiple executables in one of my Stack projects:
executable m1
hs-source-dirs: src
main-is: Main1.hs
default-language: Haskell2010
build-depends: base >= 4.7 && < 5
executable m2
hs-source-dirs: src
main-is: Main2.hs
default-language: Haskell2010
build-depends: base >= 4.7 && < 5
I would like to build only one of them at a time. From the documentation and this other question, it would seem that the following command should work:
stack build :m2
but what actually happens is that both executables, m1 and m2, are always built regardless of whether I include the package name, the package type, etc.
What could be causing this?

It's not possible at the moment. See https://github.com/commercialhaskell/stack/issues/1406

Related

Exposing internal modules to tests in Cabal

I'm contributing to an open source project which defines its project setup in a cabal file that looks like this (omitting lots of properties that are not relevant for this problem):
library
hs-source-dirs: src
build-depends:
base >= 4.9 && < 5,
some-other-deps
exposed-modules:
Data.Foo.Bar,
Data.Foo.Baz
other-modules:
Data.Foo.Bar.Internal
test-suite test
hs-source-dirs:
tests
build-depends:
foo-library
other-modules:
Foo.Bar.Tests,
Foo.Baz.Tests
Now, in order to test the feature I'm adding, I want the tests to have access to the Data.Foo.Bar.Internal module, but since it's hidden in the library, I can't access it from the tests.
I looked at the cabal docs which suggested adding a third component - an internal library (unfortunately not linkable, but search for "internal libraries"). If I understand the documentation correctly, I should be able to do something like this:
library foo-internal
hs-source-dirs: src
build-depends:
base,
some-other-deps
exposed-modules:
Data.Foo.Bar.Internal
library
hs-source-dirs: src
build-depends:
base >= 4.9 && < 5,
foo-internal,
some-other-deps
exposed-modules:
Data.Foo.Bar,
Data.Foo.Baz
test-suite test
hs-source-dirs:
tests
build-depends:
foo-library,
foo-internal
other-modules:
Foo.Bar.Tests,
Foo.Baz.Tests
but running stack build here gives me warnings that Data.Foo.Bar.Internal "should be added to exposed-modules or other-modules in ./foo.cabal", and then a build error in the test that seems to be caused by failed unification (it points to a function argument for a function defined in the library, says that its type must be Data.Foo.Bar.Internal.Qux defined in package foo, and that the type foo:Data.Foo.Bar.Internal.Qux don't match).
How can I expose internal modules to the test suite, without exposing them also to consumers of the library?
As mentioned in the comments, different components (internal libraries, executables) should have modules under different roots.
This is a recurrent point of confusion. For another example: https://stackoverflow.com/a/6711739/6863749
There is an open ticket on Cabal's issue tracker about clarifying this with a suitable warning; it's apparently non-trivial to detect this situation reliably because it overlaps with legitimate use cases: https://github.com/haskell/cabal/issues/5335

C directory in Haskell .cabal file

I have this .cabal file:
...
main-is: Main.hs
other-extensions: ForeignFunctionInterface
build-depends: base >=4.9 && <4.10
hs-source-dirs: src/haskell
c-sources: src/c/main.c ...
default-language: Haskell2010
I have a lots of c source files, all in one directory(src/c), is there a way to add them all without specifying each one? something like a regex:
-- I know this won't work
c-sources: src/c/**
Or just including the whole directory:
-- I know this won't work
c-source-dirs: src/c
?
Try: c-sources: src/c/*.c
** wildcards were added in Cabal 2.4
Wildcard examples from here: https://www.haskell.org/cabal/users-guide/developing-packages.html?highlight=c%20source#pkg-field-data-files

Haskell stack not building test executable

Background
I'm building a logfile parser in Haskell. I'm using stack to build it. Running the stack build command works happily and my project compiles. Running stack test, however, produces the following error:
parser-test: executable not found
I see the the following warning above the error message but I don't know how to avoid the redirect to which it refers.
Warning: output was redirected with -o, but no output will be generated because there is no Main module.
Relevant files
I haven't written any tests yet so the test file is as it was created by stack new. My cabal file looks like this:
...
category: Executable
build-type: Simple
-- extra-source-files:
cabal-version: >=1.10
library
hs-source-dirs: src
exposed-modules: LogParser
build-depends: base >= 4.7 && < 5
, attoparsec
, bytestring
, old-locale
, time
default-language: Haskell2010
executable parser-exe
hs-source-dirs: app
main-is: Main.hs
ghc-options: -threaded -rtsopts -with-rtsopts=-N
build-depends: base
, attoparsec
, bytestring
, old-locale
, time
, parser
default-language: Haskell2010
test-suite parser-test
type: exitcode-stdio-1.0
hs-source-dirs: test
main-is: Spec.hs
build-depends: base
, attoparsec
, bytestring
, hspec
, hspec-attoparsec
, old-locale
, time
, parser
ghc-options: -threaded -rtsopts -with-rtsopts=-N
default-language: Haskell2010
source-repository head
type: git
...
I presume I'm missing something but I can't find where what I'm missing is documented.
Desired behaviour
I should see the Test suite not yet implemented message outlined in the stack documentation.
The content of the test/Spec.hs file from the template is:
main :: IO ()
main = putStrLn "Test suite not yet implemented"
You can see this content at commercialhaskell/stack-templates in new-template.hsfiles.
I'm not sure where the module ParserSpec where line comes from (as brought up in the Github issue), but it's not part of the template. On my system, stack new bar && cd bar && stack test succeeds.
As to the reason behind the Main module requirement: it comes from the Cabal library, and as I understand it was added due to limitations in other compilers Cabal supports. In other words, GHC could technically allow this code to compile, but Cabal does not pass in those arguments to remain compatible with other compilers.

Executable within a library and while using the same library in cabal

This is my first try at an open source project, but I still can't get the right way to set up my .cabal file.
I have have library, an executable and a (soon to come) test config.
I want to use the library and within the executable, so the both of them can be used when it gets downloaded.
I followed this guide, but I'm still struggling with the cabal-config as I only get it to work while importing everything again.
My current directory
- src/
- Main.hs
- Format/
- C.hs
- Converter.hs
- Raw.hs
- RGB565.hs
- tests/...
- dist/...
- UTFTConverter.cabal
The executable Main.hs header looks like this.
module Main where
import Format.C
import Format.Converter
The library files in Format/ look like this.
module Format.{filename} where
...
This is what the cabal file looks like.
name: UTFTConverter
...
cabal-version: >=1.10
library
exposed-modules: Format.C
, Format.Converter
, Format.Raw
, Format.RGB565
build-depends: base >=4.7 && <4.8
, filepath >=1.3 && <1.4
, directory >=1.2 && <1.3
, time >=1.4 && <1.5
, bytestring >=0.10 && <0.11
, JuicyPixels >=3.2 && <3.3
hs-source-dirs: src
...
executable UTFTConverter
main-is: Main.hs
build-depends: base >=4.7 && <4.8
, filepath >=1.3 && <1.4
, directory >=1.2 && <1.3
, time >=1.4 && <1.5
, bytestring >=0.10 && <0.11
, JuicyPixels >=3.2 && <3.3
--, UTFTConverter ==0.1 <-- this does not work
hs-source-dirs: src
...
test-suite tests:
...
Without the comment this error comes up when I cabal build.
...
cabal: At least the following dependencies are missing:
UTFTConverter ==0.1
...
It works right now, but in the tutorial the executable was using the library in the same cabal file.
executable bassbull
main-is: Main.hs
ghc-options: -rtsopts -O2
build-depends: base,
bassbull, -- <-- this is the name of the library
bytestring,
cassava
hs-source-dirs: src
default-language: Haskell2010
I know this is currently working, but I would rather use it the right way from the start. Is this the "right" way?
That's due to your library version being 0.1.0.0, not 0.1. They don't match up exactly, thus cabal doesn't recognize your library as a candidate. Instead, use 0.1.* or 0.1.0.0 depending on your version policy:
executable UTFTConverter
main-is: Main.hs
build-depends: base >=4.7 && <4.8
, filepath >=1.3 && <1.4
, directory >=1.2 && <1.3
, time >=1.4 && <1.5
, bytestring >=0.10 && <0.11
, JuicyPixels >=3.2 && <3.3
, UTFTConverter ==0.1.0.0
hs-source-dirs: src
References
Cabal User Guide, section "Build Information".

Can you have an executable and library in the same cabal package

I'm new using cabal and I need my package to include a small executable as well as the whole library.
Is it possible to do so (to export an executable AND a library) or does a cabal package have a type (executable OR binary).
Is it also possible to export many executable in the same packagke ?
Yes you can. For example, here is the corresponding excerpt of the HaskellStarter project, that I highly recommend:
-- configuration for building a library
library
hs-source-dirs: src
exposed-modules:
HaskellStarter.CommitPrinter
other-modules:
HaskellStarter.Util
build-depends:
base >= 4 && < 5,
github >= 0.7.4 && < 0.8
-- configuration for an executable
executable githubCommitPrinter
hs-source-dirs: executables
main-is: Main.hs
build-depends:
base >= 4 && < 5,
haskell-starter
Notice how the library is reused in the executable dependencies.

Resources