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:
@@ -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 {
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user