-
Notifications
You must be signed in to change notification settings - Fork 17.7k
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
proposal: cmd/gofmt: minimize adjacent whitespace changes on struct layout edits #37399
Comments
Having choices of formatting based on directives in go.mod does not strike me as a good idea. gofmt is invoked in many different environments by different editors, revision control import hooks, etc. To require all those environments to preserve the go.mod directive is a big ask. And the benefit seems small. If we're going to change the formatting, let's change the formatting. (And then let's decide that gofmt is imperfect but is good enough, and let's not change the formatting. But that's just an opinion, not an argument.) |
This go.mod ‘flag’ is not meant to give a choice — it protects repositories of existing code against massive changes induced by gofmt run from CI scripts.
All these environments must now preserve Could gofmt just use
I beg to differ. It is not unusual to have 20+ lines changesets due to a typo corrected in an identifier.
Any change in default formatting needs to account for billion lines of already written code. Ie. it must recognize "old code" and "new code", in one or another way. |
It's not obvious to me that editors and revision control per-submit hooks need to know the The implication that this change would cause massive changes sounds like a good reason to not do it. Why is the benefit worth the cost? |
The design of gofmt from the start is that there are no options: go code looks consistent no matter what. As @robpike's proverb goes, "Gofmt's style is no one's favorite, yet gofmt is everyone's favorite." It's very important to the design that there aren't config knobs. This is a non-starter. Closing. |
Also note that most version control supports whitespace-insensitive diffs, like |
Proposal: Minimize adjacent whitespace changes on struct layout edits.
Author: Ohir Ripe [Wojciech S. Czarnecki]
Last updated: 2020/02/24
Discussion at https://golang.org/issues/37399
Supersedes: #37299
Abstract
I propose to change the gofmt rule regarding formatting of struct declarations to use fixed width fields both for identifier and type then let lines that does not fit this fixed schema to stay outlier.
Proposed formatting trades off readability of code that uses long identifiers (not common in Go) for improved (smaller) size of changesets introduced by adding, removing, or renaming a field in a struct declaration.
New formatting would be turned on by
go init
by agofmt version
directive added to the go.mod file.Background
With current gofmt rules slight changes to a struct declaration make for avalanche changes across declaration whitespace due to gofmt trying to layout struct declaration "pretty". Produced changeset size make diffs unreadable if any field identifier or type changes its length.
Proposal
New formatting is in force if gofmt current version is equal to or higer than provided in the go.mod file. New format fixes type and comment fields to be of fixed width, with start positions relative to current indent (cindent) position:
ie. adjusted are only parts that can fit.
Rationale
As explained in the background section.
This change will benefit all Go programmers that use source control.
Compatibility
This feature will work for a code where
gofmt 1.xx
directive is presentin the go.mod file. Ie. after
go init
or author decided to use new formatting rules.Implementation
This proposal is trivial regarding the formatting part, but otherwise it is not, as it proposes to make gofmt be aware of go.mod contents.
The text was updated successfully, but these errors were encountered: