Skip to content

Commit b2ef159

Browse files
committed
cmd/go: introduce the GOVERSION env variable
This is an extra variable available via 'go env', but not read from the user's environment. It corresponds to the same string that runtime.Version returns, assuming a program is built by the same version of Go. It's similar to the output of 'go version', but without the "go version" prefix nor the "$GOOS/$GOARCH" suffix. The main use case here is tools, which often use 'go env' to query basic information about the installed Go tree. Its version was one missing piece of information, which required an extra call to 'go version' before this change. Fixes #41116. Change-Id: I5c9d8c2ba856c816c9f4c462ba73c907b3441445 Reviewed-on: https://go-review.googlesource.com/c/go/+/265637 Run-TryBot: Daniel Martí <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Jay Conrod <[email protected]> Reviewed-by: Russ Cox <[email protected]> Trust: Jay Conrod <[email protected]> Trust: Daniel Martí <[email protected]>
1 parent 1948c00 commit b2ef159

File tree

5 files changed

+20
-1
lines changed

5 files changed

+20
-1
lines changed

src/cmd/go/alldocs.go

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/cmd/go/go_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1886,6 +1886,18 @@ func TestGoEnv(t *testing.T) {
18861886
tg.grepStdout("gcc", "CC not found")
18871887
tg.run("env", "GOGCCFLAGS")
18881888
tg.grepStdout("-ffaster", "CC arguments not found")
1889+
1890+
tg.run("env", "GOVERSION")
1891+
envVersion := strings.TrimSpace(tg.stdout.String())
1892+
1893+
tg.run("version")
1894+
cmdVersion := strings.TrimSpace(tg.stdout.String())
1895+
1896+
// If 'go version' is "go version <version> <goos>/<goarch>", then
1897+
// 'go env GOVERSION' is just "<version>".
1898+
if cmdVersion == envVersion || !strings.Contains(cmdVersion, envVersion) {
1899+
t.Fatalf("'go env GOVERSION' %q should be a shorter substring of 'go version' %q", envVersion, cmdVersion)
1900+
}
18891901
}
18901902

18911903
const (

src/cmd/go/internal/envcmd/env.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ func MkEnv() []cfg.EnvVar {
8888
{Name: "GOTMPDIR", Value: cfg.Getenv("GOTMPDIR")},
8989
{Name: "GOTOOLDIR", Value: base.ToolDir},
9090
{Name: "GOVCS", Value: cfg.GOVCS},
91+
{Name: "GOVERSION", Value: runtime.Version()},
9192
}
9293

9394
if work.GccgoBin != "" {
@@ -399,7 +400,7 @@ func getOrigEnv(key string) string {
399400

400401
func checkEnvWrite(key, val string) error {
401402
switch key {
402-
case "GOEXE", "GOGCCFLAGS", "GOHOSTARCH", "GOHOSTOS", "GOMOD", "GOTOOLDIR":
403+
case "GOEXE", "GOGCCFLAGS", "GOHOSTARCH", "GOHOSTOS", "GOMOD", "GOTOOLDIR", "GOVERSION":
403404
return fmt.Errorf("%s cannot be modified", key)
404405
case "GOENV":
405406
return fmt.Errorf("%s can only be set using the OS environment", key)

src/cmd/go/internal/help/helpdoc.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -632,6 +632,8 @@ Additional information available from 'go env' but not read from the environment
632632
If module-aware mode is disabled, GOMOD will be the empty string.
633633
GOTOOLDIR
634634
The directory where the go tools (compile, cover, doc, etc...) are installed.
635+
GOVERSION
636+
The version of the installed Go tree, as reported by runtime.Version.
635637
`,
636638
}
637639

src/cmd/go/testdata/script/env_write.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ go env -u GOPATH
6969
stderr 'unknown go command variable GODEBUG'
7070
! go env -w GOEXE=.bat
7171
stderr 'GOEXE cannot be modified'
72+
! go env -w GOVERSION=customversion
73+
stderr 'GOVERSION cannot be modified'
7274
! go env -w GOENV=/env
7375
stderr 'GOENV can only be set using the OS environment'
7476

0 commit comments

Comments
 (0)