husky icon indicating copy to clipboard operation
husky copied to clipboard

husky v9 lint-staged not triggered on pre-commit

Open chrisjbrown opened this issue 1 year ago • 9 comments

Context Please describe your issue and provide some context:

  • Mac
  • Iterm and VSCode
  • Hook doesn't run

husky 9.0.11 lint-staged 15.2.2

Used https://github.com/uulm-mrm/git_hooks to get a basic husky+lint-staged+prettier+eslint setup. If I run npx lint-staged myself in a terminal I see the expected scripts run but committing doesn't trigger the scripts and allows the commit to go through.

Running git config core.hooksPath outputs .husky/_ git --version output git version 2.45.1

troubleshooting section says to be on git 2.9 which feels like a mistake as 2.45.1 is latest

package.json

"scripts": {
    ....
    "prepare": "husky"
},
...
"lint-staged": {
    "*.js": "eslint --cache --fix",
    "*.{js,mjs,css,md,hbs}": "prettier --write"
}

.husky/pre-commit

npx lint-staged

chrisjbrown avatar May 19 '24 10:05 chrisjbrown

I also had the same problem

ghost avatar May 22 '24 08:05 ghost

Any chance your package.json isn't in the root directory of your repo?

I thought mine was broken (which is how I came across this issue), but then I found an adjusted config from a similar repo, and it's working fine now. My change is just "prepare": "cd .. && husky", but that may be irrelevant for you 🤔

orochi-kazu avatar May 25 '24 07:05 orochi-kazu

I also had the same problem

shuo-hiwintech avatar Jun 12 '24 09:06 shuo-hiwintech

I have been facing the same issue where Husky's execution suddenly stops working without apparent reason.

After investigating in my repository, I noticed that this error occurs in all Husky hooks, specifically when the following scenario happens:

  1. I initialize a project with Git using git init.
  2. I install Husky in the project following the standard recommendation.
  3. I delete the .git folder from the project using the command rm -rf .git.
  4. I reinitialize Git in the project with git init.
  5. I try to execute a Husky hook, but I observe that it stops working immediately.

To temporarily solve this issue, the only effective measure I have found is to reinstall Husky every time the .git folder is deleted.

I hope these detailed steps clarify the issue I have been experiencing with Husky in my repository.

vicasas avatar Jun 13 '24 01:06 vicasas

Happens when I manually migrated v8 -> v9

Lonli-Lokli avatar Jul 04 '24 14:07 Lonli-Lokli

Seems like the problem is Linux\Windows separator issue. As part of migration Git config (located in .git/config file) has been changed, to reflect husky's simplified hooks path before

[core]
 hooksPath = .husky

after

[core]
 hooksPath = .husky\\_

So if I change hooks path to .husky/_ - everything started to work again

Lonli-Lokli avatar Jul 04 '24 14:07 Lonli-Lokli

Same issue. I've tried every solution that exists on the internet and it still doesn't work when I commit.

victor-paydestal avatar Feb 11 '25 05:02 victor-paydestal

I couldn't get lint-staged to work with husky in my project, so I added a custom command to run lint and format in my project which now works when I make a commit. This is my config settings

{
  "scripts": {
  // other commands
  "lint": "eslint .",
  "prettier:format": "prettier --check . || prettier --write .",
  "preview": "vite preview",
  "prepare": "husky"
},

And my pre-commit hook

set -e

npm run prettier:format || true
npm run lint

[!note] The -e flag allows the hook to only terminate when an error occurs (Mandatory to allow both commands to run). Here's the result.

https://github.com/user-attachments/assets/4294ecf3-1ab3-4bff-928f-7e23f2c815bf

It's not the cleanest solution but it's better than nothing at this point. Hopefully this could help someone.

Evavic44 avatar Feb 11 '25 14:02 Evavic44

I am having the same issue but only when committing from VS Code. It appears it is using an older version of node, which I have no idea where it pulls it from.

zlasa avatar Mar 10 '25 20:03 zlasa