I try to use pre-commit
on VSCode
.
I installed ruff
, black
, mypy
and flake8
using poetry
.
Pre-commit-config.yaml
default_language_version:
python: python3.10
repos:
- repo: local
hooks:
- id: ruff
name: ruff
entry: ruff
language: python
types: [python]
args: [
--fix,
--config=app/pyproject.toml
]
- id: black
name: black
entry: black
language: python
types: [python]
args: [
--config=app/pyproject.toml
]
- id: mypy
name: mypy
entry: mypy
language: python
types: [python]
args: [
--ignore-missing-imports,
--check-untyped-defs,
--no-implicit-optional,
--config-file=app/pyproject.toml
]
- id: flake8
name: flake8
entry: flake8
language: python
types: [ python ]
args: [
--config=./app/.flake8
]
I installed pre-commit
:
pre-commit install
> pre-commit installed at .git/hooks/pre-commit
.git/hooks/pre-commit
#!/usr/bin/env bash
# File generated by pre-commit: https://pre-commit.com
# ID: 138fd403232d2ddd5efb44317e38bf03
# start templated
INSTALL_PYTHON=/Users/serg/CODING/__samolet__/lk/.venv/bin/python
ARGS=(hook-impl --config=.pre-commit-config.yaml --hook-type=pre-commit)
# end templated
HERE="$(cd "$(dirname "$0")" && pwd)"
ARGS+=(--hook-dir "$HERE" -- "$@")
if [ -x "$INSTALL_PYTHON" ]; then
exec "$INSTALL_PYTHON" -mpre_commit "${ARGS[@]}"
elif command -v pre-commit > /dev/null; then
exec pre-commit "${ARGS[@]}"
else
echo '`pre-commit` not found. Did you forget to activate your virtualenv?' 1>&2
exit 1
fi
After that I try to commit my code and get such error:
> git -c user.useConfigOnly=true commit --quiet --allow-empty-message --file -
ruff.....................................................................Failed
- hook id: ruff
- exit code: 1
Executable `ruff` not found
black....................................................................Failed
- hook id: black
- exit code: 1
Executable `black` not found
mypy.....................................................................Failed
- hook id: mypy
- exit code: 1
Executable `mypy` not found
flake8...................................................................Failed
- hook id: flake8
- exit code: 1
Executable `flake8` not found
I found a similar issue on GitHub: Pre-commit hooks executables not found. But it didn’t help me.
I tried:
pre-commit uninstall && pre-commit install
;pre-commit clean && pre-commit install-hooks
.
But unsuccessfully.
Any ideas how to fix this problem?
2
Answers
I doubt that this is an ideal solution, but in my case, installing linters globally helped. I use VSCode and make commits using the Source Control. For some reason it does not recognise (unlike the terminal) the virtual environment of the project. After installing the linters globally, the pre-commit works as it should.
you’ve written custom
repo: local
hooks (which are the escape hatch for the framework) and not configured them properlyif you use the supported repository format this would have worked without problem but you’ve bypassed the framework and are now reaping the consequences 🙂
even fixing the installation (you’d need to add
additional_dependencies: [...]
to ensure things are installed) you also have a fork bomb because you haven’t copied the configuration properlytl;dr: use the upstream repos and not
repo: local
/language: system
unless you know what you’re doing (and even then you’re doing your contributors a disservice by requiring manual setup outside the framework)disclaimer: I wrote pre-commit