Sub topic 11
Resolver Versions and How Features Unify Across Members
This chapter assumes you can read a Cargo.toml, know how to declare dependencies with features, and have a workspace with a virtual manifest and several members. It explains how Cargo decides which features of a dependency to turn on when many members share one build.
Features are additive
A feature is an optional piece of a crate that you switch on by name. Cargo treats features as additive. If any crate in the build asks for a feature of a dependency, that feature is on for every crate that uses that dependency in the same build. This is called feature unification.
In a workspace, unification happens across the members you build together. Suppose app enables the json feature of a crate and textkit does not. When you run cargo build --workspace, both members see the crate compiled once, with json enabled.
The resolver setting
The resolver is the part of Cargo that picks versions and features. Resolver version 2 changed how unification works. You set it once, in the workspace root.
[workspace]
resolver = "2"
members = ["crates/*"]A virtual manifest has no [package] table, so Cargo cannot read an edition from it. Without an explicit resolver key, Cargo falls back to resolver 1 and warns you. Set the key yourself. Edition 2021 members default to resolver 2 when they stand alone, and edition 2024 defaults to resolver 3. A virtual manifest does not infer either default.
Only the root resolver value counts. A resolver key in a member's manifest is ignored, with a warning.
What resolver 2 changes
Resolver 1 unified features across all kinds of dependency. Resolver 2 stops unifying in three cases:
- Features enabled for a target platform you are not building are ignored.
- Features on build dependencies and procedural macros are kept apart from features on normal dependencies.
- Features on dev-dependencies are not turned on unless you build something that needs them, such as tests or examples.
The practical result is fewer surprises. A std-only feature needed by a build script no longer leaks into your runtime code.
Unification depends on what you select
Cargo unifies features only among the packages you select for that command. This is a common source of confusion.
cargo build --workspace
cargo build -p textkitThe first command builds all members, so the dependency gets the union of their features. The second builds only textkit, so the dependency gets only the features textkit asks for. The same dependency can be compiled twice, with different features, across two commands. A crate that compiles in one case and fails in the other usually relies on a feature that another member enabled.
With resolver 2, the command line can also change the picture. Passing --features together with -p applies the features to the selected package rather than to the whole workspace.
Common errors and fixes
- A member builds in the workspace but fails alone. It uses a feature it never declared. Run
cargo build -p namefor each member in CI to catch this, and add the missing feature to that member'sCargo.toml. - A feature is on when you expected it off. Another member enabled it. Run
cargo tree -e features -i dependency_nameto see which package requests it. - A
[features]entry in a workspace dependency has no effect. An inheriting member can add features, but cannot remove ones that the workspace entry enables. Keep the workspace entry minimal and add features in members.
[workspace.dependencies]
serde = { version = "1", default-features = false }[dependencies]
serde = { workspace = true, features = ["derive"] }Exercise
Member app enables feature json of a shared dependency, and member textkit does not. You run cargo build -p textkit. Is json enabled for the dependency, and why?
Show answer
No. Cargo unifies features only among the selected packages, and only textkit is selected. Running cargo build --workspace would enable json, because app is then part of the build.
Exercise
Where must the resolver key be set in a workspace with a virtual manifest, and what happens if you put it in a member?
Show answer
It must be in the [workspace] table of the root manifest. A resolver key in a member is ignored with a warning.