mirror of
https://github.com/golang/go
synced 2024-11-22 10:14:40 -07:00
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: 9986537cad
GitHub-Pull-Request: golang/go#68040
Reviewed-on: https://go-review.googlesource.com/c/go/+/593255
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Michael Knyszek <mknyszek@google.com>
Reviewed-by: Michael Matloob <matloob@golang.org>
This commit is contained in:
parent
18131ec8dc
commit
935bf1395c
@ -14,6 +14,7 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
"slices"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
@ -211,7 +212,9 @@ func RunStdin(cmdline []string) {
|
|||||||
cmd.Stdin = os.Stdin
|
cmd.Stdin = os.Stdin
|
||||||
cmd.Stdout = os.Stdout
|
cmd.Stdout = os.Stdout
|
||||||
cmd.Stderr = os.Stderr
|
cmd.Stderr = os.Stderr
|
||||||
cmd.Env = cfg.OrigEnv
|
env := slices.Clip(cfg.OrigEnv)
|
||||||
|
env = AppendPATH(env)
|
||||||
|
cmd.Env = env
|
||||||
StartSigHandlers()
|
StartSigHandlers()
|
||||||
if err := cmd.Run(); err != nil {
|
if err := cmd.Run(); err != nil {
|
||||||
Errorf("%v", err)
|
Errorf("%v", err)
|
||||||
|
42
src/cmd/go/testdata/script/run_goroot_PATH.txt
vendored
Normal file
42
src/cmd/go/testdata/script/run_goroot_PATH.txt
vendored
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
# https://go.dev/issue/68005: 'go run' should run the program with its own GOROOT/bin
|
||||||
|
# at the beginning of $PATH.
|
||||||
|
|
||||||
|
[short] skip
|
||||||
|
|
||||||
|
[!GOOS:plan9] env PATH=
|
||||||
|
[GOOS:plan9] env path=
|
||||||
|
go run .
|
||||||
|
|
||||||
|
[!GOOS:plan9] env PATH=$WORK${/}bin
|
||||||
|
[GOOS:plan9] env path=$WORK${/}bin
|
||||||
|
go run .
|
||||||
|
|
||||||
|
-- go.mod --
|
||||||
|
module example
|
||||||
|
|
||||||
|
go 1.19
|
||||||
|
-- main.go --
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"os/exec"
|
||||||
|
"path/filepath"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
got, err := exec.LookPath("go")
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
want := filepath.Join(os.Getenv("GOROOT"), "bin", "go" + os.Getenv("GOEXE"))
|
||||||
|
if got != want {
|
||||||
|
fmt.Printf(`exec.LookPath("go") = %q; want %q\n`, got, want)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
-- $WORK/bin/README.txt --
|
||||||
|
This directory contains no executables.
|
Loading…
Reference in New Issue
Block a user