Skip to content

SCCACHE_BASEDIRS support for Worktree workflow #2595

Open

Description

@howardjohn

SCCACHE_BASEDIRS added in 7e02a2f is a great step to help enable cache hits across copies of repos, but doesn't work well for common git worktree workflows, since we need to statically set each base dir. This breaks a few workflows.

For example, consider a project at /home/projects/hello-world. Some common workflows I have seen are...

  • Worktrees in /home/projects/hello-world/.worktrees/<branchname>/...
  • $HOME/.local/share/opencode/worktree/<some UUID>/<some name>/...
    etc

It would be nice to be able to have these all use the shared cache. This is especially important with AI coding use cases (which is a common usage for worktrees), where all new work is done in a new branch. Usage of sccache (if this can be supported) would cut multiple minute build times at the start of each piece of work

Activity

  1. howardjohn commented on Feb 13, 2026

    @howardjohn
    Author

    Actually looking a bit more closely it seems like this only works for C anyways not rust so it wouldn't be relevant for my use

  2. HuaHuaY commented on May 11, 2026

    @HuaHuaY

    I have tried SCCACHE_BASEDIRS and it seems to work. For examples, my project is at /foo and one of my worktree is at /foo/.worktree/bar. SCCACHE_BASEDIRS should be /foo:/foo/.worktree/bar.

  3. johnkferguson commented on May 26, 2026

    @johnkferguson

    Was able to get cross-worktree Rust dedup working in a fork and wanted to share what was needed to actually get it working end-to-end,. Fork is on top of the existing Vercel rust-basedirs work.

    1. Glob patterns in SCCACHE_BASEDIRS (so $HOME/repos/*/worktrees/*/*/* works without enumerating random worktree names).
    2. Per-token argv strip before concatenation — cargo emits --remap-path-prefix /abs=virtual as two tokens, and the post-concat blob scan can't see the / boundary. Stripping each token in isolation catches it.
    3. Strip every CARGO_* env value, not a hand-coded allowlist. devenv injects CARGO_INSTALL_ROOT per-project; the existing allowlist missed it and that one var broke the hash key for every workspace crate.

    Companion requirement: set RUSTFLAGS=--remap-path-prefix=$PROJECT_ROOT=/proj per project. sccache normalises what it hashes, but it also hashes extern .rmeta content, and rustc embeds absolute paths inside rmeta. Without the remap, workspace-internal dep rmetas differ between worktrees and the dedup chain breaks downstream.

    Result on a real workspace (vault-server, 7 crates, ~300 rustc invocations): 0% → 98.6% cross-worktree Rust hit rate, ~2m → ~1m wall-clock. The 4 remaining misses are cargo build probes (path-sensitive fingerprint in OUT_DIR); not a sccache-side fix.

    Full patches + writeup: johnkferguson#1

  4. tcashel commented on Aug 18, 2026

    @tcashel

    I’m working on this because I build the same Rust project from several worktrees at once. Those checkouts have different absolute paths, so they currently miss the cache even when the source is identical.

    I split the work into two draft PRs so each behavior can be reviewed on its own:

    1. PR #2813 makes Rust cache keys honor SCCACHE_BASEDIRS.
    2. PR #2814 lets each compiler invocation tell one long-running sccache daemon which checkout root applies to that request. If SCCACHE_BASEDIRS is absent, sccache uses the config-file value. If it is present but empty, that request does not use a basedir.

    The same rules apply in normal daemon mode and with SCCACHE_CLIENT_SIDE=1. The caller passes one exact checkout root; sccache does not scan Git worktrees, expand wildcards, or special-case the tools that create them. The follow-up uses environment data already in compile requests, so it does not change the wire protocol.

    PR #2814 is one commit on top of #2813. Until #2813 merges, GitHub includes both commits in the second PR; view the follow-up-only diff. I’ll rebase #2814 onto main after #2813 merges.

    PR #2813 overlaps #2678 and #2794, so I opened it as a draft alternative.

    I took a closer look at the earlier implementations before asking anyone to choose between them. #2678 established the Rust basedir work. #2794 builds on it and takes a stricter artifact-safety approach: it requires rustc path remapping and rebuilds cached dep-info. John Ferguson’s Vercel-based experiment adds glob roots and reports 287/291 hits on a real workspace. A configured glob can cover future worktrees when their paths follow a known pattern.

    The separate piece in this stack is #2814. It lets each compiler invocation supply one exact checkout root to the same running daemon, regardless of where that worktree was created. This avoids requiring every checkout to match a daemon-wide wildcard and keeps concurrent requests isolated. #2813 is a smaller opt-in alternative for the Rust-key portion, but it has a documented tradeoff: a cache hit can retain the producer checkout’s embedded path, while #2794 is more conservative. If maintainers prefer #2794’s Rust-key model, I’m happy to adapt the request-scoped work from #2814 to it.

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