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:
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:
-
https://github.com/typicode/husky/issues/1038
-
https://github.com/typicode/husky/issues/673
-
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:
root/
│
├── .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
-
Verify Node.js Installation:
- Confirmed that Node.js and npm are installed and accessible globally.
node -v npm -v
-
Update
.husky/pre-commit
Script:- Updated the pre-commit script to set the correct PATH and explicitly call Node.js executables.
#!/bin/sh # 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
-
Convert Line Endings:
- Ensured that the script has Unix-style line endings using
dos2unix
.
- Ensured that the script has Unix-style line endings using
-
Target
client-app
Directory inlint-staged
Configuration:- Updated the
lint-staged
configuration inclient-app/package.json
to only include files from theclient-app
directory.
{ "lint-staged": { "client-app/src/**/*.{js,jsx,ts,tsx}": [ "prettier --write", "npm run lint", "npm run test" ] } }
- Updated the
-
Change Directory in Pre-commit Script:
- Ensured the
npx lint-staged
command runs within theclient-app
directory.
#!/bin/sh cd client-app npx lint-staged
- Ensured the
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.