]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: add GOWORK to go env command
authorMichael Matloob <matloob@golang.org>
Fri, 17 Sep 2021 23:38:33 +0000 (19:38 -0400)
committerMichael Matloob <matloob@golang.org>
Tue, 19 Oct 2021 18:43:12 +0000 (18:43 +0000)
GOWORK will be set to the go.work file's path, if in workspace mode
or will be empty otherwise.

For #45713
Fixes #48589

Change-Id: I163ffaf274e0a41469c1f3b8514d6f90e20423b0
Reviewed-on: https://go-review.googlesource.com/c/go/+/355689
Trust: Michael Matloob <matloob@golang.org>
Trust: Daniel Martí <mvdan@mvdan.cc>
Run-TryBot: Michael Matloob <matloob@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Bryan C. Mills <bcmills@google.com>
src/cmd/go/internal/envcmd/env.go
src/cmd/go/testdata/script/work.txt
src/cmd/go/testdata/script/work_env.txt [new file with mode: 0644]

index 1eb773407e2f21d4375546f7d7038602c12aa525..181d2a2ca1f49734a8c94f88983d6ade8da9f367 100644 (file)
@@ -152,8 +152,11 @@ func ExtraEnvVars() []cfg.EnvVar {
        } else if modload.Enabled() {
                gomod = os.DevNull
        }
+       modload.InitWorkfile()
+       gowork := modload.WorkFilePath()
        return []cfg.EnvVar{
                {Name: "GOMOD", Value: gomod},
+               {Name: "GOWORK", Value: gowork},
        }
 }
 
@@ -431,7 +434,7 @@ func getOrigEnv(key string) string {
 
 func checkEnvWrite(key, val string) error {
        switch key {
-       case "GOEXE", "GOGCCFLAGS", "GOHOSTARCH", "GOHOSTOS", "GOMOD", "GOTOOLDIR", "GOVERSION":
+       case "GOEXE", "GOGCCFLAGS", "GOHOSTARCH", "GOHOSTOS", "GOMOD", "GOWORK", "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 657cd060cc1b0724b8a081566e95988e6f10966e..613f03761521abc8ca4e5a8c9dc64a464fb210c5 100644 (file)
@@ -1,8 +1,12 @@
 ! go mod initwork doesnotexist
 stderr 'go: creating workspace file: no go.mod file exists in directory doesnotexist'
+go env GOWORK
+! stdout .
 
 go mod initwork ./a ./b
 cmp go.work go.work.want
+go env GOWORK
+stdout '^'$WORK'(\\|/)gopath(\\|/)src(\\|/)go.work$'
 
 ! go run  example.com/b
 stderr 'a(\\|/)a.go:4:8: no required module provides package rsc.io/quote; to add it:\n\tcd '$WORK(\\|/)gopath(\\|/)src(\\|/)a'\n\tgo get rsc.io/quote'
diff --git a/src/cmd/go/testdata/script/work_env.txt b/src/cmd/go/testdata/script/work_env.txt
new file mode 100644 (file)
index 0000000..de67255
--- /dev/null
@@ -0,0 +1,24 @@
+go env GOWORK
+stdout '^'$GOPATH'[\\/]src[\\/]go.work$'
+go env
+stdout '^(set )?GOWORK="?'$GOPATH'[\\/]src[\\/]go.work"?$'
+
+cd ..
+go env GOWORK
+! stdout .
+go env
+stdout 'GOWORK=("")?'
+
+cd src
+go env GOWORK
+stdout 'go.work'
+
+! go env -w GOWORK=off
+stderr '^go: GOWORK cannot be modified$'
+
+-- go.work --
+go 1.18
+
+directory a
+-- a/go.mod --
+module example.com/a