Sub topic 11
Resolver versions and how features unify across members
This chapter assumes you can write a workspace root manifest with members, that you know how [features] and optional dependencies work in a single package, and that you have used -p and --workspace. It explains how Cargo picks the feature set for each dependency when several members share one build.
Why the resolver matters in a workspace
All members share one Cargo.lock and one target directory. They can also depend on the same crate with different feature requests. Cargo has to build each crate once per build, so it must decide which features that single build gets.
The feature resolver makes this decision. It is separate from the version resolver that picks crate versions. You choose the feature resolver with the resolver key.
The three resolver versions
| Value | Cargo version | Default for | Feature behavior |
|---|---|---|---|
| `"1"` | all | edition 2015 and 2018 | Unifies features across normal, build and dev dependencies and across targets |
| `"2"` | 1.51 and later | edition 2021 | Splits those cases apart, described below |
| `"3"` | 1.84 and later | edition 2024 | Same feature rules as `"2"`, and prefers dependency versions compatible with your `rust-version` |
Resolver "2" changes three things compared with "1":
- Platform-specific dependencies. Features that a dependency enables only for a target you are not building for are ignored. Under
"1"they were still enabled. - Build scripts and proc macros. These run on the host. Their dependencies' features no longer merge with the same crates used in your normal build.
- Dev-dependencies. Their features are only enabled when you build something that needs them, such as tests, examples or benchmarks. A plain
cargo buildignores them.
Resolver "3" keeps all of this. It adds one behavior: when it picks versions, it prefers those whose declared rust-version fits yours. This is the resolver.incompatible-rust-versions = "fallback" setting. It does not change which features are enabled.
Setting the resolver in a workspace
A single package gets its default from its edition. A virtual manifest has no [package] table and so no edition. Cargo falls back to resolver "1" and prints a warning. Always set the value in the root:
[workspace]
resolver = "2"
members = ["crates/*"]The root's resolver applies to the whole workspace. A member's own edition does not change it. If the root is a normal package, its edition sets the default, and an explicit resolver key still overrides it.
How features unify across members
Features are additive. For one build, Cargo takes the union of all features requested for a crate by every package in the build. Which packages count depends on what you select.
cargo build --workspaceselects every member. Features from all of them merge.cargo build -p appselects onlyapp. Only features requested byappand its dependency graph count.cargo buildin a virtual workspace root builds thedefault-members(all members if unset), so it behaves like--workspace.
Resolver "2" does not stop unification between members. It only stops the three cases listed earlier. Two members that both depend on textkit still end up sharing one textkit build with the union of their features.
A runnable demonstration
Create three crates under crates/. The root manifest is:
[workspace]
resolver = "2"
members = ["crates/*"]
[workspace.dependencies]
textkit = { path = "crates/textkit" }The library crates/textkit/Cargo.toml defines an optional feature with no dependencies:
[package]
name = "textkit"
version = "0.1.0"
edition = "2021"
[features]
extra = []Its crates/textkit/src/lib.rs reports whether the feature was compiled in:
pub fn has_extra() -> bool {
cfg!(feature = "extra")
}The app crate turns the feature on. A member may add features on top of an inherited workspace dependency:
[package]
name = "app"
version = "0.1.0"
edition = "2021"
[dependencies]
textkit = { workspace = true, features = ["extra"] }Its crates/app/src/main.rs:
fn main() {
println!("app: extra = {}", textkit::has_extra());
}The plain crate does not request the feature:
[package]
name = "plain"
version = "0.1.0"
edition = "2021"
[dependencies]
textkit.workspace = trueIts crates/plain/src/main.rs includes a test so we can run it both ways:
fn main() {
println!("plain: extra = {}", textkit::has_extra());
}
#[cfg(test)]
mod tests {
#[test]
fn reports_extra() {
println!("extra = {}", textkit::has_extra());
}
}Now run it with different selections:
cargo run -p app
cargo run -p plain
cargo test -p plain -- --nocapture
cargo test --workspace -- --nocaptureThe first command prints app: extra = true. The second prints plain: extra = false. The third prints extra = false, because only plain's graph is in the build. The fourth prints extra = true in the plain test output, because app is also selected and its request for extra merges into the shared textkit build.
Switching between selections also forces Cargo to recompile textkit, since the feature set changed.
Inspecting the result
cargo tree shows the resolved features. These commands are the quickest way to debug a surprise:
cargo tree --workspace -e features
cargo tree -p plain -e features
cargo tree --workspace -e features -i textkitThe -i flag inverts the tree, so you see which packages asked for each feature of textkit. Use -e features with -p to confirm what a single member sees when built alone.
Consequences for design
Features must be additive. Because the union is what gets built, enabling a feature must never remove or change the meaning of existing items. A feature like no-std that removes APIs, or two features that are mutually exclusive, can break a member that never asked for them. A sibling member's request can turn them on.
A passing -p build does not prove the workspace builds. A member can compile alone because it did not see a feature that another member enables. It can also compile only in the workspace because a sibling enabled a feature it forgot to declare. Test both, for example with cargo test --workspace and a per-member loop in CI.
The lock file is unaffected. Cargo.lock records versions, not enabled features. Selecting different members changes features but not locked versions.
--features and virtual workspaces. Under resolver "1", you cannot pass --features at a virtual workspace root. Under "2" and later you can name features by package:
cargo build -p app -p plain --features textkit/extraCommon errors
- Warning about the resolver in a virtual manifest. Cargo is using
"1"because[workspace]has noresolver. Add the key. - A dev-dependency's feature "leaks" into a release build. This happens under
"1". Move to"2"or later. - A build script sees different features than expected. Under
"2", host and target builds of the same crate are separate, so a crate can compile twice with different feature sets. That is expected. - Code compiles with
--workspacebut not-p member. The member relies on a feature another member enables. Declare the feature in the member's own dependency line.
Exercise
A workspace has members server and cli. Both depend on textkit. Only server requests textkit's extra feature. You run cargo build -p cli, then cargo build --workspace. Is extra enabled in each build, and which command would you use to verify it?
Show answer
In cargo build -p cli, extra is not enabled, because only cli's dependency graph is selected. In cargo build --workspace, extra is enabled for the single shared textkit build, because server's request is unioned in. Verify with cargo tree -p cli -e features and cargo tree --workspace -e features -i textkit.
Exercise
Your virtual root manifest has no resolver key and all members use edition = "2021". Which resolver does Cargo use, and how do you fix it?
Show answer
Cargo uses resolver "1" and warns, because a virtual manifest has no edition to derive a default from. Add resolver = "2" (or "3" on Cargo 1.84 or later) under [workspace].