From 3046ae927d7664a63bb8c3a2fb3b9ca95bcf93de Mon Sep 17 00:00:00 2001 From: "Bryan C. Mills" Date: Wed, 16 Mar 2022 14:45:42 -0400 Subject: [PATCH] cmd/doc: use the 'go' command from buildCtx.GOROOT, not whatever is in $PATH For #51483. Change-Id: I6150fdf97763d858e9ab012e807515da3387c25f Reviewed-on: https://go-review.googlesource.com/c/go/+/393366 Trust: Bryan Mills Run-TryBot: Bryan Mills Reviewed-by: Ian Lance Taylor TryBot-Result: Gopher Robot --- src/cmd/doc/dirs.go | 16 ++++++++--- src/cmd/go/testdata/script/mod_doc_path.txt | 30 +++++++++++++++++++++ 2 files changed, 42 insertions(+), 4 deletions(-) create mode 100644 src/cmd/go/testdata/script/mod_doc_path.txt diff --git a/src/cmd/doc/dirs.go b/src/cmd/doc/dirs.go index cb4d45ac6c..489f490889 100644 --- a/src/cmd/doc/dirs.go +++ b/src/cmd/doc/dirs.go @@ -58,6 +58,14 @@ func dirsInit(extra ...Dir) { go dirs.walk(codeRoots()) } +// goCmd returns the "go" command path corresponding to buildCtx.GOROOT. +func goCmd() string { + if buildCtx.GOROOT == "" { + return "go" + } + return filepath.Join(buildCtx.GOROOT, "bin", "go") +} + // Reset puts the scan back at the beginning. func (d *Dirs) Reset() { d.offset = 0 @@ -181,7 +189,7 @@ func findCodeRoots() []Dir { if !testGOPATH { // Check for use of modules by 'go env GOMOD', // which reports a go.mod file path if modules are enabled. - stdout, _ := exec.Command("go", "env", "GOMOD").Output() + stdout, _ := exec.Command(goCmd(), "env", "GOMOD").Output() gomod := string(bytes.TrimSpace(stdout)) usingModules = len(gomod) > 0 @@ -230,7 +238,7 @@ func findCodeRoots() []Dir { return list } - cmd := exec.Command("go", "list", "-m", "-f={{.Path}}\t{{.Dir}}", "all") + cmd := exec.Command(goCmd(), "list", "-m", "-f={{.Path}}\t{{.Dir}}", "all") cmd.Stderr = os.Stderr out, _ := cmd.Output() for _, line := range strings.Split(string(out), "\n") { @@ -259,7 +267,7 @@ func vendorEnabled() (*moduleJSON, bool, error) { return nil, false, err } - stdout, _ := exec.Command("go", "env", "GOFLAGS").Output() + stdout, _ := exec.Command(goCmd(), "env", "GOFLAGS").Output() goflags := string(bytes.TrimSpace(stdout)) matches := modFlagRegexp.FindStringSubmatch(goflags) var modFlag string @@ -293,7 +301,7 @@ func getMainModuleAnd114() (*moduleJSON, bool, error) { {{.GoVersion}} {{range context.ReleaseTags}}{{if eq . "go1.14"}}{{.}}{{end}}{{end}} ` - cmd := exec.Command("go", "list", "-m", "-f", format) + cmd := exec.Command(goCmd(), "list", "-m", "-f", format) cmd.Stderr = os.Stderr stdout, err := cmd.Output() if err != nil { diff --git a/src/cmd/go/testdata/script/mod_doc_path.txt b/src/cmd/go/testdata/script/mod_doc_path.txt new file mode 100644 index 0000000000..57470a95c4 --- /dev/null +++ b/src/cmd/go/testdata/script/mod_doc_path.txt @@ -0,0 +1,30 @@ +# cmd/doc should use GOROOT to locate the 'go' command, +# not use whatever is in $PATH. + +# Remove 'go' from $PATH. (It can still be located via $GOROOT/bin/go, and the +# test script's built-in 'go' command still knows where to find it.) +env PATH='' +[plan9] env path='' + +go doc p.X + +-- go.mod -- +module example + +go 1.19 + +require example.com/p v0.1.0 + +replace example.com/p => ./pfork +-- example.go -- +package example + +import _ "example.com/p" +-- pfork/go.mod -- +module example.com/p + +go 1.19 +-- pfork/p.go -- +package p + +const X = 42 -- 2.50.0