From fe587ce856d1dee97829fe0ed090ba7e068335cb Mon Sep 17 00:00:00 2001 From: =?utf8?q?Daniel=20Mart=C3=AD?= Date: Mon, 26 Oct 2020 09:42:54 +0000 Subject: [PATCH] cmd/dist: include "go1.x-" in devel go version strings MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit This way, "go version" will report the "base version" or major version that the tool corresponds to. This is the same version number that is matched against build tags such as "go1.14" or "!go1.16". Obtaining this version being built is non-trivial, since we can't just import a package or query git. The added comments document the chosen mechanism, based on a regular expression. It was chosen over AST parsing as it would add a significant amount of code without much gain, given how simple the goversion.go file is. For #41116. Change-Id: I653ae935e27c13267f23898f89c84020dcd6e194 Reviewed-on: https://go-review.googlesource.com/c/go/+/264938 Run-TryBot: Daniel Martí Trust: Daniel Martí Trust: Jay Conrod TryBot-Result: Go Bot Reviewed-by: Russ Cox --- src/cmd/dist/build.go | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/src/cmd/dist/build.go b/src/cmd/dist/build.go index acf38e3785..8accb6db8f 100644 --- a/src/cmd/dist/build.go +++ b/src/cmd/dist/build.go @@ -14,6 +14,7 @@ import ( "os" "os/exec" "path/filepath" + "regexp" "sort" "strings" "sync" @@ -405,8 +406,22 @@ func findgoversion() string { } if !precise { - // Tag does not point at HEAD; add hash and date to version. - tag += chomp(run(goroot, CheckExit, "git", "log", "-n", "1", "--format=format: +%h %cd", "HEAD")) + // Tag does not point at HEAD; add 1.x base version, hash, and date to version. + // + // Note that we lightly parse internal/goversion/goversion.go to + // obtain the base version. We can't just import the package, + // because cmd/dist is built with a bootstrap GOROOT which could + // be an entirely different version of Go, like 1.4. We assume + // that the file contains "const Version = ". + + goversionSource := readfile(pathf("%s/src/internal/goversion/goversion.go", goroot)) + m := regexp.MustCompile(`(?m)^const Version = (\d+)`).FindStringSubmatch(goversionSource) + if m == nil { + fatalf("internal/goversion/goversion.go does not contain 'const Version = ...'") + } + tag += fmt.Sprintf(" go1.%s-", m[1]) + + tag += chomp(run(goroot, CheckExit, "git", "log", "-n", "1", "--format=format:%h %cd", "HEAD")) } // Cache version. -- 2.50.0