Skip to content
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

pre-commit hook behaving differently in integrated terminal and source control panel #90178

Closed
janosh opened this issue Feb 6, 2020 · 10 comments
Assignees
Labels
info-needed Issue requires more information from poster

Comments

@janosh
Copy link

janosh commented Feb 6, 2020

Steps to reproduce

Create a repo and install pre-commit

git init && pip install pre-commit

Create 3 files.

touch a.py b.py .pre-commit-config.yaml

containing

# .pre-commit-config.yaml
repos:
  - repo: https://github.com/timothycrosley/isort
    rev: 4.3.21
    hooks:
      - id: isort
# a.py
a = "a"
# b.py
import tensorflow as tf  # any 3rd party import

from a import a

Install isort commit hook via pre-commit install.

Attempt to commit b.py via VS Code's source control panel. isort will abort the commit and make changes to the file, removing the line between the local and 3rd party import. Committing from the integrated terminal via git add b.py && git commit -m foo works just fine.

Expected behaviour

Committing in source control panel should work just like from the command line.

Cause

I think this might be due to VS Code invoking isort without access to the environment of installed packages. isort therefore thinks import tensorflow as tf references another local module like a.py and imports of the same type shouldn't be separated by blank lines.

Related

Issue originally opened against the Python extension.

@joaomoreno
Copy link
Member

You're missing some critical information:

  • VSCode Version:
  • OS Version:

If you run Developer: Toggle Developer Tools and then run process.env you'll see which environment VS Code has access to. Is that any different than the environment in your shell? If so, how do you define it for your shell? Through .bashrc? .bash_profile?

@joaomoreno joaomoreno added the info-needed Issue requires more information from poster label Feb 7, 2020
@janosh
Copy link
Author

janosh commented Feb 7, 2020

@joaomoreno Sorry about that.

VSCode Version: 1.42.0
OS Version: macOS 10.15.3

PATH in terminal is

/usr/local/Caskroom/miniconda/base/envs/flownn/bin
/usr/local/Caskroom/miniconda/base/condabin
/usr/local/bin
/usr/bin
/bin
/usr/sbin
/sbin

while in the VS Code console process.env.PATH is

/usr/local/Caskroom/miniconda/base/condabin
/usr/local/bin
/usr/bin
/bin
/usr/sbin
/sbin

So as expected, the conda env flownn with 3rd party packages is missing. The reason it's in the terminal's PATH is because of a custom script conda-auto-env I'm sourcing in my .zshrc that automatically activates conda environments when entering directories containing a conda environment file. But I would expect the environment to be available in process.env.PATH as well since it's selected in VS Code's status bar:

status-bar

Is this something that could be fixed by the Python extension? I.e. make it add the selected environment to process.env.PATH?

@joaomoreno
Copy link
Member

VS Code tries to automatically source the .zshrc file as well. Could you activate the environment always, instead of based on cwd?

@janosh
Copy link
Author

janosh commented Feb 7, 2020

Wouldn't that prevent me from changing the environment while I'm in that directory? I need the ability to sometimes deactivate the env or change to a different one.

@sharm294
Copy link

I have the same problem as well.

I have the executable that a pre-commit hook needs installed in a conda environment. When I run git commit from the integrated terminal (which has the conda environment active), everything passes but trying to commit from the source control panel fails because it doesn't find the executable.

As janosh said, this should ideally be a workspace-specific setting rather than through a global configuration file. I did test activating my conda environment through my .bashrc but that didn't fix the issue in the source control panel. I also tried adding ["-l"] to terminal.integrated.shellArgs.linux but I guess the panel isn't affected from that setting.

I'm using VS code 1.41.1 on Windows 10, using the Remote-SSH extension to an Ubuntu 16.04 machine.

@janosh janosh changed the title isort commit hook behaving differently in integrated terminal and source control panel pre-commit hook behaving differently in integrated terminal and source control panel Feb 10, 2020
@joaomoreno
Copy link
Member

@janosh It's just another way of reading the cwd. Now you seem to be preemtively running something to read cwd every time it changes. But you can also just have the tool which depends on cwd read it a the time it executes. The hook should read the cwd and behave accordingly, not preemptively. Could this work in your case?

@janosh
Copy link
Author

janosh commented Feb 17, 2020

@joaomoreno Not quite sure how that would work. I'm using the zsh chpwd hook. What you're suggesting sounds like I'd have to implement all of its functionality myself. I'm trying to do as little custom setup as possible.

I also tried getting rid of said custom environment activation completely and instead use "python.terminal.activateEnvironment": true. I'd hoped that would also apply to the source control panel. Same problem though. Is this perhaps an issue that could be raised against the Python extension?

@joaomoreno
Copy link
Member

Is this perhaps an issue that could be raised against the Python extension?

I guess not, it seems specific to Git in VS Code instead.

As a workaround, since VS Code spawns git with a given cwd, then you could always write a zsh shell script with simply forwards to git and then change the git.path setting to point to that script. That way, VS Code would spawn your script, which would run your cwd-related code, and then invoke git.

@vscodebot
Copy link

vscodebot bot commented Feb 25, 2020

This issue has been closed automatically because it needs more information and has not had recent activity. See also our issue reporting guidelines.

Happy Coding!

@vscodebot vscodebot bot closed this as completed Feb 25, 2020
@github-actions github-actions bot locked and limited conversation to collaborators Apr 10, 2020
@karrtikr
Copy link
Contributor

Attempt to commit b.py via VS Code's source control panel

Question: Does VS Code use hidden terminals for running git commands, or is it simply spawning a process? @joaomoreno

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
info-needed Issue requires more information from poster
Projects
None yet
Development

No branches or pull requests

4 participants