typicode / husky

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

V9 doesnt work within vscode #1364

Closed dan2kx closed 4 months ago

dan2kx commented 4 months ago

Updating from v8 to v9 stops the following script from being executable in vscode and throws an error 3 it does work fine in command line

#!/bin/sh
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"

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

npx lint-staged

Is there a different setup required in v9 to make this work?

typicode commented 4 months ago

It should work the same 🤔 At least, there's no change that should require a different setup on the user side.

typicode commented 4 months ago

You can read more version managers here: https://typicode.github.io/husky/how-to.html#node-version-managers-and-guis

Can you put export HUSKY=2 in your .config/husky/init.sh (or ~/.huskyrc), this should output debug logs when running your commit. Feel free to paste them here.

dheeraj-jn commented 4 months ago

Not OP, but I had to restart VSCode after upgrading husky and it works fine now.

mstykow commented 4 months ago

FYI: I used to have my NVM setup in a file called ~/.huskyrc. This no longer seems to be recognized as of V9. After moving my setup into .config/husky/init.sh, as per docs, everything works fine again.

typicode commented 4 months ago

Thanks @mstykow it's a bug. I'll publish a fix.

dan2kx commented 4 months ago

@typicode so i created the following

//.config/husky/init.sh
export HUSKY=2
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"

// also tried
//.zshrc
//.husky/pre-commit
npx lint-staged

The commit still fails this time with npx: command not found the debug env didnt seem to actually change the response which was:

> git -c user.useConfigOnly=true commit --quiet --allow-empty-message --file -
.husky/pre-commit: line 1: npx: command not found
husky - pre-commit script failed (code 127)
husky - command not found in PATH=/Applications/Xcode.app/Contents/Developer/usr/libexec/git-core:/usr/local/bin:/System/Cryptexes/App/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/Apple/usr/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/local/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/appleinternal/bin

also restarted vscode - no change

typicode commented 4 months ago

Can you try putting some echo in init.sh? It doesn't seem to be loaded.

HUSKY=2 should make the output a lot more verbose.

dan2kx commented 4 months ago

do i have to call the init.sh or does husky call it if present?

also, just a suggestion; but would the file be more appropriately located in the .husky folder? i.e. .husky/init.sh or .husky/config/init.sh seems odd to me to have a separate folder for this

typicode commented 4 months ago

it should be in the $HOME directory or equivalent in Windows.

dan2kx commented 4 months ago

So the init.sh is not committed in the repo or am I misunderstanding?

typicode commented 4 months ago

That's correct

dan2kx commented 4 months ago

Isn’t that the point of husky? So that the scripts can be committed? Can we no longer add the env setup in the pre-commit file?

typicode commented 4 months ago

Hook scripts are committed.

For env setup, it depends. For an open source projects, contributors may have very different env setups, so you can't account for all them (that's why there is ~/.config/husky/init.sh).

For a private project, it's different. If everyone uses the same env setup, it can be part of the pre-commit and there's no need for init.sh.

On Windows, you would put it in C:\Users\yourusername\.config\husky\init.sh.

dan2kx commented 4 months ago

I am in a private project but i can see your logic for public repos.

So is there a reason why setting the NVM variables in the pre-commit hook wouldnt work?

typicode commented 4 months ago

No, it'll work :+1: . That being said, if you have 3 private repos, you may want to the NVM variables in init.sh so that you don't duplicate them in each pre-commit hooks.

In the end, husky isn't opinionated, you're free to do what works for you or your project :)