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

go/token: line adjustment is incorrect for comments when imports are present #51371

Open
kortschak opened this issue Feb 26, 2022 · 2 comments
Open
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@kortschak
Copy link
Contributor

kortschak commented Feb 26, 2022

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

$ go version
go version go1.17.7 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="on"
GOARCH="amd64"
GOBIN="/home/user/bin"
GOCACHE="/home/user/.cache/go-build"
GOENV="/home/user/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/user/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/user"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/home/user/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/home/user/go/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.17.7"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/dev/null"
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 -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build2236448890=/tmp/go-build -gno-record-gcc-switches"

What did you do?

https://go.dev/play/p/3FdwLDhsXfD

What did you expect to see?

Consistent line adjustment independent of the number of imports.

0
	dummy.tmpl:8
1
	dummy.tmpl:8
2
	dummy.tmpl:8
3
	dummy.tmpl:8

What did you see instead?

Broken line adjustment if imports exist.

0
	dummy.tmpl:8
1
	file.go:9:1
2
	file.go:10:1
3
	file.go:11:1

An empty import () also causes this breakage.

Additional note, this appears only to affect comments.

Cause

This appears to be at least in part due to a discordance between how an *ast.CommentGroup's position is calculated and how a //line directive works. When a line directive is part of a comment group, its pos is used to define the position of the comment group, but it refers to the next line. So changing the Pos method like so

func (g *CommentGroup) Pos() token.Pos {
	for _, c := range g.List {
		if !isDirective(c.Text[2:]) {
			return c.Pos()
		}
	}
	return g.List[0].Pos()
}

fixes the issue. This doesn't explain why imports affect the behaviour and I don't believe that it's a sensible fix.

It also raises a question about why the issue is also present when there is a blank line separating the directive from the comment group, when this is the case, the separated lines are still joined into the comment group. The reason for this is here where a single blank line is allowed between comment lines, but I don't understand why separated comments blocks should end up in the same comment group.

@kortschak kortschak changed the title go/token: line adjustment is incorrect when imports are present. go/token: line adjustment is incorrect for comments when imports are present Feb 26, 2022
@thanm thanm added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Feb 28, 2022
@thanm
Copy link
Contributor

thanm commented Feb 28, 2022

@griesemer per owners.

@griesemer
Copy link
Contributor

cc: @findleyr @golang/tools-team

@seankhliao seankhliao added this to the Unplanned milestone Aug 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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

4 participants