Skip to content

Commit ce85e17

Browse files
committed
version: use package-level variables for linker flag support
The build ID wasn't properly being set by linker flags because struct fields cannot be directly modified by the Go linker flags; only package-level variables can be modified by the Go linker flags. This change: - Moves version information to package-level variables - Updates initialization to use these variables - Improves fallback mechanism to check for `vcs.revision` first - Maintains backward compatibility with `gitrevision` This addresses issue #3986 where the build ID was displaying a hardcoded hash instead of the proper git commit hash or custom value. Note: I could not determine why `gitrevision` was being used but it may have been from historical linker flag usage, such as `-ldflags "-X main.gitrevision=$(git rev-parse HEAD)"`. Since Go 1.18 `vcs.revision` is the standard now. I have maintained the `gitrevision` look for backwards compatibility. Fixes #3986
1 parent f0cc62b commit ce85e17

File tree

2 files changed

+31
-6
lines changed

2 files changed

+31
-6
lines changed

pkg/version/fixbuild.go

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,27 @@ func init() {
99
}
1010

1111
func buildInfoFixBuild(v *Version) {
12+
if v.Build != "$Id$" {
13+
return
14+
}
15+
1216
info, ok := debug.ReadBuildInfo()
1317
if !ok {
1418
return
1519
}
16-
for i := range info.Settings {
17-
if info.Settings[i].Key == "gitrevision" {
18-
v.Build = info.Settings[i].Value
19-
break
20+
21+
for _, setting := range info.Settings {
22+
if setting.Key == "vcs.revision" {
23+
v.Build = setting.Value
24+
return
25+
}
26+
}
27+
28+
// If we didn't find vcs.revision, try the old key for backward compatibility
29+
for _, setting := range info.Settings {
30+
if setting.Key == "gitrevision" {
31+
v.Build = setting.Value
32+
return
2033
}
2134
}
2235
}

pkg/version/version.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,15 @@ import (
55
"runtime"
66
)
77

8+
// Package-level variables that can be set by linker flags
9+
var (
10+
VersionMajor = "1"
11+
VersionMinor = "24"
12+
VersionPatch = "2"
13+
VersionMetadata = ""
14+
BuildVersion = "$Id$"
15+
)
16+
817
// Version represents the current version of Delve.
918
type Version struct {
1019
Major string
@@ -16,8 +25,11 @@ type Version struct {
1625

1726
// DelveVersion is the current version of Delve.
1827
var DelveVersion = Version{
19-
Major: "1", Minor: "24", Patch: "2", Metadata: "",
20-
Build: "$Id$",
28+
Major: VersionMajor,
29+
Minor: VersionMinor,
30+
Patch: VersionPatch,
31+
Metadata: VersionMetadata,
32+
Build: BuildVersion,
2133
}
2234

2335
func (v Version) String() string {

0 commit comments

Comments
 (0)