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/ast,go/format: newline missing between modified comment and function #66439

Open
ldemailly opened this issue Mar 21, 2024 · 3 comments
Open
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@ldemailly
Copy link

ldemailly commented Mar 21, 2024

Go version

go version go1.21.8 darwin/arm64

but same with 1.22 and Go dev branch on playground

What did you do?

Mutating node.Comments.List.Text produces incorrect formatting if adding newlines to /* */ style comments:

https://go.dev/play/p/IoJy719eLRt

As part of breaking too long lines, I stepped on the issue that if I cut a multi line comment that is before a function, once output back a new line between the comment and the function is missing eg

/*
 * multi
 * line comment.
 */func main() {
}

is produced

What did you see happen?

/*
 * multi
 * line comment.
 */func main() {

What did you expect to see?

/*
 * multi
 * line comment.
*/
func main() {
@ldemailly
Copy link
Author

Same with go 1.22 and same with dev version (per playground)

Wondering if it's related to #20744 (but would love a work around in any case)

I think it happens with string literals as well so maybe a bug in my understanding of how to insert newlines

actual complete code ldemailly/lll-fixer#1

@dr2chase dr2chase added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Mar 21, 2024
@dr2chase
Copy link
Contributor

@griesemer @mvdan

@ldemailly
Copy link
Author

ldemailly commented Mar 21, 2024

Debugging (blindly, not knowing the code at all) a bit when adding a new line the buffer in printer:

"package main\f\f\xff/*\xff\f\xff * multi\xff\f\xff * line comment.\xff\f\xff */\xfffunc main() {\f}\n"

vs with X instead of \n in the update

"package main\f\f\xff/*\xff\f\xff * multiX * line comment.\xff\f\xff */\xff\nfunc main() {\f}\n"

in printer.go print()

			n := nlimit(next.Line - p.pos.Line)

next.Line - p.pos.Line is 1 when inserting an X and 0 when inserting a new line which then cause if n > 0 { to not trigger so \n is not added

Not sure how to 'dirty' the pos to ensure it gets recalculated?

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

3 participants