cmd/go: omit VCS stamping during bootstrap

cmd/dist can't easily hard-code -buildvcs=false because not all
versions of cmd/go supported for bootstrapping recognize that flag.

However, we don't want to stamp the bootstrap binaries: the stamping
is redundant with the VERSION file writted during bootstrapping (which
is why it is normally omitted for standard-library packages and
commands), and it may also interfere with building the Go repo from a
source tarball or zip file.

Fixes #54852.

Change-Id: If223f094af137c4c202d6bf622619bd2da397ec4
Reviewed-on: https://go-review.googlesource.com/c/go/+/432435
Auto-Submit: Bryan Mills <bcmills@google.com>
Reviewed-by: Michael Matloob <matloob@golang.org>
Run-TryBot: Bryan Mills <bcmills@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
diff --git a/misc/reboot/reboot_test.go b/misc/reboot/reboot_test.go
index a134affb..a1b513b 100644
--- a/misc/reboot/reboot_test.go
+++ b/misc/reboot/reboot_test.go
@@ -20,11 +20,27 @@
 		t.Skipf("skipping test that rebuilds the entire toolchain")
 	}
 
-	goroot := t.TempDir()
+	realGoroot, err := filepath.Abs(filepath.Join("..", ".."))
+	if err != nil {
+		t.Fatal(err)
+	}
+
+	// To ensure that bootstrapping doesn't unexpectedly depend
+	// on the Go repo's git metadata, add a fake (unreadable) git
+	// directory above the simulated GOROOT.
+	// This mimics the configuration one much have when
+	// building from distro-packaged source code
+	// (see https://go.dev/issue/54852).
+	parent := t.TempDir()
+	dotGit := filepath.Join(parent, ".git")
+	if err := os.Mkdir(dotGit, 000); err != nil {
+		t.Fatal(err)
+	}
+	goroot := filepath.Join(parent, "goroot")
 
 	gorootSrc := filepath.Join(goroot, "src")
 	overlayStart := time.Now()
-	if err := overlayDir(gorootSrc, filepath.Join(runtime.GOROOT(), "src")); err != nil {
+	if err := overlayDir(gorootSrc, filepath.Join(realGoroot, "src")); err != nil {
 		t.Fatal(err)
 	}
 	t.Logf("GOROOT/src overlay set up in %s", time.Since(overlayStart))
@@ -43,9 +59,9 @@
 		makeScript = "make.bash"
 	}
 
-	cmd := exec.Command(filepath.Join(runtime.GOROOT(), "src", makeScript))
+	cmd := exec.Command(filepath.Join(goroot, "src", makeScript))
 	cmd.Dir = gorootSrc
-	cmd.Env = append(os.Environ(), "GOROOT=", "GOROOT_BOOTSTRAP="+runtime.GOROOT())
+	cmd.Env = append(cmd.Environ(), "GOROOT=", "GOROOT_BOOTSTRAP="+realGoroot)
 	cmd.Stderr = os.Stderr
 	cmd.Stdout = os.Stdout
 	if err := cmd.Run(); err != nil {
diff --git a/src/cmd/go/internal/load/pkg.go b/src/cmd/go/internal/load/pkg.go
index b1e4549..a6e380b 100644
--- a/src/cmd/go/internal/load/pkg.go
+++ b/src/cmd/go/internal/load/pkg.go
@@ -2441,6 +2441,12 @@
 	}
 
 	if wantVCS && p.Module != nil && p.Module.Version == "" && !p.Standard {
+		if p.Module.Path == "bootstrap" && cfg.GOROOT == os.Getenv("GOROOT_BOOTSTRAP") {
+			// During bootstrapping, the bootstrap toolchain is built in module
+			// "bootstrap" (instead of "std"), with GOROOT set to GOROOT_BOOTSTRAP
+			// (so the bootstrap toolchain packages don't even appear to be in GOROOT).
+			goto omitVCS
+		}
 		repoDir, vcsCmd, err = vcs.FromDir(base.Cwd(), "", allowNesting)
 		if err != nil && !errors.Is(err, os.ErrNotExist) {
 			setVCSError(err)