-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: run eslint/prettier fix against files as a pre-commit hook with lint-staged #5462
Conversation
Run prettier/eslint fix before `git commit`. I have seen a lot of lint related issues in PRs. Hopefully this pre-commit hook can minimize such problems and save us from keep telling contributors to run `npm run lint:fix`.
a59e753
to
fab3a75
Compare
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.
This was a feature that I proposed a long time ago, I like that it was implemented
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.
FYI: we have been discussing lint-staged in the past in #4330 and #4382
I see two problems in this proposal:
- Linting only stages files may create false positives, where staged lint pass but full lint fails (see my first comment below).
- New git hooks are IMO adding way too much friction. I don't mind to have these checks in place for others (if you like so), as long as there is an easy and well-documented way how to disable them in my local clone. Please note that I want to keep
commit-msg
check because I find that one as useful.
const commands = [ | ||
`node packages/build/bin/run-prettier --write ${files.join(' ')}`, | ||
`node packages/build/bin/run-eslint --report-unused-disable-directives` + | ||
` --fix --cache ${files.join(' ')}`, |
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.
Please note that with TypeScript-powered linter checks, it's not enough to lint the staged files. For example, when the signature of a method inside @loopback/context
changes (e.g. a void
function is returning a Promise
now), we need to lint all packages using that particular method. Many of them will not pass no-floating-promises check.
@@ -77,7 +78,9 @@ | |||
"copyright.owner": "IBM Corp.", | |||
"husky": { | |||
"hooks": { | |||
"commit-msg": "commitlint -E HUSKY_GIT_PARAMS" | |||
"commit-msg": "commitlint -E HUSKY_GIT_PARAMS", | |||
"pre-commit": "lint-staged", |
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.
Cross-posting from #4330 (comment):
In my experience, commit hooks are not very fast, which I find annoying and thus prefer to run checks when I decide on my own. Of course, that's just me, other people may have different preferences.
and from #4382 (comment):
My experience with pre-commit hooks is not the best, the hook is often run in situations when I don't want it to be executed (e.g. when performing git rebase
) and thus slowing me down.
"commit-msg": "commitlint -E HUSKY_GIT_PARAMS" | ||
"commit-msg": "commitlint -E HUSKY_GIT_PARAMS", | ||
"pre-commit": "lint-staged", | ||
"pre-push": "npm run build" |
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.
This is even worse.
npm run build
takes morethan a minutea while to complete, that's way too long waiting just to be able to push my changes to github.- What if I have a work-in-progress branch that does not pass the build but I want the code to be backed up on GitHub and/or open a WIP pull request to discuss the early version? IIUC, this new hook will not allow me to do that.
If your main motivation for this pull request is to save typing the comments, then I'd like to offer few alternatives:
|
My primary motivation is to minimize basic issues in PRs that fail CI builds and require manual ping-pongs.
I can tolerate edge cases, for example, Please note the hooks can be skipped during rebase. See https://github.com/typicode/husky#skip-all-hooks-rebase. We can also use |
Makes sense. I am afraid pre-commit hooks are not a very reliable solution for that, I have seen pull requests that were failing commit lint checks, despite the fact that we already have a pre-commit hook for that. In general, I am reluctant to introduce duplicate checks for exceptional situations (a PR that fails CI on linting or build problem), because they negatively impact the usual path where developers run Maybe we should ask ourselves why are people not running Instead of adding pre-commit/push checks forcing developers to suffer these long waits, I am proposing a different approach:
Thank you for the pointers. Personally, I want to keep If we decide to add new hooks, then let's add all of them to |
Unfortunately not all checks can/should be performed on |
+1 for pre-commit hook. It would save me tons of context switching. I'm not in favor of pre-push check though. Build and test are something CI does well. And if either fails it probably highlights a bigger issue than a missed semicolon and would require a due attention. The primary goal of a hook is to address small nonsensial issues that are time killers otherwise. Also the full test suite never runs successfully on my Windows machine. I've made a similar PR #5616 with only a pre-commit and a small prettier tweak. |
@bajtos One could permanently disable hooks by adding |
@InvictusMB Please raise an issue to investigate the issue as it would help to identify sources of friction with the development experience. Happy to take a look. 馃憤 |
Thank you for the tip. Unfortunately, I don't want to disable all hooks - I want to keep the hook that's checking that my commit message is well formatted. |
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.
Looks like I am the only person opposing the idea of lint-staged, which makes me feel a bit uncomfortable. I was thinking a lot how to move this PR forward in a way that will allow you all to have lint-staged checks while allowing me to opt out of them.
I am proposing to implement the following behavior:
- If
process.env.LINT_STAGED
is set to string0
orfalse
, then no linting should be performed. - It would be great to implement & contribute this feature to lint-staged, but that's a long term goal.
- For this pull request, we can implement support for
LINT_STAGED
ourselves. There are many ways how to accomplish that. For example, we can implement a wrapper script forlint-staged
that will return immediately whenLINT_STAGED
is disabled. Another options is to leverage the fact that our lint-staged config is dynamically produced bylint-staged.config.js
, we can return an empty config whenLINT_STAGED
is disabled.
Thoughts?
|
||
const path = require('path'); | ||
|
||
module.exports = { |
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.
module.exports = { | |
if (['0', 'false'].includes(process.env.LINT_STAGED)) { | |
console.log('Skipping lint-staged as requested via LINT_STAGED env var.'); | |
return; | |
} | |
module.exports = { |
Close it as #5616 has been landed. |
Run prettier/eslint fix before
git commit
.I have seen a lot of lint-related issues in PRs. Hopefully this pre-push
hook can decrease such problems and save us from keep telling contributors
to run
npm run lint:fix
.A
pre-push
hooks is also added to runnpm run build
to prevent code that does not compile from being pushed to remote.@frbuceta Thank you for the inspiration at #4330
Checklist
馃憠 Read and sign the CLA (Contributor License Agreement) 馃憟
npm test
passes on your machinepackages/cli
were updatedexamples/*
were updated馃憠 Check out how to submit a PR 馃憟