version: fix build version using buildInfo (#2789)

In go1.18 buildInfo will include the git revision hash, use that to fix
the Build field of Version so that it is correct even if Delve wasn't
built using make.go.
This commit is contained in:
Alessandro Arzilli 2021-11-24 22:45:28 +01:00 committed by GitHub
parent b69bcf53d6
commit f34a1e6a5e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 0 deletions

23
pkg/version/fixbuild.go Normal file

@ -0,0 +1,23 @@
//go:build go1.18
// +build go1.18
package version
import "runtime/debug"
func init() {
fixBuild = buildInfoFixBuild
}
func buildInfoFixBuild(v *Version) {
info, ok := debug.ReadBuildInfo()
if !ok {
return
}
for i := range info.Settings {
if info.Settings[i].Key == "gitrevision" {
v.Build = info.Settings[i].Value
break
}
}
}

@ -24,6 +24,7 @@ var (
)
func (v Version) String() string {
fixBuild(&v)
ver := fmt.Sprintf("Version: %s.%s.%s", v.Major, v.Minor, v.Patch)
if v.Metadata != "" {
ver += "-" + v.Metadata
@ -38,3 +39,7 @@ var buildInfo = func() string {
func BuildInfo() string {
return fmt.Sprintf("%s\n%s", runtime.Version(), buildInfo())
}
var fixBuild = func(v *Version) {
// does nothing
}