Skip to content

fix(build): re-stamp the commit when HEAD moves - #15

Merged
josaum merged 2 commits into
mainfrom
fix/build-stamp-stale-commit
Nov 4, 2026
Merged

josaum merged 2 commits into
mainfrom
fix/build-stamp-stale-commit

Conversation

@josaum

@josaum josaum commented Nov 4, 2026

Copy link
Copy Markdown
Owner

The bug

build.rs bakes the source revision into the binary so staleness is detectable. Its header says a binary "keeps reporting the commit it was actually built from."

It declared only cargo:rerun-if-changed=build.rs, so Cargo reused the previous run's values whenever that file was unchanged. Any commit that didn't touch build.rs produced a binary stamped with an earlier commit, and the self-contract freshness check then compared against a value that had never been current.

The header described this as deliberate — no .git triggers, so branch switches never force a rebuild. But the consequence isn't limited to branch switches: the stamp refreshes only when build.rs itself is edited, which is almost never.

Observed: cargo install --path . from a freshly merged main compiled the new source and still reported the previous commit, with clean beside it.

The fix

Declare the files that actually determine git rev-parse HEAD:

  • HEAD
  • the branch ref it names (symbolic-ref --quiet, silent on a detached HEAD)
  • packed-refs, since a packed ref has no loose file

Paths come from git rev-parse --git-path, so linked worktrees and submodules — where .git is a file, not a directory — resolve rather than assuming .git/. Outside a git checkout every lookup returns None, nothing is emitted, and the stamp stays unknown.

Cost, accepted deliberately

Moving HEAD now re-runs the script, so switching branches relinks on the next build. An accurate stamp isn't available more cheaply: the commit can't be re-read without re-running the reader. A stamp that is usually wrong seems worse than an occasional relink.

Known limit, documented not fixed

LEIO_BUILD_DIRTY is still only as fresh as the last run. No rerun-if-changed can express "any tracked file differs from HEAD", so editing a source file flips the real dirty state without re-stamping. The header now says clean means "clean when last stamped", rather than implying the stamp is self-maintaining. The commit is the load-bearing half.

Verification

Empty commit touching no files at all, then a rebuild — the case the old code could not handle:

stamp before: leio-code 2.6.8 (951f4dfbe914, ...)
new HEAD:     d8999684fa4c
stamp after:  leio-code 2.6.8 (d8999684fa4c, ...)

Under the old build.rs the stamp would not have moved. cargo fmt and cargo clippy --all-targets clean.

🤖 Generated with Claude Code

josaum and others added 2 commits November 3, 2026 22:34
`build.rs` declared only `rerun-if-changed=build.rs`, so Cargo reused the
previous run's values whenever this file was unchanged. Any commit that did
not touch it produced a binary stamped with an earlier commit, and the
self-contract freshness check then compared against a value that had never
been current.

The header claimed the opposite — that a binary keeps reporting the commit it
was built from. That held only until the next commit. Observed in practice: a
`cargo install --path .` from a freshly merged `main` compiled the new source
and still reported the previous commit, with `clean` beside it.

Declare the files that decide `rev-parse HEAD` instead: `HEAD`, the branch ref
it names, and `packed-refs`. Paths come from `git rev-parse --git-path`, so
linked worktrees and submodules resolve rather than assuming `.git/`.

Moving HEAD now re-runs this script, so switching branches relinks on the next
build. That is the price of an accurate stamp: the commit cannot be re-read
without re-running the reader.

`LEIO_BUILD_DIRTY` is still only as fresh as the last run — no trigger can
express "any tracked file differs from HEAD" — and the header now says so
rather than implying the stamp is wholly self-maintaining.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@josaum
josaum merged commit f0ee8d4 into main Nov 4, 2026
4 checks passed
@josaum
josaum deleted the fix/build-stamp-stale-commit branch November 4, 2026 03:04
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