Skip to content

Commit e299381

Browse files
author
Bryan C. Mills
committed
cmd/go: fix TestScript/build_trimpath_goroot when built with a mismatched GOROOT_FINAL
Fixes golang#52236. Updates golang#51461. Change-Id: Ie91e0256afd45e9bbd60fd8cdc696363027ab696 Reviewed-on: https://go-review.googlesource.com/c/go/+/399156 Run-TryBot: Bryan Mills <[email protected]> Reviewed-by: Russ Cox <[email protected]> TryBot-Result: Gopher Robot <[email protected]>
1 parent d4dbad5 commit e299381

File tree

4 files changed

+42
-6
lines changed

4 files changed

+42
-6
lines changed

src/cmd/go/go_test.go

+4
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ func tooSlow(t *testing.T) {
7878
// (temp) directory.
7979
var testGOROOT string
8080

81+
// testGOROOT_FINAL is the GOROOT_FINAL with which the test binary is assumed to
82+
// have been built.
83+
var testGOROOT_FINAL = os.Getenv("GOROOT_FINAL")
84+
8185
var testGOCACHE string
8286

8387
var testGo string

src/cmd/go/script_test.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ func (ts *testScript) setup() {
175175
"GOPROXY=" + proxyURL,
176176
"GOPRIVATE=",
177177
"GOROOT=" + testGOROOT,
178-
"GOROOT_FINAL=" + os.Getenv("GOROOT_FINAL"), // causes spurious rebuilds and breaks the "stale" built-in if not propagated
178+
"GOROOT_FINAL=" + testGOROOT_FINAL, // causes spurious rebuilds and breaks the "stale" built-in if not propagated
179179
"GOTRACEBACK=system",
180180
"TESTGO_GOROOT=" + testGOROOT,
181181
"GOSUMDB=" + testSumDBVerifierKey,
@@ -385,6 +385,8 @@ Script:
385385
}
386386
}
387387
}
388+
case "mismatched-goroot":
389+
ok = testGOROOT_FINAL != "" && testGOROOT_FINAL != testGOROOT
388390
default:
389391
if strings.HasPrefix(cond.tag, "exec:") {
390392
prog := cond.tag[len("exec:"):]

src/cmd/go/testdata/script/README

+2
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ should only run when the condition is satisfied. The available conditions are:
9090
- [exec:prog] for whether prog is available for execution (found by exec.LookPath)
9191
- [GODEBUG:value] for whether value is one of the comma-separated entries in the GODEBUG variable
9292
- [buildmode:value] for whether -buildmode=value is supported
93+
- [trimpath] for whether the 'go' binary was built with -trimpath
94+
- [mismatched-goroot] for whether the test's GOROOT_FINAL does not match the real GOROOT
9395

9496
A condition can be negated: [!short] means to run the rest of the line
9597
when testing.Short() is false. Multiple conditions may be given for a single

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

+33-5
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,52 @@
88
# TODO(#51483): when runtime.GOROOT() returns the empty string,
99
# go/build should default to 'go env GOROOT' instead.
1010

11-
env GOROOT=
1211
env GOROOT_FINAL=
1312

13+
[trimpath] env GOROOT=
1414
[trimpath] ! go env GOROOT
1515
[trimpath] stderr '^go: cannot find GOROOT directory: ''go'' binary is trimmed and GOROOT is not set$'
16-
[trimpath] stop
16+
[trimpath] env GOROOT=$TESTGO_GOROOT
17+
18+
[short] stop
1719

20+
# With GOROOT still set but GOROOT_FINAL unset, 'go build' and 'go test -c'
21+
# should cause runtime.GOROOT() to report either the correct GOROOT
22+
# (without -trimpath) or no GOROOT at all (with -trimpath).
1823

19-
[short] skip
24+
go build -o example.exe .
25+
go build -trimpath -o example-trimpath.exe .
26+
go test -c -o example.test.exe .
27+
go test -trimpath -c -o example.test-trimpath.exe .
2028

21-
go run .
29+
env GOROOT=
30+
31+
exec ./example.exe
2232
stdout '^GOROOT '$TESTGO_GOROOT'$'
2333
stdout '^runtime '$TESTGO_GOROOT${/}src${/}runtime'$'
2434

25-
go test -v .
35+
! exec ./example-trimpath.exe
36+
stdout '^GOROOT $'
37+
stderr 'cannot find package "runtime" in any of:\n\t\(\$GOROOT not set\)\n\t'$WORK${/}gopath${/}src${/}runtime' \(from \$GOPATH\)\n\z'
38+
39+
exec ./example.test.exe -test.v
2640
stdout '^GOROOT '$TESTGO_GOROOT'$'
2741
stdout '^runtime '$TESTGO_GOROOT${/}src${/}runtime'$'
2842

43+
! exec ./example.test-trimpath.exe -test.v
44+
stdout '^GOROOT $'
45+
stderr 'cannot find package "runtime" in any of:\n\t\(\$GOROOT not set\)\n\t'$WORK${/}gopath${/}src${/}runtime' \(from \$GOPATH\)$'
46+
47+
# If a correct GOROOT is baked in to the 'go' command itself, 'go run' and
48+
# 'go test' should not implicitly set GOROOT in the process environment
49+
# (because that could mask an unexpected production dependency on the GOROOT
50+
# environment variable), but 'go generate' should (because the generator may
51+
# reasonably expect to be able to locate the GOROOT for which it is generating
52+
# code).
53+
54+
[trimpath] stop
55+
[mismatched-goroot] stop
56+
2957
! go run -trimpath .
3058
stdout '^GOROOT $'
3159
stderr 'cannot find package "runtime" in any of:\n\t\(\$GOROOT not set\)\n\t'$WORK${/}gopath${/}src${/}runtime' \(from \$GOPATH\)\nexit status 1\n\z'

0 commit comments

Comments
 (0)