Skip to content

fix: strip SCCACHE_BASEDIRS from escaped-backslash paths on Windows#2736

Open
rconde01 wants to merge 1 commit into
mozilla:mainfrom
rconde01:fix-basedirs-windows-escaped-backslashes
Open

fix: strip SCCACHE_BASEDIRS from escaped-backslash paths on Windows#2736
rconde01 wants to merge 1 commit into
mozilla:mainfrom
rconde01:fix-basedirs-windows-escaped-backslashes

Conversation

@rconde01

@rconde01 rconde01 commented Jun 13, 2026

Copy link
Copy Markdown

fix: strip SCCACHE_BASEDIRS from escaped-backslash paths on Windows

Fixes #2737.

Problem

SCCACHE_BASEDIRS has no effect on Windows. The same source compiled
from two different checkout directories produces different cache keys, so
nothing is shared across roots — the feature silently does nothing.

Root cause

On Windows, paths inside preprocessor output are emitted as C string
literals
. MSVC #line directives and clang GNU linemarkers quote them
with escaped backslash separators:

#line 1 "C:\\Users\\me\\project\\src\\main.c"

normalize_win_path converts every backslash to a forward slash, so each
escaped separator \\ becomes a double forward slash:

c://users//me//project//src//main.c

But the normalized basedir uses single slashes (c:/users/me/project/),
so strip_basedirs never finds a boundary match, and the absolute path is
left in the preprocessed output that feeds the hash.

Fix

In strip_basedirs, also search for a doubled-separator variant of each
basedir, produced by a new double_path_separators helper. Escaped paths
are now stripped in addition to the existing plain form; mixed
escaped/plain occurrences and UNC basedirs are handled too.

The extra needle and the helper are gated behind
cfg(target_os = "windows"), so non-Windows builds and behavior are
unchanged.

Tests

New unit tests (Windows-only):

  • util::tests::test_strip_basedir_windows_escaped_backslashes — MSVC
    #line, clang linemarker, and mixed escaped/plain occurrences.
  • util::tests::test_strip_basedir_windows_escaped_unc — escaped UNC
    basedir.
  • compiler::c::test::test_hash_key_basedirs_windows_escaped_backslashes
    — the same source under two different basedir roots hashes equally, and
    differs without basedirs.

All three fail before the change and pass after. cargo test --lib,
cargo fmt --check, and cargo clippy --lib are clean on the changed
code (the single pre-existing clippy warning in msvc.rs is untouched).

Manual end-to-end verification

Reproduced with real toolchains on Windows: building the same source from
two distinct absolute directories with SCCACHE_BASEDIRS set goes from
0/2 cache hits (unpatched) to 1/2 (patched) for both clang and
MSVC.

On Windows, paths in preprocessor output are emitted as C string
literals: MSVC `#line` directives and clang GNU linemarkers quote them
with escaped backslash separators, e.g.

    #line 1 "C:\\Users\\me\\project\\src\\main.c"

`normalize_win_path` converts every backslash to a forward slash, so
each escaped separator `\\` becomes a DOUBLE forward slash:

    c://users//me//project//src//main.c

The normalized basedir, however, uses single slashes
(`c:/users/me/project/`), so `strip_basedirs` never finds a match and
SCCACHE_BASEDIRS silently has no effect on Windows. Cache entries stay
tied to absolute source paths and are not shared across checkout
directories.

Fix: in `strip_basedirs`, also search for a doubled-separator variant of
each basedir (new `double_path_separators` helper), so escaped paths are
stripped as well. Both the escaped and plain forms are handled, including
UNC basedirs. The extra needle and helper are gated behind
`cfg(target_os = "windows")` so non-Windows builds are unchanged.

Adds unit tests covering MSVC `#line`, clang linemarkers, mixed
escaped/plain occurrences, UNC paths, and a hash-key test asserting the
same source compiled from two different basedir roots hashes equally.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@codecov-commenter

codecov-commenter commented Jun 14, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 96.19048% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 74.56%. Comparing base (5d52f91) to head (d927749).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
src/util.rs 94.73% 4 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2736      +/-   ##
==========================================
+ Coverage   74.51%   74.56%   +0.05%     
==========================================
  Files          70       70              
  Lines       39652    39747      +95     
==========================================
+ Hits        29546    29639      +93     
- Misses      10106    10108       +2     

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

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.

SCCACHE_BASEDIRS has no effect on Windows (escaped backslashes in preprocessor output never match)

2 participants