lefthook icon indicating copy to clipboard operation
lefthook copied to clipboard

Lefthook doesn't find executables with Desktop Git Client - npx: not found

Open bennycode opened this issue 1 year ago • 7 comments

: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 nvm to 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 avatar Dec 06 '24 12:12 bennycode

@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.

mrexox avatar Dec 06 '24 12:12 mrexox

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

image

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)

image

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.

bennycode avatar Dec 06 '24 13:12 bennycode

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.

mrexox avatar Dec 09 '24 09:12 mrexox

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.

mrexox avatar Jan 17 '25 13:01 mrexox

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 avatar Feb 03 '25 10:02 CRC32EX

@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 avatar Mar 25 '25 10:03 bennycode

@bennycode so, it works fine when you start SmartGit from the WSL? Probably because you have the PATH adjusted only in WSL 🤔

mrexox avatar Mar 25 '25 14:03 mrexox