Skip to content

Commit 0941dc4

Browse files
ekalininJay Conrod
authored and
Jay Conrod
committed
cmd/go: env -w validates GOTMPDIR value
This change makes go env -w check if GOTMPDIR is an absolute path. If GOTMPDIR is not an absolute and not existing path there will be an error at every `work.Builder.Init()`. If `go env` has `-u/-w` as argument `work.Builder.Init()` is not called. `go env -w GOTMPDIR=` work in the same way as `go env -u GOTMPDIR`. Fixes #40932 Change-Id: I6b0662302eeace7f20460b6d26c6e59af1111da2 Reviewed-on: https://go-review.googlesource.com/c/go/+/250198 Run-TryBot: Jay Conrod <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Jay Conrod <[email protected]> Trust: Bryan C. Mills <[email protected]> Trust: Jay Conrod <[email protected]>
1 parent 67edc0e commit 0941dc4

File tree

2 files changed

+46
-4
lines changed

2 files changed

+46
-4
lines changed

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

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -203,10 +203,19 @@ func runEnv(ctx context.Context, cmd *base.Command, args []string) {
203203
}
204204

205205
// Do we need to call ExtraEnvVarsCostly, which is a bit expensive?
206-
// Only if we're listing all environment variables ("go env")
207-
// or the variables being requested are in the extra list.
208-
needCostly := true
209-
if len(args) > 0 {
206+
needCostly := false
207+
if *envU || *envW {
208+
// We're overwriting or removing default settings,
209+
// so it doesn't really matter what the existing settings are.
210+
//
211+
// Moreover, we haven't validated the new settings yet, so it is
212+
// important that we NOT perform any actions based on them,
213+
// such as initializing the builder to compute other variables.
214+
} else if len(args) == 0 {
215+
// We're listing all environment variables ("go env"),
216+
// including the expensive ones.
217+
needCostly = true
218+
} else {
210219
needCostly = false
211220
for _, arg := range args {
212221
switch argKey(arg) {
@@ -269,6 +278,13 @@ func runEnv(ctx context.Context, cmd *base.Command, args []string) {
269278
}
270279
}
271280

281+
gotmp, okGOTMP := add["GOTMPDIR"]
282+
if okGOTMP {
283+
if !filepath.IsAbs(gotmp) && gotmp != "" {
284+
base.Fatalf("go env -w: GOTMPDIR must be an absolute path")
285+
}
286+
}
287+
272288
updateEnvFile(add, nil)
273289
return
274290
}

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ stdout GOARCH=
2424
stdout GOOS=
2525
stdout GOROOT=
2626

27+
# checking errors
28+
! go env -w
29+
stderr 'go env -w: no KEY=VALUE arguments given'
30+
! go env -u
31+
stderr 'go env -u: no arguments given'
32+
2733
# go env -w changes default setting
2834
env root=
2935
[windows] env root=c:
@@ -97,6 +103,26 @@ stderr 'GOPATH entry cannot start with shell metacharacter'
97103
! go env -w GOPATH=./go
98104
stderr 'GOPATH entry is relative; must be absolute path'
99105

106+
# go env -w rejects invalid GOTMPDIR values
107+
! go env -w GOTMPDIR=x
108+
stderr 'go env -w: GOTMPDIR must be an absolute path'
109+
110+
# go env -w should accept absolute GOTMPDIR value
111+
# and should not create it
112+
[windows] go env -w GOTMPDIR=$WORK\x\y\z
113+
[!windows] go env -w GOTMPDIR=$WORK/x/y/z
114+
! exists $WORK/x/y/z
115+
# we should be able to clear an env
116+
go env -u GOTMPDIR
117+
go env GOTMPDIR
118+
stdout ^$
119+
120+
[windows] go env -w GOTMPDIR=$WORK\x\y\z
121+
[!windows] go env -w GOTMPDIR=$WORK/x/y/z
122+
go env -w GOTMPDIR=
123+
go env GOTMPDIR
124+
stdout ^$
125+
100126
# go env -w/-u checks validity of GOOS/ARCH combinations
101127
env GOOS=
102128
env GOARCH=

0 commit comments

Comments
 (0)