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.
This commit is contained in:
2026-03-08 14:02:45 -07:00
parent 3c801842e4
commit 59a4a2823f
2 changed files with 16 additions and 9 deletions

View File

@@ -90,10 +90,13 @@ func init() {
VERSION = "dev-snapshot" VERSION = "dev-snapshot"
} else { } else {
if !strings.HasPrefix(VERSION, "v") { 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 { if bi, ok := debug.ReadBuildInfo(); ok {

View File

@@ -7,6 +7,7 @@ import (
"github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus"
dto "github.com/prometheus/client_model/go" dto "github.com/prometheus/client_model/go"
"golang.org/x/mod/semver"
) )
func TestCheckVersion(t *testing.T) { func TestCheckVersion(t *testing.T) {
@@ -48,9 +49,12 @@ func TestVersionInfo(t *testing.T) {
t.Error("VersionInfo().Version should not be empty") 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" { 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 // GitRevShort should be <= 7 characters if set