2
votes

I am using Stack 9.0 as my Haskell build system for my project.

I've just noticed that when I compile my Haskell project with Stack, all assertions are switched off. By contrast while doing "normal" GHC builds at the command-line, assertions are turned on by default unless explicitly switched off with the -fignore-asserts flag (link).

For instance this simple main function fails to throw an assertion error

import Control.Exception.Base

main :: IO ()
main = assert (1==2) $ print "Hello World!"

How do I edit the .cabal file of my project to turn on assertions? Currently inside my .cabal file I see the following ghc options

 ghc-options: -threaded -rtsopts -with-rtsopts=-N

which is strange because assertions have not been explicitly turned off within this list.

EDIT:

I have Version 1.1.2 x86_64 hpack-0.14.0 after running stack --version. In particular, I am using stack lts 9.0 with ghc 8.0.2

1
Which version of stack do you have stack --version? And which ghc? - epsilonhalbe
@epsilonhalbe Please see edit. - smilingbuddha
At some point, cabal decides to add -O to the ghc --make command (I've confirmed this on my machine with stack 1.1.3 and cabal 1.24.2.0). As the docs state, this also causes assertions to be turned off. I'm not actually sure why - maybe somebody more knowledgeable will show up and explain it. You can get around it by adding -fno-ignore-asserts to ghc-options (or -O0 I'm guessing, but that probably would have undesired side-effects). - user2407038
@user2407038 Hmm I don't see any difference even after adding the -fno-ignore-asserts flag. How do you see the flags used to build a particular executable, so that I can check if assertions are being disabled? - smilingbuddha
Add -v (or -v2 or -v3) to the ghc-options field. But I've also verified this behaviour (that is, stack build, adding the -fno-ignore-asserts flag and then stack build) doesn't actually take affect. Even stack build --reconfigure --force-dirty doesn't work! I had to delete the .stack-work directory. (This one definitely looks like a bug, but keep in mind that stack-1.1.2 and 1.1.3 are very, very outdated) - user2407038

1 Answers

0
votes

Try passing in the --fast flag, which will disable optimizations (via --ghc-options -O0) and allow assertions to run.