GitHub desktop Husky pre-commit hook does not

Viewed 4035

I installed husky@4 and lint-staged as per many other projects (on Mac OS11). The terminal command flow git add . and git commit -m 'something' flow works fine: Husky's pre-commit hook and lint-staged commands are picked up successfully. However, the Github Desktop pre-commit hook does not seem to be behaving.

I have tried looking in the the .git/hooks/pre-commit file and it's there:

#!/bin/sh
# husky

# Created by Husky v4.3.8 (https://github.com/typicode/husky#readme)
#   At: 3/7/2021, 12:09:26 PM
#   From: /Users/admin/devProj/prject/node_modules/husky (https://github.com/typicode/husky#readme)

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

Commands:

...
        "husky": "^4.3.8",
        "lint-staged": "^10.5.4",
...
"husky": {
        "hooks": {
            "pre-commit": "tsc --noEmit && lint-staged"
        }
    },
    "lint-staged": {
        "**/*.(js|jsx|ts|tsx)": [
            "npm run lint:fix",
            "prettier --write"
        ]
    }

Any other reasons why GitHub Desktop is not finding this?

2 Answers

Having the same problem, I eventually found a solution here

Essentially, do the following:

Add a file to your root directory ~/.huskyrc that contains the following

PATH="/usr/local/bin:$PATH"

Restart Github Desktop, and voila :)

I'd suggest upgrading to Husky version 6, because that got it working for me, although it took some extra steps specific to Windows.

-- Background I'm working on this same problem now with the latest packages...

    "husky": "^6.0.0",
    "lint-staged": "^10.5.3",

In my case, I thought that fixed the bug in my configuration -- but it actually had just completely disabled Husky because there are some complicated v4 -> v6 migration instructions: https://typicode.github.io/husky/#/?id=migrate-from-v4-to-v6

However, I think you're having the same issue I was, even though I'm on Windows -- GitHub Desktop throwing an error, probably because of "command not found" or something similar.

In my case, husky worked on the command line (git for Windows) but not in GitHub Desktop.

(At one point this was a known bug in GitHub Desktop, but that looks like that got fixed years ago.)

There's a part of the Husky docs addressing this specific error: https://typicode.github.io/husky/#/?id=command-not-found

Unfortunately, even once I finished the migration instructions for v6, I was still having the issue, and it came down to using nvm (in my case, nvm for Windows, which is less robust because it doesn't even support .nvmrc files).

I uninstalled nvm completely and reinstalled the latest stable node (15.14.0) and npm (7.10.0). But ultimately it came down to adding "C:\Program Files\Git\bin" to PATH, which finally combined with husky v6 got the pre-commit git hooks working.

Related