From 3ea5134ac658d9456c30992bab955d3926ba58e5 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Fri, 7 Aug 2026 11:14:30 -0700 Subject: [PATCH] chore: inject the version into `main` instead of a module path The `-X` target carried the full module path in both the Makefile and .goreleaser.yaml, so any change to the module path silently turned the injection into a no-op and shipped a binary reporting "dev". Target `main.version` instead, which no longer names the module, and fail `make checks` when the injection stops taking effect. Assisted-by: Codet:GPT-5.1-Codex --- .goreleaser.yaml | 2 +- Makefile | 17 +++++++++++++++-- internal/pkg/ver/version.go | 8 +++++++- internal/pkg/ver/version_test.go | 14 ++++++++++++++ main.go | 5 +++++ 5 files changed, 42 insertions(+), 4 deletions(-) diff --git a/.goreleaser.yaml b/.goreleaser.yaml index d6e454e5..c7a0f1c8 100644 --- a/.goreleaser.yaml +++ b/.goreleaser.yaml @@ -65,7 +65,7 @@ builds: flags: - -trimpath ldflags: - - -s -w -X gitea.com/gitea/runner/internal/pkg/ver.version={{ .Summary }} + - -s -w -X main.version={{ .Summary }} binary: >- {{ .ProjectName }}- {{- .Version }}- diff --git a/Makefile b/Makefile index 10b83389..0a413fa9 100644 --- a/Makefile +++ b/Makefile @@ -72,7 +72,8 @@ else endif TAGS ?= -LDFLAGS ?= -X "gitea.com/gitea/runner/internal/pkg/ver.version=v$(RELASE_VERSION)" +LDFLAGS ?= -X "main.version=v$(RELASE_VERSION)" +VERSION_CHECK_BIN := $(DIST)/version-check$(suffix $(EXECUTABLE)) .PHONY: all all: build @@ -113,7 +114,19 @@ deps-tools: ## install tool dependencies wait .PHONY: checks -checks: tidy-check fmt-check security-check ## run the non-lint source checks +checks: tidy-check fmt-check security-check version-check ## run the non-lint source checks + +.PHONY: version-check +version-check: ## verify the version is injected into the binary + @mkdir -p $(DIST) + @$(GO) build -tags '$(TAGS)' -ldflags '-s -w $(EXTLDFLAGS) -X "main.version=v0.0.0-injected"' -o $(VERSION_CHECK_BIN) . + @case "$$($(VERSION_CHECK_BIN) --version)" in \ + *v0.0.0-injected*) ;; \ + *) echo "version injection is broken, the Makefile -X target no longer matches a variable" >&2; exit 1;; \ + esac + @rm -f $(VERSION_CHECK_BIN) + @# goreleaser builds releases from its own ldflags, so a stale -X target there ships an unversioned binary + @grep -q -- '-X main.version=' .goreleaser.yaml || { echo ".goreleaser.yaml no longer injects main.version" >&2; exit 1; } .PHONY: lint lint: lint-go lint-go-windows ## lint everything diff --git a/internal/pkg/ver/version.go b/internal/pkg/ver/version.go index c86157cd..76f03efb 100644 --- a/internal/pkg/ver/version.go +++ b/internal/pkg/ver/version.go @@ -3,9 +3,15 @@ package ver -// go build -ldflags "-X gitea.com/gitea/runner/internal/pkg/ver.version=1.2.3" var version = "dev" +// SetVersion records the version injected into package main at build time. +func SetVersion(v string) { + if v != "" { + version = v + } +} + func Version() string { return version } diff --git a/internal/pkg/ver/version_test.go b/internal/pkg/ver/version_test.go index 54429176..2f8eaa7b 100644 --- a/internal/pkg/ver/version_test.go +++ b/internal/pkg/ver/version_test.go @@ -11,3 +11,17 @@ func TestVersion(t *testing.T) { t.Errorf("Version() = %q, want %q", got, version) } } + +func TestSetVersion(t *testing.T) { + t.Cleanup(func() { version = "dev" }) + + SetVersion("v1.2.3") + if got := Version(); got != "v1.2.3" { + t.Errorf("Version() = %q, want %q", got, "v1.2.3") + } + + SetVersion("") + if got := Version(); got != "v1.2.3" { + t.Errorf("Version() = %q, want %q", got, "v1.2.3") + } +} diff --git a/main.go b/main.go index cd16cfaf..2614d081 100644 --- a/main.go +++ b/main.go @@ -9,9 +9,14 @@ import ( "syscall" "gitea.com/gitea/runner/internal/app/cmd" + "gitea.com/gitea/runner/internal/pkg/ver" ) +// version is injected at build time with `-ldflags "-X main.version=v1.2.3"`. +var version = "dev" + func main() { + ver.SetVersion(version) ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM) defer stop() // run the command