Rust RUSTFLAGS Mismatch - Full Rebuilds & Cache Misses in CI
RUSTFLAGS are part of the build’s fingerprint, so when they differ between runs - env var vs .cargo/config.toml, or different values across steps - Cargo invalidates the cache and rebuilds everything, or rejects artifacts compiled with different flags.
What this error means
CI rebuilds the whole world even though nothing changed, cache restores don’t help, or you see found invalid metadata for crates compiled with different flags. The cause is that the effective RUSTFLAGS changed between the cached build and the current one.
Compiling <every crate again> # cache restored but full rebuild anyway
# RUSTFLAGS env: "-C target-cpu=native"
# .cargo/config.toml: rustflags = ["-D", "warnings"]
# the two are mutually exclusive - env wins and silently drops the config flags,
# changing the fingerprint vs the cached buildCommon causes
env RUSTFLAGS overrides config, changing the fingerprint
A RUSTFLAGS env var completely replaces the rustflags in .cargo/config.toml (they don’t merge). If one run sets the env and another relies on the config, the effective flags - and thus the build fingerprint - differ, busting the cache.
Different flags across steps or runners
A flag like -C target-cpu=native resolves differently on different runner hardware, or a step adds/removes a flag, so artifacts built in one context are invalid in another.
How to fix it
Set RUSTFLAGS in exactly one place, consistently
Pick either the env var or .cargo/config.toml for the whole pipeline and keep the value identical across steps and runners.
# .cargo/config.toml - single source of truth
[build]
rustflags = ["-D", "warnings"]
# and DO NOT also set a RUSTFLAGS env var that would override thisAvoid host-dependent flags in cached builds
- Drop
-C target-cpu=nativein CI (it varies by runner) unless every runner is identical. - Include the RUSTFLAGS value in the cache key so a flag change invalidates intentionally.
- Keep the same flags between the cache-populating build and consuming builds.
How to prevent it
- Define RUSTFLAGS in one place (env or config, not both) for the whole pipeline.
- Avoid host-specific flags like
target-cpu=nativein cached CI builds. - Fold the RUSTFLAGS value into the cargo cache key.