1
0
mirror of https://github.com/golang/go synced 2024-09-29 20:24:34 -06:00

cmd/go: add GOWORK to go env command

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>
This commit is contained in:
Michael Matloob 2021-09-17 19:38:33 -04:00
parent 1d63052782
commit d94498470b
3 changed files with 32 additions and 1 deletions

View 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)

View 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'

24
src/cmd/go/testdata/script/work_env.txt vendored Normal file
View File

@ -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