From af7683da9a049bd66370bc3dda0c2240940b7864 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ask=20Bj=C3=B8rn=20Hansen?= Date: Sun, 8 Mar 2026 14:02:45 -0700 Subject: [PATCH] fix(version): don't add "v" prefix to non-semver VERSION strings When VERSION is set to a non-tag value like "main" (from goreleaser or ldflags), the init() function unconditionally prepended "v", producing "vmain". Now only add the "v" prefix when doing so produces a valid semver string, leaving branch names and other non-semver values as-is. --- version/version.go | 9 ++++++--- version/version_test.go | 16 ++++++++++------ 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/version/version.go b/version/version.go index 8c46f10..1d3b0aa 100644 --- a/version/version.go +++ b/version/version.go @@ -90,10 +90,13 @@ func init() { VERSION = "dev-snapshot" } else { if !strings.HasPrefix(VERSION, "v") { - VERSION = "v" + VERSION + vVersion := "v" + VERSION + if semver.IsValid(vVersion) { + VERSION = vVersion + } } - if !semver.IsValid(VERSION) { - slog.Default().Warn("invalid version number", "version", VERSION) + if !semver.IsValid(VERSION) && VERSION != "dev-snapshot" { + slog.Default().Info("non-semver version", "version", VERSION) } } if bi, ok := debug.ReadBuildInfo(); ok { diff --git a/version/version_test.go b/version/version_test.go index 898b4d8..314477d 100644 --- a/version/version_test.go +++ b/version/version_test.go @@ -7,6 +7,7 @@ import ( "github.com/prometheus/client_golang/prometheus" dto "github.com/prometheus/client_model/go" + "golang.org/x/mod/semver" ) func TestCheckVersion(t *testing.T) { @@ -48,9 +49,12 @@ func TestVersionInfo(t *testing.T) { t.Error("VersionInfo().Version should not be empty") } - // Version should start with "v" or be "dev-snapshot" + // Version should start with "v", be "dev-snapshot", or be a non-semver string (e.g. branch name) if !strings.HasPrefix(info.Version, "v") && info.Version != "dev-snapshot" { - t.Errorf("Version should start with 'v' or be 'dev-snapshot', got: %s", info.Version) + // Non-semver versions like branch names ("main") are acceptable + if semver.IsValid("v" + info.Version) { + t.Errorf("Semver-like version should start with 'v', got: %s", info.Version) + } } // GitRevShort should be <= 7 characters if set @@ -398,10 +402,10 @@ func TestParseBuildTimeConsistency(t *testing.T) { func BenchmarkParseBuildTime(b *testing.B) { inputs := []string{ - "1672531200", // Unix epoch - "2023-01-01T00:00:00Z", // RFC3339 - "invalid-timestamp", // Invalid - "", // Empty + "1672531200", // Unix epoch + "2023-01-01T00:00:00Z", // RFC3339 + "invalid-timestamp", // Invalid + "", // Empty } for _, input := range inputs {