Skip to content

Commit

Permalink
cmd/dist: restore the original GOCACHE before building std and cmd
Browse files Browse the repository at this point in the history
The user is likely to run other commands that need these libraries
immediately after they are built.

For #57734.
Updates #56889.

Change-Id: I2a1a234e6031d85f017ee692ea1ace8c6e0e7355
Reviewed-on: https://go-review.googlesource.com/c/go/+/452679
Run-TryBot: Bryan Mills <bcmills@google.com>
Auto-Submit: Bryan Mills <bcmills@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Russ Cox <rsc@golang.org>
  • Loading branch information
Bryan C. Mills authored and gopherbot committed Jan 26, 2023
1 parent 8e0781e commit c611ef2
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 17 deletions.
20 changes: 14 additions & 6 deletions src/cmd/dist/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ var (
tooldir string
oldgoos string
oldgoarch string
oldgocache string
exe string
defaultcc map[string]string
defaultcxx map[string]string
Expand Down Expand Up @@ -234,11 +235,6 @@ func xinit() {
os.Setenv("GOROOT", goroot)
os.Setenv("GOROOT_FINAL", goroot_final)

// Use a build cache separate from the default user one.
// Also one that will be wiped out during startup, so that
// make.bash really does start from a clean slate.
os.Setenv("GOCACHE", pathf("%s/pkg/obj/go-build", goroot))

// Set GOBIN to GOROOT/bin. The meaning of GOBIN has drifted over time
// (see https://go.dev/issue/3269, https://go.dev/cl/183058,
// https://go.dev/issue/31576). Since we want binaries installed by 'dist' to
Expand Down Expand Up @@ -1211,7 +1207,6 @@ func cmdenv() {
xprintf(format, "GO111MODULE", "")
xprintf(format, "GOARCH", goarch)
xprintf(format, "GOBIN", gorootBin)
xprintf(format, "GOCACHE", os.Getenv("GOCACHE"))
xprintf(format, "GODEBUG", os.Getenv("GODEBUG"))
xprintf(format, "GOENV", "off")
xprintf(format, "GOFLAGS", "")
Expand Down Expand Up @@ -1340,6 +1335,12 @@ func cmdbootstrap() {
// go tool may complain.
os.Setenv("GOPATH", pathf("%s/pkg/obj/gopath", goroot))

// Use a build cache separate from the default user one.
// Also one that will be wiped out during startup, so that
// make.bash really does start from a clean slate.
oldgocache = os.Getenv("GOCACHE")
os.Setenv("GOCACHE", pathf("%s/pkg/obj/go-build", goroot))

// Disable GOEXPERIMENT when building toolchain1 and
// go_bootstrap. We don't need any experiments for the
// bootstrap toolchain, and this lets us avoid duplicating the
Expand Down Expand Up @@ -1467,6 +1468,13 @@ func cmdbootstrap() {
copyfile(pathf("%s/compile3", tooldir), pathf("%s/compile", tooldir), writeExec)
}

// Now that toolchain3 has been built from scratch, its compiler and linker
// should have accurate build IDs suitable for caching.
// Now prime the build cache with the rest of the standard library for
// testing, and so that the user can run 'go install std cmd' to quickly
// iterate on local changes without waiting for a full rebuild.
os.Setenv("GOCACHE", oldgocache)

if goos == oldgoos && goarch == oldgoarch {
// Common case - not setting up for cross-compilation.
timelog("build", "toolchain")
Expand Down
11 changes: 0 additions & 11 deletions src/cmd/dist/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -773,7 +773,6 @@ func (t *tester) registerTests() {
pkg: "fmt",
}).command(t)
unsetEnv(cmd, "GOROOT")
unsetEnv(cmd, "GOCACHE") // TODO(bcmills): ...why‽
err := cmd.Run()

if rerr := os.Rename(moved, goroot); rerr != nil {
Expand Down Expand Up @@ -1699,18 +1698,8 @@ func (t *tester) makeGOROOTUnwritable() (undo func()) {
}
}

gocache := os.Getenv("GOCACHE")
if gocache == "" {
panic("GOCACHE not set")
}
gocacheSubdir, _ := filepath.Rel(dir, gocache)

filepath.WalkDir(dir, func(path string, d fs.DirEntry, err error) error {
if suffix := strings.TrimPrefix(path, dir+string(filepath.Separator)); suffix != "" {
if suffix == gocacheSubdir {
// Leave GOCACHE writable: we may need to write test binaries into it.
return filepath.SkipDir
}
if suffix == ".git" {
// Leave Git metadata in whatever state it was in. It may contain a lot
// of files, and it is highly unlikely that a test will try to modify
Expand Down

0 comments on commit c611ef2

Please sign in to comment.