undepend removes dependencies by brute force by removing likely candidates and checking that everything compiles after removal.
The results are a list of crates that can be removed in a file called unused.sh
.
chmod +x ./unused.sh
and run it to remove all the unused deps and see if the result works for you.
Alternatively you can cat ./unused.sh
in a vscode terminal
and use the Cargo.toml hyperlinks to manually check that those crates ought to be removed.
(needs git)
Install undepend and prerequisites:
cargo install ripgrep cargo-edit undepend
Change your current dir to the dir of a freshly checked out git clone and run:
undepend
The tool will iterate over the workspace's dependencies, removing them individually and establishing if everything still compiles.
** The tool will abort if the checkout is not clean at the start. **
A recorded list of dependencies are written to unused.sh
cargo metadata is used to understand which crates are in the workspace.
For each crate we run:
cargo rm a_crate_dep
(from cargo-edit)cargo check --all-targets
cargo build --all-targets --release
cargo test --doc --release non_existent_test_name
- If we get to here without errors then possibly the dep is not needed.
git reset --hard
(to get back to a clean state)
As an optimisation undepend skips dependencies that are clearly used in the source. As such, the runtime of undepend is not too bad. (Before that optimisation it could easily take overnight for some projects. Now I've not seen anything take longer than 30 mins for big projects on a 32 core box.)
If a dependency is non-optional, is only used for one target platform and isn't directly used we may still try and remove it.
At the moment cargo check --all-targets
doesn't compile doc tests so
we try and run cargo test non_existent_test_name
- this I think forces the compile to happen.
(Tracking issues/PR for fixing this: rust-lang/cargo#6424 rust-lang/cargo#8859 )
If the dependency is optional no attempt is made to removing it.
TODO: We try and respect cargo-udeps ignore format:
[package.metadata.cargo-udeps.ignore]
(See Cargo.toml for an example)
Please reference this issue to add to the trophy case:
udeps takes a less brute-force approach of look at the incremental compile information in the target dir to base its decisions on and thus is better for regular CI use.
https://crates.io/crates/cargo-udeps
(Also compared to udeps this crate has only pure rust dependencies.)
- 0.1.1 Check debug + release mode and doc tests. Checking build dependencies also.
- 0.1.0 Initial release