Skip to content

rust: add support for SCCACHE_BASEDIRS when hashing - #2918

Merged
sylvestre merged 2 commits into
mozilla:mainfrom
ghoulspa:basedirs-rust
Nov 2, 2026
Merged

sylvestre merged 2 commits into
mozilla:mainfrom
ghoulspa:basedirs-rust

Conversation

@ghoulspa

Copy link
Copy Markdown
Contributor

The Rust implementation of the hasher doesn't respect SCCACHE_BASEDIRS currently. This PR adds support for the basedirs during the hashing process, by stripping the basedir from the working directory, the arguments, the CARGO_* variables and the env-dep values.

Not respecting basedirs causes issues when using crates that use OUT_DIR, such as serde, which miss the cache in every new checkout. Path dependency crates also miss the cache because of CARGO_MANIFEST_DIR and the working directory, and every crate that depends on them misses as well, so this fixes that too. This also fixes env-dep values by unescaping them, since rustc doubles their backslashes and the strip wouldn't match otherwise.

This change to the env-dep parser may cause new misses on Windows, once per crate, for anyone whose env-deps contain backslashes, such as serde's OUT_DIR. For all other cache keys there will be no changes, except where you use the new basedirs support.

We also added unit tests which check that the hash keys match when basedirs are in use and differ when they aren't, plus a few edge cases, and an integration test that builds with cargo from two checkouts.

Fixes #2870

@codecov-commenter

codecov-commenter commented Oct 19, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 97.18876% with 7 lines in your changes missing coverage. Please review.
✅ Project coverage is 75.35%. Comparing base (78c418b) to head (708c151).
⚠️ Report is 21 commits behind head on main.

Files with missing lines Patch % Lines
tests/sccache_cargo.rs 92.94% 6 Missing ⚠️
src/compiler/rust.rs 99.35% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2918      +/-   ##
==========================================
- Coverage   78.15%   75.35%   -2.81%     
==========================================
  Files          74       74              
  Lines       43324    43737     +413     
==========================================
- Hits        33861    32957     -904     
- Misses       9463    10780    +1317     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Comment thread src/compiler/rust.rs Resolved
Comment thread tests/sccache_cargo.rs Resolved
Comment thread tests/sccache_cargo.rs Resolved
Comment thread src/compiler/rust.rs Resolved
ghoulspa and others added 2 commits October 28, 2026 10:25
rustc escapes backslashes, newlines and carriage returns in them. Keys
change once for crates whose env-deps contain one.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The cwd, the arguments, the CARGO_* variables and the env-dep values are
hashed with the basedirs stripped, so a crate built from another checkout
listed in SCCACHE_BASEDIRS is a hit.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@sylvestre
sylvestre merged commit cfb169c into mozilla:main Nov 2, 2026
51 checks passed
@sylvestre

Copy link
Copy Markdown
Collaborator

Thanks for your PR

pamarcos added a commit to pamarcos/sccache that referenced this pull request Nov 3, 2026
Adopt upstream's local dep-info generation and remove the cached-output rewrite hook.
Keep literal environment dependencies, remap destinations, and unremapped source identities
in Rust cache keys. Restrict normalization around procedural macros and partial remap scopes.
Advance the Rust cache-key version to avoid reusing unsafe entries.

Add executable regressions comparing cached and fresh builds across checkouts, alongside a
safe cache-hit control. Document the supported cases and conservative Cargo limitations.

Related: mozilla#2918
Related: mozilla#2894
@ghoulspa
ghoulspa deleted the basedirs-rust branch November 4, 2026 13:49
katout added a commit to katout/sccache that referenced this pull request Nov 11, 2026
Since mozilla#2918, with SCCACHE_BASEDIRS the values of env-deps (the variables a
crate reads with `env!` or `option_env!`) are hashed with the basedirs
stripped. A crate that bakes `env!("CARGO_MANIFEST_DIR")` into its outputs
can then get a hit from another checkout and use that checkout's path.

SCCACHE_BASEDIRS_VERBATIM_ENV_DEPS, a comma-separated list read from the
compile's environment, names env-deps whose values are hashed verbatim, so
only crates that read them are keyed per checkout. The variable and the
names it lists are matched ignoring ASCII case on Windows, where rustc
lists the name as written in `env!`. An empty or non-UTF-8 value is
ignored, and without basedirs nothing is looked up, so default keys are
unchanged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
sylvestre pushed a commit that referenced this pull request Nov 14, 2026
Since #2918, with SCCACHE_BASEDIRS the values of env-deps (the variables a
crate reads with `env!` or `option_env!`) are hashed with the basedirs
stripped. A crate that bakes `env!("CARGO_MANIFEST_DIR")` into its outputs
can then get a hit from another checkout and use that checkout's path.

SCCACHE_BASEDIRS_VERBATIM_ENV_DEPS, a comma-separated list read from the
compile's environment, names env-deps whose values are hashed verbatim, so
only crates that read them are keyed per checkout. The variable and the
names it lists are matched ignoring ASCII case on Windows, where rustc
lists the name as written in `env!`. An empty or non-UTF-8 value is
ignored, and without basedirs nothing is looked up, so default keys are
unchanged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

3 participants