Skip to content

Commit 935bf13

Browse files
mauri870matloob
authored andcommitted
cmd/go: place GOROOT/bin at the beginning of PATH in 'go run'
This causes programs that use 'go' as a subprocess to use the same go command as the parent 'go run' command. Fixes #68005 Change-Id: I937cef474bf038a925bb74fc73e5f377b03e27b7 GitHub-Last-Rev: 9986537 GitHub-Pull-Request: #68040 Reviewed-on: https://go-review.googlesource.com/c/go/+/593255 LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Michael Knyszek <[email protected]> Reviewed-by: Michael Matloob <[email protected]>
1 parent 18131ec commit 935bf13

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

src/cmd/go/internal/base/base.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"os"
1515
"os/exec"
1616
"reflect"
17+
"slices"
1718
"strings"
1819
"sync"
1920

@@ -211,7 +212,9 @@ func RunStdin(cmdline []string) {
211212
cmd.Stdin = os.Stdin
212213
cmd.Stdout = os.Stdout
213214
cmd.Stderr = os.Stderr
214-
cmd.Env = cfg.OrigEnv
215+
env := slices.Clip(cfg.OrigEnv)
216+
env = AppendPATH(env)
217+
cmd.Env = env
215218
StartSigHandlers()
216219
if err := cmd.Run(); err != nil {
217220
Errorf("%v", err)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# https://go.dev/issue/68005: 'go run' should run the program with its own GOROOT/bin
2+
# at the beginning of $PATH.
3+
4+
[short] skip
5+
6+
[!GOOS:plan9] env PATH=
7+
[GOOS:plan9] env path=
8+
go run .
9+
10+
[!GOOS:plan9] env PATH=$WORK${/}bin
11+
[GOOS:plan9] env path=$WORK${/}bin
12+
go run .
13+
14+
-- go.mod --
15+
module example
16+
17+
go 1.19
18+
-- main.go --
19+
package main
20+
21+
import (
22+
"fmt"
23+
"os"
24+
"os/exec"
25+
"path/filepath"
26+
)
27+
28+
func main() {
29+
got, err := exec.LookPath("go")
30+
if err != nil {
31+
fmt.Println(err)
32+
os.Exit(1)
33+
}
34+
35+
want := filepath.Join(os.Getenv("GOROOT"), "bin", "go" + os.Getenv("GOEXE"))
36+
if got != want {
37+
fmt.Printf(`exec.LookPath("go") = %q; want %q\n`, got, want)
38+
os.Exit(1)
39+
}
40+
}
41+
-- $WORK/bin/README.txt --
42+
This directory contains no executables.

0 commit comments

Comments
 (0)