Skip to content

Commit 9bc5268

Browse files
author
Bryan C. Mills
committed
cmd/go,cmd/link: do not check for staleness in most tests
Instead, check that stale packages in the standard library are not rebuilt when already present in the build cache, and are not installed implicitly when rebuilt. We retain the staleness checks for the runtime package in tests involving '-i', because those are guaranteed to fail anyway if the package is stale and the "stale" failure message is arguably clearer. They can be removed if/when we remove the '-i' flag, but the runtime package is less likely to become stale because it does not have cgo dependencies. Fixes #46347 Updates #33598 Updates #35459 Updates #41696 Change-Id: I7b0a808addd930f9f4911ff53ded62272af75a40 Reviewed-on: https://go-review.googlesource.com/c/go/+/322629 Trust: Bryan C. Mills <[email protected]> Run-TryBot: Bryan C. Mills <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Cherry Mui <[email protected]> Reviewed-by: Jay Conrod <[email protected]>
1 parent 6ff0ae2 commit 9bc5268

7 files changed

+112
-63
lines changed

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

Lines changed: 0 additions & 13 deletions
This file was deleted.
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# golang.org/issue/46347: a stale runtime/cgo should only force a single rebuild
2+
3+
[!cgo] skip
4+
[short] skip
5+
6+
7+
# If we set a unique CGO_CFLAGS, the installed copy of runtime/cgo
8+
# should be reported as stale.
9+
10+
env CGO_CFLAGS=-DTestScript_cgo_stale=true
11+
stale runtime/cgo
12+
13+
14+
# If we then build a package that uses cgo, runtime/cgo should be rebuilt and
15+
# cached with the new flag, but not installed to GOROOT (and thus still stale).
16+
17+
env GOCACHE=$WORK/cache # Use a fresh cache to avoid interference between runs.
18+
19+
go build -x .
20+
stderr '[/\\]cgo'$GOEXE'["]? .* -importpath runtime/cgo'
21+
stale runtime/cgo
22+
23+
24+
# After runtime/cgo has been rebuilt and cached, it should not be rebuilt again
25+
# even though it is still reported as stale.
26+
27+
go build -x .
28+
! stderr '[/\\]cgo'$GOEXE'["]? .* -importpath runtime/cgo'
29+
stale runtime/cgo
30+
31+
32+
-- go.mod --
33+
module example.com/m
34+
35+
go 1.17
36+
-- m.go --
37+
package m
38+
39+
import "C"

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

Lines changed: 0 additions & 31 deletions
This file was deleted.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# https://golang.org/issue/44725: packages in std should have the same
2+
# dependencies regardless of whether they are listed from within or outside
3+
# GOROOT/src.
4+
5+
# Control case: net, viewed from outside the 'std' module,
6+
# should depend on vendor/golang.org/… instead of golang.org/….
7+
8+
go list -deps net
9+
stdout '^vendor/golang.org/x/net'
10+
! stdout '^golang.org/x/net'
11+
cp stdout $WORK/net-deps.txt
12+
13+
14+
# It should still report the same package dependencies when viewed from
15+
# within GOROOT/src.
16+
17+
cd $GOROOT/src
18+
19+
go list -deps net
20+
stdout '^vendor/golang.org/x/net'
21+
! stdout '^golang.org/x/net'
22+
cmp stdout $WORK/net-deps.txt
23+
24+
25+
# However, 'go mod' and 'go get' subcommands should report the original module
26+
# dependencies, not the vendored packages.
27+
28+
[!net] stop
29+
30+
env GOPROXY=
31+
go mod why -m golang.org/x/net
32+
stdout '^# golang.org/x/net\nnet\ngolang.org/x/net'

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

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

33
[!race] skip
44

5-
[!darwin] ! stale cmd/cgo # The darwin builders are spuriously stale; see #33598.
6-
75
env GOBIN=$WORK/bin
86
go install m/mtime m/sametime
97

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33
# Build our simple toolexec program.
44
go build ./cmd/mytool
55

6+
# Use an ephemeral build cache so that our toolexec output is not cached
7+
# for any stale standard-library dependencies.
8+
#
9+
# TODO(#27628): This should not be necessary.
10+
env GOCACHE=$WORK/gocache
11+
612
# Build the main package with our toolexec program. For each action, it will
713
# print the tool's name and the TOOLEXEC_IMPORTPATH value. We expect to compile
814
# each package once, and link the main package once.

src/cmd/link/dwarf_test.go

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,36 @@ import (
1919
"testing"
2020
)
2121

22+
// TestMain allows this test binary to run as a -toolexec wrapper for the 'go'
23+
// command. If LINK_TEST_TOOLEXEC is set, TestMain runs the binary as if it were
24+
// cmd/link, and otherwise runs the requested tool as a subprocess.
25+
//
26+
// This allows the test to verify the behavior of the current contents of the
27+
// cmd/link package even if the installed cmd/link binary is stale.
28+
func TestMain(m *testing.M) {
29+
if os.Getenv("LINK_TEST_TOOLEXEC") == "" {
30+
// Not running as a -toolexec wrapper. Just run the tests.
31+
os.Exit(m.Run())
32+
}
33+
34+
if strings.TrimSuffix(filepath.Base(os.Args[1]), ".exe") == "link" {
35+
// Running as a -toolexec linker, and the tool is cmd/link.
36+
// Substitute this test binary for the linker.
37+
os.Args = os.Args[1:]
38+
main()
39+
os.Exit(0)
40+
}
41+
42+
cmd := exec.Command(os.Args[1], os.Args[2:]...)
43+
cmd.Stdin = os.Stdin
44+
cmd.Stdout = os.Stdout
45+
cmd.Stderr = os.Stderr
46+
if err := cmd.Run(); err != nil {
47+
os.Exit(1)
48+
}
49+
os.Exit(0)
50+
}
51+
2252
func testDWARF(t *testing.T, buildmode string, expectDWARF bool, env ...string) {
2353
testenv.MustHaveCGO(t)
2454
testenv.MustHaveGoBuild(t)
@@ -29,17 +59,6 @@ func testDWARF(t *testing.T, buildmode string, expectDWARF bool, env ...string)
2959

3060
t.Parallel()
3161

32-
out, err := exec.Command(testenv.GoToolPath(t), "list", "-f", "{{.Stale}}", "cmd/link").CombinedOutput()
33-
if err != nil {
34-
t.Fatalf("go list: %v\n%s", err, out)
35-
}
36-
if string(out) != "false\n" {
37-
if strings.HasPrefix(testenv.Builder(), "darwin-") {
38-
t.Skipf("cmd/link is spuriously stale on Darwin builders - see #33598")
39-
}
40-
t.Fatalf("cmd/link is stale - run go install cmd/link")
41-
}
42-
4362
for _, prog := range []string{"testprog", "testprogcgo"} {
4463
prog := prog
4564
expectDWARF := expectDWARF
@@ -48,11 +67,11 @@ func testDWARF(t *testing.T, buildmode string, expectDWARF bool, env ...string)
4867
if extld == "" {
4968
extld = "gcc"
5069
}
70+
var err error
5171
expectDWARF, err = cmddwarf.IsDWARFEnabledOnAIXLd(extld)
5272
if err != nil {
5373
t.Fatal(err)
5474
}
55-
5675
}
5776

5877
t.Run(prog, func(t *testing.T) {
@@ -62,15 +81,14 @@ func testDWARF(t *testing.T, buildmode string, expectDWARF bool, env ...string)
6281

6382
exe := filepath.Join(tmpDir, prog+".exe")
6483
dir := "../../runtime/testdata/" + prog
65-
cmd := exec.Command(testenv.GoToolPath(t), "build", "-o", exe)
84+
cmd := exec.Command(testenv.GoToolPath(t), "build", "-toolexec", os.Args[0], "-o", exe)
6685
if buildmode != "" {
6786
cmd.Args = append(cmd.Args, "-buildmode", buildmode)
6887
}
6988
cmd.Args = append(cmd.Args, dir)
70-
if env != nil {
71-
cmd.Env = append(os.Environ(), env...)
72-
cmd.Env = append(cmd.Env, "CGO_CFLAGS=") // ensure CGO_CFLAGS does not contain any flags. Issue #35459
73-
}
89+
cmd.Env = append(os.Environ(), env...)
90+
cmd.Env = append(cmd.Env, "CGO_CFLAGS=") // ensure CGO_CFLAGS does not contain any flags. Issue #35459
91+
cmd.Env = append(cmd.Env, "LINK_TEST_TOOLEXEC=1")
7492
out, err := cmd.CombinedOutput()
7593
if err != nil {
7694
t.Fatalf("go build -o %v %v: %v\n%s", exe, dir, err, out)

0 commit comments

Comments
 (0)