Skip to content

Commit fe587ce

Browse files
committed
cmd/dist: include "go1.x-" in devel go version strings
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í <[email protected]> Trust: Daniel Martí <[email protected]> Trust: Jay Conrod <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Russ Cox <[email protected]>
1 parent 0182113 commit fe587ce

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

src/cmd/dist/build.go

+17-2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"os"
1515
"os/exec"
1616
"path/filepath"
17+
"regexp"
1718
"sort"
1819
"strings"
1920
"sync"
@@ -405,8 +406,22 @@ func findgoversion() string {
405406
}
406407

407408
if !precise {
408-
// Tag does not point at HEAD; add hash and date to version.
409-
tag += chomp(run(goroot, CheckExit, "git", "log", "-n", "1", "--format=format: +%h %cd", "HEAD"))
409+
// Tag does not point at HEAD; add 1.x base version, hash, and date to version.
410+
//
411+
// Note that we lightly parse internal/goversion/goversion.go to
412+
// obtain the base version. We can't just import the package,
413+
// because cmd/dist is built with a bootstrap GOROOT which could
414+
// be an entirely different version of Go, like 1.4. We assume
415+
// that the file contains "const Version = <Integer>".
416+
417+
goversionSource := readfile(pathf("%s/src/internal/goversion/goversion.go", goroot))
418+
m := regexp.MustCompile(`(?m)^const Version = (\d+)`).FindStringSubmatch(goversionSource)
419+
if m == nil {
420+
fatalf("internal/goversion/goversion.go does not contain 'const Version = ...'")
421+
}
422+
tag += fmt.Sprintf(" go1.%s-", m[1])
423+
424+
tag += chomp(run(goroot, CheckExit, "git", "log", "-n", "1", "--format=format:%h %cd", "HEAD"))
410425
}
411426

412427
// Cache version.

0 commit comments

Comments
 (0)