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

Desktop fails to run pre-commit hooks on node 16+ #12562

Open
ImRodry opened this issue Jul 3, 2021 · 40 comments
Open

Desktop fails to run pre-commit hooks on node 16+ #12562

ImRodry opened this issue Jul 3, 2021 · 40 comments
Labels
windows Issues specific Desktop usage on Windows

Comments

@ImRodry
Copy link

ImRodry commented Jul 3, 2021

Describe the bug

On Windows systems with WSL installed, GitHub desktop will run pre-commit scripts on WSL instead of running them in PowerShell regardless of which shell the user has picked to use. This results in the scripts looking for windows locations that don't exist on WSL.

Version & OS

Version 2.9.0 on Windows 10 21H1 build 19043.1081 running with WSL 2 on Ubuntu 20.04

Steps to reproduce the behavior

  1. Make some changes to a file in a project with a pre-commit script (I'm using the npm package husky which installs these)
  2. Add a commit message and try to commit
  3. You should get the following error

Expected behavior

GitHub desktop runs the pre-commit scripts on windows PowerShell, just like git does (tested this)

Actual behavior

The scripts are run on WSL which causes errors

Screenshots

image
(this path is correct for my system but may be different on others)

Logs

This log has the error multiple times, which should help.
2021-07-03.desktop.production.log

Additional context

Got this error on the repositories discordjs/discord.js and discordjs/builders (forks of them) so you can try committing on these to see if you get the same errors. I also believe this issue should have relatively high priority since I am incapable of committing through my PC (unless I use git, which is more limited on some things and harder for me)

@say25 say25 added the windows Issues specific Desktop usage on Windows label Aug 23, 2021
@steveward
Copy link
Member

@ImRodry thanks for reporting this. GitHub Desktop ships with an internal version of Git which we use for all actions inside the app. As such the environment within which your commit hooks execute will likely not exactly match that of your normal environment.

You can take a look at #9351 and see if the workaround in #9351 (comment) is applicable to your situation. One user also had success by changing the husky command from "pre-commit": "lint-staged" to"pre-commit": "npx lint-staged".

This comment and this comment also have some potential workarounds for this issue.

Let me know if any of that helps.

@ImRodry
Copy link
Author

ImRodry commented Sep 9, 2021

@steveward thanks for the response. I tried all of the suggestions you linked (some didn't apply to my scenario though) and none worked nor did they produce a different kind of error. The error is still the same as the one I attached in the initial message :/

@JosefBredereck
Copy link

JosefBredereck commented Nov 12, 2021

I get a similar error described in #13307

But for me, it's bash that fails:

/usr/bin/env: ‘bash’: No such file or directory
husky - pre-commit hook exited with code 127 (error)

Additional Info: I have now installed node V14.17.1, and it is working without errors. This issue seems to be connected to the applied version.

@nk-o
Copy link

nk-o commented Nov 17, 2021

@JosefBredereck same here, after updating node v14 to the latest v16 I see this error from Husky + Lint Staged:
image

@zprjk
Copy link

zprjk commented Dec 10, 2021

Similar problem here also. Win10 + Husky v7 + Github Desktop

Not running through WSL but I have Git Bash installed.

@cdaycmc
Copy link

cdaycmc commented Jan 4, 2022

I upgraded to Node v16 and husky stopped working with the same error as above. I then downgraded to v14 of Node and it worked again.

v16.13.1 x64 didn't work (latest LTS)
v14.18.2 x64 did work

@ImRodry ImRodry changed the title Desktop tries to run pre-commit scripts on WSL Desktop fails to run pre-commit hooks on node 16+ Jan 5, 2022
@CameronJThomas
Copy link

I had a parade of issues with Husky 7 and Node 16 on GitHub Desktop, which I resolved by:

  1. Uninstalling WSL (or, if you use it, you can install a distro).
  2. Adding C:\Program Files\Git\bin to my system PATH.
  3. Running git config stash.usebuiltin false in my repository directory.

At last it is working. The fault seems to lie somewhere between Node 16, and Desktop insisting on using its built in version of Git. 🤷

@Airkro
Copy link

Airkro commented Jan 7, 2022

Ugly but work:

npm -> npm.cmd
npx -> npx.cmd

@vtabashniuk
Copy link

Ugly but work:

npm -> npm.cmd
npx -> npx.cmd

helped. Thanks

@m-richards
Copy link

Have just run into the same underlying WSL on windows issue trying to use pre-commit (https://github.com/pre-commit/pre-commit), which generates this pre-commit hook after running pre-commit install. INSTALL_PYTHON is generated based upon the environment pre-commit install is run in. But when github desktop runs this on wsl that path is invalid so the script fails.

#!/usr/bin/env bash
# File generated by pre-commit: https://pre-commit.com
# ID: 138fd403232d2ddd5efb44317e38bf03

# start templated
INSTALL_PYTHON='c:\program files\python39\python.exe'
ARGS=(hook-impl --config=.pre-commit-config.yaml --hook-type=pre-commit)
# end templated
if [ -x "$INSTALL_PYTHON" ]; then
...

@bschaeublin
Copy link

bschaeublin commented Jan 27, 2022

I also get:

/usr/bin/env: ‘bash’: No such file or directory
husky - pre-commit hook exited with code 127 (error)

To:

Ugly but work:

npm -> npm.cmd
npx -> npx.cmd

This is more like a heavy workaround and it gets reverted automatically after updateing I guess.

@steveward
It is really weird, that github desktop does not support pre-commit hooks, as it is an essential thing for a git client.
What could github desktop do, to support it? Use Powershell? Let the user configure an alternative git bash?

jamiegluk added a commit to jamiegluk/jamiegl that referenced this issue Jan 28, 2022
This reverts commit d0f3b6d.

- Experiencing issues with commits in GitHub desktop.
- Issues appear to have no non-hacky fix.
- So reverting changes.
- see typicode/husky#1072
- see desktop/desktop#12562
- see desktop/desktop#9351
@manzoorwanijk
Copy link

Any update on this?

@Metsuryu
Copy link

I had a parade of issues with Husky 7 and Node 16 on GitHub Desktop, which I resolved by:

1. Uninstalling WSL (or, if you use it, you can install a distro).

2. Adding `C:\Program Files\Git\bin` to my system PATH.

3. Running `git config stash.usebuiltin false` in my repository directory.

At last it is working. The fault seems to lie somewhere between Node 16, and Desktop insisting on using its built in version of Git. 🤷

It worked, thanks.

@lovelongyuan
Copy link

能不能尽快解决

@lovelongyuan
Copy link

/usr/bin/env: ‘bash’: No such file or directory
husky - pre-commit hook exited with code 127 (error)

@johannesschobel
Copy link

I have the same issue:
I run a windows computer, with wsl2 (ubuntu). All development stuff (i.e., VSCode, development workspaces, ...) are installed within the WSL.
However, GitHub Desktop is installed on my regular windows computer.
I use husky-hooks to trigger special commands (i.e., npx do-fancy-stuff on pre-push or others).

I also get the error

/bin/bash: C:/Program Files/nodejs/npx: No such file or directory
husky - pre-commit hook exited with code 127 (error)

It seems that the pre-commit hook (which is executed within WSL) tries to access nodejs on my regular computer.

The solution proposed by @CameronJThomas does not work for me, the path to GIT is already set in my PATH variables (i.e., when i call PATH on the cmd).

Any other solutions?
All the best,
Johannes

@wilddeer
Copy link

wilddeer commented May 6, 2022

I've just installed everything from scratch on a fresh system (WIn 11) and I get the same issue.

image

Node v16.15.0
NPM 8.8.0

@johannesschobel
Copy link

@wilddeer try installing node14.x on your windows computer in order to get rid of this error. node14.x will show the real error (if any)

@gabe-piltzer-zendesk
Copy link

I have this issue on macOS Monterey v12.6, node v18.7.0, GitHub Desktop v3.1.1

Is this being looked at for a bug fix in GitHub Desktop or are people just expected to find different workarounds?

@MarisLorencsEmergn
Copy link

I had a parade of issues with Husky 7 and Node 16 on GitHub Desktop, which I resolved by:

  1. Uninstalling WSL (or, if you use it, you can install a distro).
  2. Adding C:\Program Files\Git\bin to my system PATH.
  3. Running git config stash.usebuiltin false in my repository directory.

At last it is working. The fault seems to lie somewhere between Node 16, and Desktop insisting on using its built in version of Git. 🤷

Step 2 resolved the issue for me.

My system:

  • Windows 10 20H2
  • Node 16.16.0
  • Git 2.31.1.windows.1
  • GitHub Desktop 3.1.2

cloydlau added a commit to cloydlau/vue-global-config that referenced this issue Nov 27, 2022
cloydlau added a commit to cloydlau/minimce that referenced this issue Nov 27, 2022
cloydlau added a commit to cloydlau/admate that referenced this issue Nov 27, 2022
cloydlau added a commit to cloydlau/coord-picker that referenced this issue Nov 27, 2022
cloydlau added a commit to cloydlau/json-editor-vue that referenced this issue Nov 27, 2022
cloydlau added a commit to cloydlau/faim that referenced this issue Nov 27, 2022
cloydlau added a commit to cloydlau/webgl-fluid that referenced this issue Nov 27, 2022
@Othmane-ElAlami
Copy link

Othmane-ElAlami commented Dec 2, 2022

Adding the following to my system path (in this order)

C:\Program Files\Git\cmd
C:\Program Files\Git\bin

and renaming C:\Windows\System32\bash.exe (which I don't use or need) to C:\Windows\System32\bash-wsl.exe.

This is the only solution that worked for me. Thanks a lot.

@Puyodead1
Copy link

I hit this problem. I believe it started after I upgraded from node 14 to node 16. On my system it initially manifested as this error:

Windows Subsystem for Linux has no installed distributions.
Distributions can be installed by visiting the Microsoft Store:
https://aka.ms/wslstore
husky - pre-commit hook failed (add --no-verify to bypass)

After installing a WSL distribution, this turned into this error:

/bin/bash: C:/Program Files/nodejs/npm: No such file or directory

husky - pre-commit hook failed (add --no-verify to bypass)

I've tried most of the things in this thread, without much success.

What has finally worked for me has been the following combination:

Adding the following to my system path (in this order)

C:\Program Files\Git\cmd
C:\Program Files\Git\bin

and renaming C:\Windows\System32\bash.exe (which I don't use or need) to C:\Windows\System32\bash-wsl.exe.

For this last step, I needed TrustedInstaller privileges to do this, which I achieved using a tool called Process Hacker.

This has been driving me nuts, I was editing the pre-commit hook to change npx -> npx.cmd and just not staging it but that was getting very annoying. This was the only thing that worked. Thank you!

@maitrakhatri
Copy link

Adding C:\Program Files\Git\bin to System Variables worked for me

@FabianMontoya
Copy link

I had a parade of issues with Husky 7 and Node 16 on GitHub Desktop, which I resolved by:

  1. Uninstalling WSL (or, if you use it, you can install a distro).
  2. Adding C:\Program Files\Git\bin to my system PATH.
  3. Running git config stash.usebuiltin false in my repository directory.

At last it is working. The fault seems to lie somewhere between Node 16, and Desktop insisting on using its built in version of Git. 🤷

The second works for me.

System Enviroment Variables -> Path -> New -> Add C:\Program Files\Git\bin -> Save

Close Github Desktop, an open again and all works.

@ebesscore
Copy link

Ugly but work:

npm -> npm.cmd
npx -> npx.cmd

Please on which file am I supposed to edit these?

@henryzeo
Copy link

henryzeo commented Mar 1, 2023

just add C:\Program Files\Git\bin to System Variables, place it after C:\Program Files\Git\cmd, then everything is fine
my system: Windows 11 21h2 node: 16.15.0 LTS git: 2.36.0.windows.1 github desktop: 3.0.0 x64

This did NOT work with git: 2.33.1.windows.1, github Desktop: 3.0.5 (x64), windows 11 pro: 10.0.22000 Build 22000

It works, Thanks
OS: Windows 11 22H2 Husky: v8.0.0 Git: version 2.38.1.windows.1 GitHub Desktop: Version 3.1.7 (x64)

@Ab2nour
Copy link

Ab2nour commented Mar 2, 2023

Ugly but work:

npm -> npm.cmd
npx -> npx.cmd

Please on which file am I supposed to edit these?

@ebesscore
The file will typically be located at your_project/.husky/pre-commit
pre-commit is the name of the file (it has no extension).

@Geczy
Copy link

Geczy commented Mar 12, 2023

this worked for me, but will it break on unix machines when they fork?

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

npx.cmd lint-staged

@Ab2nour
Copy link

Ab2nour commented Mar 12, 2023

@Geczy

It should break on UNIX machines with a message like "npx.cmd: command not found".

I addressed this issue in my reply here (so it works on both UNIX and Windows machines):
#12562 (comment)

@1071125093
Copy link

MARK

@badJaguar
Copy link

badJaguar commented May 16, 2023

@Puyodead1
Worked for me!
React 18+,
Vite 4.2.0,
yarn monorepo

@mahsarajabpour
Copy link

mahsarajabpour commented Jun 11, 2023

This worked for me:
run this command npx husky-init
typicode/husky#944 (comment)

@Geczy
Copy link

Geczy commented Jan 6, 2024

slight optimizations, but the original answer helped

Use $(...) notation instead of legacy backticks ...

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

case $(uname) in
*CYGWIN* | *MINGW* | *MSYS*)
  npx.cmd lint-staged # bug on Windows/Github Desktop: add `.cmd` extension fixes it
  ;;
*)
  npx lint-staged
  ;;
esac

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
windows Issues specific Desktop usage on Windows
Projects
None yet
Development

No branches or pull requests