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
This commit is contained in:
Lunny Xiao
2026-08-07 11:14:30 -07:00
parent 9dd9204937
commit 3ea5134ac6
5 changed files with 42 additions and 4 deletions
+1 -1
View File
@@ -65,7 +65,7 @@ builds:
flags: flags:
- -trimpath - -trimpath
ldflags: ldflags:
- -s -w -X gitea.com/gitea/runner/internal/pkg/ver.version={{ .Summary }} - -s -w -X main.version={{ .Summary }}
binary: >- binary: >-
{{ .ProjectName }}- {{ .ProjectName }}-
{{- .Version }}- {{- .Version }}-
+15 -2
View File
@@ -72,7 +72,8 @@ else
endif endif
TAGS ?= 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 .PHONY: all
all: build all: build
@@ -113,7 +114,19 @@ deps-tools: ## install tool dependencies
wait wait
.PHONY: checks .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 .PHONY: lint
lint: lint-go lint-go-windows ## lint everything lint: lint-go lint-go-windows ## lint everything
+7 -1
View File
@@ -3,9 +3,15 @@
package ver package ver
// go build -ldflags "-X gitea.com/gitea/runner/internal/pkg/ver.version=1.2.3"
var version = "dev" 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 { func Version() string {
return version return version
} }
+14
View File
@@ -11,3 +11,17 @@ func TestVersion(t *testing.T) {
t.Errorf("Version() = %q, want %q", got, version) 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")
}
}
+5
View File
@@ -9,9 +9,14 @@ import (
"syscall" "syscall"
"gitea.com/gitea/runner/internal/app/cmd" "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() { func main() {
ver.SetVersion(version)
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM) ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer stop() defer stop()
// run the command // run the command