Husky Pre-Commit Hook Ver 9.0+ Will Error when committing in Visual Studio 2022, but not in VSCode or Git CMD Line

  Kiến thức lập trình

Problem and Background

I’m encountering an issue with Husky pre-commit hooks in a multi-language repository that contains both a C# .NET Core project and a React app. The .git directory is located in the root directory, while the Node.js project is in a subdirectory (client-app).

I am getting the following error when I attempt to commit in the Git Changes window in Visual Studio 2022:

enter image description here

Oddly it commits fine if I am in VSCode or using Git CMD Line in MS Terminal.

I tried looking at similar issues in the past such as:



  3. Visual studio not happy when commiting on a repo where Husky hooks are present

I tried incorporating these troubleshooting steps indetified above but with out any success.

For more context this is my current project structure:

├── .git/
├── client-app/
│   ├── node_modules/
│   ├── src/
│   ├── package.json
│   └── .husky/
│       └── pre-commit
└── C# .NET Core project files

These are some of the additional troubleshooting steps I tried to get this working Visual Studio 2022:

Initial Troubleshooting Steps

  1. Verify Node.js Installation:

    • Confirmed that Node.js and npm are installed and accessible globally.
    node -v
    npm -v
  2. Update .husky/pre-commit Script:

    • Updated the pre-commit script to set the correct PATH and explicitly call Node.js executables.
    # Ensure the PATH includes the correct directories for node and npx
    export PATH="/mnt/c/Program Files/nodejs:/mnt/c/Program Files/Git/usr/bin:$PATH"
    echo "Running pre-commit hook"
    echo "Node version: $(/mnt/c/Program Files/nodejs/node -v)"
    echo "Npx version: $(/mnt/c/Program Files/nodejs/npx -v)"
    echo "PATH: $PATH"
    /mnt/c/Program Files/nodejs/npx lint-staged
  3. Convert Line Endings:

    • Ensured that the script has Unix-style line endings using dos2unix.
  4. Target client-app Directory in lint-staged Configuration:

    • Updated the lint-staged configuration in client-app/package.json to only include files from the client-app directory.
      "lint-staged": {
        "client-app/src/**/*.{js,jsx,ts,tsx}": [
          "prettier --write",
          "npm run lint",
          "npm run test"
  5. Change Directory in Pre-commit Script:

    • Ensured the npx lint-staged command runs within the client-app directory.
    cd client-app
    npx lint-staged

Remaining Issue

Despite all of this, the pre-commit hook still fails to recognize the Node.js path correctly. Any guidance on resolving this issue would be greatly appreciated.