gitpre-commit-hookhusky

Getting an unexpected additional `_/` in the pre-commit husky call when committing


When I do a commit, I'm getting this log output in the terminal:

.husky/pre-commit: line 8: .husky/_/_/husky.sh: No such file or directory

The commit seems to be passing with no problems on MacOS, while being prevented on Windows.

I have a pretty basic pre-commit hook that looks like this:

#!/bin/sh

# This loads nvm.sh and sets the correct PATH before running hook
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"

# Run the pre-commit hook
. "$(dirname "$0")/_/husky.sh"
npx lint-staged
npm run prettier:format-staged

What could possibly cause husky to try to find the husky.sh (which is a generated file) here: .husky/_/_/husky.sh?

Configuration:

MacOS Sonoma 14.6.1 (Apple Silicon), zsh, "husky": "^9.0.11",, git version 2.45.2, Node v20.12.2

If you need any additional information, let me know in the comments and I will add it.


Solution

  • There's this issue over at the husky repo. The husky repo owner suggests to remove both the shebang and the error-causing line, which gives me the impression the mechanism of dynamically creating a _/husky.sh file and running that in the hooks has been removed from husky.

    The author claims that starting with husky v9.1.1 this should be done "automatically" (which for me on 9.1.1 it doesn't). I've asked for clarification in that (closed) issue. Will update this answer as soon as I get updated information.

    Update: According to the owner, the "automatic removal" feature has been disabled in husky, due to issues with it:

    Hi, automatic removal has been disabled due to issues. You need to do it manually now.
    I would suggest updating to the latest version and remove
    . "$(dirname "$0")/_/husky.sh"