From: Michael Matloob Date: Mon, 19 May 2025 19:31:37 +0000 (-0400) Subject: cmd/doc: show page for the requested object X-Git-Tag: go1.25rc1~183 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=1972493904b41a34e35a1f62b18f5d91d84a26bd;p=gostls13.git cmd/doc: show page for the requested object 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 Reviewed-by: Michael Matloob Reviewed-by: Jonathan Amsterdam --- diff --git a/src/cmd/doc/main.go b/src/cmd/doc/main.go index a199991c21..bc6cf2f747 100644 --- a/src/cmd/doc/main.go +++ b/src/cmd/doc/main.go @@ -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 } } }