Search code examples
gitvisual-studiovisual-studio-codehusky

Visual studio cannot run pre/post commit hooks: /usr/bin/env 'bash': No such file or directory


We have an existing git repository with a .Net solution. We are adding a web application to the mix. The web application(angular) is in a specific subfolder.

We did install some pre-post commit hooks, to ensure the file is properly linted. Everything works fine when committing with VS Code or Source tree. But when I try to commit something directly from Visual Studio, I get this error:

/usr/bin/env: 'bash': No such file or directory

Is there a way to make it work?

For reference, here are my hooks:

post-checkout

#!/bin/sh
command -v git-lfs >/dev/null 2>&1 || { echo >&2 "\nThis repository is configured for Git LFS but 'git-lfs' was not found on your path. If you no longer wish to use Git LFS, remove this hook by deleting '.git/hooks/post-checkout'.\n"; exit 2; }
git lfs post-checkout "$@"

post-commit

#!/bin/sh
command -v git-lfs >/dev/null 2>&1 || { echo >&2 "\nThis repository is configured for Git LFS but 'git-lfs' was not found on your path. If you no longer wish to use Git LFS, remove this hook by deleting '.git/hooks/post-commit'.\n"; exit 2; }
git lfs post-commit "$@"

post-merge

#!/bin/sh
command -v git-lfs >/dev/null 2>&1 || { echo >&2 "\nThis repository is configured for Git LFS but 'git-lfs' was not found on your path. If you no longer wish to use Git LFS, remove this hook by deleting '.git/hooks/post-merge'.\n"; exit 2; }
git lfs post-merge "$@"

cd ./Src/Frontend
npx git-pull-run --pattern "package-lock.json" --command "npm install"

pre-commit

#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

cd ./Src/Frontend
npm run lint

pre-push

#!/bin/sh
command -v git-lfs >/dev/null 2>&1 || { echo >&2 "\nThis repository is configured for Git LFS but 'git-lfs' was not found on your path. If you no longer wish to use Git LFS, remove this hook by deleting '.git/hooks/pre-push'.\n"; exit 2; }
git lfs pre-push "$@"

There has to be a way of using to pre/post commit hooks while still being able to commit in VS Code and VS, right? What should I do?


Solution

  • Check your %PATH% first, which should include C:\Program Files\Git\bin, where bash.exe resides.

    Or modify, for testing, the shebang directive with:

    #!C:/Program\ Files/Git/git-bash.exe
    

    J4v4scr1pt adds in the comments:

    • having found how to set environment variables

    • adding what typicode/husky issue 1038 suggests

      Just add C:\Program Files\Git\usr\bin\cygpath.exe to you PATH environment variable and it should work perfectly

    • stating:

      I added "C:\Program Files\Git\usr\bin\" to the "User variables for userName" where "/usr/" did the trick.
      And I needed to restart VS.