Skip to content
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

precommit runs twice when using husky #432

Closed
faceyspacey opened this issue May 2, 2017 · 5 comments
Closed

precommit runs twice when using husky #432

faceyspacey opened this issue May 2, 2017 · 5 comments

Comments

@faceyspacey
Copy link
Contributor

faceyspacey commented May 2, 2017

Here's my relevant package.json:

"devDependencies": {
    "commitizen": "^2.9.6",
    "cz-conventional-changelog": "^2.0.0",
    "husky": "^0.13.2",
    "lint-staged": "^3.4.0",
    "prettier": "^1.2.2"
},
"scripts": {
   "precommit": "lint-staged --verbose",
   "commit": "git-cz",
},
"lint-staged": {
    "*.js": [
      "prettier --single-quote --semi=false --write",
      "git add"
    ]
},
"config": {
    "commitizen": {
      "path": "./node_modules/cz-conventional-changelog"
    }
},

lint-staged is correctly called once before the commitizen command line form appears, and then incorrectly husky - npm run -s precommit is run after. Typically without commitizen only lint-staged is called. Somehow commitizen is triggering husky to be called. No where do I have this script: npm run -s precommit. It seems commitizen somehow triggers it after you complete the form.

@jimthedev
Copy link
Member

jimthedev commented May 2, 2017 via email

@faceyspacey
Copy link
Contributor Author

faceyspacey commented May 3, 2017

I simply call git add, and git commit. The precommit script thanks to husky will run lint-staged. So you will see husky - npm run -s precommit as the log in the terminal (this is the same as the second log after the commitizen form).

Whereas if you run npm run commit for the first commit log you see:

> redux-first-router@0.0.0-development precommit /Users/jamesgillmore/React/redux-first-router
> lint-staged --verbose && npm test

Basically it seems 2 different versions of precommit are being run when commitizen is used, as they are logged with different messages.

ps. when is the precommit hook supposed to run relative to Commitizen? Before or after? Because it seems perhaps the correct hook is running after the form, as it has the same log as without commitizen. So perhaps what we want is the 2nd hook, but only to run before the commitizen form.

@faceyspacey
Copy link
Contributor Author

Here's a package you can clone and immediately see it in action:

https://github.com/faceyspacey/redux-first-router

@jimthedev
Copy link
Member

That is a bug in husky that happens due to a "feature" of npm-scripts that auto run scripts with the name prexxx where xxx is the name of another script. So, because you're using commit and precommit it is running them twice.

Until husky fixes this you can simply change your commitizen task to cmt or something else (cz, co, etc).

We could probably update our docs to reflect that when using commitizen and husky you should pick another name than npm run commit. Want to put in a PR?

@faceyspacey
Copy link
Contributor Author

nice find!

Here's the PR as requested: #433

christian-hawk added a commit to GluuFederation/gluu-passport that referenced this issue Mar 26, 2021
Developer can run it, if desired, using `npx cz`.

Also there are known issues using Husky and Commitizen together
- commitizen/cz-cli#558 (comment)
- commitizen/cz-cli#432 (wont fix)
- typicode/husky#99
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants