Skip to content

fix(build): key the PCH cache on compiler flags, validate the PCH build command - #3813

Merged
zackees merged 1 commit into
masterfrom
fix/pch-poisoning-flags-hash
Aug 1, 2026
Merged

zackees merged 1 commit into
masterfrom
fix/pch-poisoning-flags-hash

Conversation

@zackees

@zackees zackees commented Aug 1, 2026 •

Copy link
Copy Markdown
Member

Fixes the recurring error: redefinition of 'hsv8' PCH poisoning, which until now was only survivable by hand-deleting the PCH and its sidecars.

What I found — one correction to the premise

The starting theory was un-normalized paths. The paths turn out to be fine: ci/meson/path_normalization.py post-processes both build.ninja and compile_commands.json, and a scan of the live build dirs finds zero strict-path violations. Every -I is already forward-slash absolute.

The path spelling was still the right thread, though — just one step removed. The poisoning does not come from a bad spelling; it comes from the PCH cache being blind to the spelling changing.

1. The cache key omitted the compiler flags

ci/compile_pch.py skipped compilation whenever the input-file hash matched:

if current_hash == stored_hash:
    print("PCH inputs unchanged (hash match) - skipping compilation")
    return 0

That hash cannot see a changed include spelling. The resolved dependency set is byte-identical either way, so the digest is identical, and the PCH built under the old spelling is handed to a TU resolving headers under the new one. #pragma once fails to dedupe and the build dies.

Args were excluded deliberately, and the docstring gave the reason:

Note: compiler args are NOT included in this hash. Meson/Ninja already tracks compiler flag changes via build.ninja regeneration.

That reasoning doesn't hold. The build tool does reschedule the target when the command line changes — and then compile_pch.py vetoes the rebuild. The veto was the bug.

The rest of the docstring records a real constraint I had to respect: invalidate_stale_pch computes the same hash and never sees the args, so folding args into it made the two disagree and deleted every freshly built PCH. Hence a separate .flags_hash sidecar rather than a changed input hash.

Two deliberate choices:

  • The flags hash is not normalized. Normalizing would collapse exactly the difference that must invalidate. This is the one place in the build where C:/x/src and C:\x\src must not compare equal.
  • A PCH with no flags sidecar is discarded, so trees already poisoned self-heal on the next build instead of needing manual deletion.

The sibling ci/wasm_compile_pch.py has hashed its flags all along (compute_flags_hash), so this brings the native path in line with a pattern already proven in-repo.

2. The strict-path validator could not see the PCH build command

find_strict_path_violations scanned only compile_commands.json. The PCH is a Meson custom target, so it isn't there:

occurrences in compile_commands.json
compile_pch.py (the build command) 0
-include-pch (the consume flags) 365

So the flags a PCH is consumed with were validated, and the flags it is built with were not — even though a mismatch between those two is the entire failure mode. Now build.ninja is scanned as well, being the only generated file that contains that command.

Measured 0 pre-existing violations in both meson-quick and meson-debug before wiring it in, so it enforces at zero tolerance with no baseline.

Verification

Both guards are only worth having if they fire, so I proved each does.

End-to-end, against the real generated PCH command:

--- run 1: unchanged flags (expect SKIP) ---
PCH inputs unchanged (hash match) - skipping compilation
--- run 2: one -I respelled (expect REBUILD, not silent reuse) ---
PCH compiler flags changed - rebuilding

Run 2 is the bug: it previously printed "inputs unchanged - skipping" and handed the build a poisoned PCH. Run 1 confirms there's no cache regression — unchanged builds still skip.

Self-heal, on the actually-poisoned tree that prompted this:

PCH tracking files missing — removing test_pch.h.pch to force rebuild
PCH tracking files missing — removing FastLED.h.pch to force rebuild

ci/tests/test_pch_staleness_guards.py — 9 new tests, including that the two separator spellings hash differently, that arg boundaries are unambiguous (['-IC:/a','-IC:/b'] must not hash like ['-IC:/a-IC:/b']), and that a backslash / relative / dot-component -I in a build.ninja PCH command is caught.

check result
bash test --clean --cpp 276/276 unit, 83/83 examples — no redefinition errors
uv run python -m pytest ci/tests/test_pch_staleness_guards.py ci/tests/test_meson_private_include_normalization.py 16 passed
bash lint no blocking violations (40 pre-existing warn-only, unchanged)

Left alone deliberately

ci/lint_meson/ has two checkers, BackslashPathChecker and PathNormJoinChecker, that give contradictory advice about whether Meson's / operator is safe on Windows. The generated build.ninja supports BackslashPathChecker's premise. That contradiction wants resolving, but it is not what caused this bug and folding it in here would muddy the diff — worth a follow-up.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes

    • Improved precompiled-header cache validation so changes to compiler options correctly trigger rebuilding.
    • Added stricter detection of invalid include paths in generated build commands, including commands not listed in compilation databases.
    • Removed stale cached build artifacts when required validation metadata is missing.
  • Tests

    • Added coverage for compiler-option changes, include-path variations, and Ninja command path validation.

…ld command

Two guards for the same failure: a PCH built under one include spelling
gets reused under another, clang re-parses headers the PCH already
contains, #pragma once fails to dedupe, and the build dies with
"redefinition of 'hsv8'".

1. compile_pch.py short-circuited on the input-file hash alone. That hash
   cannot see a changed include spelling -- the resolved dependency set is
   byte-identical either way, so the digest is identical and the stale PCH
   is silently reused. Compiler args were excluded deliberately, and the
   stated reason ("Meson/Ninja already tracks flag changes via build.ninja
   regeneration") does not hold: the build tool does reschedule the target
   on a command-line change, but compile_pch.py then vetoes the rebuild.
   That veto was the bug.

   Add a .flags_hash sidecar, hashed from the raw argument vector and
   deliberately NOT normalized -- normalizing would collapse exactly the
   difference that must invalidate. Kept separate from the input hash,
   which invalidate_stale_pch also computes without ever seeing the args;
   mixing them is what previously deleted every freshly built PCH.

   A PCH with no flags sidecar is treated as untrustworthy and discarded,
   so trees already poisoned self-heal on the next build.

2. find_strict_path_violations scanned only compile_commands.json. The PCH
   is a Meson custom target, so its build command appears there zero times
   while the consuming -include-pch flags appear 365 times: the flags a PCH
   is consumed with were validated, the flags it was built with were not.
   Scan build.ninja too -- the only generated file containing that command.

Verified end to end against the real generated command: unchanged flags
still skip compilation (no cache regression), while respelling a single -I
now reports "PCH compiler flags changed - rebuilding" where it previously
reported "inputs unchanged - skipping". Zero pre-existing violations in
either build dir, so the new scan enforces at zero tolerance.

Co-Authored-By: Claude <[email protected]>
@coderabbitai

coderabbitai Bot commented Aug 1, 2026 •

Copy link
Copy Markdown

Review Change Stack

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: f270f5a2-02ca-4ef3-960a-280128886814

📥 Commits

Reviewing files that changed from the base of the PR and between bce4bc7 and 254c792.

📒 Files selected for processing (3)
  • ci/compile_pch.py
  • ci/meson/path_normalization.py
  • ci/tests/test_pch_staleness_guards.py

📝 Walkthrough

Walkthrough

The CI changes add compiler-argument hashes to PCH cache validation and scan generated Ninja commands for strict-path violations. Regression tests cover hash behavior, path formats, and missing Ninja files.

Changes

CI validation updates

Layer / File(s) Summary
PCH compiler-flag cache validation
ci/compile_pch.py
PCH reuse now requires matching input and compiler-flag hashes. Missing metadata removes stale tracking files, and successful builds persist the flag hash.
Strict-path scanning for generated commands
ci/meson/path_normalization.py, ci/tests/test_pch_staleness_guards.py
Command validation is centralized and applied to compile commands and COMMAND = lines in build.ninja. Tests cover hash stability, argument changes, path violations, and missing Ninja files.

Estimated code review effort: 3 (Moderate) | ~20 minutes

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/pch-poisoning-flags-hash

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@zackees
zackees merged commit 25dd64f into master Aug 1, 2026
10 of 12 checks passed
@zackees
zackees deleted the fix/pch-poisoning-flags-hash branch August 1, 2026 17:46
@fastled-project-sync fastled-project-sync Bot moved this from Triage to Done in FastLED Tracker Aug 1, 2026
zackees added a commit that referenced this pull request Oct 16, 2026
* docs(skills): add /pch-debug skill and pch_probe.py

A skill that auto-activates on PCH failures ('redefinition of',
'redefines default argument', 'modified since the precompiled header',
tests that pass only after --clean). It explains the identity model
(#pragma once fails early; include guards hide the double read), the two
mechanisms in FastLED's history (path-spelling drift: #2685/zccache#619,
#3813; stale PCH accepted: #4839, #4437, #2995/zccache#710), and a
probe-before-clean procedure.

pch_probe.py: 'inputs' dumps what the PCH recorded (falls back to scanning
the PCH bytes when clang refuses a stale PCH) and flags odd spellings,
duplicate spellings and stale inputs; 'tu' runs a real compile_commands
entry with -H (optionally through zccache) and lists project headers
re-opened from disk next to their PCH spelling.

* pch_probe: llvm-bcanalyzer input records; repo-root-based project-header check (#4860 review)

- inputs: read the PCH with llvm-bcanalyzer -dump when available (PATH or
  LLVM_BCANALYZER): exact recorded spelling, size and mtime per input,
  without clang's stale-PCH validation; stale = size/mtime differ from the
  record (clang's own check). Falls back to -module-file-info / byte scan.
- Project headers are now classified by resolving under the repo root
  (derived from the script location), not by matching /src/ or /tests/ in
  the raw spelling: relative recorded entries like tests/... count, and any
  --build-dir location works.

* pch_probe: resolve relative recorded inputs against the build dir (#4860 review)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

1 participant