Skip to content

rust: remap basedirs in rustc outputs with SCCACHE_BASEDIRS_REMAP_PATHS - #3021

Open
katout wants to merge 1 commit into
mozilla:mainfrom
katout:rust-basedirs-remap
Open

katout wants to merge 1 commit into
mozilla:mainfrom
katout:rust-basedirs-remap

Conversation

@katout

@katout katout commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

With SCCACHE_BASEDIRS, a cache hit can hand a crate the outputs of another checkout, and rustc writes that checkout's absolute paths into the rmeta, rlib and debug info, so they differ from what a local compile would produce.

With SCCACHE_BASEDIRS_REMAP_PATHS set to true, on or 1 in the environment of a compile, sccache passes rustc --remap-path-prefix for the basedirs. Paths under the cwd's basedirs become relative to the cwd, so diagnostics still lead to the files, and basedirs of the output directory below them are mapped from the cwd too. The output directory's other basedirs (a dependency's OUT_DIR) have no such path in every checkout and go under /sccache/basedir. The mappings go before the user's own, which win where both match, and are hashed with the basedirs stripped. It is read from the compile's environment like SCCACHE_DIRECT, so it needs no config or protocol change and works in client-side mode. On Windows its name matches case-insensitively. Crates cargo compiles outside the workspace root are each mapped to ., so their file names do not tell them apart. Such compiles are not distributed. Without the variable, keys and commands are unchanged.

Tested on Windows with unit tests in rust.rs, fmt, clippy, cargo test --locked --lib and tests/sccache_cargo.rs, which also builds a path dependency outside the checkout with a build script (system and dist were not run), and, before the review changes, by building a workspace with a build-script dependency from two checkouts on Windows: the outputs were identical with the variable and differed without. Independent of #2984; whichever lands second needs a small rebase.

Refs #2765

@codecov-commenter

codecov-commenter commented Nov 11, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 98.61878% with 5 lines in your changes missing coverage. Please review.
✅ Project coverage is 79.70%. Comparing base (1616af1) to head (3fa5250).

Files with missing lines Patch % Lines
tests/sccache_cargo.rs 95.00% 3 Missing ⚠️
src/compiler/rust.rs 99.31% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3021      +/-   ##
==========================================
+ Coverage   79.68%   79.70%   +0.01%     
==========================================
  Files          75       75              
  Lines       46636    46899     +263     
==========================================
+ Hits        37161    37379     +218     
- Misses       9475     9520      +45     

☔ 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 Outdated
(
&shared,
&nested,
vec![remap(&root, up), remap(&shared, here), remap(&ws, here)],

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.

here ws maps to . like the cwd, so ws/x and shared/x both become ./x
is that intended? diagnostics on generated files would point at the wrong place

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Not intended, fixed. ws is now mapped to ../ws, the same path it gets through root, so its name is also in the key. Basedirs the cwd can't reach map under /sccache/basedir instead of ..

Comment thread src/compiler/rust.rs Outdated
let weak_toolchain_key = rust_dist_toolchain_weak_key(&m, &self.sysroot);
let remap_arguments = if env_vars
.iter()
.any(|(k, v)| k == "SCCACHE_BASEDIRS_REMAP_PATHS" && v == "1")

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.

only 1? SCCACHE_DIRECT accepts true/on/1 too, could be consistent, no?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Made it consistent with SCCACHE_DIRECT: true, on, and 1 are accepted now. The name is also matched ignoring case on Windows.

Comment thread src/compiler/rust.rs Outdated
if cwd
.components()
.chain(output_dir.components())
.any(|c| c == Component::ParentDir)

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.

silently no remapping here, could you please add a debug! so it is visible?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Added a debug!.

Comment thread src/compiler/rust.rs Outdated
let from_cwd = in_cwd.iter().find_map(|outer| {
let rest = dir.strip_prefix(outer).ok()?;
let depth = cwd.components().count() - outer.components().count();
Some(iter::repeat_n("..", depth).collect::<PathBuf>().join(rest))

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.

same depth + repeat_n("..") as above, could be a small helper, no?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Moved it into an up_to closure, used in both places.

@sylvestre

Copy link
Copy Markdown
Collaborator

sorry but it needs to be rebased

@katout
katout force-pushed the rust-basedirs-remap branch 2 times, most recently from fb551a0 to 5892f69 Compare November 19, 2026 15:05
@katout

katout commented Nov 19, 2026

Copy link
Copy Markdown
Contributor Author

Rebased on main and squashed into one commit. Also: a .. in the output directory now skips only its mappings, not the cwd's; a debug! says when a remapped compile is not distributed; the docs note that crates built outside the workspace root are all mapped to .; and a cargo test covers a path dependency outside the checkout.

@sylvestre

Copy link
Copy Markdown
Collaborator

sorry but it needs to be rebased

With basedirs, a cache hit can restore a crate compiled in another
checkout, whose absolute paths rustc wrote into the rmeta, rlib and debug
info. See mozilla#2765.

With SCCACHE_BASEDIRS_REMAP_PATHS set to true, on or 1 in the
environment of a compile (the name is matched ignoring case on Windows),
sccache passes rustc --remap-path-prefix for the basedirs, so these
outputs are the same in every checkout. Paths under the basedirs of the
cwd become relative to the cwd, so diagnostics still lead to the files
(crates compiled outside the workspace root are each mapped to `.`, so
their file names do not tell them apart); basedirs of the output
directory below them are mapped from the cwd too, so that their names
are in the key. The output directory's other basedirs, such as the
OUT_DIR of a crates.io dependency, have no such path in every checkout
and go under /sccache/basedir, which names no local file. The mappings
go before the user's own, which take precedence, and are hashed with the
basedirs stripped. Such compiles are not distributed, since the mappings
name local paths.
@katout

katout commented Nov 21, 2026

Copy link
Copy Markdown
Contributor Author

Rebased on main, now on top of #3020: a compile is not distributed when it is incremental or its basedirs are remapped.

@katout
katout force-pushed the rust-basedirs-remap branch from 5892f69 to 3fa5250 Compare November 21, 2026 04:30
obra added a commit to prime-radiant-inc/sccache that referenced this pull request Nov 25, 2026
obra added a commit to prime-radiant-inc/sccache that referenced this pull request Nov 25, 2026
…and why

Carries mozilla#3065 (per-compile SCCACHE_BASEDIRS) so parallel
worktrees share cached workspace crates. PRIME-RADIANT.md records why,
the measurements, and why mozilla#3021 isn't carried (chao's rustc wrapper
remaps every rustc call instead). primeradiant-release.yml publishes
Linux x86_64 and Apple Silicon binaries for tags.

Claude-Session: https://claude.ai/code/session_01LxWKLQwo8hQv5emM5dWmFy
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.

3 participants