golangci-lint
golangci-lint copied to clipboard
Disable a formatter for a path
Welcome
- [x] Yes, I've searched similar issues on GitHub and didn't find any.
- [x] I agree to follow this project's Code of Conduct
How did you install golangci-lint?
Brew
Your feature request related to a problem? Please describe
(using version 2.0.1)
Can you make it possible to disable a specific formatter for a give path?
For linters we have
exclusions:
rules:
# Exclude some linters from running on tests files.
- linters:
- containedctx
- dogsled
path: _test.go
Use case: I want to enable golines everywhere but not on my _test.go files.
Describe the solution you'd like
formatters:
enable:
- golines
exclusions:
paths:
- linters:
- golines
path: _test.go
Describe alternatives you've considered
Keeping golines disabled
Additional context
No response
Supporter
- [ ] I am a sponsor through GitHub or OpenCollective
Hey, thank you for opening your first Issue ! 🙂 If you would like to contribute we have a guide for contributors.
Hello,
Currently, you cannot disable one formatter for a path, but you can exclude a path (for all formatters):
formatters:
paths:
- _test.go
Can you explain why do you want to apply formatters except one on a specific file?
Thank you for the quick response!
We have a lot of long one-liner in our units tests that I want to keep in a single line. Such as:
createdUser := client.User.Create().SetID("dummy_id").SetName("Dummy User").SetEmail("[email protected]").SaveX(ctx)
Making them multiline makes our tests much longer and less readable, so I'd like to disable this formatter for test files
Same here. We have this interface, and I would prefer to keep it single-lined.
Something like //noformat:golines would be great for specific pieces of code
The exclusions based on a path are possible, but a directive for formatters will never happen: formatters are not linters, the formatting is applied to raw files, not parsed files.
@ldez got it, thanks
@ldez I thought I'd share another use-case where we want to exclude a formatter.
In a large codebase we want to introduce a new formatter (gci in this case), but apply the changes incrementally so it's not a big bang change; follow up PRs can then remove each exclusion for a smaller diff / blast radius.
@rcrowe I don't get the point of formatting imports incrementally: formatters are not linters, there is no real need to review the formatting so that the changes can be applied in one go.
One PR with only formatting is better than multiple PRs with unrelated changes related to formatting.
IMO, this is not a "valid" use case.
@ldez It was with the aim to reduce conflicts across branches, to be incrementally raise PRs and fix things up.
We can just create a standalone config file targeting just these formatters, so not blocked. Thought helpful to give an example in the wild.
It was with the aim to reduce conflicts across branches, to be incrementally raise PRs and fix things up.
This is much more of a "valid" use case explanation. I still think it's better to apply the formatting in a reduced number of PRs, but I understand your context. Thank you for the clarification.