Skip to content

fix(depgraph): normalize workspace-path inputs out of rustc cache key (#139) - #160

Merged
zackees merged 2 commits into
mainfrom
fix/depgraph-cache-key-normalize
May 10, 2026
Merged

zackees merged 2 commits into
mainfrom
fix/depgraph-cache-key-normalize

Conversation

@zackees

@zackees zackees commented May 10, 2026

Copy link
Copy Markdown
Collaborator

Summary

Fixes item #1 of #139 — cache hits sat at 0.35% (2227 miss / 8 hit / 53 err in the compile journal) because the rustc cache key embedded inputs that vary per on-disk workspace location. Same project checked out at ...\zccache and ...\zccache2 produced disjoint key spaces; every dep crate cold-missed.

After this change, key inputs are independent of workspace location while remaining sensitive to everything that actually affects compiled output.

TDD split (two atomic commits on the branch)

Branch keeps RED and GREEN as separate commits for posterity. PR will squash-merge.

RED — 6c0a3bd test(depgraph): RED tests pin path-independence of rustc cache key

Four new tests in crates/zccache-depgraph/src/context.rs:

  • rustc_context_key_ignores_cargo_manifest_dir — same crate at two paths must hash equal.
  • rustc_context_key_ignores_cargo_manifest_path — Cargo.toml path is volatile.
  • rustc_context_key_sensitive_to_cargo_pkg_version — negative control; passes on main and after fix.
  • rustc_context_key_ignores_extern_directory_prefix — same-named rmeta in two target dirs must hash equal.

Confirmed RED (3 of 4 failing) before the GREEN commit; failure output is in the RED commit body.

GREEN — 0fb9338 fix(depgraph): normalize path-dependent inputs out of rustc cache key

  1. New VOLATILE_CARGO_ENV_VARS constant in context.rs listing CARGO_MANIFEST_DIR and CARGO_MANIFEST_PATH. Filtered both in from_parsed_args and inside context_key() (defense-in-depth — the public struct may be hand-built).
  2. Extern paths hash only Path::file_name() via a new extern_path_key helper. Cargo's per-package metadata= hash is part of the file name, so identity is preserved; the absolute directory prefix is dropped.

What is intentionally kept:

  • CARGO_PKG_VERSION, CARGO_PKG_NAME, CARGO_PKG_AUTHORS, every other CARGO_PKG_* — they feed env!() macros baked into rustc output. The version-sensitivity test asserts this.
  • The existing rustc_different_extern_paths_different_key test (different file names) still passes, so distinct rlibs still get distinct keys.

What was considered and rejected:

  • Hashing extern paths relative to a workspace root: rustc invocations don't have a single stable root, and cargo's metadata= filename already carries the identity bit. Using only the filename is simpler and strictly stronger.
  • Filtering by ad-hoc allowlist of CARGO_PKG_*: would silently drop future CARGO_* env vars cargo introduces. Denylist is conservative.

Test plan

  • soldr cargo test -p zccache-depgraph --lib — 318 passed, 0 failed (315 prior + 3 new RED→GREEN).
  • soldr cargo check --workspace --all-targets — clean.
  • soldr cargo clippy --workspace --all-targets -- -D warnings — clean.
  • soldr cargo fmt --all -- --check — clean.
  • ./test — all green, no real failures (only a benign soldr session-end daemon-not-running message at the very end of the run).
  • CI Linux / macOS / Windows passes (pre-existing reds on the windows / Check soldr-bundled-version block and daemon: event_log::test_rotation_triggers_at_threshold flakes on slow CI runners #146 event-log flake are unrelated to this PR).

Hit-rate impact

Not measurable in CI — the journal lives on the developer's machine and the same-workspace path doesn't vary across CI runs. The path-independence unit tests are the proxy: T1/T2/T4 exercise exactly the invalidation vector that produced the 0.35% number. Expected outcome on the developer's machine: turn 0.35% into 80%+ across project clones and Cargo.toml edits.

Closes #139

🤖 Generated with Claude Code

zackees and others added 2 commits May 10, 2026 16:38
)

Adds four failing tests asserting that `RustcCompileContext::context_key`
is invariant under workspace-location changes. Demonstrates fix #1 from
issue #139 — cache hits at 0.35% because the same crate at two on-disk
paths gets two different keys.

New tests:
- rustc_context_key_ignores_cargo_manifest_dir — vary CARGO_MANIFEST_DIR
- rustc_context_key_ignores_cargo_manifest_path — vary CARGO_MANIFEST_PATH
- rustc_context_key_sensitive_to_cargo_pkg_version — negative control,
  PASSES on main (CARGO_PKG_VERSION must still affect the key)
- rustc_context_key_ignores_extern_directory_prefix — same-filename
  rmeta in two different target dirs must hash the same

Current failure output (RED state):

    ---- rustc_context_key_ignores_cargo_manifest_dir ----
    assertion `left == right` failed: CARGO_MANIFEST_DIR is volatile ...

    ---- rustc_context_key_ignores_cargo_manifest_path ----
    assertion `left == right` failed: CARGO_MANIFEST_PATH is volatile ...

    ---- rustc_context_key_ignores_extern_directory_prefix ----
    assertion `left == right` failed: extern rmeta paths with the same
    filename ... must produce equal cache keys ...

    test result: FAILED. 315 passed; 3 failed.

GREEN implementation follows in next commit.

Refs #139

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
…#139)

Cache hits sat at 0.35% (2227 misses / 8 hits / 53 errors in the compile
journal) because the rustc cache key embedded inputs that vary per
on-disk workspace location. Same project at `...\zccache` vs
`...\zccache2` produced two disjoint key spaces, so every dep-crate
compile cold-missed.

Two changes:

1. Strip volatile CARGO_* env vars before hashing.
   Introduces `VOLATILE_CARGO_ENV_VARS = ["CARGO_MANIFEST_DIR",
   "CARGO_MANIFEST_PATH"]`. Both are absolute paths handed in by cargo
   and neither affects compiled output in a way that other key
   components do not already cover (the source path is hashed directly,
   `CARGO_PKG_*` carries the identity of the crate). The filter is
   applied in `from_parsed_args` AND in `context_key()` itself —
   defense-in-depth so the public struct cannot be hand-built into a
   path-sensitive key.

   Kept (and asserted by negative-control test): `CARGO_PKG_VERSION`,
   `CARGO_PKG_NAME`, every other `CARGO_PKG_*` — these feed `env!()`
   macros and are baked into rustc output.

   Already-excluded for orthogonal reasons (unchanged): `CARGO_MAKEFLAGS`
   (job-server token), `CARGO_INCREMENTAL` (handled via `-C incremental`
   stripping).

2. Hash only the file-name component of `--extern name=path` values.
   Cargo embeds its per-package `metadata=` hash in the file name
   (`libserde-abc123.rmeta`), which uniquely identifies the extern's
   identity. The directory prefix is incidental — it changes per
   workspace location and per target-dir layout, and including it
   cascaded misses through the entire dep graph after any move.

   Two extern values with different file names (e.g. `liba_v1.rlib` vs
   `liba_v2.rlib`) still produce different keys, so the existing
   `rustc_different_extern_paths_different_key` test continues to pass.

Tests added in the preceding RED commit (T1, T2, T4) now pass; T3
(`CARGO_PKG_VERSION` sensitivity) was already passing on main and
continues to pass — the filter is intentionally narrow.

    test result: ok. 318 passed; 0 failed; 0 ignored

Refs #139

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@codecov

codecov Bot commented May 10, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 94.59459% with 4 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
crates/zccache-depgraph/src/context.rs 94.59% 4 Missing ⚠️

📢 Thoughts on this report? Let us know!

@zackees
zackees merged commit facbada into main May 10, 2026
8 of 9 checks passed
@zackees
zackees deleted the fix/depgraph-cache-key-normalize branch May 10, 2026 23:57
zackees added a commit that referenced this pull request May 11, 2026
Patch bump covering everything merged since v1.4.0:

- fix(daemon,ci): tolerate unknown sessions and stop killing soldr's
  daemon (#168, closes #166 + #167)
- feat(cli): expose session_end_idempotent for all daemon callers (#163)
- fix(daemon): deterministic flush primitive for event log (#162)
- feat(cli,daemon): relocate daemon exe to global runtime-binaries dir (#158)
- fix(depgraph): normalize workspace-path inputs out of rustc cache key (#160)
- perf(ci): trim platform Test, drop --all-targets from Check, split
  Test/Check jobs (#161, #164, #165)
- chore(ci): drop invalid target-cache-paths from build-target action (#157)

Co-authored-by: Claude Opus 4.7 (1M context) <[email protected]>
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.

perf(stop-hook): zccache cache hits 0.2% — Stop-hook is 5 sequential builds

1 participant