From 792548a4835f019b40bf8bd4442c893eae280467 Mon Sep 17 00:00:00 2001 From: Michael Matloob Date: Fri, 30 May 2025 16:16:27 -0400 Subject: [PATCH] cmd/go/internal/cfg: fix GOROOT setting when forcing host config 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 Reviewed-by: Michael Matloob Reviewed-by: Michael Pratt --- src/cmd/go/internal/cfg/cfg.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/cmd/go/internal/cfg/cfg.go b/src/cmd/go/internal/cfg/cfg.go index d583447cf6..a4edd854f1 100644 --- a/src/cmd/go/internal/cfg/cfg.go +++ b/src/cmd/go/internal/cfg/cfg.go @@ -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. -- 2.50.0