-
Notifications
You must be signed in to change notification settings - Fork 1
Report real version info for go-install builds #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+140
−1
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| package main | ||
|
|
||
| import "runtime/debug" | ||
|
|
||
| // buildInfo resolves the version metadata shown by --version. GoReleaser | ||
| // injects real values via ldflags for release binaries; go-install and | ||
| // plain go-build binaries keep the defaults, so fall back to the module | ||
| // build info Go embeds in every binary. | ||
| func buildInfo() (string, string, string) { | ||
| info, ok := debug.ReadBuildInfo() | ||
| if !ok { | ||
| return version, commit, date | ||
| } | ||
| return resolveBuildInfo(info, version, commit, date) | ||
| } | ||
|
|
||
| func resolveBuildInfo(info *debug.BuildInfo, v, c, d string) (string, string, string) { | ||
| if v == "dev" && info.Main.Version != "" && info.Main.Version != "(devel)" { | ||
| v = info.Main.Version | ||
| } | ||
| fromVCS := false | ||
| dirty := false | ||
| for _, s := range info.Settings { | ||
| switch s.Key { | ||
| case "vcs.revision": | ||
| if c == "none" { | ||
| c = s.Value | ||
| fromVCS = true | ||
| } | ||
| case "vcs.time": | ||
| if d == "unknown" { | ||
| d = s.Value | ||
| } | ||
| case "vcs.modified": | ||
| dirty = s.Value == "true" | ||
| } | ||
| } | ||
| // A build from a tree with uncommitted changes must not claim to | ||
| // exactly match the reported commit. | ||
| if fromVCS && dirty { | ||
| c += "-dirty" | ||
| } | ||
| return v, c, d | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "runtime/debug" | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestResolveBuildInfo(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| info *debug.BuildInfo | ||
| v, c, d string | ||
| wantV string | ||
| wantC string | ||
| wantD string | ||
| }{ | ||
| { | ||
| name: "go install binary resolves module version", | ||
| info: &debug.BuildInfo{ | ||
| Main: debug.Module{Version: "v1.2.0"}, | ||
| }, | ||
| v: "dev", c: "none", d: "unknown", | ||
| wantV: "v1.2.0", wantC: "none", wantD: "unknown", | ||
| }, | ||
| { | ||
| name: "local git build resolves vcs metadata", | ||
| info: &debug.BuildInfo{ | ||
| Main: debug.Module{Version: "(devel)"}, | ||
| Settings: []debug.BuildSetting{ | ||
| {Key: "vcs.revision", Value: "abc1234"}, | ||
| {Key: "vcs.time", Value: "2026-07-15T00:00:00Z"}, | ||
| }, | ||
| }, | ||
| v: "dev", c: "none", d: "unknown", | ||
| wantV: "dev", wantC: "abc1234", wantD: "2026-07-15T00:00:00Z", | ||
| }, | ||
| { | ||
| name: "goreleaser ldflags win over build info", | ||
| info: &debug.BuildInfo{ | ||
| Main: debug.Module{Version: "v1.2.0"}, | ||
| Settings: []debug.BuildSetting{ | ||
| {Key: "vcs.revision", Value: "abc1234"}, | ||
| }, | ||
| }, | ||
| v: "v1.2.0", c: "deadbeef", d: "2026-07-15", | ||
| wantV: "v1.2.0", wantC: "deadbeef", wantD: "2026-07-15", | ||
| }, | ||
| { | ||
| name: "dirty checkout marks the commit", | ||
| info: &debug.BuildInfo{ | ||
| Main: debug.Module{Version: "(devel)"}, | ||
| Settings: []debug.BuildSetting{ | ||
| {Key: "vcs.revision", Value: "abc1234"}, | ||
| {Key: "vcs.modified", Value: "true"}, | ||
| }, | ||
| }, | ||
| v: "dev", c: "none", d: "unknown", | ||
| wantV: "dev", wantC: "abc1234-dirty", wantD: "unknown", | ||
| }, | ||
| { | ||
| name: "ldflags commit is never marked dirty", | ||
| info: &debug.BuildInfo{ | ||
| Settings: []debug.BuildSetting{ | ||
| {Key: "vcs.revision", Value: "abc1234"}, | ||
| {Key: "vcs.modified", Value: "true"}, | ||
| }, | ||
| }, | ||
| v: "v1.2.0", c: "deadbeef", d: "2026-07-15", | ||
| wantV: "v1.2.0", wantC: "deadbeef", wantD: "2026-07-15", | ||
| }, | ||
| { | ||
| name: "empty build info keeps defaults", | ||
| info: &debug.BuildInfo{}, | ||
| v: "dev", c: "none", d: "unknown", | ||
| wantV: "dev", wantC: "none", wantD: "unknown", | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| gotV, gotC, gotD := resolveBuildInfo(tt.info, tt.v, tt.c, tt.d) | ||
| if gotV != tt.wantV || gotC != tt.wantC || gotD != tt.wantD { | ||
| t.Errorf("resolveBuildInfo() = (%q, %q, %q), want (%q, %q, %q)", | ||
| gotV, gotC, gotD, tt.wantV, tt.wantC, tt.wantD) | ||
| } | ||
| }) | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.