![rustlang](https://lemmyrs.org/pictrs/image/95332d2c-71a8-4adc-8c0b-af1dadc92051.png?format=webp&thumbnail=48)
Fixing feature unification compilation time issues with `cargo-hackerman`
https://crates.io/crates/cargo-hackermanIf you have a workspace with dependencies you probably noticed that sometimes cargo
seemingly unnecessary recompile external dependencies as you switch between different members of your workspace.
This is caused by something called feature unification ([1]). Since features by design should be additive only cargo
tries to avoid redundant work by using a superset of all required features. Problem comes when there are multiple crates in the workspace require external dependencies with different set of features. When you are working with the workspace as a whole - unified features include all the dependencies, when you target a single crate - unified features will include only features of that crate's dependencies.
What's worse - if you are using nix
with crate2nix
to manage dependencies - you'll get no feature unification at all and every dependency with each unique combination of features is considered a separate dependency so the same crate can be compiled (and linked in) multiple times - I've seen 20+ copies of a single crate.
Unless there are special requirements it is better to make sure that all the external dependencies have exact same set of features enabled across the workspace. One option is to do it by hand manually editing Cargo.toml
files for individual dependencies or with inherited workspace dependencies. As with anything manual - it would be error prone.
That's where cargo-hackerman
comes in. It can check if there are feature unification issues in the workspace so you can run it in CI if you want to do it manually or it can apply the smallest possible hack by itself (and remove it later).
This is not a new crate, we've been using it in production with a large workspace for over a year now.
In addition to feature unification it can help with some other compilation time things giving easy answers to questions like :
- are there any duplicate dependencies in my workspace?
- why is this dependency or feature on dependency is required?
- what are all the dependencies of this crate?
- where is the repository of this crate?
With updated bpaf
documentation on https://crates.io/crates/cargo-hackerman should be always up to date and cli - a bit more user friendly