haskell / cabal

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

`cabal exec` violates -Wunused-packages #9082

Open brandonchinn178 opened 1 year ago

brandonchinn178 commented 1 year ago

Describe the bug When -Wunused-packages is enabled, building or running tests with cabal exec errors with all dependencies being used.

To Reproduce Steps to reproduce the behavior:

cabal-version: 3.0
name: foo
version: 0
library
  exposed-modules: Foo
  build-depends: base
  ghc-options: -Werror -Wunused-packages
cabal exec cabal v2-build

This results in

Build profile: -w ghc-9.6.1 -O1
In order, the following will be built (use -v for more details):
 - foo-0 (lib) (first run)
Preprocessing library for foo-0..
Building library for foo-0..
Loaded package environment from ...

<no location info>: error: [GHC-42258] [-Wunused-packages, Werror=unused-packages]
    The following packages were specified via -package or -package-id flags,
    but were not needed for compilation:
      - ghc-bignum-1.3 (exposed by flag -package-id ghc-bignum-1.3)
      - ghc-prim-0.10.0 (exposed by flag -package-id ghc-prim-0.10.0)
      - rts-1.0.2 (exposed by flag -package-id rts-1.0.2)
Error: cabal: Failed to build foo-0.

Expected behavior cabal v2-build and cabal exec cabal v2-build should behave the same.

System information

Additional context Add any other context about the problem here.

gbaz commented 1 year ago

I don't know if/how this could be fixed in cabal. Its really an issue with the "purpose" of cabal exec, combined with the nature of what ghc flags do. Cabal exec needs to construct an environment file that has all packages in the closure of what the specified deps are, by design. However, that's going to be a superset of the packages literally depended on directly by a package. So when build is invoked in this environment, there will necessarily be additional packages available beyond those a package makes use of directly, and the heuristics of -Wunused-packages will correctly notice this.

I feel like its potentially more a ghc/upstream issue -- in particular ghc should warn about unused packages passed as package-id flags, but not those that are actually provided in an envfile but then treated as though they were passed as such flags...