]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go/internal/cfg: fix GOROOT setting when forcing host config
authorMichael Matloob <matloob@golang.org>
Fri, 30 May 2025 20:16:27 +0000 (16:16 -0400)
committerMichael Matloob <matloob@google.com>
Tue, 3 Jun 2025 14:30:34 +0000 (07:30 -0700)
We manage the state using a bunch of global config, so we need to make
sure we're doing things in the right order. In this case, the SetGOROOT
function was being called in init, setting the GOROOT on the global
Context, but when we reset the context in ForceHost we lost the goroot
configuration. We need to call SetGOROOT in ForceHost to re-set the
GOROOT on the new context.

This was uncovered by CL 677558 because a go command that was built with
trimpath would try to use its runtime.GOROOT(), which wouldn't be valid
in trimpath mode. Setting GOROOT properly with SetGOROOT will use the
value from findGOROOT, assuming GOROOT isn't set in the environment,
and findGOROOT will try to determine GOROOT using the path of the go
command executable.

For #71867

Change-Id: I731b6c5d859b4504fc128b29ab904e3a2886ff3c
Reviewed-on: https://go-review.googlesource.com/c/go/+/677636
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Michael Matloob <matloob@google.com>
Reviewed-by: Michael Pratt <mpratt@google.com>
src/cmd/go/internal/cfg/cfg.go

index d583447cf6e711f1baad1d3cf1730b8a663b4fc1..a4edd854f1d35a787b4768c00ed3688a42948d5e 100644 (file)
@@ -227,6 +227,8 @@ func ForceHost() {
        // Recompute the build context using Goos and Goarch to
        // set the correct value for ctx.CgoEnabled.
        BuildContext = defaultContext()
+       // Call SetGOROOT to properly set the GOROOT on the new context.
+       SetGOROOT(Getenv("GOROOT"), false)
        // Recompute experiments: the settings determined depend on GOOS and GOARCH.
        // This will also update the BuildContext's tool tags to include the new
        // experiment tags.