9

Up until recently -- I only noticed this a couple days ago -- my git pre-commit hook was working. I'm writing a react app and using Husky, TSLint, and Prettier to clean and lint my code before committing. Now, when I change and commit files, the pre-commit hook doesn't run.

My project structure looks like this:

- project
  - .git/
  - react/   <- the frontend
    - node_modules/
    - src/
    - package.json
    - (other files)
  - nodejs/  <- the server
    - node_modules/
    - src/
    - package.json
    - (other files)
  - package.json
  - (other files)

If I manually execute the hook, it seems to run fine:

[/project/react] # git status
On branch fixHusky
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    modified:   MyFile.ts

[/project/react] # ../.git/hooks/pre-commit
husky > pre-commit (node v12.6.0)
  ↓ Stashing changes... [skipped]
    → No partially staged files found...
  ✔ Running linters...

[/project/react] # 

But when I actually try to commit, husky doesn't run:

[/project/react] #  git commit -m "testing husky"
[fixHusky cf17a6b] testing husky
 1 file changed, 1 insertion(+), 1 deletion(-)

[/project/react] # 

Any idea why it isn't running?

whiterook6
  • 3,270
  • 3
  • 34
  • 77
  • Why `../.git/hooks/pre-commit` and not `./.git/hooks/pre-commit`? Maybe do you have two directories `.git` (`../.git` and `./.git`)? – aropan Jul 17 '19 at 22:30
  • Did you put the hook in your package json? – evolutionxbox Jul 17 '19 at 22:36
  • I used `../` instead of `./` because our project has two directories, one for a server and one for react. The git repo is in the root but the linting should only happen in the react folder. I'll edit the question to be more clear. – whiterook6 Jul 18 '19 at 15:53

2 Answers2

12

Updating Husky by running yarn add --dev husky fixed the problem. I have no idea why it stopped working, but husky was very out of date anyways.

whiterook6
  • 3,270
  • 3
  • 34
  • 77
6

Check if git config core.hooksPath has been set to a different path than its default: $GIT_DIR/hooks

Check also that GIT_DIR (environment variable) is not currently set.

In both cases, Git would look for that hook not where you would expect (and currently have your pre-commit hook)

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • It looks like both are empty. What should GIT_DIR be set to? Without it, I tried setting the hooksPath manually: `git config core.hooksPath "hooks/*"` but no dice. – whiterook6 Jul 18 '19 at 21:18