Skip to content

fix preprocessor cache mode vs basedirs - #2868

Merged
sylvestre merged 3 commits into
mozilla:mainfrom
avikivity:basedirs-preprocessor-cache-mode
Nov 21, 2026
Merged

sylvestre merged 3 commits into
mozilla:mainfrom
avikivity:basedirs-preprocessor-cache-mode

Conversation

@avikivity

Copy link
Copy Markdown
Contributor

The basedirs feature strips the directory prefix from a cache entry, so two names
in different prefixes (common with worktrees) can intermatch.

Re-add the prefix to the key to avoid different worktrees from interfering with
each other.

Fixes #2863.

@codecov-commenter

codecov-commenter commented Sep 20, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 96.62698% with 17 lines in your changes missing coverage. Please review.
✅ Project coverage is 79.86%. Comparing base (f847172) to head (aeaa5aa).

Files with missing lines Patch % Lines
src/compiler/preprocessor_cache.rs 96.38% 9 Missing ⚠️
tests/system.rs 96.07% 8 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2868      +/-   ##
==========================================
+ Coverage   79.68%   79.86%   +0.17%     
==========================================
  Files          75       75              
  Lines       46663    47158     +495     
==========================================
+ Hits        37185    37661     +476     
- Misses       9478     9497      +19     

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

@avikivity

Copy link
Copy Markdown
Contributor Author

Codecov Report

❌ Patch coverage is 98.41270% with 4 lines in your changes missing coverage. Please review. ✅ Project coverage is 76.46%. Comparing base (0f9467c) to head (96b6479).

Files with missing lines Patch % Lines
tests/system.rs 94.80% 4 Missing ⚠️
Additional details and impacted files

@@            Coverage Diff             @@
##             main    #2868      +/-   ##
==========================================
+ Coverage   76.32%   76.46%   +0.13%     
==========================================
  Files          72       72              
  Lines       40180    40426     +246     
==========================================
+ Hits        30668    30911     +243     
- Misses       9512     9515       +3     

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

These are assertions in tests, not production code.

/// The current format is 1 header byte for the version + bincode encoding
/// of the [`PreprocessorCacheEntry`] struct.
const FORMAT_VERSION: u8 = 1;
const FORMAT_VERSION: u8 = 2;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bumping the version makes read() return UnknownFormat on every entry already in a local cache, and c.rs propagates that with ? so the first build after upgrade fails. could we treat an unknown version as a miss?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

preprocessor_cache_entry_hash_key() hashes the version into the digest, so we won't hit it in the cache anyway.

I'll have unknown versions ignored, it makes sense.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a new commit ignoring entries that cannot be deserialized.

Comment thread src/compiler/c.rs Outdated
// listed share one preprocessor cache entry, so the include files it
// records have to be checked against this tree's copies and not against
// the ones that happened to write the entry.
let tree_root = compilation_tree_root(&absolute_input_path, &cwd, storage.basedirs())?;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could this move inside the use_preprocessor_cache_mode branch? it's only used there

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Comment thread src/compiler/preprocessor_cache.rs Outdated
/// compiled now, which is the only tree whose headers say anything about
/// this compilation. Do not infer this from `path` being relative: the
/// preprocessor can name a file outside every basedir relatively too.
under_basedir: bool,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nobody will read 8 lines here :) please cut it to two

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Comment thread docs/Local.md

- When a source file was compiled and its results were cached, a header file would have been included if it existed, but it did
not exist at the time. sccache does not know about such files, so it cannot invalidate the result if the header file later exists.
With `basedirs` this widens: a header that exists in one checkout and not in another, at a position the compiler searches first

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the same explanation is now in README.md, docs/Caching.md, docs/Configuration.md and here. please keep one and link to it

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@avikivity
avikivity force-pushed the basedirs-preprocessor-cache-mode branch from 96b6479 to 4f5c41e Compare September 27, 2026 09:44
@avikivity

Copy link
Copy Markdown
Contributor Author

v2:

  • ignore unparseable cache entries rather than failing (new commit)
  • moved tree_root computation into branch where it is used
  • trimmed comments
  • deduplicated documentation

@avikivity
avikivity force-pushed the basedirs-preprocessor-cache-mode branch from 4f5c41e to 674fac6 Compare September 27, 2026 09:51
@avikivity

Copy link
Copy Markdown
Contributor Author

v3: rebase past logical conflict and fix fallout

@avikivity

Copy link
Copy Markdown
Contributor Author

v4: rebase

@codspeed

codspeed Bot commented Oct 1, 2026 •

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

1 warning

⚠️ 1 benchmark is dominated by system calls

System calls cannot be simulated consistently and are left out of the measure. The reported value understates the real cost. Switch to the Walltime instrument to measure them.

Learn more about system calls

See affected benchmarks

✅ 62 untouched benchmarks
⏩ 6 skipped benchmarks1


Comparing avikivity:basedirs-preprocessor-cache-mode (aeaa5aa) with main (f847172)

Open in CodSpeed

Footnotes

  1. 6 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

@avikivity

Copy link
Copy Markdown
Contributor Author

Merging this PR will degrade performance by 10.81%

⚠️ Different runtime environments detected

Some benchmarks with significant performance changes were compared across different runtime environments,
which may affect the accuracy of the results.
Open the report in CodSpeed to investigate

❌ 2 regressed benchmarks ✅ 56 untouched benchmarks ⏩ 6 skipped benchmarks1

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Mode Benchmark BASE HEAD Efficiency
❌ Simulation time_macro_finder_chunked 862.8 µs 1,026.4 µs -15.94%
❌ Simulation time_macro_finder_no_macros 2.9 ms 3 ms -5.36%
Tip

Investigate this regression by commenting @codspeedbot fix this regression on this PR, or directly use the CodSpeed MCP with your agent.

Comparing avikivity:basedirs-preprocessor-cache-mode (c29f44c) with main (54b6f72)

Open in CodSpeed

Footnotes

  1. 6 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

I think this is a false positive.

@avikivity

Copy link
Copy Markdown
Contributor Author

@sylvestre please re-review

ran-yuan-rui pushed a commit to ran-yuan-rui/sirius that referenced this pull request Oct 7, 2026
Fixes sirius-db#2031

With SCCACHE_BASEDIRS (sirius-db#631) the cache key no longer depends on the
checkout path, but sccache 0.18.0's preprocessor cache mode (the default
for a disk cache) stores absolute include paths in its manifests and
validates against the first tree's files. Two symptoms follow when
worktrees share a cache:

- a hit can return another checkout's object file when the .cpp is
identical but a header differs (silent wrong code;
mozilla/sccache#2863), and
- the restored depfile lists the other checkout's headers, so ninja
tracks the wrong files and header edits no longer rebuild dependents.

The upstream fix is mozilla/sccache#2868 (not yet released). So if that
is included in an sccache version that we use in our env, this line can
be removed again (noted in the comment).

This should fix potential build issues we actually encountered and
resulted in breaking gb300 nightlies.

## Checklist
- [x] Read CONTRIBUTING.md and ensure PR meets "reviewability" checklist
- [ ] Cover changes with new or existing tests
- [ ] Document configuration changes in code and summarize in the
description above
- [ ] Update human and agent documentation (README.md, docs/, skills,
CLAUDE.md)

## References

Co-authored-by: Claude Sonnet 5.5 <noreply@anthropic.com>
@loganrosen

Copy link
Copy Markdown
Contributor

Tested this (c29f44c) against a reduced version of what we hit in Firefox, where each checkout's srcdir/objdir is in SCCACHE_BASEDIRS and local disk cache uses preprocessor cache mode.

Two trees A and B with an identical main.c that does return V; from h.h, compiled with absolute paths (clang -c $T/src/main.c -I$T/src -o $T/main.o) and SCCACHE_BASEDIRS=$T. Build A, stop the server, then build B, all against the same SCCACHE_DIR.

header differs (A: V=1, B: V=2) headers identical
0.18.0 B hits and returns 1 (A's object) B hits
this PR B misses and returns 2 B hits

Same result with SCCACHE_CLIENT_SIDE true and false. So it fixes the wrong-object case without giving up the cross-checkout hit it exists for. Would be great to get this reviewed; Firefox has turned off its default basedirs for local builds until it lands (https://bugzilla.mozilla.org/show_bug.cgi?id=2086340).

@avikivity

Copy link
Copy Markdown
Contributor Author

v5: rebase

@flip1995

flip1995 commented Nov 4, 2026

Copy link
Copy Markdown
Contributor

I've let claude take a look at this PR, and it found an edge case that this PR doesn't cover: If the header is read from one basedir, but the source file from another basedir.

IMO this seems like a really weird sccache configuration, so it shouldn't be blocking this PR. But I thought, I'd share the reproducer.

Claude analysis

PR #2868 returns a stale object when two basedirs meet in one compilation

Not committed. #2868

The PR records an include file relative to whichever basedir it lies under, but
re-roots it at the basedir the input file lies under. When a compilation reads
a header from one basedir while its source lives in another - a source tree and
an out-of-source build tree, both listed - the lookup validates a path that
belongs to neither: the header's path relative to its own basedir, pasted under
the source's. If a file happens to exist there, the entry validates against a
file the compiler never opened, and a changed header goes unnoticed.

Reproducer

Needs gcc, objdump and sccache on $PATH. Stops and restarts the local
server, and wipes /tmp/sccache-pr2868.

Base directories and compiler arguments are both absolute here, which is the
shape a CMake build has. Base directories can only be absolute in any case:
sccache refuses to start with a relative one.

#!/usr/bin/env bash
set -e

R=/tmp/sccache-pr2868
rm -rf "$R"
mkdir -p "$R/src/gen" "$R/build/gen" "$R/cache"

cat > "$R/src/main.c" <<'EOF'
#include <gen/config.h>

int value(void) { return CONFIG_VALUE; }
EOF

# The header the compiler actually opens, found through -I.
echo '#define CONFIG_VALUE 111' > "$R/build/gen/config.h"
# Same name, same path relative to the other basedir. Never opened: the include
# is in angle brackets, so the source file's own directory is not searched.
echo '#define CONFIG_VALUE 111' > "$R/src/gen/config.h"

cat > "$R/sccache.toml" <<EOF
basedirs = ["/tmp/sccache-pr2868/src", "/tmp/sccache-pr2868/build"]

[cache.disk]
dir = "/tmp/sccache-pr2868/cache"
EOF

# Preprocessor cache mode does not record files modified during the compilation.
touch -d '-30 seconds' "$R/src/main.c" "$R/build/gen/config.h" "$R/src/gen/config.h"

export SCCACHE_CONF="$R/sccache.toml"
sccache --stop-server >/dev/null 2>&1 || true
sccache --start-server >/dev/null

sccache gcc -c "$R/src/main.c" -I"$R/build" -o "$R/first.o"

# Change the header the compiler reads. The one under src/ stays as it was.
echo '#define CONFIG_VALUE 222' > "$R/build/gen/config.h"
touch -d '-30 seconds' "$R/build/gen/config.h"

sccache gcc -c "$R/src/main.c" -I"$R/build" -o "$R/second.o"

echo -n "second.o returns: "
objdump -d --no-show-raw-insn "$R/second.o" | grep -m1 -o '\$0x[0-9a-f]*'
echo "  \$0xde (222) is correct, \$0x6f (111) is the stale object"
sccache --show-stats | grep -E "^Cache (hits|misses) +[0-9]"

What it prints

gcc 16.2.1, Linux, three builds of the same tree:

second.o
0.18.0 (0f9467c) $0xde, 0 hits / 2 misses correct
PR #2868 (96b6479) $0x6f, 1 hit / 1 miss stale object
basedirs-preprocessor-fix $0xde, 0 hits / 2 misses correct

So on this case the PR is a regression against the version it fixes. It does fix
the case in #2863, which this does not contradict.

To see which file it validated, delete /tmp/sccache-pr2868/src/gen/config.h before the second
compilation and run with SCCACHE_LOG=debug SCCACHE_ERROR_LOG=/tmp/sccache.log:

.../src/gen/config.h is in a preprocessor cache entry but can't be read
    (No such file or directory (os error 2))

The header is at build/gen/config.h. Nothing ever looks there.

Same cause, without the second file

Drop /tmp/sccache-pr2868/src/gen/config.h from the reproducer entirely and the result is
correct but preprocessor cache mode stops working: the re-rooted path never
exists, so every compilation is a preprocessor cache miss, including a second
compilation of exactly the same thing in the same directory.

basedirs = [src, build]   0 preprocessor cache hits, 1 miss
basedirs = [src]          1 preprocessor cache hit,  0 misses

The object cache still hits, so this only costs the preprocessing time it was
meant to save. docs/Local.md in the PR says a base directory pointing at a
build tree separate from the source tree "costs hits rather than correctness" -
the hits half matches, the correctness half does not.

Fix

Refusing to record an include whose basedir is not the compilation's tree root
turns the stale object into a plain miss. Recording which basedir each include
came from, and re-rooting each at that one, would keep the hits as well.

@avikivity

Copy link
Copy Markdown
Contributor Author

v6: rebase

@avikivity
avikivity force-pushed the basedirs-preprocessor-cache-mode branch from da3a666 to 6cab607 Compare November 4, 2026 13:19
@avikivity

Copy link
Copy Markdown
Contributor Author

I've let claude take a look at this PR, and it found an edge case that this PR doesn't cover: If the header is read from one basedir, but the source file from another basedir.

IMO this seems like a really weird sccache configuration, so it shouldn't be blocking this PR. But I thought, I'd share the reproducer.

Claude analysis

PR #2868 returns a stale object when two basedirs meet in one compilation

Not committed. #2868

The PR records an include file relative to whichever basedir it lies under, but re-roots it at the basedir the input file lies under. When a compilation reads a header from one basedir while its source lives in another - a source tree and an out-of-source build tree, both listed - the lookup validates a path that belongs to neither: the header's path relative to its own basedir, pasted under the source's. If a file happens to exist there, the entry validates against a file the compiler never opened, and a changed header goes unnoticed.

Reproducer

Needs gcc, objdump and sccache on $PATH. Stops and restarts the local server, and wipes /tmp/sccache-pr2868.

Base directories and compiler arguments are both absolute here, which is the shape a CMake build has. Base directories can only be absolute in any case: sccache refuses to start with a relative one.

#!/usr/bin/env bash
set -e

R=/tmp/sccache-pr2868
rm -rf "$R"
mkdir -p "$R/src/gen" "$R/build/gen" "$R/cache"

cat > "$R/src/main.c" <<'EOF'
#include <gen/config.h>

int value(void) { return CONFIG_VALUE; }
EOF

# The header the compiler actually opens, found through -I.
echo '#define CONFIG_VALUE 111' > "$R/build/gen/config.h"
# Same name, same path relative to the other basedir. Never opened: the include
# is in angle brackets, so the source file's own directory is not searched.
echo '#define CONFIG_VALUE 111' > "$R/src/gen/config.h"

cat > "$R/sccache.toml" <<EOF
basedirs = ["/tmp/sccache-pr2868/src", "/tmp/sccache-pr2868/build"]

[cache.disk]
dir = "/tmp/sccache-pr2868/cache"
EOF

# Preprocessor cache mode does not record files modified during the compilation.
touch -d '-30 seconds' "$R/src/main.c" "$R/build/gen/config.h" "$R/src/gen/config.h"

export SCCACHE_CONF="$R/sccache.toml"
sccache --stop-server >/dev/null 2>&1 || true
sccache --start-server >/dev/null

sccache gcc -c "$R/src/main.c" -I"$R/build" -o "$R/first.o"

# Change the header the compiler reads. The one under src/ stays as it was.
echo '#define CONFIG_VALUE 222' > "$R/build/gen/config.h"
touch -d '-30 seconds' "$R/build/gen/config.h"

sccache gcc -c "$R/src/main.c" -I"$R/build" -o "$R/second.o"

echo -n "second.o returns: "
objdump -d --no-show-raw-insn "$R/second.o" | grep -m1 -o '\$0x[0-9a-f]*'
echo "  \$0xde (222) is correct, \$0x6f (111) is the stale object"
sccache --show-stats | grep -E "^Cache (hits|misses) +[0-9]"

What it prints

gcc 16.2.1, Linux, three builds of the same tree:

second.o
0.18.0 (0f9467c) $0xde, 0 hits / 2 misses correct
PR #2868 (96b6479) $0x6f, 1 hit / 1 miss stale object
basedirs-preprocessor-fix $0xde, 0 hits / 2 misses correct
So on this case the PR is a regression against the version it fixes. It does fix the case in #2863, which this does not contradict.

To see which file it validated, delete /tmp/sccache-pr2868/src/gen/config.h before the second compilation and run with SCCACHE_LOG=debug SCCACHE_ERROR_LOG=/tmp/sccache.log:

.../src/gen/config.h is in a preprocessor cache entry but can't be read
    (No such file or directory (os error 2))

The header is at build/gen/config.h. Nothing ever looks there.

Same cause, without the second file

Drop /tmp/sccache-pr2868/src/gen/config.h from the reproducer entirely and the result is correct but preprocessor cache mode stops working: the re-rooted path never exists, so every compilation is a preprocessor cache miss, including a second compilation of exactly the same thing in the same directory.

basedirs = [src, build]   0 preprocessor cache hits, 1 miss
basedirs = [src]          1 preprocessor cache hit,  0 misses

The object cache still hits, so this only costs the preprocessing time it was meant to save. docs/Local.md in the PR says a base directory pointing at a build tree separate from the source tree "costs hits rather than correctness" - the hits half matches, the correctness half does not.

Fix

Refusing to record an include whose basedir is not the compilation's tree root turns the stale object into a plain miss. Recording which basedir each include came from, and re-rooting each at that one, would keep the hits as well.

Fixed in v7

Comment thread tests/system.rs Outdated

let compiler = match find_compilers()
.into_iter()
.find(|c| c.name == "gcc" || c.name == "clang")

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the compiler lookup + daemon setup is now copied in 3 tests, could you please dedup it?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added find_gcc_or_clang and restart_local_daemon_with helpers.

Comment thread src/util.rs
basedirs
.iter()
.filter(|basedir| haystack.starts_with(basedir))
.map(|basedir| basedir.len())

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

on windows this length comes from the normalized path, but callers slice the original encoded with it.
to_lowercase can change the byte length of non-ASCII chars, so the split could be off. could we normalize both or map back?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added unnormalized_len() helper

@avikivity
avikivity force-pushed the basedirs-preprocessor-cache-mode branch from b5d6860 to 5ac59e1 Compare November 9, 2026 15:11
@avikivity

Copy link
Copy Markdown
Contributor Author

v10:

  • Replaced awkward nested Option with new enum ValidFor
  • use Windows-safe length measurement for paths
  • added helper for encoding the key
  • added helpers for finding the compiler and restarting the daemon

Comment thread src/util.rs Outdated
.filter(|(_, b)| is_separator(b))
.nth(separators - 1)
.map(|(i, _)| i + 1)
.unwrap()

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could this return an Option (or use expect with a reason) instead of unwrap()? separators - 1 also underflows if a basedir ever comes without a trailing separator

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, returning an Option, and avoiding underflow.

Comment thread src/compiler/preprocessor_cache.rs Outdated

/// Split an included file's absolute path at the basedir it lies under, if
/// that is `tree_root`, and tell whether it lies under another basedir.
fn strip_basedir(

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

strip_basedir is too close to util::strip_basedirs, which does something else. maybe relativize_include?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@avikivity
avikivity force-pushed the basedirs-preprocessor-cache-mode branch from 5ac59e1 to ca3cd0a Compare November 12, 2026 11:35
@avikivity

Copy link
Copy Markdown
Contributor Author

v11:

  • massaged basedir_prefix_len() to be less dependent on the correctness of its input
  • renamed strip_basedir to relativize_include

@avikivity
avikivity force-pushed the basedirs-preprocessor-cache-mode branch from ca3cd0a to 18ff55e Compare November 16, 2026 12:41
@avikivity

Copy link
Copy Markdown
Contributor Author

v12: rebased

Comment thread docs/Local.md Outdated
mtime, ctime, device and inode are unchanged, so a cache hit does not re-read unchanged headers. Files changed less than two
seconds before being hashed are not remembered, as their timestamps may not tell a quick rewrite apart.

When [`basedirs`](Configuration.md) is set, every checkout listed shares a preprocessor cache entry, and the

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you please cut it to 3-4 lines? thanks

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trimmed.

Comment thread src/compiler/preprocessor_cache.rs Outdated
return Ok((absolute(), false));
};
let in_tree_root = match tree_root {
Some(tree_root) => same_basedir(&encoded[..len], &encode_dir(tree_root)?),

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

encode_dir(tree_root) is recomputed for every include, could it be done once in add_result?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Comment thread src/compiler/preprocessor_cache.rs Outdated
fn admits(&self, tree_root: Option<&Path>) -> bool {
match (self, tree_root) {
(Self::AnyTree, _) => true,
(Self::Tree(pinned), Some(root)) => pinned == root.as_os_str(),

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same_basedir normalizes on windows but this compares exactly, so the same tree with a different case won't match. normalize here too?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@avikivity
avikivity force-pushed the basedirs-preprocessor-cache-mode branch from 18ff55e to a740560 Compare November 16, 2026 16:36
@avikivity

Copy link
Copy Markdown
Contributor Author

v13:

  • trimmed over-long description of basedirs in Local.md
  • encode_dir now runs once per include
  • path comparison on Windows in admits() is normalized

Comment thread src/compiler/preprocessor_cache.rs Outdated

let header_a = tree_a.join("inc").join("cfg.h");
let mut entry = PreprocessorCacheEntry::new();
entry.add_result(

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the add_result + lookup_result_digest setup is copied in most of these new tests, could be a small helper, no?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, extracted into two new helpers

@avikivity
avikivity force-pushed the basedirs-preprocessor-cache-mode branch from a740560 to 5a0ffaf Compare November 17, 2026 20:39
@avikivity

Copy link
Copy Markdown
Contributor Author

v14: two new helpers for tests: entry_with_header() and hits() to reduce code repetition

@sylvestre

Copy link
Copy Markdown
Collaborator

sorry but it needs to be rebased

avikivity and others added 3 commits November 21, 2026 18:18
A preprocessor cache entry that fails to read or deserialize, such as
one truncated by a crash or written in another format, failed the
whole compilation. Fall back to preprocessing instead; the miss path
then overwrites the entry.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
With basedirs set, the preprocessor cache entry key no longer names one
checkout, so every checkout listed shares an entry. The include files the
entry records were absolute paths into whichever checkout wrote it, so the
lookup validated that checkout's headers against themselves, always matched,
and handed back its object cache key. A second checkout whose headers differ
got the first one's object file.

Record an include that lives under the basedir of the tree being compiled
relative to it, and re-root it at lookup time at the tree being compiled:
the basedir containing the input file, or the working directory if the input
is outside all of them. Includes outside every basedir stay absolute.

An include under a different basedir, such as a separate build tree that is
also listed, cannot be re-rooted: a list of basedirs does not say which build
tree belongs to which checkout, and re-rooting it at the source's basedir
checks a file the compiler never opened. Keep its absolute path and pin the
result that recorded it to the writer's tree root, so it only counts for
compilations of that tree.

Bump FORMAT_VERSION; entries written before this cannot be read under the
new rule.

Fixes mozilla#2863.

Co-authored-by: Logan Rosen <loganrosen@gmail.com>
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The include list records only the files the preprocessor opened, not the
directories it searched and found empty, so two checkouts that resolve an
include to different files can still share a result. Saying that differing
headers never share one claimed more than the code does.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@avikivity
avikivity force-pushed the basedirs-preprocessor-cache-mode branch from 5a0ffaf to aeaa5aa Compare November 21, 2026 16:19
@avikivity

Copy link
Copy Markdown
Contributor Author

sorry but it needs to be rebased

No problem.

@avikivity

Copy link
Copy Markdown
Contributor Author

v15: rebased

@sylvestre
sylvestre merged commit 4a138d7 into mozilla:main Nov 21, 2026
50 checks passed
@sylvestre

Copy link
Copy Markdown
Collaborator

thanks and sorry it took forever!

giancao pushed a commit to giancao/sirius that referenced this pull request Nov 27, 2026
## Description

Bumps sccache from 0.18.0 to 0.20.1 in the Pixi environment and in the
`sccache-action` `version` inputs of `check.yml` and `test.yml`, which
must match. 0.20.1 rather than 0.20.0 because 0.20.0 could fail to start
the server with the GHA cache backend under rate limiting.

`SCCACHE_DIRECT = "false"` stays. 0.20 includes mozilla/sccache#2868,
which fixes the wrong-object half of the original problem. Depfiles are
still broken, though: a preprocessor cache hit restores the depfile
stored with the shared, basedir-stripped object entry. After a clean
rebuild in a second worktree, ninja then tracks the first worktree's
headers, and local header edits stop triggering rebuilds. I reproduced
this with 0.20.1 on a two-checkout ninja setup. With direct mode off,
depfiles are generated locally and edits rebuild correctly. The comment
now states the condition for removing it.

Expect a cold sccache cache on the first CI runs after the version
change.

## Checklist
- [x] Read CONTRIBUTING.md and ensure PR meets "reviewability" checklist
- [x] Cover changes with new or existing tests
- [x] Document configuration changes in code and summarize in the
description above
- [ ] Update human and agent documentation (README.md, docs/, skills,
CLAUDE.md)

## References

- https://github.com/mozilla/sccache/releases/tag/v0.20.0
- https://github.com/mozilla/sccache/releases/tag/v0.20.1
lando-worker Bot pushed a commit to mozilla-firefox/firefox that referenced this pull request Nov 29, 2026
…ccache 0.20 or later. r=firefox-build-system-reviewers,sylvestre

sccache 0.20 fixes the preprocessor cache returning objects built in another
checkout with basedirs set (mozilla/sccache#2868), so the automation-only
limit from bug 2086340 is no longer needed with it. As before that bug,
objects shared between local checkouts keep the original checkout's paths
in their debug information unless path remapping is enabled.

Differential Revision: https://phabricator.services.mozilla.com/D347164
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 + Preporcessor cache mode hands out another checkout's object file

5 participants