-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
doc: improve documentation for goimport local-prefixes #3349
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
Merged
Merged
Changes from 4 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
d61f629
Add better documentation for goimport local-prefixes
sebastien-rosset 349d563
Merge branch 'master' into importdocimprovement
sebastien-rosset b13722b
Add better documentation for goimport local-prefixes
sebastien-rosset 4ce3c0e
Merge branch 'importdocimprovement' of github.com:CiscoM31/golangci-l…
sebastien-rosset e4a50f5
Improve documentation of 'goimports' linter
sebastien-rosset b6a52e2
Improve documentation of 'goimports' linter
sebastien-rosset 507450d
Improve documentation
sebastien-rosset 7e14c3a
Improve documentation
sebastien-rosset e0bb080
Merge branch 'master' into importdocimprovement
sebastien-rosset 19204f9
improve documentation
sebastien-rosset 560e0b0
Merge branch 'importdocimprovement' of github.com:CiscoM31/golangci-l…
sebastien-rosset aa9161d
Merge branch 'master' into importdocimprovement
sebastien-rosset a5f7046
Merge branch 'master' into importdocimprovement
sebastien-rosset 58e9fcd
Merge branch 'master' into importdocimprovement
sebastien-rosset eb5f5db
Merge branch 'master' into importdocimprovement
sebastien-rosset File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's great to be clear and have documentation but this is very verbose and no other linter explains what error message is expected when the linter fail. I also feel it's even less important for linters with fixers like
goimports
has so by just runninggolangci-lint run --fix
the issues will be resolved.Do you think it would be enough to do this change?
The information that it's after 3rd party packages is already there and with the change above it's clear it will be in a separate group.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the existing golangci-lint doc, it's not easy to relate why
goimports
is a linter. When I read the documentation at https://golangci-lint.run/usage/linters/, there is an entry forgoimports
, which links to https://pkg.go.dev/golang.org/x/tools/cmd/goimports, the go formatting tool. This quite unusual. For most (all?) other linter entries, there is a link to an actual linter, where the README file explains what the linter is. But in the case ofgoimports
, it's just a link to thegoimports
command.The
goimports
command is generally understood to be a formatting tool for go imports, not a linter. That creates the initial confusion: why is goimports a linter? It's meant to format code, not to create linter issues.Furthermore, In the linked https://pkg.go.dev/golang.org/x/tools/cmd/goimports#section-documentation, there is no explanation about
local-prefixes
, especially in the context of linters. Thegoimports
command does accept a documented-local
argument, but its intent is to reformat code, not to raise linter issues.In the golangci-lint context, the
goimports
linters does not "put imports beginning with prefix as a separate group". Instead, it creates a linter issue if the existing source code is not grouped as expected. This linter is not meant to be a code reformatting tool.Finally, afaik this is a comma-separated list of prefixes. There could be more than one prefix.
So overall, there is quite a bit of implied knowledge. This linter would be easier to use with more explicit documentation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's the same for
gofmt
which also links to the documentation view since there is no other link to provde for linters/formatters from stdlib. I think the difference here is built-in go tools and 3rd party linters.The linter is to check if the formatter has been run. The same goes for the
gofmt
linter. There are other formatters as well such aswhitespace
andgofumpt
that work in the same way. I would also like to add thatgolangci-lint
is also a formatter, that's what the--fix
flag is for. It reformats your code according to your selected linters (that checks if your code is formatted according to said formatters). Personally I rungoimports
on save in my editor but it's totally valid to rungolangci-lint --fix
on save as well.Yeah maybe it's bad that the config parameter and the flag isn't the same. I think the config was used to actually clarify it's just a prefix but I don't know why this was chosen.
I would say reformatting is one of the things it does, but not only. By default it does not, it just reports diffs. You have to specify the
-w
flag to reformat the code. The same goes forgofmt
and other style linters. We even havegci
which is basically doing the same job asgoimports
; it fails if your imports are not in the configured order and it sorts them if you use the--fix
flag. So this is a pretty standard thing forgolangci-lint
.I might be biased here but I think
goimports
is no different from a lot of other linters and formatters and a pretty common one since it's from stdlib. Since there are more formatters that formats your code with--fix
(or the standalone linter) I don't thinkgoimports
documentation should be treated differently, this is already documented underissues
configuration and Command-Line Options.To me, I think your second alternative without the note about formatting vs. checking if formatted sounds best, that is: