-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix environment-dependent git pre-commit hooks #10165
Comments
@joaomoreno @janosh In think the best (generic) way is here is if python extension can provide the environment variables. This way pyhton extension can provide the activated environment variables or set the PATH variable (depends on the type of environment). To achieve this the extension will need a hook from VS Code so we can provide these env vars when it spawns git process. |
We have marked this issue as "needs decision" to make sure we have a conversation about your idea. We plan to leave this feature request open for at least a month to see how many 👍 votes the opening comment gets to help us make our decision. |
Any updates? I would realy appreciate this behaviour! |
Sorry, no updates on this yet. |
In my particular case of That is, uninstall Might still be worth it for the Python extension to activate the environment indicated by the status bar during the commit process for cases where a global solution does not exist. |
I'm also having this issue, but only recently. Actually, pre-commit was working fine until the last update of VSCode, so not sure what changed. |
Definitely. In my case, a commit hook runs pyright and needs access to the virtual environment. Thanks for this issue... it took me a while to find one. |
I'm having this issue as well, also recently and on Windows. Specifically the VS Code Source Control panel uses the wrong Python path (does not respect the interpreter selected in the Status Bar) during commit when pre-commit is installed. |
Same issue for me. Do not have access to the activated conda environment when using the VS Code Source Control Panel. What I am trying to do is to run |
Here is how I managed to get it working: Create file
And call it from within
|
Just make the git commit hooks respect the python environment selected in the bottom status bar. |
Any progress on this? My pre-commit hooks have been being consoled only for a while. @LudwigStumpp s solution seems to work for any individual.
However, when it comes to team development, I don't think it is possible for the team to share the same wrap shell. We use the same python, but there could be different virtual environment names, different managers (conda or venv), or even different shells. Actually, I had a similar issue when I was working with node.js 's git pre-commit hook, where git did not active correct npm version. git module should really resolve and load users' real console for running its commands. |
@guoquan could you please upvote my solution so that others see that it is working? :) |
@LudwigStumpp Definitely! That should solve the issue for independent developers. What I wanted to bring into the discussion is, in team development, one can only make very humble assumptions to this Digging into pre-commit, it seems it will create and maintain So my current workaround is to install (only) Looking back at the issue, what we need now is vsc to activate the correct environment that contains |
any updates on this? |
@JoshFerge this issue haven't been prioritized yet. We plan to do it eventually but no ETAs for that yet. |
- The unresolved issue is that vscode doesn't play nice with pre-commit installed in the venv: microsoft/vscode-python#10165 - For now, the accepted workaround seems to be to install pre-commit for in your system python instance.
Since pip 23/pep 668 we are discouraged to deploy pre-commit into the systems python deployment. As a consequence this feature is more needed than ever before. While pre-commit may still be available, the use of local pre-commit-hooks (which allow to decouple from systems such as GitHub) no longer works. Also, the workaround using the |
Adding onto #10165 (comment) for the specific issue around pytest local testing this worked for me when running git commit via VSCODE to run pytest without defining an extra script:
|
To me, it's not even clear which environment the pre-commit hook inside VSCode uses? Conda's This is easily the most annoying part about VSCode atm... |
I have a pre-commit hook that calls mypy. It ought to work, but mypy isn't found, apparently because the remote virtual environment isn't being activated before git is called. Terminals get the environment activated correctly. So commits work in the terminal. I can, in an ugly way involving full paths, use the localgit suggestion above, so that git runs in my remote virtual environment. But I really didn't expect to have to do this. |
Works assuming one uses Poetry. |
How is this still open after 3+ years? Kind of absurd when you think about how simple the fix is and how many people are having the issue. |
@gresavage it's actually not simple as the SCM component of VS Code has no concept of Python interpreters or environments. So to make this work the SCM system in VS Code core would need to be expanded so we could somehow inject environment details so that Also note that this issue finally hit the top 5 most upvoted issues, but it's also half the number of upvotes of the first and second issue (and the first issue is actually newer than this one). So while we understand this impacts people, we still have other issues that the community has flagged as more important to them overall. We are definitely not ignoring this issue, but our team is only so big and we are expected to support all Python users in VS Code which is not an insignificant number based on our download count. But if you believe I have misunderstood the complexity of what it will take to resolve this problem, then this extension and VS Code are open source and we do appreciate and accept community contributions. If you or anyone else wants to propose a fix and a PR for it we will happily review it. |
Thank you for the information! That is quite helpful and definitely puts
the issue into a clearer scope. Until your comment it seemed like there was
little to no activity from devs towards closing this issue.
I'd agree the overall handling of virtual environments and environment
variables needs TLC and would trickle-down to closing a host of other
issues... at least with most other issues relating to those two things
there is a more elegant workaround which at least somewhat integrates with
VS Code even if it might be round-about. With this particular issue,
however, I am currently forced to modify `.git/hooks/pre-commit` and
activate the workspace environment there... fortunately my local workspace
env doesn't change much. Nevertheless in my case it makes writing
dockerfiles for developer use a tricky business for the patchwork that
needs to be done.
At the moment I unfortunately have neither the time nor the expertise to
contribute - as enticing as that standing offer is. I am just a simple mech
e rather than a proper comp sci so I don't have much programming skill
outside of decades of python so it will be a while before I would be able
to be up-and-running enough to meaningfully contribute.
I am sure we all eagerly await those changes - they sound extremely useful.
Maybe some fleshed out CLI documentation and functionality will accompany
them too 😜. I will say I am surprised that there is not a bigger team
dedicated to Python since it seems like it has one of the highest user
bases for VS Code.
…On Tue, Aug 22, 2023, 13:49 Brett Cannon ***@***.***> wrote:
@gresavage <https://github.com/gresavage> it's actually not simple as the
SCM component of VS Code has no concept of Python interpreters or
environments. So to make this work the SCM system in VS Code core would
need to be expanded so we could somehow inject environment details so that
git itself picks up. This would probably come about via environment
variables and we are actively reworking our environment activation support
in the terminal to use environment variables, so hopefully this work can
then inform an API change in VS Code core for SCM execution and we can
build one feature on top of the other's hard work.
Also note that this issue finally hit the top 5 most upvoted issues
<https://github.com/microsoft/vscode-python/issues?q=is%3Aissue+is%3Aopen+sort%3Areactions-%2B1-desc>,
but it's also half the number of upvotes of the first and second issue (and
the first issue is actually newer than this one). So while we understand
this impacts people, we still have other issues that the community has
flagged as more important to them overall. We are definitely not ignoring
this issue, but our team is only so big and we are expected to support all
Python users in VS Code which is not an insignificant number based on our
download count.
But if you believe I have misunderstood the complexity of what it will
take to resolve this problem, then this extension and VS Code are open
source and we do appreciate and accept community contributions. If you or
anyone else wants to propose a fix and a PR for it we will happily review
it.
—
Reply to this email directly, view it on GitHub
<#10165 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AB45WG6ZTONS52WIU7UTQG3XWTWLRANCNFSM4KW74PJQ>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
As you can see by our issue count, there's unfortunately simply too many issues for us to keep all ancillary issues up-to-date while we are working on something. Plus we don't want to give people false hopes on when things will be completed.
"CLI documentation" for which CLI? |
I appreciate your input. It is nice to know this issue has eyes on it and
some impactful changes are indeed on the way which may enable moving this
issue along.
"CLI documentation" for which CLI? code?
Yes, both `code` and `code-server`
Just about the only commands and options I know the CLI to accept are the
ones which are documented explicitly in the CLI documentation (i.e. install
extensions, add folders to workspace, set ports). If more exist I don't
know of them.
This is all relatively unrelated to the original thread but in particular
it would be nice if the CLI supported any of the menu commands. In my case
I struggled setting up a Docker container with `code-server` for
development because I couldn't find a CLI command to import a profile.
AFAIK `devcontainers` wouldn't work because it creates a "docker-in-docker"
problem. Eventually i settled with saving a `code-workspace` file but this
is limited in its abilities too... such as the position and layout settings
of tools.
…On Wed, Aug 23, 2023, 14:55 Brett Cannon ***@***.***> wrote:
Until your comment it seemed like there was little to no activity from
devs towards closing this issue.
As you can see by our issue count, there's unfortunately simply too many
issues for us to keep all ancillary issues up-to-date while we are working
on something. Plus we don't want to give people false hopes on when things
will be completed.
Maybe some fleshed out CLI documentation
"CLI documentation" for which CLI? code?
—
Reply to this email directly, view it on GitHub
<#10165 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AB45WG6M4ZBUAXFUMSNK6ELXWZGZNANCNFSM4KW74PJQ>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
All feedback on the CLI should go to https://github.com/microsoft/vscode . |
Not sure if this helps, but we had a similar problem, however, we're using pre-commit hooks via nix (https://github.com/cachix/pre-commit-hooks.nix). Nevertheless, I think our workaround could be adapted to other circumstances like python virtual environment. Essentially, we're "polluting" pre-commit hooks, so they'll load the necessary environment without needing VSCode to run in the environment. I hope this helps:
|
very good, will do! @kfigiela very good! That is actually pretty similar to what I've done as well - I also use My biggest gripe with doing it this way is that if the project/workspace interpreter is changed later on by the user then the script will need to be updated and then |
I've been poking around on this and am considering trying to put together an Open Source Contribution (though I'll have some learning to do with VSCode extensions) to try resolving this, but have a couple questions I'm trying to track down/understand before I can do that.
Either way, I figured I'd surface what I'd learned and prompt with any potential help to try getting momentum on a potential solution going here. |
@kr-hansen thanks for the input. Here is what I can say in regards to (1) and (2)
In my case it is using echo SHELL="${SHELL}" This means that there is no project-specific environment configuration being done - just whatever is in
With conda activate <env_name> |
Not a solution but a simple workaround: Start |
This is a follow up to #9948 and vscode#90178. After looking at the problem of environment-dependent pre-commit hooks from both the extension's and the editor's side, it seems to me the best solution would be for the Python extension to make sure that whatever environment the user has selected in the status bar is also active during the commit process (using VS Code's source control panel).
I think this would be the expected behavior by most users if you asked them. At least it took me by surprise that my active environment doesn't apply during the commit process even though it's indicated in the status bar. Hence why it took me a while to figure out this is the reason my pre-commit hooks aren't working.
One way to activate the status bar environment in the spawned git process might be as @joaomoreno mentioned to have a
git
wrapper that's local to the source control panel (i.e. doesn't affectgit
anywhere else) and simply activates the environment before handing over to realgit
.The text was updated successfully, but these errors were encountered: