I've dockerized my project and I want to be able to execute pre-commit hooks within the container. This currently work for everything except pyright.
Here's my .pre-commit-config.yaml file:
repos:
- repo: local
hooks:
- id: pyright
name: pyright
entry: scripts/type-check.sh
language: system
types: [python]
pass_filenames: true
Here's my scripts/type-check.sh:
#!/bin/bash -ue
if [[ -n $* ]]; then
# Don't attempt to source a virtualenv when you're running inside the container.
# The correct environment is the global environment in that case.
if ! type -f /.dockerenv >/dev/null 2>&1; then
echo "Attempting to activate a virtualenv if present..."
# Active virtual environment (check several possibles places), it helps if the script
# is executed from PyCharm (e.g. configuring it as an External Tool) or
# with GitHub Desktop
# shellcheck disable=SC1090
test -f venv/bin/activate && source "$_"
# shellcheck disable=SC1090
test -f .venv/bin/activate && source "$_"
# shellcheck disable=SC1090
test -f env/bin/activate && source "$_"
else
pyright "$@"
fi
As you can see, the reason we're using a script is because our developers tend to use a name for their virtualenv that they personally like the best, so we can't use a hook like: repo: https://github.com/RobertCraigie/pyright-python directly because it forces us to hardcode the venv name. However, this setup works fine locally and should in theory be fine in the container too.
Instead we get this error:
Running inside the container, skipping virtualenv activation.
* Environment already exists: /root/.cache/pyright-python/nodeenv
Traceback (most recent call last):
File "/usr/local/bin/pyright", line 8, in <module>
sys.exit(entrypoint())
File "/usr/local/lib/python3.8/site-packages/pyright/cli.py", line 34, in entrypoint
sys.exit(main(sys.argv[1:]))
File "/usr/local/lib/python3.8/site-packages/pyright/cli.py", line 19, in main
return run(*args, **kwargs).returncode
File "/usr/local/lib/python3.8/site-packages/pyright/cli.py", line 25, in run
pkg_dir = install_pyright(args, quiet=None)
File "/usr/local/lib/python3.8/site-packages/pyright/_utils.py", line 64, in install_pyright
node.run(
File "/usr/local/lib/python3.8/site-packages/pyright/node.py", line 105, in run
binary = _ensure_available(target)
File "/usr/local/lib/python3.8/site-packages/pyright/node.py", line 38, in _ensure_available
return Binary(path=_ensure_node_env(target), strategy=Strategy.NODEENV)
File "/usr/local/lib/python3.8/site-packages/pyright/node.py", line 65, in _ensure_node_env
_install_node_env()
File "/usr/local/lib/python3.8/site-packages/pyright/node.py", line 98, in _install_node_env
subprocess.run(args, check=True)
File "/usr/local/lib/python3.8/subprocess.py", line 516, in run
raise CalledProcessError(retcode, process.args,
subprocess.CalledProcessError: Command '['/usr/local/bin/python', '-m', 'nodeenv', '/root/.cache/pyright-python/nodeenv']' returned non-zero exit status 2.
For reference, here's what's installed in the container:
I've dockerized my project and I want to be able to execute
pre-commit
hooks within the container. This currently work for everything exceptpyright
.Here's my
.pre-commit-config.yaml
file:Here's my
scripts/type-check.sh
:As you can see, the reason we're using a script is because our developers tend to use a name for their virtualenv that they personally like the best, so we can't use a hook like:
repo: https://github.com/RobertCraigie/pyright-python
directly because it forces us to hardcode thevenv
name. However, this setup works fine locally and should in theory be fine in the container too.Instead we get this error:
For reference, here's what's installed in the container: