Skip to content

Wire SCCACHE_BASEDIRS into Rust hash key #2652

Open

Description

@weihanglo

Problem

SCCACHE_BASEDIRS was done by #2521. Pretty impressive!

However, that only deals with C compiler. Rust compiler wasn't covered.

In Rust, the most popular build tool Cargo by default using absolute paths everywhere. Such as

I think at least we need to implement a basic support to eliminate some of the most common absolute paths from the cache key, like --remap-path-prefix=/abs/path, -Clinker=/abs/path, -Clin-arge=something,/abs/path, and others.

Drawback

SCCACHE_BASEDIRS only normalize paths for cache keys not the real compiler invocations. That means embedded path in binaries are shared across different build root.

This might be a user problem, as they can fix it via prefix-map flag, like -fdebug-prefix-map, -ffile-prefix-map, -fmacro-prefix-map in C compiler and --remap-path-prefix in rustc.

Activity

  1. weihanglo commented on Mar 24, 2026

    @weihanglo
    ContributorAuthor

    This may need #2651 when taking --remap-path-prefix usage into account.

  2. AdamGS commented on Apr 14, 2026

    @AdamGS

    I have some work that at least passes artificial tests, are there any integration tests or other things you think might be useful to run before I put up a PR?

  3. mmastrac commented on Apr 14, 2026

    @mmastrac
    Contributor

    I have some work that at least passes artificial tests, are there any integration tests or other things you think might be useful to run before I put up a PR?

    @AdamGS, we've got a somewhat-tested-but-sloppy patch that isn't ready to upstream available at https://github.com/vercel/sccache/pull/10/changes if you'd like to steal any of the tests and/or functionality from it.

  4. weihanglo commented on Apr 14, 2026

    @weihanglo
    ContributorAuthor

    -Clin-arge=something,/abs/path is one kind of patterns that easily forget to handle.

  5. kennethsinder commented on Aug 10, 2026

    @kennethsinder

    Confirmed on sccache 0.17.0, including client-side mode. Two byte-identical Rust rlib compilations from sibling checkout roots produced two misses even when SCCACHE_BASEDIRS contained both source and target roots. The daemon reported the bases correctly.

    There is an additional Rust-specific key input beyond path-bearing arguments: RustHasher::generate_hash_key hashes cwd directly as input 9 because the directory can appear in the rlib. Base-directory normalization therefore cannot become sound by changing only the key. The compile must also use an equivalent --remap-path-prefix, and the differing source half of that flag must itself be normalized before hashing.

    A useful regression should compile the same rlib from two sibling roots, not only compare constructed argument hashes. Expected: first miss, second Rust hit, and neither artifact contains the physical checkout root.

    We are mitigating this downstream with an immutable, toolchain-keyed Cargo target snapshot restored by filesystem copy-on-write. It preserves lane isolation and lets Cargo validate its own fingerprints, but fixing this issue would recover the compiler-object layer too.

  6. xdustinface commented on Oct 7, 2026

    @xdustinface
    Contributor

    Some numbers on what this is worth, from a project that builds in many git worktrees. These are on macOS: a full cargo build of a crate with about 220 dependency compiles, with a second worktree building the same commit after the first through one sccache server.

    Most of the misses #2882 leaves trace back to serde_core. Its key includes its OUT_DIR env-dep, which sits under each checkout's target directory. So it misses and is rebuilt, and the new rlib then changes the key of everything that depends on it. Normalising OUT_DIR is what takes it from 182 to 221.

    Two things we needed beyond what #2813 does, in case they help:

    • Among env-deps we only normalised OUT_DIR. Another env-dep holding the base dir, like env!("CARGO_MANIFEST_DIR"), can end up in running code, so for those crates we kept the unnormalised key rather than share an artifact carrying another checkout's path.
    • After a hit from another checkout, the replayed dep-info file still names that checkout's paths, including OUT_DIR, and cargo reads them back. We rewrite the stored base dir to the current one on a hit.

    Happy to test #2813 against the same setup, or turn either of these into a follow-up on top of it.

  7. loganrosen commented on Oct 11, 2026

    @loganrosen
    Contributor

    Some data from trying #2813 + #2814 on Firefox (macOS arm64). Firefox gets built in lots of git worktrees and vendors all its crates in-tree under third_party/rust, so it hits a few things beyond the registry-deps case.

    Linker paths. Even with both PRs applied, a leaf crate still got different keys across worktrees. Firefox sets CARGO_TARGET_AARCH64_APPLE_DARWIN_LINKER=<srcdir>/build/cargo-linker, which gets hashed verbatim as a CARGO_ env var, and cargo turns it into -C linker=<srcdir>/build/cargo-linker, whose value isn't normalized either. Normalizing both (the linker doesn't affect rlib output) gave identical keys for that crate. I have this as a small patch on top of #2813 if it's useful.

    Proc-macros. After that, everything downstream of a proc-macro misses. Proc-macros aren't cacheable, so each worktree builds its own, and their dylib contents go into dependents' keys. #2882 handles the macOS output-path part, but it isn't enough here: because the crates are vendored inside each checkout, the .rustc metadata section itself contains the checkout path. All 59 of our proc-macros differed in that section between two worktrees. Firefox only adds --remap-path-prefix to target RUSTFLAGS, and since cargo is invoked with --target, host/proc-macro builds don't get it.

    I checked that remapping fixes this. Building serde_derive 1.0.228 with syn/quote/proc-macro2 from two checkouts' vendored dirs:

    • without remap: all rlibs and serde_derive's .rustc section differ
    • with --remap-path-prefix=<checkout>=/topsrcdir: the syn/quote/proc-macro2 rlibs are byte-identical and serde_derive's .rustc section is identical. The dylib as a whole still differs because of the output path, which is what rust: hash proc-macro externs by their crate metadata #2882 covers.

    So for projects that vendor crates (or use path deps, see #2870), getting cross-checkout Rust hits seems to need all of: #2813/#2814, normalizing -C linker/CARGO_TARGET_*_LINKER, #2882, and remapping paths for host builds too. That last part is either up to the project (which needs -Zhost-config/target-applies-to-host today) or sccache adding a remap for basedirs itself, like #2765 suggests.

    Also for anyone testing this locally: #2863 reproduces in client-side mode, which is Firefox's default, so I backed out my patched build until #2868 lands.

  8. weihanglo commented on Oct 11, 2026

    @weihanglo
    ContributorAuthor

    From my experiment, to get full cross-workspace cache reuse, you probably need trim-paths support (cargo native --remap-path-prefix support). It will be stabilized at 1.101.

  9. loganrosen commented on Oct 19, 2026

    @loganrosen
    Contributor

    Thanks, that was it. With nightly 1.101 and trim-paths = "all", plus #2813, #2814, #2882 and a local patch normalizing the linker path, a second Firefox worktree got 705/712 Rust hits (details in #2813 (comment)).

    It's also enough to set it only for host builds (CARGO_PROFILE_RELEASE_BUILD_OVERRIDE_TRIM_PATHS=all). Proc-macro metadata then no longer embeds the checkout path, and every target crate hit without remapping target builds at all. That's useful for projects like Firefox that don't want to change paths in shipped debug info.

  10. Kostoprav19 commented on Oct 26, 2026

    @Kostoprav19

    Hello! Thanks everyone pushing this change - when we can expect it to be shipped?

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    Priority

    None yet

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions