Skip to content

rust: cache incremental compiles with SCCACHE_ALLOW_INCREMENTAL=cache - #3020

Merged
sylvestre merged 1 commit into
mozilla:mainfrom
katout:rust-cache-incremental
Nov 20, 2026
Merged

sylvestre merged 1 commit into
mozilla:mainfrom
katout:rust-cache-incremental

Conversation

@katout

@katout katout commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

Crates cargo compiles with -C incremental (workspace members and path dependencies in the dev profile) are never cached. #257/#259 excluded them because rustc reusing its incremental state beats a cache fetch, and a hit leaves that state stale. That holds for local rebuilds, but with a shared cache these crates miss whenever nothing is built locally yet (fresh checkout or worktree, CI). On a ~300-crate workspace on Windows, a second checkout from an empty target went from 2,327 to 778 CPU seconds; the cost is the first rebuild after an edit, 390 CPU seconds instead of ~95.

With SCCACHE_ALLOW_INCREMENTAL=cache these compiles are cached; 1 keeps its meaning. The incremental directory is left out of the key and out of the dep-info and file-names runs, but the key still records that the compile is incremental. They are not distributed. Without cache nothing changes, keys included, so CACHE_VERSION is unchanged. On Windows the server matches the name ignoring case, as env::var does in the client.

Tested on Windows with fmt, clippy, cargo test --locked --lib and tests/sccache_cargo.rs; system and dist were not run.

Refs #236

@codecov-commenter

codecov-commenter commented Nov 11, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 97.31544% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 72.51%. Comparing base (43c90af) to head (4bb282f).

Files with missing lines Patch % Lines
tests/sccache_cargo.rs 94.11% 2 Missing ⚠️
src/compiler/rust.rs 99.10% 1 Missing ⚠️
tests/helpers/mod.rs 0.00% 1 Missing ⚠️

❗ There is a different number of reports uploaded between BASE (43c90af) and HEAD (4bb282f). Click for more details.

HEAD has 7 uploads less than BASE
Flag BASE (43c90af) HEAD (4bb282f)
14 7
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3020      +/-   ##
==========================================
- Coverage   79.16%   72.51%   -6.66%     
==========================================
  Files          75       75              
  Lines       46166    44867    -1299     
==========================================
- Hits        36548    32535    -4013     
- Misses       9618    12332    +2714     

☔ 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/commands.rs Outdated
// With the opt-in, `-C incremental=` compiles are left uncached (CannotCache),
// or cached with SCCACHE_CACHE_INCREMENTAL=1.
let allow_incremental = env::var("SCCACHE_ALLOW_INCREMENTAL").as_deref() == Ok("1")
|| env::var("SCCACHE_CACHE_INCREMENTAL").is_ok_and(|v| {

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.

two variables for one feature is a bit confusing.
maybe SCCACHE_ALLOW_INCREMENTAL=cache as you suggested would be simpler, 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.

Switched to SCCACHE_ALLOW_INCREMENTAL=cache. 1 keeps its current meaning.

Comment thread docs/Rust.md Outdated
* Procedural macros that read files from the filesystem may not be cached properly.
* A procedural macro is identified by its crate metadata, so a change to native code it links, with no change to its Rust source or dependencies, is not seen.
* `rustc`'s incremental compilation needs to be disabled. See [The Cargo Book](https://doc.rust-lang.org/cargo/reference/profiles.html#incremental). sccache exits with an error when `CARGO_INCREMENTAL=1` is set, unless `SCCACHE_ALLOW_INCREMENTAL=1` is set: then only the incremental crates (workspace members and path dependencies) go uncached.
* Crates compiled with `rustc`'s incremental compilation (by default, in the debug profile, cargo's workspace members and path dependencies, see [The Cargo Book](https://doc.rust-lang.org/cargo/reference/profiles.html#incremental)) are not cached. sccache exits with an error when `CARGO_INCREMENTAL=1` is set, unless `SCCACHE_ALLOW_INCREMENTAL=1` is set: then only the incremental crates go uncached.

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.

"are not cached" is a bit misleading now that they can be, maybe "are not cached by default"?

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.

Changed to "are not cached by default", also in the README.

@katout
katout force-pushed the rust-cache-incremental branch from 7bfeedf to 7f68e32 Compare November 13, 2026 12:34
@katout

katout commented Nov 13, 2026

Copy link
Copy Markdown
Contributor Author

Squashed the fixes into one commit. Besides the two you asked for, I deduplicated the two cargo incremental tests, made cache match exactly like 1, and shortened the incremental notes in the docs.

@katout
katout force-pushed the rust-cache-incremental branch from 7f68e32 to 6768db0 Compare November 19, 2026 14:59
Crates compiled with `-C incremental=<dir>` (cargo's default for workspace
members and path dependencies in the dev profile) are never cached, so a
cache shared between checkouts or machines misses on them whenever they
are not built locally yet.

With SCCACHE_ALLOW_INCREMENTAL=cache in the environment of the compile,
they are cached; like `1`, it also allows CARGO_INCREMENTAL=1. The
incremental directory is left out of the cache key and out of the dep-info
and file-names runs of rustc, but the key records that the compile is
incremental, since rustc's outputs differ (e.g. the default
codegen-units). A hit leaves the incremental directory as it was; a miss
compiles incrementally as asked. Such compiles are not distributed. On
Windows the variable's name is matched ignoring case, as env::var does in
the client. Without it, nothing changes, keys included.
@katout
katout force-pushed the rust-cache-incremental branch from 6768db0 to 4bb282f Compare November 19, 2026 15:05
@katout katout changed the title rust: cache incremental compiles with SCCACHE_CACHE_INCREMENTAL=1 rust: cache incremental compiles with SCCACHE_ALLOW_INCREMENTAL=cache Nov 19, 2026
@sylvestre
sylvestre merged commit 6f6e390 into mozilla:main Nov 20, 2026
51 checks passed
@sylvestre

Copy link
Copy Markdown
Collaborator

Thanks for your PR

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