From 85a068fdf21bd2e4475a87ee049af4fbe797bcbe Mon Sep 17 00:00:00 2001 From: Jay Conrod Date: Thu, 30 Sep 2021 10:46:03 -0700 Subject: runtime/debug: add GoVersion to BuildInfo BuildInfo now includes the version of Go used to build a binary, as reported by runtime.Version() or 'go version'. For #37475 Change-Id: Id07dda357dc70599d64a9202dab894c7288de1de Reviewed-on: https://go-review.googlesource.com/c/go/+/353888 Trust: Jay Conrod Run-TryBot: Jay Conrod TryBot-Result: Go Bot Reviewed-by: Bryan C. Mills --- src/debug/buildinfo/buildinfo.go | 3 ++- src/debug/buildinfo/buildinfo_test.go | 7 ++++--- 2 files changed, 6 insertions(+), 4 deletions(-) (limited to 'src/debug') diff --git a/src/debug/buildinfo/buildinfo.go b/src/debug/buildinfo/buildinfo.go index 8def2eae6e..f84429a342 100644 --- a/src/debug/buildinfo/buildinfo.go +++ b/src/debug/buildinfo/buildinfo.go @@ -71,7 +71,7 @@ func ReadFile(name string) (info *BuildInfo, err error) { // accessed through the given ReaderAt. Most information is only available for // binaries built with module support. func Read(r io.ReaderAt) (*BuildInfo, error) { - _, mod, err := readRawBuildInfo(r) + vers, mod, err := readRawBuildInfo(r) if err != nil { return nil, err } @@ -79,6 +79,7 @@ func Read(r io.ReaderAt) (*BuildInfo, error) { if err := bi.UnmarshalText([]byte(mod)); err != nil { return nil, err } + bi.GoVersion = vers return bi, nil } diff --git a/src/debug/buildinfo/buildinfo_test.go b/src/debug/buildinfo/buildinfo_test.go index 765bf24627..ab307d75c2 100644 --- a/src/debug/buildinfo/buildinfo_test.go +++ b/src/debug/buildinfo/buildinfo_test.go @@ -142,7 +142,8 @@ func TestReadFile(t *testing.T) { { name: "valid_modules", build: buildWithModules, - want: "path\texample.com/m\n" + + want: "go\t$GOVERSION\n" + + "path\texample.com/m\n" + "mod\texample.com/m\t(devel)\t\n", }, { @@ -157,7 +158,7 @@ func TestReadFile(t *testing.T) { { name: "valid_gopath", build: buildWithGOPATH, - want: "", + want: "go\t$GOVERSION\n", }, { name: "invalid_gopath", @@ -193,7 +194,7 @@ func TestReadFile(t *testing.T) { } else if got, err := info.MarshalText(); err != nil { t.Fatalf("unexpected error marshaling BuildInfo: %v", err) } else { - got := string(got) + got := strings.ReplaceAll(string(got), runtime.Version(), "$GOVERSION") if got != tc.want { t.Fatalf("got:\n%s\nwant:\n%s", got, tc.want) } -- cgit v1.3