Skip to content

rust: normalize cache keys with SCCACHE_BASEDIRS - #2813

Open
tcashel wants to merge 1 commit into
mozilla:mainfrom
tcashel:codex/rust-basedirs
Open

tcashel wants to merge 1 commit into
mozilla:mainfrom
tcashel:codex/rust-basedirs

Conversation

@tcashel

@tcashel tcashel commented Aug 18, 2026 •

Copy link
Copy Markdown

Rust builds of the same source in different worktrees currently get different cache keys. This makes Rust hashing honor SCCACHE_BASEDIRS, using the longest matching root for source paths, remap sources, relevant environment values, and the working directory. The Rust cache-key version advances to 7.

This is opt-in and changes cache keys, not embedded artifact paths. A cached crate using env!("CARGO_MANIFEST_DIR") can retain the original checkout path. Tests cover cross-root reuse, source-change misses, argument framing, and Windows path forms.

Rebased onto current upstream. Formatting, clippy, and the locked unit/integration suite pass on Apple silicon. CUDA cases skip locally; native Windows and CUDA validation are left to CI.

Request-scoped configuration follows in #2814. Related to #2595. Developed with OpenAI Codex.

@codecov-commenter

codecov-commenter commented Aug 26, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 99.23077% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 76.61%. Comparing base (0f9467c) to head (b1fdda1).
⚠️ Report is 234 commits behind head on main.

Files with missing lines Patch % Lines
src/compiler/rust.rs 99.25% 2 Missing ⚠️
src/util.rs 99.37% 1 Missing ⚠️
tests/sccache_cargo.rs 98.52% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2813      +/-   ##
==========================================
+ Coverage   76.32%   76.61%   +0.28%     
==========================================
  Files          72       72              
  Lines       40180    40638     +458     
==========================================
+ Hits        30668    31134     +466     
+ Misses       9512     9504       -8     

☔ 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.

@tcashel

tcashel commented Sep 2, 2026

Copy link
Copy Markdown
Author

@sylvestre This is ready for review. #2814 and #2838 are stacked follow-ups. The new CI runs are also waiting for maintainer approval. Thanks! Please let me know if i'm doing something wrong, in code or in process!

@codspeed

codspeed Bot commented Oct 4, 2026

Copy link
Copy Markdown
Contributor

Merging this PR will degrade performance by 10.82%

⚠️ Different runtime environments detected

Some benchmarks with significant performance changes were compared across different runtime environments,
which may affect the accuracy of the results.

Open the report in CodSpeed to investigate

❌ 2 regressed benchmarks
✅ 56 untouched benchmarks
⏩ 6 skipped benchmarks1

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Mode Benchmark BASE HEAD Efficiency
❌ Simulation time_macro_finder_chunked 862.8 µs 1,026.4 µs -15.93%
❌ Simulation time_macro_finder_no_macros 2.9 ms 3 ms -5.39%

Tip

Investigate this regression by commenting @codspeedbot fix this regression on this PR, or directly use the CodSpeed MCP with your agent.


Comparing tcashel:codex/rust-basedirs (b1fdda1) with main (0f9467c)

Open in CodSpeed

Footnotes

  1. 6 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

@loganrosen

Copy link
Copy Markdown
Contributor

I tried this together with #2814 and #2882 on Firefox's Rust build, in two worktrees at the same commit on macOS. With nightly 1.101 and trim-paths = "all", the second worktree got 705/712 Rust hits. Two things still carried checkout paths into the key:

  • CARGO_TARGET_<triple>_LINKER and the -C linker=<path> that Cargo passes to rustc from it. Firefox points the linker at a script inside the checkout, so every crate missed until I normalized both locally. The run above includes that patch.
  • CARGO_TRIM_PATHS_REMAP, which holds :-separated <abs path>=<replacement> pairs. It shows up in the env of the rustc probe compiles that build scripts run (anyhow, thiserror, proc-macro2), so those five missed.

The other two misses were a build script generating code non-deterministically, which isn't sccache's problem. Also, this now conflicts with main in rust.rs after the recent dep-info change.

Comment thread src/util.rs
/// Strips the longest configured base directory from a complete path.
/// Configured base directories must be normalized and end with a slash.
#[doc(hidden)]
pub fn strip_path_basedirs<'a>(value: &'a [u8], basedirs: &[Vec<u8>]) -> &'a [u8] {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we already have strip_basedirs_from_arg and normalize_win_path for this.
could we reuse them instead of adding a second matcher with its own Windows logic?

Comment thread src/util.rs
fn strip_windows_path_basedirs<'a>(value: &'a [u8], basedirs: &[Vec<u8>]) -> &'a [u8] {
// Unicode case folding can change byte lengths. Keep non-ASCII values
// unchanged so offsets into the original value remain valid.
if !value.is_ascii() {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so any Windows path with a non-ASCII character is never normalized?
C:\Users\Jérôme\... is common, and normalize_win_path already handles UTF-8.

Comment thread src/config.rs
{
normalize_win_path(&bytes)
let mut normalized = normalize_win_path(&bytes);
strip_windows_verbatim_prefix(&mut normalized);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this, and the trailing slash change below, also affect C/C++ basedirs.
Please split that into a separate PR, with a test for the C side.

Comment thread src/compiler/rust.rs
digest.update(replacement);
}

fn encode_rust_value(value: &OsStr) -> &[u8] {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need this wrapper? as_encoded_bytes() directly would be clearer

Comment thread src/compiler/rust.rs
value.as_encoded_bytes()
}

fn sort_paths_for_hash(paths: &mut [PathBuf], basedirs: Option<&[Vec<u8>]>) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please document the why in one line: the order matters when paths outside the basedir sit between paths inside it

Comment thread src/compiler/rust.rs
});
}

fn is_path_cargo_env(var: &OsStr) -> bool {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why a hardcoded list here while env deps get normalized when the value starts with a basedir?
could we do the same for every CARGO_ var?

Comment thread README.md
---

Normalizing Paths with `SCCACHE_BASEDIRS`
Normalizing paths with `SCCACHE_BASEDIRS`

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unrelated change, please drop it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants