Skip to content

link: add stamped VCS build info - #4695

Open
sluongng wants to merge 1 commit into
bazel-contrib:masterfrom
sluongng:sluongng/vcs-buildinfo-stamping
Open

link: add stamped VCS build info#4695
sluongng wants to merge 1 commit into
bazel-contrib:masterfrom
sluongng:sluongng/vcs-buildinfo-stamping

Conversation

@sluongng

@sluongng sluongng commented Aug 19, 2026

Copy link
Copy Markdown
Contributor

Stamped rules_go binaries cannot report their source revision or dirty
state through runtime/debug or go version -m. Users therefore cannot
apply the same provenance checks available for binaries built by cmd/go.
This matters because release verification depends on that provenance.

Map STABLE_VCS, STABLE_VCS_REVISION, STABLE_VCS_TIME, and
STABLE_VCS_MODIFIED workspace values to Go's vcs.* build settings. These
names follow Bazel's stable-status convention while mirroring Go's
build information keys, including the true or false value used by
vcs.modified. Normalize timestamps to UTC.

Own one filtered status action on each configured go_context_data
target and let eligible executable links consume its shared output. The
action graph is:

stable-status.txt
        │
        ▼
one GoVCSStamp action
owned by configured //:go_context_data
        │
        ▼
go_context_data.vcsstamp
        │
        ├── eligible GoLink for binary A
        ├── eligible GoLink for binary B
        └── eligible GoLink for test C

This avoids declaring the same filter action for every binary while
keeping the raw stable-status file out of GoLink inputs, so unrelated
status changes do not relink binaries.

Emit VCS settings only when the target and the package metadata
selected for BuildInfo.Main both come from the main repository. Carry
the selected metadata file through embeds and tests, then infer
its provenance from File.owner at the link boundary. Infer target
provenance from the active Go context and treat unknown metadata
owners as untrusted.

Embedding the current revision in each eligible stamped go_test binary
changes that executable whenever HEAD changes. Creating a commit,
amending a commit message, or rebasing therefore invalidates cached
test results even when the test source did not change. Compilation
outputs remain reusable. Document this cost and recommend keeping
VCS stamping disabled for routine development while enabling it for
release or provenance-verification builds.

Cover shared action inputs, stable refreshes, go_binary and go_test,
direct links, external provenance, and non-executable link modes.

Future work

External binaries are intentionally out of scope for this change. A binary from a version-pinned go_repository continues to report its module version through BuildInfo.Main.Version, but this PR does not emit vcs.* settings for external-repository targets. A future change could add repository-specific VCS metadata for commit-pinned external repositories; it must come from that external repository rather than the main workspace's status file.

@sluongng
sluongng force-pushed the sluongng/vcs-buildinfo-stamping branch from 51afe0b to 5b3e00a Compare August 21, 2026 12:57
fmeum pushed a commit that referenced this pull request Aug 21, 2026
**What type of PR is this?**

Feature

**What does this PR do? Why is it needed?**

rules_go records dependency modules in `BuildInfo.Deps`, but leaves
`BuildInfo.Main` empty even when package metadata identifies the module
that
owns an executable. Build-info consumers therefore cannot report the
main
module path and version.

This change passes two distinct values to the linker:

- The executable package's import path populates `BuildInfo.Path`.
- The Go PURL in its package metadata populates `BuildInfo.Main`.

Package metadata is kept unconditionally on `GoInfo` and archive data.
At link
time, the target metadata is selected as the main module and that module
path
is filtered from the candidate dependencies. This prevents a `go_test`
binary
from reporting its own module in `BuildInfo.Deps` without a test-only
suppression flag or a duplicate provider field.

The new `go.binary` and `go.link` parameters are appended to preserve
positional compatibility for custom Starlark rules. Tests cover ordinary
binaries, embedded main packages, test binaries, binaries without
metadata,
and direct `go.archive` to `go.link` callers.

**Which issue(s) does this PR fix?**

No linked issue.

**Other notes for review**

Build-info stack:

1. #4694 — add main-module build info (this PR)
2. #4695 — add stamped VCS build info

The second draft is cumulative against `master` because both heads are
fork-owned. It depends on this PR and is rebased after this PR changes.

Validation:

- `bazel --nohome_rc test //tests/...` — 467 passed, 1 Darwin-only test
skipped
- `bazel test //go/tools/builders:all
//tests/core/go_binary:buildinfo_test
//tests/core/starlark:package_metadata_test //tests:buildifier_test`
- `bazel build //docs/...`
- `bazel test //docs/...`
- `git diff --check`
@sluongng
sluongng force-pushed the sluongng/vcs-buildinfo-stamping branch 12 times, most recently from e6be626 to 5f18f10 Compare August 27, 2026 21:36
Stamped rules_go binaries cannot report their source revision or dirty
state through runtime/debug or go version -m. Users therefore cannot
apply the same provenance checks available for binaries built by cmd/go.
This matters because release verification depends on that provenance.

Map STABLE_VCS, STABLE_VCS_REVISION, STABLE_VCS_TIME, and
STABLE_VCS_MODIFIED workspace values to Go's vcs.* build settings. These
names follow Bazel's stable-status convention while mirroring Go's
build information keys, including the true or false value used by
vcs.modified.  Normalize timestamps to UTC.

Own one filtered status action on each configured go_context_data
target and let eligible executable links consume its shared output. The
action graph is:

```text
stable-status.txt
        │
        ▼
one GoVCSStamp action
owned by configured //:go_context_data
        │
        ▼
go_context_data.vcsstamp
        │
        ├── eligible GoLink for binary A
        ├── eligible GoLink for binary B
        └── eligible GoLink for test C
```

This avoids declaring the same filter action for every binary while
keeping the raw stable-status file out of GoLink inputs, so unrelated
status changes do not relink binaries.

Emit VCS settings only when the target and the package metadata
selected for BuildInfo.Main both come from the main repository. Carry
the selected metadata file through embeds and tests, then infer
its provenance from File.owner at the link boundary. Infer target
provenance from the active Go context and treat unknown metadata
owners as untrusted.

Embedding the current revision in each eligible stamped go_test binary
changes that executable whenever HEAD changes. Creating a commit,
amending a commit message, or rebasing therefore invalidates cached
test results even when the test source did not change. Compilation
outputs remain reusable. Document this cost and recommend keeping
VCS stamping disabled for routine development while enabling it for
release or provenance-verification builds.

Cover shared action inputs, stable refreshes, go_binary and go_test,
direct links, external provenance, and non-executable link modes.
@sluongng
sluongng force-pushed the sluongng/vcs-buildinfo-stamping branch from 5f18f10 to 13d314e Compare August 28, 2026 08:04
@sluongng
sluongng marked this pull request as ready for review August 28, 2026 08:17
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