]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: introduce the GOVERSION env variable
authorDaniel Martí <mvdan@mvdan.cc>
Tue, 27 Oct 2020 18:12:03 +0000 (18:12 +0000)
committerDaniel Martí <mvdan@mvdan.cc>
Tue, 10 Nov 2020 18:33:37 +0000 (18:33 +0000)
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í <mvdan@mvdan.cc>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Jay Conrod <jayconrod@google.com>
Reviewed-by: Russ Cox <rsc@golang.org>
Trust: Jay Conrod <jayconrod@google.com>
Trust: Daniel Martí <mvdan@mvdan.cc>

src/cmd/go/alldocs.go
src/cmd/go/go_test.go
src/cmd/go/internal/envcmd/env.go
src/cmd/go/internal/help/helpdoc.go
src/cmd/go/testdata/script/env_write.txt

index e8bfff1e699fe1b0418564ba225719cedc180193..47076570a66c173a32ad0e4723a687ad0064798a 100644 (file)
 //             If module-aware mode is disabled, GOMOD will be the empty string.
 //     GOTOOLDIR
 //             The directory where the go tools (compile, cover, doc, etc...) are installed.
+//     GOVERSION
+//             The version of the installed Go tree, as reported by runtime.Version.
 //
 //
 // File types
index ee0cd8e2c72947e8420be1855c8fd4e2f78242ae..a02231fa98324df1eeff128399a233d829ea014f 100644 (file)
@@ -1886,6 +1886,18 @@ func TestGoEnv(t *testing.T) {
        tg.grepStdout("gcc", "CC not found")
        tg.run("env", "GOGCCFLAGS")
        tg.grepStdout("-ffaster", "CC arguments not found")
+
+       tg.run("env", "GOVERSION")
+       envVersion := strings.TrimSpace(tg.stdout.String())
+
+       tg.run("version")
+       cmdVersion := strings.TrimSpace(tg.stdout.String())
+
+       // If 'go version' is "go version <version> <goos>/<goarch>", then
+       // 'go env GOVERSION' is just "<version>".
+       if cmdVersion == envVersion || !strings.Contains(cmdVersion, envVersion) {
+               t.Fatalf("'go env GOVERSION' %q should be a shorter substring of 'go version' %q", envVersion, cmdVersion)
+       }
 }
 
 const (
index d65ace879db6487897fe829d71f8968e1bb3fb8f..46af36eb11666f8514bf766b1d59febe279548af 100644 (file)
@@ -88,6 +88,7 @@ func MkEnv() []cfg.EnvVar {
                {Name: "GOTMPDIR", Value: cfg.Getenv("GOTMPDIR")},
                {Name: "GOTOOLDIR", Value: base.ToolDir},
                {Name: "GOVCS", Value: cfg.GOVCS},
+               {Name: "GOVERSION", Value: runtime.Version()},
        }
 
        if work.GccgoBin != "" {
@@ -399,7 +400,7 @@ func getOrigEnv(key string) string {
 
 func checkEnvWrite(key, val string) error {
        switch key {
-       case "GOEXE", "GOGCCFLAGS", "GOHOSTARCH", "GOHOSTOS", "GOMOD", "GOTOOLDIR":
+       case "GOEXE", "GOGCCFLAGS", "GOHOSTARCH", "GOHOSTOS", "GOMOD", "GOTOOLDIR", "GOVERSION":
                return fmt.Errorf("%s cannot be modified", key)
        case "GOENV":
                return fmt.Errorf("%s can only be set using the OS environment", key)
index 50cf911407ed2891d6d849461bba56aa331263e9..98f58441b4de605a75084887d3ea7c57232c8ee9 100644 (file)
@@ -632,6 +632,8 @@ Additional information available from 'go env' but not read from the environment
                If module-aware mode is disabled, GOMOD will be the empty string.
        GOTOOLDIR
                The directory where the go tools (compile, cover, doc, etc...) are installed.
+       GOVERSION
+               The version of the installed Go tree, as reported by runtime.Version.
        `,
 }
 
index 0af22ed421c24d4425af5839b689ab4742cf7aec..bda1e57826ee1bb9dbea4cac1e3571f533fda501 100644 (file)
@@ -69,6 +69,8 @@ go env -u GOPATH
 stderr 'unknown go command variable GODEBUG'
 ! go env -w GOEXE=.bat
 stderr 'GOEXE cannot be modified'
+! go env -w GOVERSION=customversion
+stderr 'GOVERSION cannot be modified'
 ! go env -w GOENV=/env
 stderr 'GOENV can only be set using the OS environment'