8

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?

Phil Lucks
  • 2,704
  • 6
  • 31
  • 57

2 Answers2

4

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 :)

sfletche
  • 47,248
  • 30
  • 103
  • 119
  • Nice, this worked with the (probably obvious) caveat that you have to add the correct path to where `yarn` is installed. Mine was installed in my user directory. – matt Jan 18 '23 at 00:11
1

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 same 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.

yoyoyojoe
  • 103
  • 2
  • 8
  • 2
    Not sure why the downvotes. This problem was killing me, and adding `"C:\Program Files\Git\bin"` as Derek recommends fixed the problem for me as well. Too bad I can only give one upvote. – EugeneZ Jun 20 '22 at 00:21
  • I could make it work for me using this. https://github.com/typicode/husky/issues/1163#issuecomment-1500086343 – Piyush Aggarwal Apr 07 '23 at 08:56