Repository navigation
fix preprocessor cache mode vs basedirs - #2868
Conversation
Codecov Report❌ Patch coverage is
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. 🚀 New features to boost your workflow:
|
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; |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Added a new commit ignoring entries that cannot be deserialized.
| // 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())?; |
There was a problem hiding this comment.
could this move inside the use_preprocessor_cache_mode branch? it's only used there
| /// 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, |
There was a problem hiding this comment.
nobody will read 8 lines here :) please cut it to two
|
|
||
| - 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 |
There was a problem hiding this comment.
the same explanation is now in README.md, docs/Caching.md, docs/Configuration.md and here. please keep one and link to it
96b6479 to
4f5c41e
Compare
|
v2:
|
4f5c41e to
674fac6
Compare
|
v3: rebase past logical conflict and fix fallout |
674fac6 to
c29f44c
Compare
|
v4: rebase |
Merging this PR will not alter performance1 warning
Comparing Footnotes
|
I think this is a false positive. |
|
@sylvestre please re-review |
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>
|
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
Same result with |
c29f44c to
312a24d
Compare
|
v5: rebase |
|
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 analysisPR #2868 returns a stale object when two basedirs meet in one compilationNot committed. #2868 The PR records an include file relative to whichever basedir it lies under, but ReproducerNeeds gcc, objdump and Base directories and compiler arguments are both absolute here, which is the #!/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 printsgcc 16.2.1, Linux, three builds of the same tree:
So on this case the PR is a regression against the version it fixes. It does fix To see which file it validated, delete The header is at Same cause, without the second fileDrop The object cache still hits, so this only costs the preprocessing time it was FixRefusing to record an include whose basedir is not the compilation's tree root |
312a24d to
da3a666
Compare
|
v6: rebase |
da3a666 to
6cab607
Compare
Fixed in v7 |
|
|
||
| let compiler = match find_compilers() | ||
| .into_iter() | ||
| .find(|c| c.name == "gcc" || c.name == "clang") |
There was a problem hiding this comment.
the compiler lookup + daemon setup is now copied in 3 tests, could you please dedup it?
There was a problem hiding this comment.
Added find_gcc_or_clang and restart_local_daemon_with helpers.
| basedirs | ||
| .iter() | ||
| .filter(|basedir| haystack.starts_with(basedir)) | ||
| .map(|basedir| basedir.len()) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Added unnormalized_len() helper
b5d6860 to
5ac59e1
Compare
|
v10:
|
| .filter(|(_, b)| is_separator(b)) | ||
| .nth(separators - 1) | ||
| .map(|(i, _)| i + 1) | ||
| .unwrap() |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Done, returning an Option, and avoiding underflow.
|
|
||
| /// 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( |
There was a problem hiding this comment.
strip_basedir is too close to util::strip_basedirs, which does something else. maybe relativize_include?
5ac59e1 to
ca3cd0a
Compare
|
v11:
|
ca3cd0a to
18ff55e
Compare
|
v12: rebased |
| 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 |
There was a problem hiding this comment.
could you please cut it to 3-4 lines? thanks
| return Ok((absolute(), false)); | ||
| }; | ||
| let in_tree_root = match tree_root { | ||
| Some(tree_root) => same_basedir(&encoded[..len], &encode_dir(tree_root)?), |
There was a problem hiding this comment.
encode_dir(tree_root) is recomputed for every include, could it be done once in add_result?
| 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(), |
There was a problem hiding this comment.
same_basedir normalizes on windows but this compares exactly, so the same tree with a different case won't match. normalize here too?
18ff55e to
a740560
Compare
|
v13:
|
|
|
||
| let header_a = tree_a.join("inc").join("cfg.h"); | ||
| let mut entry = PreprocessorCacheEntry::new(); | ||
| entry.add_result( |
There was a problem hiding this comment.
the add_result + lookup_result_digest setup is copied in most of these new tests, could be a small helper, no?
There was a problem hiding this comment.
yes, extracted into two new helpers
a740560 to
5a0ffaf
Compare
|
v14: two new helpers for tests: entry_with_header() and hits() to reduce code repetition |
|
sorry but it needs to be rebased |
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>
5a0ffaf to
aeaa5aa
Compare
No problem. |
|
v15: rebased |
|
thanks and sorry it took forever! |
## 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
…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
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.