Skip to content

fix(rust): write dep-info locally not restoring from cache - #2894

Merged
sylvestre merged 3 commits into
mozilla:mainfrom
weihanglo:local-dep-info
Oct 10, 2026
Merged

sylvestre merged 3 commits into
mozilla:mainfrom
weihanglo:local-dep-info

Conversation

@weihanglo

Copy link
Copy Markdown
Contributor

What is this

This fixes #984

A cache entry may come from another directory, or another machine with distributed compilation.
Its dep-info names paths that don't exist here.
Cargo only reads prerequisites,
so today it is mostly hit by -Zbinary-dep-depinfo (build-std).
Once Rust cache keys honor SCCACHE_BASEDIRS (see #2652),
cargo would rebuild forever on stale prerequisites (see rust-lang/cargo#17571).

sccache already runs rustc --emit dep-info while hashing.
After this PR, the dep-info output is now derived from that, with the rules of the real compilation, and written while hashing. It is byte-identical to rustc's own.
(except for the dist toolchain archive below since sccache need to be aware if that changes)

How to review

Reviewing this commit by commit is highly recommended.

Other notes

The Rust dist outputs rewriter is removed. It remapped dep-info paths from the server, and added the custom dist toolchain archive as a prerequisite (9981926).
The archive is now added when writing dep-info during hashing, so cache hits get it too.

CACHE_VERSION is also bumped, as older clients would fail to extract dep-info from new entries.

🤖 LLM disclosure: Like my previous PRs, this was initially generated by LLM after a series of design discussions, and handcrafted afterwards. I am responsible for the code change.

Also pin that dep-info is a cached output,
not written while hashing.
@codecov-commenter

codecov-commenter commented Oct 10, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 95.89552% with 11 lines in your changes missing coverage. Please review.
✅ Project coverage is 77.84%. Comparing base (214cde1) to head (4ac324f).
⚠️ Report is 2 commits behind head on main.

Files with missing lines Patch % Lines
tests/sccache_cargo.rs 88.88% 7 Missing ⚠️
src/compiler/rust.rs 98.04% 4 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2894      +/-   ##
==========================================
+ Coverage   77.66%   77.84%   +0.17%     
==========================================
  Files          72       72              
  Lines       41574    41785     +211     
==========================================
+ Hits        32290    32529     +239     
+ Misses       9284     9256      -28     

☔ 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
.arg("--print=target-libdir")
.env_clear()
.envs(env_vars.to_vec());
let hashed_archive = dist_archive.clone();

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 variable? cloning dist_archive inside the async block would do the same

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.

Seems like it. The async block is async move so a pre clone is needed.

Comment thread src/compiler/rust.rs Outdated

// Write dep-info from this invocation rather than restoring it from the cache,
// which may come from another directory or machine (mozilla/sccache#984).
// The dist toolchain archive is a prerequisite so swapping it triggers a rebuild.

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.

on a local cache miss, rustc still gets --emit dep-info and overwrites this file, so the archive prerequisite is dropped, no?
so it ends up in the dep-info on hits but not on local misses

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.

Yes, I think that is expected. A local miss on main doesn't include the archive either, and that artifact is built by local toolchain, so the archive isn't one of its inputs.

A cache hit keeps it because the entry may come from a distributed compile, which was built by toolchain archive.

I added a comment explaining this.

Comment thread src/compiler/rust.rs
})?;
let path = cwd.join(&dep_info);
fs::write(&path, text)
.with_context(|| format!("Failed to write dep-info to {}", path.display()))?;

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 writes the file even if the compilation fails afterwards
is that ok for cargo? a short comment would help

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.

Cargo also leaves a dep-info behind when a compilation fail, so it is fine. I added comment with some code permalinks to rustc and cargo.

(I love permalink so we know the source of true in other tools)

A cache entry may come from another directory,
or another machine with distributed compilation.
Its dep-info names paths that don't exist here (mozilla#984).
Cargo only reads prerequisites,
so today it is mostly hit by `-Zbinary-dep-depinfo` (build-std).
Once Rust cache keys honor `SCCACHE_BASEDIRS` (mozilla#2652),
cargo would rebuild forever on stale prerequisites (rust-lang/cargo#17571).
Make-style consumers reading rule targets are always affected.

sccache already runs `rustc --emit dep-info` while hashing.
The dep-info output is now derived from that,
with the rules of the real compilation,
and written while hashing.
It is byte-identical to rustc's own,
except for the dist toolchain archive below.

The Rust dist outputs rewriter is removed.
It remapped dep-info paths from the server,
and added the custom dist toolchain archive as a prerequisite (9981926).
The archive is now added when writing dep-info,
so cache hits get it too.

`CACHE_VERSION` is bumped,
as older clients would fail to extract dep-info from new entries.

Known gap: for a staticlib-only `cargo check`,
the written dep-info lacks the `.rmeta` rule.
That `.rmeta` was never cached anyway.

@weihanglo weihanglo left a comment

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 some lengthy comments to capture some integration details. Let me know if it is appropriate or too much.

Comment thread src/compiler/rust.rs
.arg("--print=target-libdir")
.env_clear()
.envs(env_vars.to_vec());
let hashed_archive = dist_archive.clone();

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.

Seems like it. The async block is async move so a pre clone is needed.

Comment thread src/compiler/rust.rs
})?;
let path = cwd.join(&dep_info);
fs::write(&path, text)
.with_context(|| format!("Failed to write dep-info to {}", path.display()))?;

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.

Cargo also leaves a dep-info behind when a compilation fail, so it is fine. I added comment with some code permalinks to rustc and cargo.

(I love permalink so we know the source of true in other tools)

Comment thread src/compiler/rust.rs Outdated

// Write dep-info from this invocation rather than restoring it from the cache,
// which may come from another directory or machine (mozilla/sccache#984).
// The dist toolchain archive is a prerequisite so swapping it triggers a rebuild.

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.

Yes, I think that is expected. A local miss on main doesn't include the archive either, and that artifact is built by local toolchain, so the archive isn't one of its inputs.

A cache hit keeps it because the entry may come from a distributed compile, which was built by toolchain archive.

I added a comment explaining this.

@weihanglo

Copy link
Copy Markdown
Contributor Author

Thanks for the review btw!

@sylvestre
sylvestre merged commit 81c2834 into mozilla:main Oct 10, 2026
51 checks passed
@sylvestre

Copy link
Copy Markdown
Collaborator

Thanks for your PR

@weihanglo
weihanglo deleted the local-dep-info branch October 10, 2026 18:03
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
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.

sccache generates incorrect dep-info for Rust project

3 participants