Skip to content

difftastic: deterministic parser link order for reproducible builds - #283

Merged
bryan-minimal merged 1 commit into
mainfrom
bryan/difftastic-deterministic-link-order
Jun 22, 2026
Merged

difftastic: deterministic parser link order for reproducible builds#283
bryan-minimal merged 1 commit into
mainfrom
bryan/difftastic-deterministic-link-order

Conversation

@bryan-minimal

@bryan-minimal bryan-minimal commented Jun 22, 2026

Copy link
Copy Markdown
Member

What

Makes the difftastic package build byte-reproducibly with a one-word build.rs change.

Root cause

difftastic's build.rs compiles its 11 vendored tree-sitter parsers in parallel:

parsers.par_iter().for_each(|p| p.build());   // rayon

Each cc::Build::compile() prints its cargo:rustc-link-lib directive when it finishes, so under parallel compilation the parser static libs link in non-deterministic completion order. Those parser tables are large (~99 MB of .rodata), so reshuffling their link order relocates everything — yielding distributed, same-size differences across .text / .rodata / .data / .rela.dyn between builds (measured ~16% of .text, ~8% of .rodata).

This sits above the codegen layer, so the existing -C codegen-units=1 + CONST_RANDOM_SEED (from #254) couldn't fix it — it's link order, not codegen or compile-time RNG.

Fix

Serialize parser compilation (par_iter()iter()) so the cargo:rustc-link-lib directives are emitted in deterministic source order. Applied as a grep-guarded sed in build.sh.

Verification

repro-verify difftastic (two from-scratch builds + repro-check diff): byte-identical ✅ (was: ~9.5% of bytes differing before the fix). minimal check green.

Generalizable gotcha: any crate whose build.rs builds multiple native libs via par_iter emits link directives in nondeterministic order — worth keeping in mind for other Rust packages.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Chores
    • Enhanced build process to improve determinism and reliability for difftastic compilation.

difftastic's build.rs compiles its 11 vendored tree-sitter parsers with
`parsers.par_iter().for_each(|p| p.build())` (rayon). Each
cc::Build::compile() emits its `cargo:rustc-link-lib` directive when it
finishes, so under parallel compilation the parser static libs link in
non-deterministic completion order. The parser tables are large (~99 MB
of .rodata), so reshuffling their link order moves everything — producing
distributed, same-size differences across .text/.rodata/.data/.rela.dyn
build-to-build. codegen-units=1 and CONST_RANDOM_SEED can't fix this; it's
link order, not codegen.

Serialize parser compilation (par_iter -> iter) so the link directives are
emitted in deterministic source order. Verified byte-identical across two
from-scratch builds (repro-verify).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Jun 22, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

packages/difftastic/build.sh gains a build-time patch that replaces parsers.par_iter().for_each with parsers.iter().for_each in build.rs, forcing serial tree-sitter parser compilation. A verification step immediately follows and exits with an error if the patch string is not present in the file.

Changes

Deterministic Parser Compilation Patch

Layer / File(s) Summary
Serial build patch and verification
packages/difftastic/build.sh
Adds a sed invocation to swap par_iter for iter in build.rs and a grep guard that aborts the build if the patched string is absent after the edit.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~3 minutes

Suggested reviewers

  • twitchyliquid64

Poem

🐇 A rayon of parsers ran wild and free,
In parallel chaos, non-deterministically!
But a patch with a sed brought them into a line,
Now iter walks steady, sequential and fine.
No more racing parsers — the build's now divine! ✨

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title directly and specifically describes the main change: making difftastic builds deterministic by fixing parser link order for reproducibility.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ 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 bryan/difftastic-deterministic-link-order

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

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
packages/difftastic/build.sh (1)

9-18: 🧹 Nitpick | 🔵 Trivial | ⚡ Quick win

Consider enhancing verification to confirm the original pattern was removed.

The current grep verification only checks that parsers.iter().for_each exists after patching, but doesn't verify that parsers.par_iter().for_each was actually removed. If upstream changes the code structure so the sed pattern no longer matches, but parsers.iter().for_each happens to exist elsewhere (e.g., in a comment or different context), the verification would incorrectly pass.

Adding a second check would make the verification more robust and explicit:

🔍 Enhanced verification
 sed -i 's/parsers\.par_iter()\.for_each/parsers.iter().for_each/' build.rs
 grep -q 'parsers.iter().for_each' build.rs || { echo "ERROR: difftastic par_iter->iter patch did not apply — build.rs changed" >&2; exit 1; }
+! grep -q 'parsers\.par_iter()\.for_each' build.rs || { echo "ERROR: original par_iter pattern still present after patch" >&2; exit 1; }

This verifies both that the desired pattern exists AND the undesired pattern is absent, making the patch verification bidirectional.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/difftastic/build.sh` around lines 9 - 18, The current verification
only confirms that the desired pattern `parsers.iter().for_each` exists after
the sed command, but does not verify that the original undesired pattern
`parsers.par_iter().for_each` was actually removed. Add a second grep check
after the existing grep verification to explicitly confirm that
`parsers.par_iter().for_each` is no longer present in build.rs. Use a negated
grep check (grep with appropriate flags to fail if the pattern is found) to
ensure the original pattern has been removed, making the verification
bidirectional and more robust to upstream code changes.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@packages/difftastic/build.sh`:
- Around line 9-18: The current verification only confirms that the desired
pattern `parsers.iter().for_each` exists after the sed command, but does not
verify that the original undesired pattern `parsers.par_iter().for_each` was
actually removed. Add a second grep check after the existing grep verification
to explicitly confirm that `parsers.par_iter().for_each` is no longer present in
build.rs. Use a negated grep check (grep with appropriate flags to fail if the
pattern is found) to ensure the original pattern has been removed, making the
verification bidirectional and more robust to upstream code changes.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: cf0e665e-9ee2-4c05-b042-c383be777f39

📥 Commits

Reviewing files that changed from the base of the PR and between f7f44e1 and 35e9ebb.

📒 Files selected for processing (1)
  • packages/difftastic/build.sh

@bryan-minimal
bryan-minimal added this pull request to the merge queue Jun 22, 2026
Merged via the queue into main with commit b7561de Jun 22, 2026
4 checks passed
@bryan-minimal
bryan-minimal deleted the bryan/difftastic-deterministic-link-order branch June 22, 2026 17:08
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.

2 participants