typicode / husky

Git hooks made easy 🐶 woof!
https://typicode.github.io/husky
MIT License
32.33k stars 1.02k forks source link

Hooks do not run on Windows #780

Closed flo62134 closed 3 years ago

flo62134 commented 3 years ago

Hi,

I'm trying to use Husky on a personal project of mine. I can't manage to make hooks running.

When mannually running husky.sh, here is the output with the debug option enabled:

husky:debug husky v4.3.0 - husky.sh
husky:debug Current working directory is /c/Users/flore/Documents/chrome-extension
husky:debug husky.sh config not found, skipping hook

This is the content of .git/hooks/pre-commit:

#!/bin/sh
# husky

# Created by Husky v4.3.0 (https://github.com/typicode/husky#readme)
#   At: 2020-10-23 20:37:49
#   From: undefined (https://github.com/typicode/husky#readme)

. "$(dirname "$0")/husky.sh"

This is an extract from package.json:

  "husky": {
    "hooks": {
      "pre-commit": "ng test --watch=false",
      "pre-push": "ng test --watch=false"
    }
  },

I tried:

Running the script .git/hooks/pre-commit myself works fine though. This is the only way to make this work.

My config:

Thanks for your help!

pelukron commented 3 years ago

Hey guys, same issue here, lemme show you configuration

package.json image

I removed it and reinstall, tried with npm rebuild and still facing same issue hook/pre-commit image

--edit

Jesseyx commented 3 years ago

Same issue!

kirkoman commented 3 years ago

@flo62134 please clarify the issue you are seeing. @pelukron, @Jesseyx what do you mean by "same issue"?

Running husky.sh from the command-line is not how you exercise the hooks so the output given is expected. The contents of pre-commit script under .git/hooks is also expected. Note the leading . in front of the call to husky.sh inside the pre-commit hook. This sources the script so that basename inside husky.sh produces the name of the hook that called it. By running husky.sh script directly, you are basically asking husky to "run the git hook named husky.sh" but there is no such hook in git.

The way to run the hooks is by performing git operations. What happens when you commit? What is not working?

Jerome1337 commented 3 years ago

I had the same problem on Ubuntu 20 (WSL2).

I fixed it after couple minutes of investigating, the problem was the wrong git hookspath set during Husky installation.

I think the problem came from this commit https://github.com/typicode/husky/commit/cde7acedb69e224bc4465aa7d131fbddc0a9883b (I never had problem in my previous projects before this commit) and hookspath value is wrong on some OS (working on macos without problems)

After running git config core.hookspath ".git/hooks" everything was working.

My problem can be related to this issue.

flo62134 commented 3 years ago

Thanks a lot @Jerome1337 you fixed my issue with git config core.hookspath ".git/hooks"! Hooks are triggered on precommit 👍

alexGuerraDev commented 3 years ago

git config core.hookspath ".git/hooks" This doesn't work for me 😢

chriso86 commented 3 years ago

@alexeyten

Just chiming in here, after struggling with this.

Did this:

  1. npm i husky-init -D
  2. npx husky-init - You shouldn't need to run npm i after this, but maybe do it anyway
  3. npm i @commitlint/config-conventional @commitlint/cli -D
  4. created the hook shell file that I wanted as ".husky/commit-msg" (no file extension) with the following contents:
    #!/bin/sh
    # .husky/commit-msg (v7)
    # ...
    npx --no-install commitlint --edit "$1"
    # or
    # yarn commitlint --edit $1
  5. created "commitlint.config.js" file at project root with these contents:
    module.exports = {
    extends: ['@commitlint/config-conventional']
    };

Then created a commit and my commit-msg and pre-commit hooks work fine.

The docs are pretty helpful: https://typicode.github.io/husky/#/?id=install