简体   繁体   中英

Pre-commit error: cannot spawn .git/hooks/pre-commit: No such file or directory

I think this is a Windows/WSL/Linux issue. anyone able to assist as when i install pre-commit and try to commit i get the error:

> git -c user.useConfigOnly=true commit --quiet --allow-empty-message --file -
error: cannot spawn .git/hooks/pre-commit: No such file or directory

all software and actions are taken on wsl2 or vscode terminal (which connects to wsl)

  • git clone (I can commit, branch etc fine from vscode)
  • pre-commit run -a runs succesfully
  • pre-commit install (says setup is successful)
  • python3 is installed and working on wsl

looking in the .git i can see the hooks folder and the pre-commit file which is below:

#!/home/linuxbrew/.linuxbrew/opt/pre-commit/libexec/bin/python3
# File generated by pre-commit: https://pre-commit.com
# ID: 138fd403232d2ddd5efb44317e38bf03
import os
import sys

# we try our best, but the shebang of this script is difficult to determine:
# - macos doesn't ship with python3
# - windows executables are almost always `python.exe`
# therefore we continue to support python2 for this small script
if sys.version_info < (3, 3):
    from distutils.spawn import find_executable as which
else:
    from shutil import which

# work around https://github.com/Homebrew/homebrew-core/issues/30445
os.environ.pop('__PYVENV_LAUNCHER__', None)

# start templated
INSTALL_PYTHON = '/home/linuxbrew/.linuxbrew/opt/pre-commit/libexec/bin/python3'
ARGS = ['hook-impl', '--config=.pre-commit-config.yaml', '--hook-type=pre-commit']
# end templated
ARGS.extend(('--hook-dir', os.path.realpath(os.path.dirname(__file__))))
ARGS.append('--')
ARGS.extend(sys.argv[1:])

DNE = '`pre-commit` not found.  Did you forget to activate your virtualenv?'
if os.access(INSTALL_PYTHON, os.X_OK):
    CMD = [INSTALL_PYTHON, '-mpre_commit']
elif which('pre-commit'):
    CMD = ['pre-commit']
else:
    raise SystemExit(DNE)

CMD.extend(ARGS)
if sys.platform == 'win32':  # https://bugs.python.org/issue19124
    import subprocess

    if sys.version_info < (3, 7):  # https://bugs.python.org/issue25942
        raise SystemExit(subprocess.Popen(CMD).wait())
    else:
        raise SystemExit(subprocess.call(CMD))
else:
    os.execvp(CMD[0], CMD)

UPDATE

Its a VSCode issue.. doing a manual vscode commit uses windows git and so looks for python3 in windows. If i use the wsl terminal and run the git commands they work and use pre-commit

Guess my question is, is there a way to use vscode git with wsl or does it have to use windows?

@Staggerlee011, looking at your script, it seems that you are missing the #!/bin/sh at the top of your script. Adding this to my pre-commit script, saving, and running a commit was successful. To be honest, I did not know that could be the solution, because it looks like a comment, but it did. Thinking about it though, it seems that Git recognizes it, and processes it.

OK!

So this is purely a vscode / wsl integration issue and nothing to do with pre-commit .

Its caused by vscode using windows git, While i installed pre-commit and all the tools for it on wsl .

So you can make VSCode use WSL git repos and apps via using the extension: wsl - remote

you might be able to also update the settings.json with git.path: but i havent tested that!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM