Crates.io | cargo-prune-features |
lib.rs | cargo-prune-features |
version | 0.0.0 |
source | src |
created_at | 2022-07-14 18:05:49.415845 |
updated_at | 2022-07-14 18:05:49.415845 |
description | Find unused enabled feature flags and prune them. |
homepage | https://github.com/TimonPost/cargo-prune-features |
repository | https://github.com/TimonPost/cargo-prune-features |
max_upload_size | |
id | 625817 |
size | 27,060 |
This cargo tool allows you to find and prune enabled, but, unused feature flags from your project.
There are three subcommands in this library:
Use cargo purge-features --help
to fetch more details about available subcommands and their configurations.
Run cargo install purge-features
or download the binary and build it your self.
cargo purge-features analyze --workspace "C:/some_path/to/project" --report "C:/some_path/to/project/report.json"
After it finished running, check the report.json
and use this for the next two steps.
You can generate a simple HTML report from the json to make it easier to inspect results.
cargo purge-features build-report --input "C:/some_path/to/project/report.json" --output "C:/some_path/to/project/report.html"
You can choose to manually fix your dependencies or use the command in the next step.
It is possible to auto-apply the findings of the first command. But keep in mind the disclaimers.
cargo purge-features purge --input "C:/some_path/to/project/report.json"
This library works for both workspaces and individual crates. In the context of a workspace it will just iterate each crate in the workspace-definition and run the same process it does for a single crate.
For a single crate. it removes a feature of a dependency and then compiles the project to see if it still compiles. If so, we can 'believe' that the feature can be removed. Yes, this implies some overhead by recompiling the project for each feature flag enabled. However, this is a one-time thing and if you have a large project, just let it run for a while. But, the compiler will not perform a complete clean rebuild which is in our favor.
Furthermore, This library uses cargo_toml to remove or add features. It loads a TOML file into memory, modifies the dependency features, serializes the Manifest
, and writes it back to the toml-file. Then it starts compiling, and after it finishes running, the original content is written back as if nothing had happened.
But before doing all of that, we need to know which features to remove in the first case. This library uses cargo-metadata to collect all enabled features from the dependencies. Features can be enabled in several ways. Manually by features = ['x', 'y']
tag, or by the default-features=false/true
tag. Also, features can enable 0-n other features e.g default=[x,y]
. So, this library collects all enabled features, whether they are implicitly or explicitly enabled. After it collects all enabled features for a dependency, it will remove them one-by-one and compile the project as described above.
During the process, a json report is updated for each crate to ensure that if it crashes the progress is not lost. Use the cargo prune-features build-report
command to visualize this report.
Finally, this library also has the option to apply all suggestions automatically by running cargo prune-features apply
command. For this task, toml-edit is used because it doesn't mess with formatting, comments, and spaces, in the TOML-file.
--target x
to the cargo purge-features
command.This tool is very new, and one can expect problems. If you have problems, please open an issue with the problematic Cargo.toml
file. That allows me to quickly debug the permutation process.