Skip to content

Lefthook doesn't work on Windows with npm -g #1165

@IagonaDevTeam

Description

@IagonaDevTeam

Description

On MacOS after doing the install of lefthook globally and having done lefthook install -f in project everything works as intended. On Windows, the other dev keeps getting: "Can't find lefthook in PATH" (Even after adding the global npm directory to Windows PATH (User And System)).

lefthook.yml

pre-commit:
commands:
pint:
run: vendor/bin/pint {staged_files}
glob: "*.php"
stage_fixed: true

Commands to reproduce

  1. Install lefthook globally (npm -g PACKAGE)
  2. Do the lefthook install (Or with -f)
  3. Try to commit something on Windows using PHPStorm or PowerShell

Lefthook version

1.13.6

Possible solution

After some digging, this pre-commit file modification seems to fix the issue. It was because the pre-commit didn't seems to use the global NPM packages. Here is the fully modified pre-commit file:

#!/bin/sh
 
if [ "$LEFTHOOK_VERBOSE" = "1" -o "$LEFTHOOK_VERBOSE" = "true" ]; then
  set -x
fi
 
if [ "$LEFTHOOK" = "0" ]; then
  exit 0
fi
 
call_lefthook()
{
  if test -n "$LEFTHOOK_BIN"
  then
    "$LEFTHOOK_BIN" "$@"
  elif lefthook -h >/dev/null 2>&1
  then
    lefthook "$@"
  else
    # FIX HERE add npm global bin to PATH (works on macOS/Linux/Windows Git Bash)
    if command -v npm >/dev/null 2>&1; then
      NPM_PREFIX=$(npm config get prefix 2>/dev/null || echo "")
      if [ -n "$NPM_PREFIX" ]; then
        case "$(uname | tr '[:upper:]' '[:lower:]')" in
          mingw*|msys*) NPM_BIN="${NPM_PREFIX}/";;
          *)            NPM_BIN="${NPM_PREFIX}/bin";;
        esac
        export PATH="$PATH:$NPM_BIN"
      fi
    fi
   #END FIX HERE
 
    dir="$(git rev-parse --show-toplevel)"
    osArch=$(uname | tr '[:upper:]' '[:lower:]')
    cpuArch=$(uname -m | sed 's/aarch64/arm64/;s/x86_64/x64/')
    if test -f "$dir/node_modules/lefthook-${osArch}-${cpuArch}/bin/lefthook"
    then
      "$dir/node_modules/lefthook-${osArch}-${cpuArch}/bin/lefthook" "$@"
    elif test -f "$dir/node_modules/@evilmartians/lefthook/bin/lefthook-${osArch}-${cpuArch}/lefthook"
    then
      "$dir/node_modules/@evilmartians/lefthook/bin/lefthook-${osArch}-${cpuArch}/lefthook" "$@"
    elif test -f "$dir/node_modules/@evilmartians/lefthook-installer/bin/lefthook"
    then
      "$dir/node_modules/@evilmartians/lefthook-installer/bin/lefthook" "$@"
    elif test -f "$dir/node_modules/lefthook/bin/index.js"
    then
      "$dir/node_modules/lefthook/bin/index.js" "$@"
    elif command -v npx >/dev/null 2>&1
    then
      npx lefthook "$@"
    elif go tool lefthook -h >/dev/null 2>&1
    then
      go tool lefthook "$@"
    elif bundle exec lefthook -h >/dev/null 2>&1
    then
      bundle exec lefthook "$@"
    elif yarn lefthook -h >/dev/null 2>&1
    then
      yarn lefthook "$@"
    elif pnpm lefthook -h >/dev/null 2>&1
    then
      pnpm lefthook "$@"
    elif swift package lefthook >/dev/null 2>&1
    then
      swift package --build-path .build/lefthook --disable-sandbox lefthook "$@"
    elif command -v mint >/dev/null 2>&1
    then
      mint run csjones/lefthook-plugin "$@"
    elif uv run lefthook -h >/dev/null 2>&1
    then
      uv run lefthook "$@"
    elif mise exec -- lefthook -h >/dev/null 2>&1
    then
      mise exec -- lefthook "$@"
    elif devbox run lefthook -h >/dev/null 2>&1
    then
      devbox run lefthook "$@"
    else
      echo "Can't find lefthook in PATH"
    fi
  fi
}
 
call_lefthook run pre-commit

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions