Why does a no_std firmware crate on Cargo's resolver "1" fail to cross-compile when only its build-dependency enables serde's std, and what does resolver "2" change?
answer
- one feature set for every kind
- the build script leaks into the target
- build, dev, platform, proc-macro
- a split can mean two builds
basics
~20 sResolver 1 unifies features across all dependency kinds, so the build script's std request also lands on the firmware's serde, which cannot build without std. Resolver 2 keeps build-dependencies, proc-macros, inactive dev-dependencies and other platforms' dependencies apart.
solid answer
~40 sResolver `"1"` gives a package one feature set no matter where it is requested. The firmware writes `serde = { version = "1", default-features = false }`, but its build script's `[build-dependencies]` entry asks for serde's defaults, which include `std`, and that request is unified into the serde compiled for `thumbv7em-none-eabihf`. That target has no `std`, so `serde_core` fails with E0463. Resolver `"2"` stops unifying in three cases: build-dependencies and proc-macros, dev-dependencies unless a test or example is being built, and target-specific dependencies for a platform not being built. The cost is that a crate may be compiled twice with different features. Edition 2021 defaults to `"2"`; edition 2024 defaults to `"3"`, which keeps these feature rules.
code
toml · 11 lines[package]
name = "codec"
version = "0.1.0"
edition = "2024"
resolver = "1"
[dependencies]
serde = { version = "1", default-features = false }
[build-dependencies]
serde = "1"go deeper
Recall that Cargo has resolver versions, that newer editions select a newer one by default, and that build-dependencies are compiled for the build script on the host.
Explain why resolver 1 leaks a build-dependency's std into a no_std build and list the cases where resolver 2 keeps feature sets apart.
Diagnose the E0463 in serde_core from cargo tree output, fix it with the resolver setting in the root manifest, and check cargo tree --duplicates for the extra builds the split causes.
Decide when to keep a split versus aligning features to save build time, and make sure every workspace root, virtual ones included, states its resolver explicitly.
## Two passes, and where features get decided Cargo's **resolver** first picks versions for Cargo.lock, then runs again to decide which **features** each compiled crate gets for the current command. The feature pass has its own rules, selected by the `resolver` key: `"1"` (the old default), `"2"` and `"3"`. Resolver `"3"`, added in Cargo 1.84 and the default for edition 2024 packages, changes version selection to respect `rust-version`; its feature rules are those of `"2"`. Which versions get picked is covered with the resolver and Cargo.lock; this question is about features only. ## What resolver "1" does to the firmware The manifest of a no_std firmware crate: - `[dependencies]` has `serde = { version = "1", default-features = false }`, because serde's `std` feature cannot build for a microcontroller; - `[build-dependencies]` has `serde = "1"`, which requests serde's default feature set, `std` included, for the build script that runs on the developer's machine. Resolver `"1"` unifies features for a package **no matter where it is specified**. The build script's request for `std` therefore lands on the serde compiled for the firmware too. On the host nothing looks wrong. Cross-compiling with `cargo build --lib --target thumbv7em-none-eabihf`, `serde_core` is built with `std` for a target that has none, and rustc stops with E0463, "can't find crate for `std`", adding a note that the target may not support the standard library. ## The three splits resolver "2" makes Resolver `"2"` avoids unifying features in these three situations, and only these: 1. **Build-dependencies and proc-macros** do not share features with normal dependencies, so the build script gets `std` and the firmware does not. 2. **Dev-dependencies** add their features only when a target that needs them is built, such as `cargo test`, an example, or `cargo build --all-targets`; a plain `cargo build` of the library ignores them. 3. **Target-specific dependencies** for a platform that is not being built contribute no features: a `[target.'cfg(windows)'.dependencies]` entry asking for serde's `std` does nothing on macOS. Resolver `"2"` also changes the **command-line flags**: `--features` may name features of any package selected with `-p` or `--workspace`, which resolver `"1"` allowed only for the package in the current directory. Running `cargo tree -e normal,build -f "{p} {f}"` on the firmware shows the difference: under `"1"` both serde entries list `default,std`; under `"2"` the normal one lists no features and only the build-dependency lists `default,std`. ## Seeing the dev-dependency split The dev-dependency rule is easy to observe. Give the library a normal `log = "0.4"` and a dev-dependency `log = { version = "0.4", features = ["std"] }`: - under resolver `"2"`, `cargo tree -e normal -f "{p} {f}"`, which approximates what `cargo build` compiles, shows `log` with no features; - `cargo tree -e normal,dev`, closer to what `cargo test` compiles, shows `log` with `alloc,std`; - under resolver `"1"`, both views show `alloc,std`, because the dev request leaks into every build. The `cargo tree` page itself warns that its output is close to, but not guaranteed identical to, what a given command builds. ## The cost of splitting Separate feature sets mean separate builds. Under `"2"` the firmware's dependency graph compiles `serde_core` twice when built for the host, once per feature set. The Cargo book recommends checking for this with `cargo tree --duplicates` and looking for entries with the **same version**; if the split is not needed, giving both entries the same features lets Cargo build the crate once. When cross-compiling with `--target`, build-dependencies are built separately anyway, so the split costs nothing extra there. ## Which resolver a build actually uses | Manifest | Feature resolver | |---|---| | edition 2015 or 2018, no `resolver` key | `"1"` | | edition 2021, no `resolver` key | `"2"` | | edition 2024, no `resolver` key | `"3"` (feature rules of `"2"`) | | explicit `resolver = "..."` | that version | - The resolver is a **workspace-wide** setting taken from the top-level package or, in a virtual workspace, from `[workspace]`; a dependency's own `resolver` key is ignored. - A library therefore cannot protect its users by setting `resolver = "2"`; the application's manifest decides. - Dev-dependencies of dependencies never affect features, under any resolver; the dev-dependency rule only matters for the top-level package or workspace members.
- Under resolver "2", when does a dev-dependency's feature request reach the library's own copy of that crate?Only when a target that needs dev-dependencies is being built: `cargo test`, an example, a bench, or `cargo build --all-targets`. A plain `cargo build` of the library ignores dev-dependency features.
- A library on crates.io sets `resolver = "2"`; does that protect applications that still use resolver "1"?No. The resolver is taken only from the top-level package or the workspace root, and a dependency's `resolver` key is ignored. The application's manifest decides.
- Why might switching to resolver "2" make a build slower, and how do you check?A crate requested with different features by a build-dependency and a normal dependency is now compiled twice. `cargo tree --duplicates` lists it, with the same version appearing twice; aligning the features merges the builds.
- Build-dependencies have always had their own feature set under every resolver
- Resolver 2 also stops unifying features between two normal dependencies
- A library can force resolver 2 on its users by setting it in its own manifest
- Resolver 3 changes feature unification again for the 2024 edition
- Dev-dependency features never reach the library, even under cargo test
Assumes Cargo 1.99; resolver "3" (Cargo 1.84, the default for edition 2024 packages) keeps resolver "2"'s feature rules and changes only MSRV-aware version selection.