The -Wunused-packages warning does not say which project and component issued the warning
It's a bit annoying when compiling a large project with multiple components to search which actual component issued -Wunused-packges warning, especially when one is starting to use -Wunused-packages in a large project.
The DynFlags contains homeUnitId_ :: UnitId field, but this is not good enough for this kind of information for a few reasons. When compiling a test component it turns out the name held in UnitId is main, which is not very informative. Also showing the hash which is included as part of UnitId is not necessary. It will also complicate ghc tests (as in one of the tests the warning message is tested, and the hash could change more often than desirable).
For this reason I suggest the build too would inform GHC about the component it's trying to build, e.g. cabal could say it's compiling bytestring:test:bytestring-test, which would be included in DynFlags the same way UnitId is there.
I have an experimental ghc branch in coot/unused-packages. Compiling a simple project with cabal shows these warnings:
In order, the following will be built (use -v for more details):
- test-unused-packages-0.1.0.0 (lib) (first run)
- test-unused-packages-0.1.0.0 (test:test-test) (first run)
Configuring library for test-unused-packages-0.1.0.0..
Preprocessing library for test-unused-packages-0.1.0.0..
Building library for test-unused-packages-0.1.0.0..
<no location info>: warning: [GHC-42258] [-Wunused-packages]
The following packages were specified via -package or -package-id flags,
but were not needed for compilation of test-unused-packages-0.1.0.0-inplace:
- bytestring-0.11.3.1 (exposed by flag -package-id bytestring-0.11.3.1)
[1 of 1] Compiling MyLib ( src/MyLib.hs, /tmp/test/dist-newstyle/build/x86_64-linux/ghc-9.5.20221123/test-unused-packages-0.1.0.0/build/MyLib.o, /tmp/test/dist-newstyl
e/build/x86_64-linux/ghc-9.5.20221123/test-unused-packages-0.1.0.0/build/MyLib.dyn_o )
Configuring test suite 'test-test' for test-unused-packages-0.1.0.0..
Preprocessing test suite 'test-test' for test-unused-packages-0.1.0.0..
Building test suite 'test-test' for test-unused-packages-0.1.0.0..
<no location info>: warning: [GHC-42258] [-Wunused-packages]
The following packages were specified via -package or -package-id flags,
but were not needed for compilation of main:
- bytestring-0.11.3.1 (exposed by flag -package-id bytestring-0.11.3.1)
- test-unused-packages-0.1.0.0 (exposed by flag -package-id test-unused-packages-0.1.0.0-inplace)
[1 of 1] Compiling Main ( test/Main.hs, /tmp/test/dist-newstyle/build/x86_64-linux/ghc-9.5.20221123/test-unused-packages-0.1.0.0/t/test-test/build/test-test/test-test
-tmp/Main.o )
<no location info>: warning: [GHC-42258] [-Wunused-packages]
The following packages were specified via -package or -package-id flags,
but were not needed for compilation of main:
- bytestring-0.11.3.1 (exposed by flag -package-id bytestring-0.11.3.1)
- test-unused-packages-0.1.0.0 (exposed by flag -package-id test-unused-packages-0.1.0.0-inplace)
[2 of 2] Linking /tmp/test/dist-newstyle/build/x86_64-linux/ghc-9.5.20221123/test-unused-packages-0.1.0.0/t/test-test/build/test-test/test-test