]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/doc: use the 'go' command from buildCtx.GOROOT, not whatever is in $PATH
authorBryan C. Mills <bcmills@google.com>
Wed, 16 Mar 2022 18:45:42 +0000 (14:45 -0400)
committerBryan Mills <bcmills@google.com>
Thu, 17 Mar 2022 20:09:38 +0000 (20:09 +0000)
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>

src/cmd/doc/dirs.go
src/cmd/go/testdata/script/mod_doc_path.txt [new file with mode: 0644]

index cb4d45ac6ced8b9a76b418cd2ca78a1285fdfdb5..489f49088993718edc09bfc80ce5b161d4c43e43 100644 (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 {
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 (file)
index 0000000..57470a9
--- /dev/null
@@ -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