From 42a46206b97ca7cf4246d3c1230a70cf42ad5bb0 Mon Sep 17 00:00:00 2001 From: "Bryan C. Mills" Date: Fri, 9 Sep 2022 17:29:12 -0400 Subject: [PATCH] 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 Reviewed-by: Michael Matloob Run-TryBot: Bryan Mills TryBot-Result: Gopher Robot --- misc/reboot/reboot_test.go | 24 ++++++++++++++++++++---- src/cmd/go/internal/load/pkg.go | 6 ++++++ 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/misc/reboot/reboot_test.go b/misc/reboot/reboot_test.go index a134affbc2..a1b513b432 100644 --- a/misc/reboot/reboot_test.go +++ b/misc/reboot/reboot_test.go @@ -20,11 +20,27 @@ func TestRepeatBootstrap(t *testing.T) { 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 @@ func TestRepeatBootstrap(t *testing.T) { 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 b1e4549d71..a6e380b89f 100644 --- a/src/cmd/go/internal/load/pkg.go +++ b/src/cmd/go/internal/load/pkg.go @@ -2441,6 +2441,12 @@ func (p *Package) setBuildInfo(autoVCS bool) { } 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)