pyright-python icon indicating copy to clipboard operation
pyright-python copied to clipboard

Pre-commit install hook doesn't actually install Pyright

Open Timmmm opened this issue 2 years ago • 5 comments

Pyright is only actually installed on the first use. This makes installing Pyright in a docker image so you avoid re-downloading it for every CI run (and depending on unreliable networks / github servers) difficult.

Here's what we have to do currently:

RUN python3 -m pip install --no-cache-dir pre-commit

# Make a git repo with a pre-commit config that we can use to pre-install the
# hook tools so they aren't installed on every run.
WORKDIR /opt/repo

# Add the pre-commit config (this just contains the hooks we use).
COPY .pre-commit-config.yaml .

# Pre-install tools used by the pre-commit hooks we have so that we don't
# have to install them on every CI run. pre-commit requires us to be in a git
# repo.
RUN git init --quiet && pre-commit install-hooks --config .pre-commit-config.yaml

# Unfortunately the Pyright hook does not *actually* install Pyright when you
# use install-hooks so we have to run it on some Python.
RUN printf '"""\nTest\n"""\n\nprint("hello")\n' > main.py && \
    git config --global user.email "[email protected]" && \
    git config --global user.name "Your Name" && \
    git add . && \
    git commit -m "Add python script" && \
    pre-commit run -a

# This prevents the Pyright hook from accessing the network.
ENV PYRIGHT_PYTHON_IGNORE_WARNINGS=1

It's a mild annoyance. (Also slightly annoying that pre-commit forces you to make a git repo to install things.)

Timmmm avatar Oct 27 '23 08:10 Timmmm

Are you just looking to trigger an install without typechecking? Does #178 help?

Kache avatar Nov 09 '23 11:11 Kache

Yes, but unfortunately that doesn't help because if you run pyright via pre-commit it isn't actually installed for the user, so when I run pyright --help it just says pyright: not found.

As far as I understand it, behind the scenes pre-commit is just running pip3 install pyright, so I guess you could either arrange for pip to run pyright --help (effectively) after install (which looks annoying) or maybe pre-commit should be able to run a post-install command (it can't at the moment).

Timmmm avatar Nov 09 '23 13:11 Timmmm

i raised #231 which would solve this problem

DetachHead avatar Nov 15 '23 04:11 DetachHead

Yes, but unfortunately that doesn't help because if you run pyright via pre-commit it isn't actually installed for the user, so when I run pyright --help it just says pyright: not found.

As far as I understand it, behind the scenes pre-commit is just running pip3 install pyright, so I guess you could either arrange for pip to run pyright --help (effectively) after install (which looks annoying) or maybe pre-commit should be able to run a post-install command (it can't at the moment).

Your comment inspired me, and I found a workaround.

COPY .pre-commit-config.yaml .
SHELL ["/bin/bash", "-c"]
RUN git init . && \
    pre-commit install-hooks && \
    eval "\$(find ~/.cache/pre-commit | grep 'bin/pyright\$') --help"

acefei avatar Jan 19 '24 08:01 acefei

i've created a fork of pyright that fixes this issue: https://github.com/detachhead/basedpyright

it includes all of the node dependencies as part of the pypi package, so it doesn't need to install anything on its initial run.

# .pre-commit-config.yaml

repos:
  - repo: https://github.com/DetachHead/basedpyright
    rev: v1.8.0
    hooks:
    - id: basedpyright

DetachHead avatar Jan 19 '24 14:01 DetachHead