Command-line tool for finding Bazel dependencies which are not actually used in code.
An unused Bazel dependency is an unnecessary entry in the deps
field of a java_library
or java_binary
target in
a BUILD
file, meaning the code being compiled doesn't actually have any dependency on that library. Over time, unused
dependencies accumulate as code changes and BUILD
files are not updated to reflect
those changes.
Unused dependencies:
- increase local build and sync times
- increase CI run times
- lead to larger deployment artifacts
The problems posed by unused dependencies will only get worse as your code grows and ages... unless there were a way to programmatically detect and remove them. Luckily, there now is!
- Run
unused-deps.sh <bazelPath> <outputFile>
bazelPath
is the path to the target or directory to analyze in Bazel label format. This can either mean " analyze" this target or analyze all targets underneath this directory.outputFile
is the absolute path to the file to which to write the Buildozer commands- This could take a while (in our experience, running it on four million lines of Java will take around six hours). If you are analyzing a smaller subsection it should be much faster. The script will print out the progress of the build that it runs, so you can get a rough idea of how much time is left.
- Run the Buildozer commands with
bash <outputFile>
to programmatically remove the detected unused dependencies - Verify that the target still compiles using
bazel build <bazelPath>
. If your build fails, read the ** Troubleshooting** section below.
> ./unused-deps.sh //src/main/java/com/stripe/payments/server/worker/... worker-commands.txt
...
Ignored dependencies: 3802
------------------------------
Considered dependencies: 467
Used dependencies: 432 (93%)
Unused dependencies: 35 (7%)
------------------------------
Time elapsed: 0 seconds
Wrote buildozer commands to remove unused dependencies to: worker-commands.txt
> bash worker-commands.txt
fixed src/main/java/com/stripe/payments/server/worker/db/BUILD
...
fixed src/main/java/com/stripe/payments/server/worker/workflows/interfaces/BUILD
> bazel build //src/main/java/com/stripe/payments/server/worker/db/...
...
INFO: Build completed successfully, 2867 total actions
This tool does not support the following: