]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/doc: show page for the requested object
authorMichael Matloob <matloob@golang.org>
Mon, 19 May 2025 19:31:37 +0000 (15:31 -0400)
committerMichael Matloob <matloob@golang.org>
Tue, 20 May 2025 20:30:28 +0000 (13:30 -0700)
This fixes a bug where we start pkgsite for every requested object,
rather than the one that we would have printed the documentation for.
To make things simple, we'll run the logic that prints the
documentation, but with an io.Discard writer. Then we can tell if the
documentation was found based on the return values of those functions.

For #68106

Change-Id: Ibf2ab1720f381d7214fc9239b9c2e915c91f7f7b
Reviewed-on: https://go-review.googlesource.com/c/go/+/674555
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Michael Matloob <matloob@google.com>
Reviewed-by: Jonathan Amsterdam <jba@google.com>
src/cmd/doc/main.go

index a199991c21d42e1b75d9eeacddc8ffb7fdea48bd..bc6cf2f7476939a0375223e8ba3b0cf334579669 100644 (file)
@@ -126,6 +126,11 @@ func do(writer io.Writer, flagSet *flag.FlagSet, args []string) (err error) {
                        return err
                }
        }
+       if serveHTTP {
+               // We want to run the logic below to determine a match for a symbol, method,
+               // or field, but not actually print the documentation to the output.
+               writer = io.Discard
+       }
        var paths []string
        var symbol, method string
        // Loop until something is printed.
@@ -163,21 +168,25 @@ func do(writer io.Writer, flagSet *flag.FlagSet, args []string) (err error) {
                        panic(e)
                }()
 
-               if serveHTTP {
-                       return doPkgsite(pkg, symbol, method)
-               }
+               var found bool
                switch {
                case symbol == "":
                        pkg.packageDoc() // The package exists, so we got some output.
-                       return
+                       found = true
                case method == "":
                        if pkg.symbolDoc(symbol) {
-                               return
+                               found = true
                        }
                case pkg.printMethodDoc(symbol, method):
-                       return
+                       found = true
                case pkg.printFieldDoc(symbol, method):
-                       return
+                       found = true
+               }
+               if found {
+                       if serveHTTP {
+                               return doPkgsite(pkg, symbol, method)
+                       }
+                       return nil
                }
        }
 }