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/gofmt: non-idempotent formatting of line comments #13702
Comments
Unfortunate but probably shouldn't be running gofmt on files with //line comments anyway. Can wait for Go 1.7. |
@griesemer, do you still want to do this for Go 1.9? It missed the past 4 releases and still repros. |
@bradfitz I will need to see who complicated the fix is, first. I won't get to this before June 1. |
I don't think we should do anything here: Fixing this would mean that once we see a //line directive we're not allowed to move anything that follows later to a different line. At the very least, that is the assumption we have to make until we see the next //line comment. But that interferes directly with gofmt's main purpose which is to organize white space for uniform formatting. One "solution" would be to not move any lines that follow a //line comment. But it's pretty tricky to get this correct under all circumstances. I'm not convinced the amount of complexity is worth the few cases where it might matter. I don't want to go down that route. The situation is additionally complicated by the fact that the go/printer has a mode that emits //line comments. I've recently fixed #5945 which makes sure that the above-mentioned mode only prints //line directives immediately before non-whitespace. Code that is printed that way will pass through gofmt undisturbed. In other words, don't use //line directives in front of empty lines - use them where it matters. Or don't gofmt such files. Closing as unfortunate. |
tl;dr: Access http://play.golang.org/p/Ry5OUCK43C; Click Format; Click Format.
After running the following snippet through
gofmt -w
:The output is:
This output looks correct with a single newline between
//line
andfunc main
. However if we rungofmt -w
again on the same file the output is different:Now the newline between
//line
andfunc main
was removed. This behavior is different from what would happen if instead of//line
we had a regular commented line.This was tested both on master and 1.5.1:
The text was updated successfully, but these errors were encountered: