Skip to content

Commit 1a193b4

Browse files
committed
cmd/go: use AppendPath(OrigEnv) as env for tool executable
Before this change, when go tool wass used to start a tool defined in a go.mod tool directive, it used the environment the go command was running in. The issue with doing that is that the go command sets various environment variables from the computed environment when invoking a subcommand. That is used to standardise the environment for the various tools invoked by the go command, but it is not the expectatation of tools invoked by the go command, especially since those environment variables may change the behavior of the tool run. Instead use the same environment we use in go run to start the executable: the original environment (with minor modifications) saved before we start explicitly setting the envornment, with GOROOT/bin added to the path so that sub commands that run the go tool use the proper go tool binary. Fixes #70544 Change-Id: Ifbf0040a2543113638eec7232323eb9de1d61529 Reviewed-on: https://go-review.googlesource.com/c/go/+/631836 Reviewed-by: Conrad Irwin <[email protected]> Reviewed-by: Sean Liao <[email protected]> Reviewed-by: Sam Thanawalla <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent 18bbcff commit 1a193b4

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

src/cmd/go/internal/tool/tool.go

+6
Original file line numberDiff line numberDiff line change
@@ -309,12 +309,18 @@ func runBuiltTool(b *work.Builder, ctx context.Context, a *work.Action) error {
309309
return nil
310310
}
311311

312+
// Use same environment go run uses to start the executable:
313+
// the original environment with cfg.GOROOTbin added to the path.
314+
env := slices.Clip(cfg.OrigEnv)
315+
env = base.AppendPATH(env)
316+
312317
toolCmd := &exec.Cmd{
313318
Path: cmdline[0],
314319
Args: cmdline,
315320
Stdin: os.Stdin,
316321
Stdout: os.Stdout,
317322
Stderr: os.Stderr,
323+
Env: env,
318324
}
319325
err := toolCmd.Start()
320326
if err == nil {

0 commit comments

Comments
 (0)