aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorBryan C. Mills <bcmills@google.com>2022-03-16 14:45:42 -0400
committerBryan Mills <bcmills@google.com>2022-03-17 20:09:38 +0000
commit3046ae927d7664a63bb8c3a2fb3b9ca95bcf93de (patch)
tree6ddf15b6d46c4d60bf12678eb658cc83b7ccb0ba /src
parent2b0ac284cf4b81badb1c29e4fa299ea25cb9318f (diff)
downloadgo-3046ae927d7664a63bb8c3a2fb3b9ca95bcf93de.tar.xz
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>
Diffstat (limited to 'src')
-rw-r--r--src/cmd/doc/dirs.go16
-rw-r--r--src/cmd/go/testdata/script/mod_doc_path.txt30
2 files changed, 42 insertions, 4 deletions
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