haskell / cabal

Official upstream development repository for Cabal and cabal-install
https://haskell.org/cabal
Other
1.58k stars 685 forks source link

Conditions don't work with flags #10070

Open leana8959 opened 1 month ago

leana8959 commented 1 month ago

Describe the bug Hello,

I tried to use conditions with flags. I tried to use a dev flag that sets -O0 and a release flag that sets -O1. However, no matter what I set the flags to, dry-running cabal always shows that it's building with -O1.

To Reproduce Steps to reproduce the behavior: Here's my repository with a minimal example: https://github.com/leana8959/cabal-opts-repr

Setting release=false, dev=true

$ cabal build -f-release -fdev --dry-run -v #this should set flag -O0 (but got O1)
... (elided)
Build profile: -w ghc-9.6.5 -O1
In order, the following would be built:
 - simple-0.1.0.0 (exe:simple-bin) (first run)

Setting release=true, dev=false

$ cabal build -frelease -f-dev --dry-run -v #this should set flag -O1 (and got O1)
... (elided)
Build profile: -w ghc-9.6.5 -O1
In order, the following would be built:
 - simple-0.1.0.0 (exe:simple-bin) (first run)

Expected behavior I should be able to toggle the conditionals using flags in the command line.

System information

Kleidukos commented 1 month ago

I can reproduce with cabal-head.

The freeze file produced is:

active-repositories: hackage.haskell.org:merge
constraints: any.base ==4.18.2.1,
             any.ghc-bignum ==1.3,
             any.ghc-prim ==0.10.0,
             any.rts ==1.0.2,
             simple +dev -release
index-state: hackage.haskell.org 2024-05-31T06:57:26Z

and so we see that +dev is the default.

Unrelated to whether or not this should be in cabal.project file (matter of taste), this seems to be an infidelity in how the flags are processed.

philderbeast commented 1 month ago

Same deal with the alternate flag syntax:

$ cabal build --flags="-release +dev" --dry-run
Build profile: -w ghc-9.6.5 -O1
In order, the following would be built (use -v for more details):
 - simple-0.1.0.0 (exe:simple-bin) (first run)
andreabedini commented 1 week ago

It looks like the message is wrong but what is passed to GHC is correct.

$ cabal clean && cabal build -f-release -fdev -v | grep 'O.'
Build profile: -w ghc-9.8.2 -O1
Running: /home/andrea/.ghcup/bin/ghc ... src/Main.hs -O0 -hide-all-packages
Running: /home/andrea/.ghcup/bin/ghc ... -O0 -hide-all-packages