Skip to content

rust: with basedirs set, strip the Cargo build dir from env-dep values - #3051

Open
art-shen wants to merge 3 commits into
mozilla:mainfrom
art-shen:basedirs-cargo-build-root
Open

art-shen wants to merge 3 commits into
mozilla:mainfrom
art-shen:basedirs-cargo-build-root

Conversation

@art-shen

@art-shen art-shen commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Problem: with basedirs set and a shared build.build-dir such as ~/.cache/cargo-build/{workspace-path-hash}, two checkouts of a project still miss on crates that use build-script output, e.g. serde. Their dep-info lists OUT_DIR, and sccache hashes its value, a path inside the build dir. That dir is outside the checkouts, so basedirs don't strip it, and the workspace hash in its path makes it different per checkout, so it can't be added as a basedir either.

Change: with basedirs set, also strip the Cargo build dir from env-dep values. Below it, paths are the same in every workspace.

The build dir is the nearest parent of --out-dir that has .rustc_info.json or CACHEDIR.TAG; Cargo writes these at the top of a build dir. Either can be missing (CACHEDIR.TAG if the dir existed before Cargo ran, .rustc_info.json with CARGO_CACHE_RUSTC_INFO=0), so both are checked. The markers are used instead of a fixed path shape because the layout below the build dir differs between Cargo versions. If neither is found, nothing changes.

Without basedirs, keys stay the same and nothing is read from disk. Vars in SCCACHE_BASEDIRS_VERBATIM_ENV_DEPS stay verbatim. With basedirs and at least one env-dep, the search costs up to two stat calls per parent of --out-dir, up to the build dir. A crate that reads a file from its OUT_DIR at run time can now get another checkout's OUT_DIR from the cache, and that file may be gone. SCCACHE_BASEDIRS_VERBATIM_ENV_DEPS=OUT_DIR avoids this.

New test test_equal_hashes_out_dir_in_cargo_build_root fails without this change.

Tests: cargo nextest run --locked --lib --bins --tests. tests/sccache_args.rs does not compile on main (#3077 dropped the imports for #[serial] and SCCACHE_BIN), so it was not run.

@art-shen art-shen changed the title rust: with basedirs set, key env-deps relative to the Cargo build root rust: with basedirs set, strip the Cargo build root from env-dep values Nov 19, 2026
@art-shen
art-shen force-pushed the basedirs-cargo-build-root branch from 4c17d63 to 1c0c868 Compare November 20, 2026 00:07
@art-shen art-shen changed the title rust: with basedirs set, strip the Cargo build root from env-dep values rust: strip the parent of --out-dir from env-dep values when basedirs is set Nov 20, 2026
@art-shen
art-shen force-pushed the basedirs-cargo-build-root branch from 1c0c868 to 5c4eba7 Compare November 20, 2026 00:18
@art-shen art-shen changed the title rust: strip the parent of --out-dir from env-dep values when basedirs is set rust: with basedirs set, strip the Cargo build dir from env-dep values Nov 20, 2026
@codecov-commenter

codecov-commenter commented Nov 20, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 98.07692% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 72.78%. Comparing base (75ef6fa) to head (de6a763).

Files with missing lines Patch % Lines
src/compiler/rust.rs 98.07% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3051      +/-   ##
==========================================
+ Coverage   72.74%   72.78%   +0.03%     
==========================================
  Files          77       77              
  Lines       44680    44732      +52     
==========================================
+ Hits        32504    32556      +52     
  Misses      12176    12176              

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

@art-shen
art-shen force-pushed the basedirs-cargo-build-root branch 2 times, most recently from 6b6ba89 to 02dfeb6 Compare November 20, 2026 10:13
Comment thread src/compiler/rust.rs Resolved
Comment thread src/compiler/rust.rs Resolved
Comment thread src/compiler/rust.rs Resolved
Comment thread src/compiler/rust.rs Resolved
@art-shen

This comment was marked as outdated.

@art-shen
art-shen force-pushed the basedirs-cargo-build-root branch from 5ac8047 to aa881ed Compare November 23, 2026 22:13
Comment thread src/compiler/rust.rs Resolved
A build script's OUT_DIR points into Cargo's build dir. With a shared build-dir outside the checkout, its path differs per workspace and no basedir covers it.
Without env-deps there is nothing to strip, so the parents of --out-dir need no stat calls. Keys are unchanged.
A crate served from another workspace's entry keeps that workspace's OUT_DIR, so a file it opens there at run time may be gone (a cleaned shared build dir, another machine). Compile-time include! reads are unaffected.
@art-shen
art-shen force-pushed the basedirs-cargo-build-root branch from aa881ed to de6a763 Compare December 2, 2026 19:00
@art-shen

art-shen commented Dec 2, 2026

Copy link
Copy Markdown
Contributor Author

Rebased onto main to fix the conflict with #3065, with two commits on top: efc6932 for the review point, and de6a763, which adds a README note. A crate that reads a file from its OUT_DIR at run time can now get another checkout's path from the cache, and that file may be gone.

My earlier comment said the fixes were in a separate commit. They aren't any more: they're squashed into the first commit.

@art-shen

art-shen commented Dec 2, 2026

Copy link
Copy Markdown
Contributor Author

The unix CI failures aren't from this PR: tests/sccache_args.rs doesn't build on main. 08f94c4 added tests that use #[serial] and SCCACHE_BIN, and #3077, merged after it, removed both imports. Windows jobs pass.

#3071 strips the same env-dep values. I tested both with a crate that include!s a file from OUT_DIR and one that depends on it, building one checkout into a second target dir. Without basedirs, #3071 gets 2 hits on cargo 1.99 but 2 misses on cargo 1.100, whose build dir has no deps dir. This PR misses that case without basedirs; with basedirs set it hits on both. Whichever lands first, the other will need a rebase.

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