Skip to content
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

cmd/doc: inconsistent removal of directive comments #56592

Closed
thockin opened this issue Nov 4, 2022 · 8 comments
Closed

cmd/doc: inconsistent removal of directive comments #56592

thockin opened this issue Nov 4, 2022 · 8 comments
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@thockin
Copy link

thockin commented Nov 4, 2022

What version of Go are you using (go version)?

$ go version
go version go1.19.1 linux/amd64

Does this issue reproduce with the latest release?

yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/thockin/.cache/go-build"
GOENV="/home/thockin/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/thockin/src/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/thockin/src/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.19.1"
GCCGO="gccgo"
GOAMD64="v1"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/home/thockin/src/go/src/k8s.io/kubernetes/go.mod"
GOWORK=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build3419971970=/tmp/go-build -gno-record-gcc-switches"

What did you do?

Given this file:

package p

type T struct {
	// Field is a field.
	//magic:true
	Field int
}

Run go doc.

$ go doc --all .
package p // import "."


TYPES

type T struct {
	// Field is a field.
	//magic:true
	Field int
}

Note the "magic" directive is present.

$ go doc --all T
package p // import "."

type T struct {
	// Field is a field.
	//magic:true
	Field int
}

Still has magic directive.

$ go doc --all T.Field
package p // import "."

type T struct {
    // Field is a field.
    Field int
}

This time, the magic is detected as a directive and removed. Shouldn't that happen in all cases?

What did you expect to see?

Consistent stripping of "directives".

What did you see instead?

Inconsistent stripping of "directives".

@seankhliao seankhliao changed the title affected/package: cmd/doc cmd/doc: inconsistent removal of directive comments Nov 5, 2022
@seankhliao seankhliao added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Nov 5, 2022
@thockin
Copy link
Author

thockin commented Nov 7, 2022

cmd/doc/pkg.go :: func Package.emit() calls format.Node() but the field-centric invocation calls into each struct-field's field.Doc.Text() which calls go/ast/ast.go CommentGroup.Text() which checks isDirective().

@seankhliao
Copy link
Member

cc @robpike

@seankhliao seankhliao added this to the Unplanned milestone Nov 19, 2022
@robpike
Copy link
Contributor

robpike commented Nov 20, 2022

This is surely an issue with the go/doc package, not the cmd/doc command, which just formats what the package provides.

@griesemer

@thockin
Copy link
Author

thockin commented Mar 31, 2023

Hi all, this popped up again - is there any way I can help get traction on it?

@gopherbot
Copy link

Change https://go.dev/cl/483055 mentions this issue: cmd/doc: format field doc comments when printing entire struct

@thockin
Copy link
Author

thockin commented Apr 10, 2023

Thanks! I tried it at gotip and I see something unexpected:

given this file:

// Package p is a package.
//
//magic:true
package p

// T is a type
//
//magic:true
type T struct {
	// Field is a field
	//magic:true
	Field int
}

go doc shows:

$ go doc --all .
package p // import "hockin.org/p"

Package p is a package.

TYPES

type T struct {
	// Field is a field
	//magic:true
	Field int
}
    T is a type

Note the directive is still present. gotip says:

$ gotip doc --all .
package p // import "hockin.org/p"

Package p is a package.

TYPES

type T struct {
	// Field is a field

	Field int
}
    T is a type

The directive is gone (yay) but there's now a blank like. Is that expected? @ianlancetaylor

@ianlancetaylor
Copy link
Contributor

@thockin It turns out to be rather painful to avoid. Or to put it more fairly, I was sufficiently exhausted by the effort tracking through all the code that I didn't feel like trying to fix that too.

It's because go/ast records positions for all comments and tokens, and go/format honors those positions to try to recreate the same format as the original file. That means that without extra work, when a comment line disappears, go/format inserts an extra line to get back to the expected position. Just disregarding the positions doesn't work, as that would mean that we lose track of which comments come before a field and which comments appear on the same line as a field. So removing the blank line is non-trivial, or, at least, I haven't seen a simple solution.

I do think that the gotip output is better. But as you say it's perhaps not ideal. By all means open a new issue.

@thockin
Copy link
Author

thockin commented Apr 11, 2023

ACK! Thanks.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

5 participants