Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Does husky v5 work at all in Windows? #864

Closed
robross0606 opened this issue Feb 10, 2021 · 17 comments
Closed

Does husky v5 work at all in Windows? #864

robross0606 opened this issue Feb 10, 2021 · 17 comments
Labels

Comments

@robross0606
Copy link

I'm trying to migrate from v4 to v5 of Husky, but it completely fails to work when using git on Windows. All I get is:

> git -c user.useConfigOnly=true commit --quiet --allow-empty-message --file -
error: cannot spawn .husky/pre-commit: No such file or directory

But my repo includes a file called pre-commit in the .husky folder.

@typicode
Copy link
Owner

Hi @robross0606,

Yes, it should. What terminal are you using? Could you re-run with HUSKY_DEBUG env variable set to 1?

@nemchik
Copy link

nemchik commented Feb 12, 2021

I encountered this same issue but resolved it. At first I had manually created my own blank .husky/pre-commit file, and did not know I needed the file to start with the following likes:

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

Once I prepended those lines to the file it seemed to start working without issue.

@aarongoldenthal
Copy link

It works for me with just the shebang line, which I think makes it consistent with other git hooks on Windows since they run in git bash (e.g. see https://stackoverflow.com/questions/18277429/executing-git-hooks-on-windows).

@nemchik
Copy link

nemchik commented Feb 15, 2021

That would make sense, I did not try with just the shebang. I originally found this issue thinking I had the same problem when I had no shebang in the file, just had the lines I wanted to run (lint-staged).

@aarongoldenthal
Copy link

Looking again at the husky.sh contents, the command runs, but skips a lot of the husky-specific code (for disabling, sourcing .huskyrc, etc).

It does seems like the migration documentation could be a lot clearer. I started looking at the v5 usage docs, not the migration docs, and if we had all tried to use the husky add command instead of copying content it would have done all of this for us.

@lauralouiset
Copy link

Does husky v6 fix this issue?

@typicode
Copy link
Owner

typicode commented Apr 1, 2021

@lauralouiset if you create them with husky add ... or husky set ... it should work (it will create them with correct shebang and file permission). Let me know if that's not the case.

I'll update docs in migration section to make it clearer that husky add should be used.

@nemchik
Copy link

nemchik commented Apr 1, 2021

Ok, so I'm having ongoing issues with using husky (v6 at this point). I am using Windows. I have git version 2.31.0.windows.1. I primarily use git via CLI in pwsh.exe (currently version 7.1.3) from inside VSCode's (version 1.55.0 as of writing) integrated terminal.

I have installed husky:

I deleted my .husky/ folder in the root of my repo (will document how I add back what I need below). I also located my .git/hooks/ folder and left the folder in place but emptied all the files from it.
I ran

npx husky-init

I added

"prepare": "husky install"

to my package.json (husky-init might do this automatically, but I had added it before my first time using husky-init so I'm documenting here that I have it done)
I ran

npm install

I ran

npx husky set .husky/pre-commit 'npx --no-install lint-staged'
npx husky add .husky/pre-commit 'npm run build'
npx husky add .husky/pre-commit 'git add dist/.'

The first command cleared out the existing pre-commit file and created a new one and the second two commands added to the new file. The end result is this:

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

npx --no-install lint-staged
npm run build
git add dist/.

And the following file structure (seen using git bash to run ls -lahR .husky/ on the repo)

$ ls -lahR .husky/
.husky/:
total 6.0K
drwxr-xr-x 1 enemchik 1049089   0 Apr  1 13:51 ./
drwxr-xr-x 1 enemchik 1049089   0 Apr  1 13:52 ../
-rw-r--r-- 1 enemchik 1049089   2 Apr  1 13:51 .gitignore
drwxr-xr-x 1 enemchik 1049089   0 Apr  1 13:51 _/
-rwxr-xr-x 1 enemchik 1049089 100 Apr  1 13:51 pre-commit*

.husky/_:
total 1.0K
drwxr-xr-x 1 enemchik 1049089   0 Apr  1 13:51 ./
drwxr-xr-x 1 enemchik 1049089   0 Apr  1 13:51 ../
-rwxr-xr-x 1 enemchik 1049089 592 Oct 26  1985 husky.sh*

I modified a file in my repository (one that would trigger lint-staged, but that's irrelevant, it could be any file tracked by git). I then ran

git add .
git commit -m "test"

and I get the following error:

node:internal/modules/cjs/loader:927
  throw err;
  ^

Error: Cannot find module 'C:\Program Files\Git\node_modules\npm\bin\npx-cli.js'
←[90m    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:924:15)←[39m
←[90m    at Function.Module._load (node:internal/modules/cjs/loader:769:27)←[39m
←[90m    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:76:12)←[39m
←[90m    at node:internal/main/run_main_module:17:47←[39m {
  code: ←[32m'MODULE_NOT_FOUND'←[39m,
  requireStack: []
}
husky - pre-commit hook exited with code 1 (error)

I've spent hours googling this and one common thing i see people recommend is to check the PATH environment variable and the order. I do have C:\Program Files\Git\cmd and C:\Program Files\nodejs\ in my system PATH environment variable (git first, then nodejs).

Sometimes when i try to commit I will get a message that /_/husky.sh is not found, which to me looks like . "$(dirname "$0")/_/husky.sh" (in the pre-commit file) is interpreting $(dirname "$0") as nothing or blank. And sometimes I will try to commit and the terminal will sit there doing literally nothing:
image
This screenshot is from running git commit -m "test" directly after getting the Error: Cannot find module 'C:\Program Files\Git\node_modules\npm\bin\npx-cli.js' error above. So without modifying anything at all just running commit again i've got different results than the first run. I have sometimes seen the hang happen without any preceding commands. The outcome of running commits is one of the issues I've just mentioned (in summary):

  • /_/husky.sh is not found
  • Error: Cannot find module 'C:\Program Files\Git\node_modules\npm\bin\npx-cli.js' node seeming to look in the wrong path for modules
  • the terminal just hangs and does nothing (what you see in the screenshot)

In the first two cases in that list I also end up seeing a sh.exe.stackdump file generated in my repo:

Stack trace:
Frame        Function    Args
000FFFFA628  001800644A0 (000FFFFA848, 00000000002, 00000000000, 000FFFFDE50)
000FFFFDE50  001800666CC (00000000064, 00000000000, 0000000020C, 00000000000)
000FFFFAD50  0018014AEE8 (00000000000, 00000000000, 00000000000, 00000000000)
000FFFFB150  001801750E4 (00180148FDE, 0018014F75F, 000FFFFB198, 00010000000)
000FFFFB150  001801471EB (00000010004, 00000000008, 000FFFFAF00, 00000000000)
000FFFFB150  00180219CB8 (00000000000, 00000000000, 00000000001, 001005D1D38)
000FFFFB150  0018021A112 (00100425DA8, 000FFFFB100, 000FFFFB0D8, 00000000006)
000FFFFB150  001800DAA25 (00000000000, 000FFFFB51C, 00000000009, 000FFFFFFFF)
000FFFFB150  0018014F925 (0080004A580, 00000000018, 00800046160, 00000000003)
00800046160  00180150F21 (00000080000, 00010000000, 0018014226B, 00000000014)
00800046160  0018014226B (00000080000, 00010000000, 0018014226B, 00000000014)
00800046160  0010041440F (0010000001F, 000FFFFC0E0, 00180069F2C, 00000000000)
0080004A580  001004C1E4F (000FFFFFFFF, 00000000000, 00000000000, 00800049AA0)
00000000000  00100415650 (000FFFFC120, 000FFFFFFFF, 00180175158, 00800049AA0)
00000000000  0010045A0E8 (001004D1164, 00000000004, 001005D6318, 00000000001)
00000000000  0010042FB73 (001800DA94C, 000FFFFC5A0, 0018023EF60, 001004D1E00)
00800049270  0010043484E (0009999000B, 000FFFFC5A0, 00100000048, 000FFFFC48C)
00000000000  00100435B40 (00000000000, 00000000000, 00000000000, 00000000000)
5000001000000002  00100437626 (00000000007, 00000000000, 00000000001, 00800049330)
5000001000000002  0010043777F (00000000000, 00000000040, 00800048BC0, 00000000000)
5000001000000002  00100430B31 (00100000000, 00100437720, 00800048C60, 00800048C60)
00000000007  00100430FC1 (00000000000, 000FFFFC720, 000FFFFC6C8, 00000000050)
001005D6558  001004393F6 (0000000001F, 00000000012, 00000000000, 00000000000)
00800049200  001004C11EC (000FFFFFFFF, 00000000000, 000FFFFCA90, 00800048D60)
00000000000  00100415650 (001004306B0, 00000000004, 00800048C00, 00800048D60)
000FFFFCA90  001004178E4 (001004CD548, 00000000004, 00800046A60, 001005DAEA8)
000FFFFCA90  00100401E1F (00000000002, 00000000001, 00000000001, 00000000008)
000FFFFCA90  001004BFB92 (000FFFFCC70, 00800000160, 00000000030, 8080808080808080)
000FFFFCCE0  0018004AF0A (00000000000, 00000000000, 00000000000, 00000000000)
000FFFFFFF0  00180048846 (00000000000, 00000000000, 00000000000, 00000000000)
000FFFFFFF0  001800488F4 (00000000000, 00000000000, 00000000000, 00000000000)
End of stack trace

Some good news! I can replace the contents of my .husky/pre-commit file with:

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

and see the following when I commit:

> git commit -m "test"
.husky/_/husky.sh
[master 5421303] test
 1 file changed, 1 insertion(+)
>

however when I then change the exact same file I just changed (an .md file in my repo for testing commits) and rerun

git add .
git commit -m "test"

I see:

> git commit -m "test"
/_/husky.sh
[master a10c59a] test
 1 file changed, 1 deletion(-)
>

(notice the different path output for the husky.sh file)

So nothing changed but a single .md file, and one commit shows .husky/_/husky.sh while the other shows /_/husky.sh.

I honestly suspect that this may not even be an issue with husky itself, but possibly with git bash. Oddly I only started having these issues after playing with husky@5. When I was using husky@4 i did not have these issues, however if I switch my repo back to husky@4 now I have similar issues (not identical, but essentially broken in the same functional way).

I would chalk this up to a mistake I've made, and I would just assume I did something wrong, but a colleague did all the same steps documented above and has the exact same results I have. I'm open to any suggestions on how to get this resolved. For the moment our "solution" is we've got into the habit of running git commit -m "test" -n with the -n flag on the end to skip hooks, which is the opposite of what we really want.

@nemchik
Copy link

nemchik commented Apr 2, 2021

Just a small follow up, I tried https://github.com/toplenboren/simple-git-hooks as well and had similar issues, which really lead me to believe there's something going on with git bash or with my environment or configuration. Oddly though, my co-worker has the same issues (using husky) on his machine. I'm not really sure where to look to get this resolved though.

@antonis-zisis
Copy link

I was having a similar issue while using the pre-commit hook on Windows WSL (windows terminal, zsh).

This part doesn' work . "$(dirname "$0")/_/husky.sh". It works fine on macOS and outside Windows WSL though.

So I changed that to: . "$(pwd)/.husky/_/husky.sh" and it works!

I tested this on Windows WSL, Windows outside WSL, and macOS.

Not sure if I am missing some configuration in WSL, @nemchik are you getting this through Windows WSL?

Thanks

@nemchik
Copy link

nemchik commented Apr 14, 2021

The issue I am having is using cmd.exe, powershell.exe, and pwsh.exe (PowerShell 7) on Windows. When running git commit ... git's bash.exe is used to run the git hooks which seems to be where the problem is. wsl seem to work fine for me. VSCode's git UI seems to work fine as well (not sure what shell it's executing git in).

@josdejong
Copy link

I had issues too installing the latest husky (6.0.0) on Windows 10: I was following the "Usage" section in readme which says npx husky add .husky/pre-commit "npm test" and some other things, which do not work on Windows apparently.

When following the section "Usage", "Automatic" on the documentation website however all worked like a charm: npx husky-init && npm install

@stale
Copy link

stale bot commented Jul 2, 2021

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the wontfix label Jul 2, 2021
@stale stale bot closed this as completed Jul 9, 2021
@ddublon
Copy link

ddublon commented Apr 9, 2024

so what is the solution I am still having issue

@josdejong
Copy link

@ddublon have you tried #864 (comment)?

@ddublon
Copy link

ddublon commented Apr 10, 2024

@ddublon have you tried #864 (comment)?

@josdejong

I am using this template

https://github.com/suren-atoyan/react-pwa/blob/master/.husky/pre-commit

you see its the same as in the instructions

@nemchik
Copy link

nemchik commented Apr 10, 2024

It seems the newest version of husky does not require shebang at the top of the file, or including the husky script.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

9 participants