Lefthook doesn't find executables with Desktop Git Client - npx: not found
:wrench: Summary
When I try to push with my Git Desktop client (SmartGit) then lefthook doesn't find npx. If I push from within VS Code, it works.
Here is my lefthook.yml:
pre-commit:
parallel: true
commands:
format:
glob: '*.{css,html,json,less,md,scss,yml}'
run: npx -c "cross-env NODE_OPTIONS=--max_old_space_size=4096 prettier --write --log-level error {staged_files}"
lint:
glob: '*.{js,jsx,ts,tsx}'
run: npx eslint {staged_files} --fix
Lefthook version
v1.9.0
Steps to reproduce
- Use a Git Desktop client (like SmartGit)
- Use
nvmto install and run Node.js
Expected results
Lefthook finding npx and other tools
Actual results
Lefthook failing to find npx:
\u2503 lint \u276f sh: 1: npx: not found exit status 127\u2503 format \u276f sh: 1: npx: not found exit status 127
Possible Solution
- There was a workaround here: https://github.com/evilmartians/lefthook/issues/178#issuecomment-959862247
- Apparently it was fixed with https://github.com/evilmartians/lefthook/pull/391 by @mrexox but it doesn't seem like it
@bennycode , do you start VSCode from terminal? It's surprising that it works without any extra configuration.
Lefthook inherits envs from the environment it was called from. If it's your terminal, you usually have all envs, but with a desktop app you probably miss some PATH adjustments.
Please, use rc option if you use some desktop app with lefthook (you can put it to lefthook-local.yml, see examples)
Fast fix: add a rc file with the line export PATH=$PATH:<path where npx lives>
which npx
But better solution is to repeat your shell rc file adjustments.
Hi @mrexox and thank you for your fast feedback. It helped me to narrow down the problem...
I am using Windows 11 and the Windows Subsystem for Linux. My SmartGit is installed in the WSL as it supports GUI applications. There is indeed a difference in how I can start my SmartGit desktop client...
Starting from Start Menu
If I run SmartGit from the Windows start menu, then the commit in combination with Lefthook fails as it cannot find npx:
sh: 1: npx: not found
Starting from Ubuntu App (Terminal)
When I initially launch my terminal using the Ubuntu app on Windows and run SmartGit (~/smartgit/bin/smartgit.sh), I don't encounter any issues with Lefthook. However, it seems that Lefthook is not executed at all on a pre-commit.
Actually, when committing through VS Code (launched from my Start menu), the pre-commit hook is not executed either.
I see. Well, in this case I can suggest you to install lefthook via winget, so it is available from anywhere (does it work for Winget like this?).
This is the first time I meet your use case and it's interesting if installation via winget can fix this.
How do you install lefthook on your system?
I've recently added a lefthook option which allows you to specify the full path to lefthook executable. You can create a lefthook-local.yml with the following content:
lefthook: /usr/bin/lefthook # you may need to determine this location
I hope this helps. Using lefthook with desktop tools always requires to make sure that you don't depend on your shell environment.
lefthook is not Windows friendly.
So, Just use scripts.
lefthook.yml
pre-commit:
scripts:
"hello.sh":
runner: bash
.lefthook\pre-commit\hello.sh
echo "hello"
@CRC32EX thank you for the tip! I tried it using a Shell script but I am getting the same error. With a shell script I also didn't find an opportunity to make use of {staged_files}.
@mrexox I added lefthook: /usr/bin/lefthook to my lefthook.yml but it didn't help. I am using lefthook v1.11.4 locally installed in my project.
I am currently making sure that I start SmartGit from my WSL Ubuntu App Terminal instead of the starting menu.
@bennycode so, it works fine when you start SmartGit from the WSL? Probably because you have the PATH adjusted only in WSL 🤔