Skip to content

Commit 7914369

Browse files
cmd/go: update BuildContext.GOROOT and build.Tooldir with computed GOROOT
This is necessary to make a relocated GOROOT work correctly. Fixes #20997 Change-Id: I18624bd2e109721066cd9e4a887a12583ab79f5d Reviewed-on: https://go-review.googlesource.com/48550 Run-TryBot: Ian Lance Taylor <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Brad Fitzpatrick <[email protected]>
1 parent 4dbcacd commit 7914369

File tree

3 files changed

+51
-13
lines changed

3 files changed

+51
-13
lines changed

src/cmd/dist/test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,38 @@ func (t *tester) registerTests() {
434434
})
435435
}
436436

437+
// On the builders only, test that a moved GOROOT still works.
438+
if os.Getenv("GO_BUILDER_NAME") != "" {
439+
t.tests = append(t.tests, distTest{
440+
name: "moved_goroot",
441+
heading: "moved GOROOT",
442+
fn: func(dt *distTest) error {
443+
t.runPending(dt)
444+
moved := t.goroot + "-moved"
445+
if err := os.Rename(t.goroot, moved); err != nil {
446+
return err
447+
}
448+
449+
// Run `go test fmt` in the moved GOROOT.
450+
cmd := exec.Command(filepath.Join(moved, "bin", "go"), "test", "fmt")
451+
cmd.Stdout = os.Stdout
452+
cmd.Stderr = os.Stderr
453+
// Don't set GOROOT in the environment.
454+
for _, e := range os.Environ() {
455+
if !strings.HasPrefix(e, "GOROOT=") {
456+
cmd.Env = append(cmd.Env, e)
457+
}
458+
}
459+
err := cmd.Run()
460+
461+
if rerr := os.Rename(moved, t.goroot); rerr != nil {
462+
log.Fatalf("failed to restore GOROOT: %v", rerr)
463+
}
464+
return err
465+
},
466+
})
467+
}
468+
437469
// Test that internal linking of standard packages does not
438470
// require libgcc. This ensures that we can install a Go
439471
// release on a system that does not have a C compiler

src/cmd/go/internal/cfg/cfg.go

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,18 @@ var CmdEnv []EnvVar
6060

6161
// Global build parameters (used during package load)
6262
var (
63-
Goarch string
64-
Goos string
63+
Goarch = BuildContext.GOARCH
64+
Goos = BuildContext.GOOS
6565
ExeSuffix string
66-
Gopath []string
66+
Gopath = filepath.SplitList(BuildContext.GOPATH)
6767
)
6868

69+
func init() {
70+
if Goos == "windows" {
71+
ExeSuffix = ".exe"
72+
}
73+
}
74+
6975
var (
7076
GOROOT = findGOROOT()
7177
GOBIN = os.Getenv("GOBIN")
@@ -78,6 +84,16 @@ var (
7884
GO386 = objabi.GO386
7985
)
8086

87+
// Update build context to use our computed GOROOT.
88+
func init() {
89+
BuildContext.GOROOT = GOROOT
90+
// Note that we must use runtime.GOOS and runtime.GOARCH here,
91+
// as the tool directory does not move based on environment variables.
92+
// This matches the initialization of ToolDir in go/build,
93+
// except for using GOROOT rather than runtime.GOROOT().
94+
build.ToolDir = filepath.Join(GOROOT, "pkg/tool/"+runtime.GOOS+"_"+runtime.GOARCH)
95+
}
96+
8197
func findGOROOT() string {
8298
if env := os.Getenv("GOROOT"); env != "" {
8399
return filepath.Clean(env)

src/cmd/go/internal/work/build.go

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -643,16 +643,6 @@ func InstallPackages(args []string, forGet bool) {
643643
}
644644
}
645645

646-
func init() {
647-
cfg.Goarch = cfg.BuildContext.GOARCH
648-
cfg.Goos = cfg.BuildContext.GOOS
649-
650-
if cfg.Goos == "windows" {
651-
cfg.ExeSuffix = ".exe"
652-
}
653-
cfg.Gopath = filepath.SplitList(cfg.BuildContext.GOPATH)
654-
}
655-
656646
// A Builder holds global state about a build.
657647
// It does not hold per-package state, because we
658648
// build packages in parallel, and the builder is shared.

0 commit comments

Comments
 (0)