Skip to content

Commit cacac8b

Browse files
author
Bryan C. Mills
committed
cmd/dist: do not unset GOROOT_FINAL prior to running tests
Also do not unset it by default in the tests for cmd/go. GOROOT_FINAL affects the GOROOT value embedded in binaries, such as 'cmd/cgo'. If its value changes and a build command is performed that depends on one of those binaries, the binary would be spuriously rebuilt. Instead, only unset it in the specific tests that make assumptions about the GOROOT paths embedded in specific compiled binaries. That may cause those tests to do a little extra rebuilding when GOROOT_FINAL is set, but that little bit of extra rebuilding seems preferable to spuriously-stale binaries. Fixes #39385 Change-Id: I7c87b1519bb5bcff64babf1505fd1033ffa4f4fb Reviewed-on: https://go-review.googlesource.com/c/go/+/236819 Run-TryBot: Bryan C. Mills <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Dmitri Shuralyov <[email protected]>
1 parent e64675a commit cacac8b

File tree

9 files changed

+40
-18
lines changed

9 files changed

+40
-18
lines changed

src/cmd/addr2line/addr2line_test.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,18 +74,26 @@ func testAddr2Line(t *testing.T, exepath, addr string) {
7474
t.Fatalf("Stat failed: %v", err)
7575
}
7676
fi2, err := os.Stat(srcPath)
77+
if gorootFinal := os.Getenv("GOROOT_FINAL"); gorootFinal != "" && strings.HasPrefix(srcPath, gorootFinal) {
78+
if os.IsNotExist(err) || (err == nil && !os.SameFile(fi1, fi2)) {
79+
// srcPath has had GOROOT_FINAL substituted for GOROOT, and it doesn't
80+
// match the actual file. GOROOT probably hasn't been moved to its final
81+
// location yet, so try the original location instead.
82+
fi2, err = os.Stat(runtime.GOROOT() + strings.TrimPrefix(srcPath, gorootFinal))
83+
}
84+
}
7785
if err != nil {
7886
t.Fatalf("Stat failed: %v", err)
7987
}
8088
if !os.SameFile(fi1, fi2) {
8189
t.Fatalf("addr2line_test.go and %s are not same file", srcPath)
8290
}
83-
if srcLineNo != "89" {
84-
t.Fatalf("line number = %v; want 89", srcLineNo)
91+
if srcLineNo != "97" {
92+
t.Fatalf("line number = %v; want 97", srcLineNo)
8593
}
8694
}
8795

88-
// This is line 88. The test depends on that.
96+
// This is line 96. The test depends on that.
8997
func TestAddr2Line(t *testing.T) {
9098
testenv.MustHaveGoBuild(t)
9199

src/cmd/dist/test.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -178,15 +178,6 @@ func (t *tester) run() {
178178
return
179179
}
180180

181-
// We must unset GOROOT_FINAL before tests, because runtime/debug requires
182-
// correct access to source code, so if we have GOROOT_FINAL in effect,
183-
// at least runtime/debug test will fail.
184-
// If GOROOT_FINAL was set before, then now all the commands will appear stale.
185-
// Nothing we can do about that other than not checking them below.
186-
// (We call checkNotStale but only with "std" not "cmd".)
187-
os.Setenv("GOROOT_FINAL_OLD", os.Getenv("GOROOT_FINAL")) // for cmd/link test
188-
os.Unsetenv("GOROOT_FINAL")
189-
190181
for _, name := range t.runNames {
191182
if !t.isRegisteredTestName(name) {
192183
fatalf("unknown test %q", name)

src/cmd/go/go_test.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,6 @@ func TestMain(m *testing.M) {
124124
fmt.Printf("SKIP\n")
125125
return
126126
}
127-
os.Unsetenv("GOROOT_FINAL")
128127

129128
flag.Parse()
130129

@@ -180,6 +179,11 @@ func TestMain(m *testing.M) {
180179
}
181180
testGOROOT = goEnv("GOROOT")
182181
os.Setenv("TESTGO_GOROOT", testGOROOT)
182+
// Ensure that GOROOT is set explicitly.
183+
// Otherwise, if the toolchain was built with GOROOT_FINAL set but has not
184+
// yet been moved to its final location, programs that invoke runtime.GOROOT
185+
// may accidentally use the wrong path.
186+
os.Setenv("GOROOT", testGOROOT)
183187

184188
// The whole GOROOT/pkg tree was installed using the GOHOSTOS/GOHOSTARCH
185189
// toolchain (installed in GOROOT/pkg/tool/GOHOSTOS_GOHOSTARCH).
@@ -216,8 +220,10 @@ func TestMain(m *testing.M) {
216220
}
217221
testCC = strings.TrimSpace(string(out))
218222

219-
if out, err := exec.Command(testGo, "env", "CGO_ENABLED").Output(); err != nil {
220-
fmt.Fprintf(os.Stderr, "running testgo failed: %v\n", err)
223+
cmd := exec.Command(testGo, "env", "CGO_ENABLED")
224+
cmd.Stderr = new(strings.Builder)
225+
if out, err := cmd.Output(); err != nil {
226+
fmt.Fprintf(os.Stderr, "running testgo failed: %v\n%s", err, cmd.Stderr)
221227
canRun = false
222228
} else {
223229
canCgo, err = strconv.ParseBool(strings.TrimSpace(string(out)))

src/cmd/go/script_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ func (ts *testScript) setup() {
130130
"GOPROXY=" + proxyURL,
131131
"GOPRIVATE=",
132132
"GOROOT=" + testGOROOT,
133+
"GOROOT_FINAL=" + os.Getenv("GOROOT_FINAL"), // causes spurious rebuilds and breaks the "stale" built-in if not propagated
133134
"TESTGO_GOROOT=" + testGOROOT,
134135
"GOSUMDB=" + testSumDBVerifierKey,
135136
"GONOPROXY=",

src/cmd/go/testdata/script/README

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ Scripts also have access to these other environment variables:
3434
GOPATH=$WORK/gopath
3535
GOPROXY=<local module proxy serving from cmd/go/testdata/mod>
3636
GOROOT=<actual GOROOT>
37+
GOROOT_FINAL=<actual GOROOT_FINAL>
3738
TESTGO_GOROOT=<GOROOT used to build cmd/go, for use in tests that may change GOROOT>
3839
HOME=/no-home
3940
PATH=<actual PATH>

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
[short] skip
22

3+
# If GOROOT_FINAL is set, 'go build -trimpath' bakes that into the resulting
4+
# binary instead of GOROOT. Explicitly unset it here.
5+
env GOROOT_FINAL=
6+
37
# Set up two identical directories that can be used as GOPATH.
48
env GO111MODULE=on
59
mkdir $WORK/a/src/paths $WORK/b/src/paths

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22

33
mkdir $WORK/new/bin
44

5+
# In this test, we are specifically checking the logic for deriving
6+
# the value of GOROOT from runtime.GOROOT.
7+
# GOROOT_FINAL changes the default behavior of runtime.GOROOT,
8+
# and will thus cause the test to fail if it is set when our
9+
# new cmd/go is built.
10+
env GOROOT_FINAL=
11+
512
go build -o $WORK/new/bin/go$GOEXE cmd/go &
613
go build -o $WORK/bin/check$GOEXE check.go &
714
wait

src/cmd/link/dwarf_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ func testDWARF(t *testing.T, buildmode string, expectDWARF bool, env ...string)
3333
t.Fatalf("go list: %v\n%s", err, out)
3434
}
3535
if string(out) != "false\n" {
36-
if os.Getenv("GOROOT_FINAL_OLD") != "" {
37-
t.Skip("cmd/link is stale, but $GOROOT_FINAL_OLD is set")
36+
if strings.HasPrefix(testenv.Builder(), "darwin-") {
37+
t.Skipf("cmd/link is spuriously stale on Darwin builders - see #33598")
3838
}
3939
t.Fatalf("cmd/link is stale - run go install cmd/link")
4040
}

src/cmd/objdump/objdump_test.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,11 @@ func testDisasm(t *testing.T, printCode bool, printGnuAsm bool, flags ...string)
138138
args = append(args, flags...)
139139
args = append(args, "fmthello.go")
140140
cmd := exec.Command(testenv.GoToolPath(t), args...)
141-
cmd.Dir = "testdata" // "Bad line" bug #36683 is sensitive to being run in the source directory
141+
// "Bad line" bug #36683 is sensitive to being run in the source directory.
142+
cmd.Dir = "testdata"
143+
// Ensure that the source file location embedded in the binary matches our
144+
// actual current GOROOT, instead of GOROOT_FINAL if set.
145+
cmd.Env = append(os.Environ(), "GOROOT_FINAL=")
142146
t.Logf("Running %v", cmd.Args)
143147
out, err := cmd.CombinedOutput()
144148
if err != nil {

0 commit comments

Comments
 (0)