1
0
mirror of https://github.com/golang/go synced 2024-11-23 05:10:09 -07:00

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 <bcmills@google.com>
Run-TryBot: Bryan Mills <bcmills@google.com>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
This commit is contained in:
Bryan C. Mills 2022-03-16 14:45:42 -04:00 committed by Bryan Mills
parent 2b0ac284cf
commit 3046ae927d
2 changed files with 42 additions and 4 deletions

View File

@ -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 {

View File

@ -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