Skip to content

x/tools/go/analysis: pkgsite documentation for golang.org/x/tools/go/analysis/passes/* often lacks details #58950

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
hyangah opened this issue Mar 9, 2023 · 6 comments
Assignees
Labels
Documentation Issues describing a change to documentation. FrozenDueToAge pkgsite Tools This label describes issues relating to any tools in the x/tools repository.
Milestone

Comments

@hyangah
Copy link
Contributor

hyangah commented Mar 9, 2023

I am not sure if this is a feature request for pkgsite, or it's just because the go analysis analyzer code was structured in a way not friendly with go documentation. Analyzers often place the helpful details in Doc constant, but this long doc is hidden from documentation

https://pkg.go.dev/golang.org/x/tools/go/analysis/passes/nilness

Screenshot 2023-03-09 at 9 10 51 AM

$ go doc
package nilness // import "golang.org/x/tools/go/analysis/passes/nilness"

Package nilness inspects the control-flow graph of an SSA function and reports
errors such as nil pointer dereferences and degenerate nil pointer comparisons.

const Doc = ...
var Analyzer = &analysis.Analyzer{ ... }

$ go doc Doc

Would be nice if the documentation page of analyzers presents sufficient details.
The pattern I saw in similar situation was to generate the package doc (doc.go) from the types but not sure if that's the best way.

cc @adonovan @golang/tools-team

@hyangah hyangah added the pkgsite label Mar 9, 2023
@gopherbot gopherbot added this to the Unreleased milestone Mar 9, 2023
@adonovan
Copy link
Member

adonovan commented Mar 9, 2023

I agree that doc.go makes sense. See https://go.dev/cl/474935 for a sketch of a starting point.

xtools$ go run ./go/analysis/passes/nilness/cmd/nilness/main.go 
nilness: check for redundant or impossible nil comparisons

Usage: nilness [-flag] [package]

The nilness checker inspects the control-flow graph of each function in
a package and reports nil pointer dereferences, degenerate nil
pointers, and panics with nil values. A degenerate comparison is of the form
x==nil or x!=nil where x is statically known to be nil or non-nil. These are
often a mistake, especially in control flow related to errors. Panics with nil
values are checked because they are not detectable by
...

$ go run golang.org/x/pkgsite/cmd/pkgsite@latest & 
$ open http://localhost:8080/golang.org/x/tools/go/analysis/passes/nilness

nilness

@gopherbot
Copy link
Contributor

Change https://go.dev/cl/474935 mentions this issue: go/analysis: ExtractDoc unifies Analyzer.Doc and package doc comment

@adonovan adonovan changed the title x/pkgsite: documentation for golang.org/x/tools/go/analysis/passes/* often lack details x/tools/go/analysis: pkgsite documentation for golang.org/x/tools/go/analysis/passes/* often lacks details Mar 9, 2023
@gopherbot gopherbot added Tools This label describes issues relating to any tools in the x/tools repository. Documentation Issues describing a change to documentation. labels Mar 9, 2023
@seankhliao
Copy link
Member

I wonder if that ExtractDoc function is something that could go in maybe the standard library go/doc?
It's something I've wanted a few times

@hyangah
Copy link
Contributor Author

hyangah commented Mar 13, 2023

Thanks @adonovan

https://go.dev/cl/474935's approach is

  • place the doc for an analyzer in the package document
  • extract the relevant section from the package document and wire it to analysis.Analyzer.Doc.
  • provide a helper function ExtractDoc to extract the section

(+) One copy and place where the document is
(-) To support multiple analyzers in one package, we may end up with some convention in the package documentation organization.
(-) Some features available in comment doc (cross-reference, etc) may leak to the analyzer doc which.

I was originally thinking a different approach

  • have a tool to extract analysis.Analyzer.Doc contents
  • populate doc.go using the tool which can be automated with go:generate
  • may have a test that ensures consistency of doc.go.

(+) No magic keyword or convention in the package documentation.
(-) Two copies of the same contents.
(-) Consistency between two copies is not automatically enforced.

@adonovan
Copy link
Member

I think the benefit of having a single copy (that is automatically readable in pkgsite) probably offsets the risk of markup (e.g. cross-refs) being accidentally included in the plain text version. Perhaps we can apply the go/doc/comment parser and format such things, so we get the best of both worlds: HTML in the browser and plain text in the terminal?

I agree we need to support multiple analyzers. Perhaps we should require a section called # Analyzer foo, and make ExtractDoc(doc, "foo") fail if if doesn't find a section of that name.

gopherbot pushed a commit to golang/tools that referenced this issue Mar 21, 2023
This CL defines an internal function, ExtractDoc, which extracts
documentation from the analyzer's package doc. This allows a single
doc comment to serve as both the package doc comment (which is
served by sites such as pkg.go.dev) and the analyzer documentation
(which is accessible through the command-line tool), appropriately
formatted for both media.

Also, change all our analyzers with nontrivial documentation
to use it.

The chosen syntax permits a single doc comment to document multiple
analyzers and looks good in the pkgsite HTML rendering and the
go vet help output. The HTML heading anchors are predictable.

For now this is internal, but we might want to publish it.
(After a proposal.)

Updates golang/go#58950
See golang/go#57906

Change-Id: Ifc0f48e54c3e42bc598649a7139e178a1a653c13
Reviewed-on: https://go-review.googlesource.com/c/tools/+/474935
Run-TryBot: Alan Donovan <adonovan@google.com>
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
gopls-CI: kokoro <noreply+kokoro@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
@findleyr
Copy link
Member

Looks good at master: https://pkg.go.dev/golang.org/x/tools/go/analysis/passes/nilness@master

Closing this as the work is done, and x/tools will automatically be released at some point.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Documentation Issues describing a change to documentation. FrozenDueToAge pkgsite Tools This label describes issues relating to any tools in the x/tools repository.
Projects
None yet
Development

No branches or pull requests

5 participants