-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Add a pre-commit hook to check all files #4046
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
Conversation
Hey, thank you for opening your first Pull Request ! |
This PR shouldn't be merged. @hanzo By forcing a check of all files in a repo during every pre-commit, you'll end up seeing unrealted linting errors which then prevent otherwise valid commits from being permitted. For example, if my commit changes only file A but unrelated file B has linting errors, checking all files means my commit will be blocked until I either fix file B, or use
If you really want your entire code base to be checked ahead of every commit, then I'd suggest creating a local hook with what's in your PR. |
Thanks for your reply, @davidjb. I'm aware of pre-commit's intended behavior. To describe my organization's setup a little better: we have a microservice architecture with over 300 Go repos, and we want our linters to run the same way on every repo, whether you're running locally before committing or whether it's a CI check. It turns out that pre-commit works great for both, and we can easily manage the versions of golangci-lint and other linters in the .pre-commit-config.yaml file in each repo via automation. In every case we just use I understand that this might not be the most common scenario for users of golangci-lint. The part that's not clear to me is why the behavior added by #3521 needs to be hardcoded in the pre-commit hook, when it could easily be achieved by configuration instead? |
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.
When I originally added the .pre-commit-hooks.yaml in this repo (#311) I intentionally did not include --new-from-rev
. pre-commit should catch the majority of issues locally, but it's explicitly designed to be used in CI - see their first-class https://pre-commit.ci/ service - and running it in CI lets you reuse the same linter definition instead of having a separate definition (e.g. running golangci-lint directly) which will get out of sync.
I'd also agree that enabling new-from-rev is an option downstream customers can enable easily - either in .golangci.yml, or by adding args: [--new-from-rev, HEAD]
in their .pre-commit-config.yaml. Conversely, removing this flag is much harder (.golangci.yml config won't work as CLI args take precedence; can't use args
in .pre-commit-config.yaml, must override the whole entry
so blocked from getting future upstream fixes). I'd suggest we prefer correctness for varied workflows over optimizing for a specific workflow.
Using The 2 points of view about I don't use Note: using |
Thanks for your input, @ldez. I agree that it seems like the best path forward is to define two hooks. Even ignoring the CI use case, |
I would prefer the opposite:
|
I've updated the PR as you suggested to add a hook named I've also added descriptions for both hooks to make their behavior more obvious to future users. My team had to spend weeks undoing all of the lint errors that were introduced after #3521 silently broke our CI for months. |
@ldez thanks for updating the title. Anything else you'd like to see changed before this is ready to merge? |
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.
LGTM
I will not create a release only for pre-commit. Sponsoring is a good way to sustain open source maintainers: sponsor me |
#3521 added
--new-from-rev HEAD
to the pre-commit hook with the goal of making it easier to integrate golangci-lint into an existing code base without needing to address all existing lint errors. The problem with only checking changed files is that it makes it far too easy for new lint errors to be introduced: as soon as a developer runsgit commit --no-verify
they will never again be warned about the lint errors they just added. It's common to commit some incomplete code changes to save your progress before you're ready to polish the code and clean up every lint error, so you rungit commit --no-verify
and now the lint errors are permanently ignored.The same is true for CI: a unit test that runs golangci-lint via pre-commit can now only warn about errors that were introduced in the most recent commit instead of the entire pull request. This also prevents linters from running against the entire codebase post-merge to catch lint errors that were caused by interactions between multiple PRs.
Instead of hardcoding
--new-from-rev HEAD
in the pre-commit hook, developers who desire this behavior can achieve this via configuration as described in this comment.cc @davidjb @ldez - curious to hear your thoughts on this