typicode / husky

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

Issue with path resolution on Windows #1397

Open piozygmunt opened 2 months ago

piozygmunt commented 2 months ago

Hi. After upgrading to version 9 from 8 hooks on windows stopped working. Based on some quick debugging it seems that path to the actual script is not being resolved correctly. To confirm this i echo'ed script path in h file:

#!/usr/bin/env sh
echo $0
set -x
h="${0##*/}"
s="${0%/*/*}/$h"

[ ! -f "$s" ] && exit 0
...

which gives me this output:

.husky\_/pre-commit
++ h=pre-commit
++ s='.husky\_/pre-commit/pre-commit'
++ '[' '!' -f '.husky\_/pre-commit/pre-commit' ']'
++ exit 0

Notice that path contains both forward and back slashes and because of that s="${0%/*/*}/$h" returns incorrect path. Could it be something with my setup or the regexp needs to be updated ?

OS: Microsoft Windows [Version 10.0.19045.4046]

Jxlle commented 2 months ago

+1

I have a similar problem. I use GitKraken which uses WSL to execute its git commands, but I selected the Windows installed Git executable. The following script inside all of the default hook files

#!/usr/bin/env sh
. "${0%/*}/h"

works on my Windows terminal but not through GitKraken because it uses the Windows path separator \ in the file path variable 0. For example, the path in the script variable will be .husky\_\post-checkout, and instead of it being modified to .husky\_\h, it will be .husky\_\post-checkout/h and fail.

QuantumQuin commented 2 months ago

+1 for gitkraken issue

DVersavel commented 2 months ago
sergei-lobanov commented 2 months ago

I have issue like this, when I did upgrade from v8 to v9

According to migration guide paths to husky.sh should be removed

v8:

#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

cd desktop && yarn test

v9: cd desktop && yarn test

But if remove the path, it works on MAC/Unix, but on Windows I can't commit anymore

error: cannot spawn desktop/.husky/pre-commit: No such file or directory

Jxlle commented 2 months ago

I have created a PR that fixes the issue on my end: https://github.com/typicode/husky/pull/1402

typicode commented 2 months ago

Thanks for the report.

@hyperupcall do you have any thoughts on this?

I find this behavior surprising as I'm not aware of other Git GUI tools on Windows having this issue?

siddheshranade commented 2 months ago

But if remove path, it works on MAC/Unix, but on windows I can't commit anymore error: cannot spawn desktop/.husky/pre-commit: No such file or directory

I'm facing the exact same issue as @sergei-lobanov mentioned above. I'm using Windows 10.

When I restore these two script lines:

#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

It works again perfectly as it did, even after keeping the change of "prepare": "husky" from "prepare": "husky install".

Should I update to v9 with these scripts restored? Please advise.

cc @typicode

DVersavel commented 1 month ago

Any news on this issues?

DanielRose commented 1 month ago

I have the same problem. It is not working in SourceTree and not in VS Code. When adding the lines back, it works.

pabloimrik17 commented 3 weeks ago

Same here on Windows 10 with Webstorm