Why does stack not pass my ghc-options to the comp

2019-05-11 20:35发布

问题:

In the cabal file I specify the GHC options -Wall and -O2:

name:               Test
version:            0.1.0.0
build-type:         Simple
cabal-version:      >=1.8

executable Test
  hs-source-dirs:   src
  main-is:          Test.hs
  build-depends:    base >=4.8 && <4.10
  ghc-options:      -Wall -O2

When I compile the program Test.hs:

data Color = Red | Green | Blue

foo :: Color -> Int
foo Red = 0
foo Green = 1
-- foo Blue is intentionally missing!!

I get the error:

Preprocessing executable 'Test' for Test-0.1.0.0...
[1 of 1] Compiling Main             ( src/Test.hs, .stack-work/dist/x86_64-linux-nopie/Cabal-1.24.2.0/build/Test/Test-tmp/Main.o )

/home/user/Projekte/HaskellTutorials/Test/src/Test.hs:1:1: error:
    The IO action ‘main’ is not defined in module ‘Main’

--  While building package Test-0.1.0.0 using:
      /home/user/.stack/setup-exe-cache/x86_64-linux-nopie/Cabal-simple_mPHDZzAJ_1.24.2.0_ghc-8.0.2 --build
      dir=.stack-work/dist/x86_64-linux-nopie/Cabal-1.24.2.0 build exe:Test --ghc-options " -ddump-hi -ddump-to-file"
    Process exited with code: ExitFailure 1

The error about the missing main action is not the problem. Look at the text at the end of the second last line:

build exe:Test --ghc-options " -ddump-hi -ddump-to-file"

Why do I not see my GHC options -Wall -O2? (I fear, I did some stupid little mistake ...)

PS:
stack version is: Version 1.5.1, Git revision 600c1f01435a10d127938709556c1682ecfd694e (4861 commits) x86_64 hpack-0.17.1
LTS: 8.17

回答1:

The options -ddump-hi -ddump-to-file are being passed by Stack to Cabal. Cabal then adds them to the options specified in the .cabal file before passing them along to GHC.

If you run Stack with:

stack -v --cabal-verbose

and search through the output, you'll see that your options are in fact being passed to GHC.

As @epsilonhalbe noted, GHC isn't complaining about the more minor issues when it discovers that main is missing, so if you add:

main = undefined

to the bottom of your program, you'll get the warnings you're expecting.