-
Notifications
You must be signed in to change notification settings - Fork 17.5k
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: Trailing newline in comments is removed since 1.19 #54489
Comments
cc @rsc |
The situation where I find this particularly annoying is when I'm using indented blocks. I think they look best with blank lines around them for symmetry. And gofmt apparently does too, because it will add those blank lines if they're missing. But it gets rid of the trailing blank line if it happens to be the last line in the comment. For example, this:
is turned into this:
I think it looks best like this:
but gofmt deletes the last blank line. There are some examples of this in the stdlib; for instance, compare these from strings:
|
See #57388 (closed as duplicate of this issue) for additional commentary and insight. |
cc @griesemer for thoughts. My intuition is that I prefer godoc styles to be consistent, and when a project has a mix of godocs in both styles (with and without the trailing empty line), it distracts my reading and editing of godocs. So I lean against reverting to the old system, where we let the user choose whether to leave the trailing line on each godoc. If there are cases where a trailing empty line helps readability, such as when a godoc ends in an indented section like @cespare says, then perhaps we should consider enforcing the trailing empty line in those cases. One could argue that always requiring empty lines around indented blocks, even when they are at the end of a godoc, would improve consistency, since the following is not properly formatted:
If a godoc doesn't end in a block quote, I don't think a trailing line helps, and most of the code I've read and written doesn't use one in that case. So I'd definitely be against always enforcing a trailing empty line. |
See also robpike/typo#3 (comment) |
@mvdan - It shouldn't enforce a trailing empty line, it should just leave one alone if it's present. There's plenty of prior art in gofmt for this approach of "if it can go both ways and it's not too bad then leave it alone". I realise that as the author of Having |
I don't think gofumpt is part of the equation here. Per this issue, gofmt already seems to be a bit too strict, and that strictness came with the godoc redesign and reformatting, not gofumpt. In my previous comment, I advocate that perhaps gofmt should require an empty line after every indented block in a godoc, as I think that would solve the problem that @cespare mentions. And I can agree that, in the example that he shows, the new behavior hurts readability, and breaks the symmetry with code blocks to some extent. As for allowing a trailing empty line in other cases, like your original example - I personally don't think such an empty line helps readability in those other cases. That's of course subjective, but gofmt is always a balance between consistency and user preference. The vast majority of Go users seem to not mind the new strictness, so I'm inclined to only fix indented blocks. I'm only a secondary maintainer for gofmt, though. I'd prefer to get @griesemer's input to make a decision :) |
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes.
What operating system and processor architecture are you using (
go env
)?What did you do?
I saved an existing file that had a dense and detailed comment followed by a dense and detailed function signature, that contained a deliberate trailing newline to separate the last paragraph from the function's signature to improve readability:
What did you expect to see?
I expected to see the final newline in the comment body preserved.
What did you see instead?
The final newline was removed:
I'd really like to see the old behaviour restored here as function signatures like the one in my example can be crowded out by the last line of the preceding paragraph.
The text was updated successfully, but these errors were encountered: