GitHub desktop Husky pre-commit hook does not
Asked Answered
B

3

9

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?

Brigid answered 7/3, 2021 at 19:17 Comment(0)
C
5

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

Chicalote answered 3/8, 2022 at 18:11 Comment(1)
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.Pickmeup
F
2

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.

Ferromanganese answered 16/4, 2021 at 22:3 Comment(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.Shererd
I could make it work for me using this. github.com/typicode/husky/issues/1163#issuecomment-1500086343Giralda
C
0

This is how you can solve it using Mac OS X.

Directly in the pre-commit file use the which command to find where your npm or in my case yarn is globally installed. I also use nvm so I ended up with a which output path like /Users/my-name/.nvm/versions/node/v20.12.2/bin/yarn.

In your .husky/pre-commit file do the following:

$(which yarn) run pretty-quick --staged && $(which yarn) lint

Note: Make sure yarn is globally installed in the nvm default version which is the one used by GitHub Desktop.

Culliton answered 30/5 at 20:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.