running pre-commit python package in Windows gives ExecutableNotFoundError: Executable `/bin/sh`

Question:

I am working on a project where pre-commit==2.15.0 was added to the python requirements file. I installed the requirements. Now when I try to do a git commit I get the following error:

An unexpected error has occurred: ExecutableNotFoundError: Executable `/bin/sh` not found
Check the log at C:Usersusername.cachepre-commitpre-commit.log

In my pre-commit log I have:

pre-commit version: 2.15.0
sys.version:
    3.9.0 (tags/v3.9.0:9cf6752, Oct  5 2020, 15:34:40) [MSC v.1927 64 bit (AMD64)]
sys.executable: c:usersusernameappdatalocalprogramspythonpython39python.exe
os.name: nt
sys.platform: win32

Traceback (most recent call last):
  File "c:usersusernameappdatalocalprogramspythonpython39libsite-packagespre_commiterror_handler.py", line 65, in error_handler
    yield
  File "c:usersusernameappdatalocalprogramspythonpython39libsite-packagespre_commitmain.py", line 368, in main
    return hook_impl(
  File "c:usersusernameappdatalocalprogramspythonpython39libsite-packagespre_commitcommandshook_impl.py", line 231, in hook_impl
    retv, stdin = _run_legacy(hook_type, hook_dir, args)
  File "c:usersusernameappdatalocalprogramspythonpython39libsite-packagespre_commitcommandshook_impl.py", line 42, in _run_legacy
    cmd = normalize_cmd((legacy_hook, *args))
  File "c:usersusernameappdatalocalprogramspythonpython39libsite-packagespre_commitparse_shebang.py", line 82, in normalize_cmd
    exe = normexe(cmd[0])
  File "c:usersusernameappdatalocalprogramspythonpython39libsite-packagespre_commitparse_shebang.py", line 61, in normexe
    _error('not found')
  File "c:usersusernameappdatalocalprogramspythonpython39libsite-packagespre_commitparse_shebang.py", line 51, in _error
    raise ExecutableNotFoundError(f'Executable `{orig}` {msg}')
pre_commit.parse_shebang.ExecutableNotFoundError: Executable `/bin/sh` not found

I work in Windows where my teammates work on Macs.

It looks like precommit is trying to reference the /bin/sh script which is not in Windows. How do I get this precommit working?

Asked By: afriedman111

||

Answers:

your previous git hook is using a non-portable shebang (#!/bin/sh) (in your case this file will be located at .git/hooks/pre-commit.legacy — originally .git/hooks/pre-commit)

if you adjust the tool to use #!/usr/bin/env sh then pre-commit will be able to run it (even on windows)

alternatively, if you don’t want to use pre-commit in migration mode run pre-commit install --force

you’re also using an outdated version of pre-commit which may be contributing to your issues — so I’d recommend upgrading that was well


disclaimer: I created pre-commit

Answered By: anthony sottile