Skip to content

Stale check-cfg warnings in cache (Rust 2024) #2619

@BartMassey

Description

@BartMassey

Note: This bug report was written by Claude Code based on a debugging session where Claude isolated this issue with my help; it was then edited by me. I have verified the report independently: to the best of my knowledge and ability it is correct, and clearer and more complete than what I would have written.

Summary

When using sccache with Rust 2024 edition projects, check-cfg warnings about unknown feature values are cached and persist even after:

  • The Cargo.toml is properly configured with the features
  • Running cargo clean
  • Restarting the sccache server

The cached warnings only clear when either:

  1. The sccache cache directory is manually deleted (rm -rf ~/.cache/sccache)
  2. sccache is bypassed entirely (RUSTC_WRAPPER="" cargo check)
  3. The package name is changed (forcing a new cache key)

This causes confusing behavior where cargo is passing the correct --check-cfg flags to rustc, but the compilation output still shows warnings about missing features.

Environment

  • Rust version: 1.93.1 (01f6ddf75 2026-02-11)
  • Cargo version: 1.93.1 (083ac5135 2025-12-15)
  • sccache version: 0.13.0
  • OS: Linux 6.18.9+deb14-amd64
  • Edition: 2024

Reproduction Steps

1. Create a new library with Rust 2024 edition (WITHOUT features initially):

cargo new --lib test_sccache_bug
cd test_sccache_bug

2. Edit Cargo.toml to use edition 2024 but DON'T add features yet:

[package]
name = "test_sccache_bug"
version = "0.1.0"
edition = "2024"

3. Edit src/lib.rs to reference a feature that doesn't exist:

#![cfg_attr(not(feature = "std"), no_std)]

4. Build with sccache enabled to cache the warning:

cargo check --no-default-features

Expected: Warning about unknown std feature (this is correct behavior at this point)

Actual: You'll see the expected warning:

warning: unexpected `cfg` condition value: `std`
 --> src/lib.rs:1:17
  |
1 | #![cfg_attr(not(feature = "std"), no_std)]
  |                 ^^^^^^^^^^^^^^^ help: remove the condition
  |
  = note: no expected values for `feature`
  = help: consider adding `std` as a feature in `Cargo.toml`

This warning is now cached by sccache.

5. Fix the configuration by adding the feature to Cargo.toml:

[package]
name = "test_sccache_bug"
version = "0.1.0"
edition = "2024"

[features]
std = []
default = ["std"]

6. Build again - the warning should disappear but doesn't:

cargo check --no-default-features

Expected: No warnings (the std feature is now properly defined)

Actual: The same warning still appears! This is the bug.

7. Verify cargo is passing correct flags:

cargo clean
cargo check --no-default-features -vv 2>&1 | grep "check-cfg"

You'll see that cargo IS correctly passing:

--check-cfg 'cfg(feature, values("default", "std"))'

This proves cargo knows about the feature and is telling rustc, but sccache is returning the cached warning.

8. Confirm it's an sccache caching issue:

# Bypass sccache - warning disappears
cargo clean
RUSTC_WRAPPER="" cargo check --no-default-features
# No warning!

# Or clear sccache cache - warning disappears
rm -rf ~/.cache/sccache
cargo clean
cargo check --no-default-features
# No warning!

Root Cause Analysis

When cargo passes updated --check-cfg flags to rustc via sccache, sccache appears to return cached compilation results (including cached warnings) from a previous compilation where the flags were different or missing.

The cache key doesn't seem to properly account for changes in the --check-cfg flags, causing stale warnings to persist even when the underlying issue has been fixed in the project configuration.

Expected Behavior

sccache should either:

  1. Include --check-cfg flags in its cache key calculation, or
  2. Not cache warning/diagnostic output separately from the compilation itself, or
  3. Invalidate cached results when --check-cfg flags change

Impact

This issue is particularly problematic because:

  • It causes confusion during development (warnings that shouldn't exist)
  • It makes it unclear whether project configuration is correct
  • Standard debugging steps (cargo clean) don't resolve it
  • It specifically affects Rust 2024 edition projects using the new check-cfg features
  • New users may think there's a problem with their Cargo.toml when it's actually correct

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions