Skip to content

build(provenance): stamp -dirty-<digest> of the worktree diff into the version banner (rubash#827) - #939

Merged
caomengxuan666 merged 2 commits into
masterfrom
fix/827
Oct 20, 2026
Merged

caomengxuan666 merged 2 commits into
masterfrom
fix/827

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Summary

Closes the rubash-side half of the provenance blind spot behind #827 / #488: a bare git rev-parse --short HEAD in build: never guaranteed content. The v1.4.0 incident shipped a binary whose banner named a commit while compiling that commit plus uncommitted edits — two different dirty trees could print the same banner.

This PR mirrors on rubash's own stamp what niubash's build.rs already does for the sibling rubash checkout (niubash PR #424, b3ef4fb):

  • Dirty stamp: a tracked tree differing from HEAD (git status --porcelain --untracked-files=no, same rule as git describe --dirty / diff-index --quiet HEAD) now stamps <rev>-dirty-<8 hex>; the digest is SHA-256(git diff HEAD --binary)[..4] — staged and unstaged edits both count, untracked files never do. Same edits → same digest; any change → different banner.
  • Pin can't launder dirty: an explicit RUBASH_BUILD_HASH override still wins the hash half (release pipelines pin the packaged commit), but when a repo is present the -dirty[-<digest>] half is appended regardless — a pin names the claim, the digest marks the truth.
  • Invalidation kept honest: deliberately zero rerun-if-changed — cargo's default whole-package scan is what recomputes the dirty half on every uncommitted edit. Emitting any rerun-if-changed would opt OUT of that scan and could bake a stale clean stamp into a dirty build. (Trade-off, now documented: a HEAD move with a byte-identical worktree — e.g. committing exactly what was built — keeps the previous stamp until the next file change. This can only over-claim dirtiness, never under-claim it; the incident shape was under-claiming.)

Status of #827 acceptance items

Test plan

  • cargo test --test build_rs_dirty — 5/5 fixture tests (clean → bare HEAD; dirty → stable content digest that tracks edits; staged-but-uncommitted counts; untracked ignored; restore returns bare HEAD)
  • Live check on this worktree while dirty: build: d4fc195c-dirty-225d2d9f (debug)
  • cargo build --locked — lockfile consistent; cargo fmt --check clean
  • cargo test --lib build_info predicate verified against the live stamp (note: cargo test --lib currently fails to compile on Windows due to a pre-existing master regression — src/builtins/cd.rs:486,491 #[cfg(windows)] test still passes &mut HashMap after the perf(env): write-behind marker strings, read from structured flags #918 VarTable refactor; unrelated to this change, left for its own fix)

Refs #827

Generated with Devin

…e version banner (rubash#827)

#488 blind spot proven in the v1.4.0 incident: the installed niu.exe
printed an honest HEAD hash while the tree it compiled carried uncommitted
edits — "hash honest, content ahead". A bare `git rev-parse --short HEAD`
never guarantees content, so two different dirty trees could print the
same banner.

Mirror the niubash-side fix (its build.rs already stamps `-dirty-<8 hex>`
for the sibling rubash checkout) on rubash's own stamp:

- A tracked tree differing from HEAD (`git status --porcelain
  --untracked-files=no`, same rule as `git describe --dirty` /
  `diff-index --quiet HEAD`) appends `-dirty-<8 hex>` where the digest is
  SHA-256(`git diff HEAD --binary`)[..4]: staged and unstaged edits both
  count, untracked files never do.
- The explicit `RUBASH_BUILD_HASH` release pin wins the hash half but can
  no longer launder a dirty tree — the dirty half is still appended when
  a repo is present.
- Deliberately keep zero `rerun-if-changed`: cargo's default
  whole-package scan is the freshness contract that recomputes the dirty
  half on every uncommitted edit; emitting any rerun-if-changed would opt
  out of that scan and could serve a stale clean stamp into a dirty
  build.

Verified: this worktree (dirty) builds `build: d4fc195-dirty-225d2d9f
(debug)`; new tests/build_rs_dirty.rs exercises the stamp against
throwaway fixture repos (clean -> bare HEAD, dirty -> stable content
digest, staged counts, untracked ignored, restore returns bare HEAD).

Refs #827

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@caomengxuan666

Copy link
Copy Markdown
Member Author

Verdict: blocking flaw in the invalidation contract — the exact #827/#488 incident shape (dirty build, bare banner) still reproduces on this branch. Leaving unmerged until the rerun wiring is fixed. Independent review on a detached worktree at 2d29290b, Windows, cargo 1.99.0.

What verified clean

Where it breaks

The PR's load-bearing claim —

Deliberately zero rerun-if-changed: cargo's default whole-package scan is what recomputes the dirty half on every uncommitted edit.

— is false on this toolchain, because build.rs:29 also emits cargo:rerun-if-env-changed=RUBASH_BUILD_HASH. Emitting any rerun-if-* directive — including rerun-if-env-changed — opts the build script out of cargo's implicit package-wide rescan; it is exactly the opt-out class the design note attributes solely to rerun-if-changed. The stamp therefore only recomputes when the script happens to rerun for other reasons (fresh target/, build.rs edit, cargo clean, pin change) — not on uncommitted edits.

Empirical matrix (detached worktree @ 2d29290b)

step tree rebuild banner verdict
1 clean, first build build.rs runs 2d29290b correct
2 append 1 line to src/build_info.rs lib recompiles 2d29290b stale bare — under-claim: the "hash honest, content ahead" incident shape survives the fix
3 same dirty tree, touch build.rs build.rs reruns 2d29290b-dirty-421b9bb6 correct
4 restore file → clean tree lib recompiles 2d29290b-dirty-421b9bb6 stale dirty — over-claim on a clean tree
5 different single-file edit lib recompiles 2d29290b-dirty-421b9bb6 stale digest naming a different dirty state

Both PR verifications structurally could not catch this: the live dirty check was a first build in a fresh worktree (build.rs always runs there), and tests/build_rs_dirty.rs invokes revision_label_for_checkout in-process, bypassing cargo invalidation entirely. The design note's "can only over-claim dirtiness, never under-claim it" is also inverted by step 2 — under-claiming is precisely the #827 blind spot.

Why this blocks

#827's rubash-half acceptance is that a binary can no longer print a clean commit while compiling HEAD+edits. Step 2 shows that shape remains one incremental rebuild away on this branch. Mechanism and tests are good; the invalidation wiring is the defect.

Suggested direction

Emit no directive at all (restores the default whole-package rescan) and take the explicit pin out of build.rs: emit the git-derived stamp under a separate cargo:rustc-env variable (e.g. RUBASH_GIT_STAMP), and let build_info.rs keep preferring option_env!("RUBASH_BUILD_HASH") for the pinned half (the pin's own staleness semantics are pre-existing; release pipelines set it before a fresh build). cargo invalidation is not reachable from tests/, but the 5-step matrix above is a cheap manual check worth recording in the PR description or a test comment.

Refs #827, #488

… (rubash#827)

PR #939 review (comment 6296907080) falsified the load-bearing claim that
only `rerun-if-changed` opts out of cargo's whole-package rescan: the
`cargo:rerun-if-env-changed=RUBASH_BUILD_HASH` line at build.rs:29 opted
out on its own, so the dirty half of the stamp was not recomputed on
incremental rebuilds — a dirty tree still printed a bare HEAD (the exact
#827 "hash honest, content ahead" incident shape), and restoring the
tree left a stale `-dirty-<digest>` naming a state that no longer
existed.

Rework per the review's direction:

- build.rs emits NO `cargo:rerun-if-*` directive of any kind, restoring
  cargo's default whole-package rescan: every uncommitted tracked edit
  re-runs the script and re-computes the stamp.
- The explicit `RUBASH_BUILD_HASH` pin moves out of build.rs entirely and
  is read crate-side via `option_env!` in build_info.rs; rustc dep-info
  already tracks `option_env!` reads, so pin changes recompile the crate
  without any build-script env hook.
- The git-derived stamp is emitted under its own name
  (`RUBASH_GIT_STAMP`, plus `RUBASH_GIT_DIRTY_SUFFIX` for the dirty half)
  so it can never be mistaken for — or overwrite — a pipeline pin.
- Pin-still-can't-launder-dirty is preserved: the suffix is appended to
  a pinned hash too (pin + `-dirty[-<digest>]`).

Tests: build_rs_dirty gains a stamp == revision + suffix decomposition
case and a source-level guard asserting no `rerun-if` emission ever
reappears (cargo invalidation is not reachable in-process; the 5-step
rebuild matrix is verified manually in the PR).

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@caomengxuan666

Copy link
Copy Markdown
Member Author

Reworked per the blocking review above (comment 6296907080). Commit fa903da2.

What changed

  • build.rs emits NO cargo:rerun-if-* directive at all. The rerun-if-env-changed=RUBASH_BUILD_HASH line was itself the opt-out the design note attributed solely to rerun-if-changed; deleting it restores cargo's default whole-package rescan, which is what re-computes the dirty half on every uncommitted edit.
  • The RUBASH_BUILD_HASH pin moved out of build.rs. build_info.rs now reads it crate-side via option_env!; rustc dep-info already tracks option_env! reads, so a changed pin recompiles the crate without any build-script env hook.
  • The git-derived stamp got its own name: cargo:rustc-env=RUBASH_GIT_STAMP (full stamp) plus RUBASH_GIT_DIRTY_SUFFIX (just the -dirty[-<digest>] half, emitted only when non-empty). A git stamp can never be mistaken for — or overwrite — a pipeline pin.
  • Pin-still-can't-launder-dirty preserved: build_info.rs composes a pinned stamp as pin + RUBASH_GIT_DIRTY_SUFFIX, so a pinned hash still gains the dirty half on a dirty tree.

Verification (detached worktree @ fa903da2, Windows, incremental rebuilds)

The review's 5-step matrix, re-run:

step tree banner verdict
1 clean, first build fa903da2 correct
2 append 1 line to src/usage.rs fa903da2-dirty-0650e270 fixed — was the stale-bare-HEAD blocker
3 restore → clean fa903da2 fixed — no stale -dirty
4 different single-file edit fa903da2-dirty-63965379 correct — digest tracks the changed dirty state

Pin path (new code, also verified end-to-end):

tree RUBASH_BUILD_HASH banner
dirty (same edit as step 4) testpin testpin-dirty-63965379 — same suffix as the unpinned stamp, pin does not launder dirty
clean testpin testpin
clean unset fa903da2 — dep-info env tracking recompiles on unset too

Tests: cargo test --test build_rs_dirty 7/7 — the original 5 fixtures unchanged, plus two new ones: the_dirty_suffix_is_exactly_the_stamps_dirty_half (decomposition invariant the pin path depends on) and build_rs_emits_no_rerun_if_directives (source-level guard; cargo invalidation itself is not reachable in-process, hence the manual matrix above). cargo test --lib is red at src/builtins/cd.rs:486/491 — pre-existing on origin/master (VarTable vs HashMap, from 4aaedfb), unrelated to this PR; build_info's own test module verified standalone via rustc --test (4/4).

Ticket repro still PAIR-OK on this build: HOME=$PWD/newhome + -c 'mkdir ~/x && cd ~/x && pwd' → /d/repo/rubash-wt-827-rw/newhome/x, rc=0.

@caomengxuan666
caomengxuan666 merged commit 3cd3cfa into master Oct 20, 2026
11 of 12 checks passed
@caomengxuan666

Copy link
Copy Markdown
Member Author

已合并(回执:上轮评审 #issuecomment-6296907080)

Squash merge:3cd3cfa70e3d105d8a706293386edf7a3c05646d(master,fa903da2 的变更,squash 父 0c89a24)。master push CI 全绿:https://github.com/unixwin/rubash/actions/runs/39473542221(Rust tests / Rust tests (macOS) / Cross-target / GNU goldens / Shell functionality 全 pass)。PR 头 CI 同绿(fa903da2 的 macOS 任务首轮 15m 处 cancelled,为基础设施侧;--failed 重跑 4 分钟内全绿)。

上轮 BLOCKER 复核:全部翻转

复核在上轮同一失效语义命题上重跑矩阵(Windows,cargo 1.99.0,自建 detached worktree @ fa903da):

上轮矩阵 上轮判决 本轮实证(fa903da2)
1. 净树首建 正确 build: fa903da2 (debug) 裸 HEAD ✓(不变)
2. src/build_info.rs 加 1 行 → 增量重建 陈旧裸 HEAD build.rs 重跑(verbose 可见 Running …build-script-build),banner fa903da2-dirty-d3bfdd72 ✓ 翻转
3. 同脏树 touch build.rs 正确 fa903da2-dirty-6b1dad6b ✓(不变)
4. 还原文件 → 增量重建 陈旧 -dirty 回裸 fa903da2,无陈旧 -dirty ✓ 翻转
5. 改另一文件 digest 不变 src/usage.rs 加 1 行 → -dirty-6b1dad6b ≠ 同文件改内容的 -dirty-0122a620 ≠ -dirty-d3bfdd72,digest 随内容逐次变化 ✓ 翻转

机理确认:build.rs 全文零 cargo:rerun-if-*(连注释外字符串都没有;println! 恰 2 处,均为 cargo:rustc-env=),cargo 默认整包扫描即失效源。tests/build_rs_dirty.rs 的 build_rs_emits_no_rerun_if_directives 源码级守卫钉死回归。

钉值路径(返工新契约)

  • 脏树上 RUBASH_BUILD_HASH=testpin 注入 → testpin-dirty-6b1dad6b:钉值只赢修订半段,脏摘要如实追加,不洗白 ✓
  • 钉值 testpin→testpin2 改变、零文件改动 → lib+bin 重编(rustc dep-info 跟踪 option_env! 读的 env 值),banner 随之更新;清空钉值 → 回 git 戳 ✓
  • build_info 模块测试单独 rustc 编译 4/4 通过(第 5 例 not(windows) 门控,平台正确)。

票面复现保持

HOME=$PWD/newhome rubash -c 'mkdir ~/x && cd ~/x && pwd' → 输出 <worktree>/newhome/x,rc=0,PAIR-OK。

夹带检查

diff 仅 6 个文件:build.rs、src/build_info.rs、tests/build_rs_dirty.rs、Cargo.toml/Cargo.lock(sha2 build/dev 双依赖,#[path] 测试目标所需)、CHANGELOG.md。无夹带。

备注:master 上 cargo test --lib 目标的 cd.rs:486/491 E0308 为存量问题(该文件在 origin/master 与本 PR 顶逐字节一致),与本 PR 无关,已有 master 侧 commit 跟进处理。

@caomengxuan666

Copy link
Copy Markdown
Member Author

返工复审通过,已合并(完整矩阵见合并说明:#issuecomment-6298212882)。

上轮 BLOCKER(rerun-if-env-changed 退出整包扫描 → 脏树增量重建仍印裸 HEAD)在 fa903da 上实证翻转:build.rs 现零 cargo:rerun-if-*(全文仅 2 处 cargo:rustc-env=,源码级守卫测试钉死);同一 5 步矩阵重跑,脏树增量重建带 -dirty-<digest> 且 digest 随内容逐次变化、还原回裸 HEAD 无陈旧脏戳、钉值不洗白脏摘要、改钉值零文件改动即重编(option_env! dep-info 生效)。7/7 build_rs_dirty、票面 HOME 复现 PAIR-OK。

Squash:master 3cd3cfa7,push CI 全绿(runs/39473542221)。

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.

1 participant