From c6addd16778039ff86e5c6bcbcbe7ea40664d0f2 Mon Sep 17 00:00:00 2001 From: phm07 <22707808+phm07@users.noreply.github.com> Date: Thu, 26 Mar 2026 15:43:25 +0100 Subject: [PATCH 1/4] fix: long version info not displayed correctly in release builds --- .goreleaser.yml | 5 +++- internal/cmd/version/version.go | 24 +++++------------- internal/version/version.go | 44 +++++++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+), 19 deletions(-) diff --git a/.goreleaser.yml b/.goreleaser.yml index e6131acfa..6f32991a0 100644 --- a/.goreleaser.yml +++ b/.goreleaser.yml @@ -1,7 +1,6 @@ version: 2 before: hooks: - - go mod tidy - ./scripts/completions.sh - go run ./scripts manpages @@ -16,6 +15,8 @@ builds: - -w - -X {{ .ModulePath }}/internal/version.version={{ .Version }} - -X {{ .ModulePath }}/internal/version.versionPrerelease={{- if .IsSnapshot -}}dev+{{ .ShortCommit }}{{- end -}} + - -X {{ .ModulePath }}/internal/version.commit={{ .Commit }} + - -X {{ .ModulePath }}/internal/version.commitDate={{ .CommitDate }} env: - CGO_ENABLED=0 @@ -110,6 +111,8 @@ kos: - -w - -X {{ .ModulePath }}/internal/version.version={{ .Version }} - -X {{ .ModulePath }}/internal/version.versionPrerelease={{- if .IsSnapshot -}}dev+{{ .ShortCommit }}{{- end -}} + - -X {{ .ModulePath }}/internal/version.commit={{ .Commit }} + - -X {{ .ModulePath }}/internal/version.commitDate={{ .CommitDate }} - -X {{ .ModulePath }}/internal/state/config.defaultConfigPathOverride=/config.toml snapshot: diff --git a/internal/cmd/version/version.go b/internal/cmd/version/version.go index 6899565ed..98f8322e7 100644 --- a/internal/cmd/version/version.go +++ b/internal/cmd/version/version.go @@ -3,7 +3,6 @@ package version import ( "fmt" "runtime" - "runtime/debug" "text/tabwriter" "github.com/spf13/cobra" @@ -35,25 +34,14 @@ func runVersion(cmd *cobra.Command, _ []string) error { fmt.Fprintf(tw, "go version:\t%s (%s)\n", runtime.Version(), runtime.Compiler) fmt.Fprintf(tw, "platform:\t%s/%s\n", runtime.GOOS, runtime.GOARCH) - if info, ok := debug.ReadBuildInfo(); ok { - rev := getSettingsValue(info.Settings, "vcs.revision", "unknown") - if modified := getSettingsValue(info.Settings, "vcs.modified", "false"); modified == "true" { - rev += " (modified)" - } - - fmt.Fprintf(tw, "revision:\t%s\n", rev) - fmt.Fprintf(tw, "revision date:\t%s\n", getSettingsValue(info.Settings, "vcs.time", "unknown")) + rev := version.Commit + if version.Modified { + rev += " (modified)" } + + fmt.Fprintf(tw, "revision:\t%s\n", rev) + fmt.Fprintf(tw, "revision date:\t%s\n", version.CommitDate) return tw.Flush() } return nil } - -func getSettingsValue(settings []debug.BuildSetting, key, def string) string { - for _, setting := range settings { - if setting.Key == key { - return setting.Value - } - } - return def -} diff --git a/internal/version/version.go b/internal/version/version.go index 43ae7d36d..3b6ca9d97 100644 --- a/internal/version/version.go +++ b/internal/version/version.go @@ -1,5 +1,7 @@ package version +import "runtime/debug" + var ( // version is a semver version (https://semver.org). version = "1.62.0" // x-releaser-pleaser-version @@ -16,4 +18,46 @@ var ( } return version }() + + // Can be set by goreleaser because debug.ReadBuildInfo() is not available for goreleaser builds + commit = "" + + // Commit is the latest commit hash during build time + Commit = func() string { + if commit != "" { + return commit + } + return getSettingsValue("vcs.revision", "unknown") + }() + + // Can be set by goreleaser because debug.ReadBuildInfo() is not available for goreleaser builds + commitDate = "" + + // CommitDate is the timestamp of the latest commit during build time in RFC3339 + CommitDate = func() string { + if commitDate != "" { + return commitDate + } + return getSettingsValue("vcs.time", "unknown") + }() + + // Modified specifies whether the git worktree was dirty during build time + Modified = func() bool { + return getSettingsValue("vcs.modified", "false") == "true" + }() + + // used for getSettingsValue + info, ok = debug.ReadBuildInfo() ) + +func getSettingsValue(key, def string) string { + if !ok { + return def + } + for _, setting := range info.Settings { + if setting.Key == key { + return setting.Value + } + } + return def +} From b78bb2975863c2a9dc0ddfe30e0766381fe18c7f Mon Sep 17 00:00:00 2001 From: phm07 <22707808+phm07@users.noreply.github.com> Date: Thu, 26 Mar 2026 16:41:48 +0100 Subject: [PATCH 2/4] Use .FullCommit --- .goreleaser.yml | 4 ++-- internal/version/version.go | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.goreleaser.yml b/.goreleaser.yml index 6f32991a0..060c29e69 100644 --- a/.goreleaser.yml +++ b/.goreleaser.yml @@ -15,7 +15,7 @@ builds: - -w - -X {{ .ModulePath }}/internal/version.version={{ .Version }} - -X {{ .ModulePath }}/internal/version.versionPrerelease={{- if .IsSnapshot -}}dev+{{ .ShortCommit }}{{- end -}} - - -X {{ .ModulePath }}/internal/version.commit={{ .Commit }} + - -X {{ .ModulePath }}/internal/version.commit={{ .FullCommit }} - -X {{ .ModulePath }}/internal/version.commitDate={{ .CommitDate }} env: - CGO_ENABLED=0 @@ -111,7 +111,7 @@ kos: - -w - -X {{ .ModulePath }}/internal/version.version={{ .Version }} - -X {{ .ModulePath }}/internal/version.versionPrerelease={{- if .IsSnapshot -}}dev+{{ .ShortCommit }}{{- end -}} - - -X {{ .ModulePath }}/internal/version.commit={{ .Commit }} + - -X {{ .ModulePath }}/internal/version.commit={{ .FullCommit }} - -X {{ .ModulePath }}/internal/version.commitDate={{ .CommitDate }} - -X {{ .ModulePath }}/internal/state/config.defaultConfigPathOverride=/config.toml diff --git a/internal/version/version.go b/internal/version/version.go index 3b6ca9d97..8b21b5489 100644 --- a/internal/version/version.go +++ b/internal/version/version.go @@ -22,7 +22,7 @@ var ( // Can be set by goreleaser because debug.ReadBuildInfo() is not available for goreleaser builds commit = "" - // Commit is the latest commit hash during build time + // Commit is the latest full commit hash during build time Commit = func() string { if commit != "" { return commit From 31d62eabe10ffd9d7b9b77d284196473c41757ea Mon Sep 17 00:00:00 2001 From: phm07 <22707808+phm07@users.noreply.github.com> Date: Thu, 26 Mar 2026 16:45:48 +0100 Subject: [PATCH 3/4] Use .IsGitDirty --- .goreleaser.yml | 2 ++ internal/version/version.go | 6 ++++++ 2 files changed, 8 insertions(+) diff --git a/.goreleaser.yml b/.goreleaser.yml index 060c29e69..81220eed2 100644 --- a/.goreleaser.yml +++ b/.goreleaser.yml @@ -17,6 +17,7 @@ builds: - -X {{ .ModulePath }}/internal/version.versionPrerelease={{- if .IsSnapshot -}}dev+{{ .ShortCommit }}{{- end -}} - -X {{ .ModulePath }}/internal/version.commit={{ .FullCommit }} - -X {{ .ModulePath }}/internal/version.commitDate={{ .CommitDate }} + - -X {{ .ModulePath }}/internal/version.modified={{ .IsGitDirty }} env: - CGO_ENABLED=0 @@ -113,6 +114,7 @@ kos: - -X {{ .ModulePath }}/internal/version.versionPrerelease={{- if .IsSnapshot -}}dev+{{ .ShortCommit }}{{- end -}} - -X {{ .ModulePath }}/internal/version.commit={{ .FullCommit }} - -X {{ .ModulePath }}/internal/version.commitDate={{ .CommitDate }} + - -X {{ .ModulePath }}/internal/version.modified={{ .IsGitDirty }} - -X {{ .ModulePath }}/internal/state/config.defaultConfigPathOverride=/config.toml snapshot: diff --git a/internal/version/version.go b/internal/version/version.go index 8b21b5489..08fe442ad 100644 --- a/internal/version/version.go +++ b/internal/version/version.go @@ -41,8 +41,14 @@ var ( return getSettingsValue("vcs.time", "unknown") }() + // Can be set by goreleaser because debug.ReadBuildInfo() is not available for goreleaser builds + modified = "" + // Modified specifies whether the git worktree was dirty during build time Modified = func() bool { + if modified != "" { + return modified == "true" + } return getSettingsValue("vcs.modified", "false") == "true" }() From baad337afbc4477ed9e962d09ccff78d77bfbc82 Mon Sep 17 00:00:00 2001 From: phm07 <22707808+phm07@users.noreply.github.com> Date: Thu, 26 Mar 2026 16:49:21 +0100 Subject: [PATCH 4/4] Add debug.ReadBuildInfo() explanation comment --- internal/version/version.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/internal/version/version.go b/internal/version/version.go index 08fe442ad..6edc914f3 100644 --- a/internal/version/version.go +++ b/internal/version/version.go @@ -56,6 +56,10 @@ var ( info, ok = debug.ReadBuildInfo() ) +// getSettingsValue is a helper for getting values from debug.ReadBuildInfo() +// This is only a fallback for builds that do not use goreleaser, since goreleaser +// usually injects the above variables using ldflags. debug.ReadBuildInfo() will be +// used for example when installing using 'go install'. func getSettingsValue(key, def string) string { if !ok { return def