VSCode integrated source control and pre-commit
Asked Answered
A

3

25

When using https://pre-commit.com with VSCode hooks that depend on packages installed in a Python venv. In pre-commit on can specify to use "system" as environment. This works great from the terminal with desired venv active.

However using the integrated source control it seems that global interpeter is accessed and thus required packages are not available.

Is there a workaround here? As for now I specify the entrypoint "path/to/bin/python -m package" in pre-commit config. However I do think that also the integrated source control should respect at least the selected interpeter.

Any thoughts?

Abner answered 15/10, 2020 at 10:18 Comment(0)
N
4

VSCode seems to choose willy-nilly what it respects and what it doesn't. I have the same situation - committing on the integrated terminal works fine; doing it from the Source Control sidebar gives me a "Python can't be found" message (which only appeared after installing a pre-commit hook).

My guess is you'll have to, by trial and error, install pre-commit in every Python installation in your system until you find which one the sidebar is invoking.

Natale answered 4/5, 2021 at 16:23 Comment(3)
I have no solution, but a similar problem here. I have a pre-commit hook installed, commitlint, along with other hooks (eslint...). When I try to commit from the Source Control sidebar, eslint passes while commitlint fails because "Executable commitlint not found". However, I have no problem commiting with the integrated terminal. Somewhere, the Source Control doesn't search the executable where it should. – Roselinerosella
VSCode seems to use the venv it is started in. If you open a terminal, activate a venv and start code from inside the venv git uses the activated venv. – Delaryd
Has anyone found a solution for this? I have JS projects, where the git hook works when using the terminal, but using the vscode source control, the hook is ignored. – Lyso
L
0

TLDR: I have found a solution, but it requires some manual work initially.

At first I also tried to use pre-commit, but came to see the same problem as some of my colleagues reporting about VS Code not being able to execute it correctly when staging and committing over the GUI. (I always use my terminal).


So I wrote my own quality-gate.sh script, that:

  1. runs source venv/bin/activate
  2. and only afterwards runs the script previously defined under pre-commit-config.yaml

I then added the git hook manually inside of .git/hooks folder, by:

  1. Creating the pre-commit hook mv pre-commit.sample pre-commit
  2. Opening the script: vim pre-commit
  3. Getting rid of all the contents inside the previous sample file
  4. Adding the script I'd like to run:
#!/bin/sh
sh ./quality-gate.sh
  1. Saving the file: ESC + :wq
  2. Done! πŸŽ‰

βœ… Now the script is run whenever I have staged (aka added) some changes and run "git commit ...".

This also works for the GUI approach in VS Code - the "Commit" loading bar will be running as long as your script runs - so you might have to be patient, depending on what exactly your pre-commit script does.

I also included an exit 1 command inside of my quality-gate.sh if any of the checks failed, so that the commit will be aborted.



Additional info:

As we are using a monorepo, I included some logic with changed_files=$(git diff --name-only --cached) in order to get a list of changed_project folders, in order to only run pre-commit hooks if there is a staged file inside of the respective folder - aka there are any changes - else the checks will be skipped.

As this is off topic, I will not include it here - but feel free to comment, if further instructions are needed, or if you are interested in this topic.

Letterperfect answered 12/9, 2023 at 9:11 Comment(0)
M
0

The only simple workaround that actually worked for me is Arigion's suggestion:

Start VSCode from the terminal (run code) within the environment that you want to use. Then, VSCode uses that same environment for the system pre-commit hooks.

We are still waiting for a proper solution that does not require any workaround: https://github.com/microsoft/vscode-python/issues/10165 (upvote to increase priority)

Meghannmegiddo answered 16/7 at 8:12 Comment(0)

© 2022 - 2024 β€” McMap. All rights reserved.