cmd/dist: run a checkNotStale on the builders before running tests

This should be a no-op, but if the sharded builders are for some
reason distributing stale snapshots — or testing them with mismatched
environments — this should catch them out at a relatively low cost
(#24300 notwithstanding).

Given the frequently at which (*tester).runPending already checks for
staleness, we do not expect the impact of this extra check to be
significant for most builders.

For #33598
Updates #24300

Change-Id: I197d6a69c72e2eec9e4563b459206de76c89e8a1
Reviewed-on: https://go-review.googlesource.com/c/go/+/223755
Trust: Bryan C. Mills <bcmills@google.com>
Run-TryBot: Bryan C. Mills <bcmills@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Dmitri Shuralyov <dmitshur@golang.org>
diff --git a/src/cmd/dist/test.go b/src/cmd/dist/test.go
index de9135c..5935011 100644
--- a/src/cmd/dist/test.go
+++ b/src/cmd/dist/test.go
@@ -139,27 +139,35 @@
 		goInstall("go", append([]string{"-a", "-i"}, toolchain...)...)
 	}
 
-	// Complete rebuild bootstrap, even with -no-rebuild.
-	// If everything is up-to-date, this is a no-op.
-	// If everything is not up-to-date, the first checkNotStale
-	// during the test process will kill the tests, so we might
-	// as well install the world.
-	// Now that for example "go install cmd/compile" does not
-	// also install runtime (you need "go install -i cmd/compile"
-	// for that), it's easy for previous workflows like
-	// "rebuild the compiler and then run run.bash"
-	// to break if we don't automatically refresh things here.
-	// Rebuilding is a shortened bootstrap.
-	// See cmdbootstrap for a description of the overall process.
-	//
-	// But don't do this if we're running in the Go build system,
-	// where cmd/dist is invoked many times. This just slows that
-	// down (Issue 24300).
-	if !t.listMode && os.Getenv("GO_BUILDER_NAME") == "" {
-		goInstall("go", append([]string{"-i"}, toolchain...)...)
-		goInstall("go", append([]string{"-i"}, toolchain...)...)
-		goInstall("go", "std", "cmd")
-		checkNotStale("go", "std", "cmd")
+	if !t.listMode {
+		if os.Getenv("GO_BUILDER_NAME") == "" {
+			// Complete rebuild bootstrap, even with -no-rebuild.
+			// If everything is up-to-date, this is a no-op.
+			// If everything is not up-to-date, the first checkNotStale
+			// during the test process will kill the tests, so we might
+			// as well install the world.
+			// Now that for example "go install cmd/compile" does not
+			// also install runtime (you need "go install -i cmd/compile"
+			// for that), it's easy for previous workflows like
+			// "rebuild the compiler and then run run.bash"
+			// to break if we don't automatically refresh things here.
+			// Rebuilding is a shortened bootstrap.
+			// See cmdbootstrap for a description of the overall process.
+			goInstall("go", append([]string{"-i"}, toolchain...)...)
+			goInstall("go", append([]string{"-i"}, toolchain...)...)
+			goInstall("go", "std", "cmd")
+		} else {
+			// The Go builder infrastructure should always begin running tests from a
+			// clean, non-stale state, so there is no need to rebuild the world.
+			// Instead, we can just check that it is not stale, which may be less
+			// expensive (and is also more likely to catch bugs in the builder
+			// implementation).
+			willTest := []string{"std"}
+			if t.shouldTestCmd() {
+				willTest = append(willTest, "cmd")
+			}
+			checkNotStale("go", willTest...)
+		}
 	}
 
 	t.timeoutScale = 1