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

Make setting PS1 optional in pythonTerminalEnvVarActivation experiment #21959

Closed
levrik opened this issue Sep 11, 2023 · 12 comments
Closed

Make setting PS1 optional in pythonTerminalEnvVarActivation experiment #21959

levrik opened this issue Sep 11, 2023 · 12 comments
Assignees
Labels
feature-request Request for new features or functionality info-needed Issue requires more information from poster triage-needed Needs assignment to the proper sub-team

Comments

@levrik
Copy link

levrik commented Sep 11, 2023

My prompt already includes the activated virtual environment, so it would be great if setting PS1 could be made optional.

image

@levrik levrik added the feature-request Request for new features or functionality label Sep 11, 2023
@github-actions github-actions bot added the triage-needed Needs assignment to the proper sub-team label Sep 11, 2023
@levrik
Copy link
Author

levrik commented Sep 11, 2023

I actually found the following code in the .venv/bin/activate which was executed before:

if [ -z "${VIRTUAL_ENV_DISABLE_PROMPT-}" ] ; then
    _OLD_VIRTUAL_PS1="${PS1-}"
    if [ "xstudio-py3.9" != x ] ; then
        PS1="(studio-py3.9) ${PS1-}"
    else
        PS1="(`basename \"$VIRTUAL_ENV\"`) ${PS1-}"
    fi
    export PS1
fi

I figured that VIRTUAL_ENV_DISABLE_PROMPT is set to 1 for me. This seems to be automatically set by my prompt (starship.rs) as it already appends the activated environment by itself.

I guess that VS Code simply also has to respect this env variable and not modify PS1 if it's set.

@karrtikr
Copy link

We use .venv/bin/activate to figure out what variables to set in terminal, one of which it returns us is the PS1.

This seems to be automatically set by my prompt (starship.rs) as it already appends the activated environment by itself.

I assume this happens in one of the shell initialization scripts (.bashrc). Unfortunately Python extension does not have access to that yet, so we cannot tell whether VIRTUAL_ENV_DISABLE_PROMPT is set at terminal level. Can you try setting it at system level at /etc/environment? Add the following:

VIRTUAL_ENV_DISABLE_PROMPT=1

@github-actions github-actions bot added the info-needed Issue requires more information from poster label Sep 11, 2023
@levrik
Copy link
Author

levrik commented Sep 12, 2023

@karrtikr I guess the extra prompt wasn't showing up before as .venv/bin/activate was executed after the shell was initialized? In my .zshrc I'm executing eval "$(starship init zsh)" which indeed does VIRTUAL_ENV_DISABLE_PROMPT=1 internally.

/etc/environment seems to be a Linux-only thing and doesn't work on macOS. Setting this variable under ~/.zprofile has the effect of (.venv) to be prepended to the prompt instead of (studio-py3.9). So it seems to be respected when added to this file but doesn't have the desired effect? Same result when adding it to ~/.zshenv instead of ~/.zprofile.

@github-actions github-actions bot removed the info-needed Issue requires more information from poster label Sep 12, 2023
@karrtikr
Copy link

I guess the extra prompt wasn't showing up before as .venv/bin/activate was executed after the shell was initialized?

Yep, so the script had the context of .zshrc which we don't have with the experiment.

Unfortunately it has to be done system wide in order for all of VS Code to pick it up, so none of the terminal profile specific scripts would help. Try this answer: https://stackoverflow.com/a/588442, make sure to reboot system after setting var.

@github-actions github-actions bot added the info-needed Issue requires more information from poster label Sep 12, 2023
@levrik
Copy link
Author

levrik commented Sep 12, 2023

@karrtikr TBH this sounds quite excessive. I got quite excited about this experiment as the command injection often interfered with other scripts like oh-my-zsh update when opening the terminal. I'll disable it for now again and hope that a solution can be found before this gets released to everyone. Either automatic or through a manual configuration.

@github-actions github-actions bot removed the info-needed Issue requires more information from poster label Sep 12, 2023
@karrtikr
Copy link

Another option could be to launch VS Code via code . from zsh terminal itself.

Other than that, manually configuring /etc/launchd.conf to add setenv VIRTUAL_ENV_DISABLE_PROMPT 1, followed by a restart seems to be only option right now.

@karrtikr
Copy link

With microsoft/vscode#145234 we're hoping to have the context of terminal variables as well, which is when this issue should go away automatically 🙂 Closing as limitation for now.

@karrtikr karrtikr closed this as not planned Won't fix, can't repro, duplicate, stale Sep 12, 2023
@github-actions github-actions bot added the info-needed Issue requires more information from poster label Sep 12, 2023
@levrik
Copy link
Author

levrik commented Sep 12, 2023

@karrtikr Okay. I'll keep an eye on that other ticket then and try to work around it for now.

@github-actions github-actions bot removed the info-needed Issue requires more information from poster label Sep 12, 2023
@karrtikr
Copy link

Thanks! Appreciate the early feedback

@karrtikr
Copy link

@levrik FYI in pre-release version of extension, adding the following .env in workspace root:

VIRTUAL_ENV_DISABLE_PROMPT=1

should also work.

@levrik
Copy link
Author

levrik commented Sep 22, 2023

Thanks for the hint. This actually works. After checking the output of the Python extension I realized that it actually contains env variables Starship (my prompt) sets.

It sets VIRTUAL_ENV_DISABLE_PROMPT to 1 here: https://github.com/starship/starship/blob/43b2d42cd526e34c5f0290e7409fbd6d3a54e908/src/init/starship.zsh#L88

When putting an export in front of this line, it actually works out-of-the-box without .env. I don't know how the extension fetches the environment variables so I wonder if this is an oversight on this extension's side or on Starship's side.

@karrtikr
Copy link

Oh, that's interesting. I'm wondering that maybe exporting a variable sets it for the system? Will look into it. (Thanks for the hint 😉)

@github-actions github-actions bot added the info-needed Issue requires more information from poster label Sep 22, 2023
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 23, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feature-request Request for new features or functionality info-needed Issue requires more information from poster triage-needed Needs assignment to the proper sub-team
Projects
None yet
Development

No branches or pull requests

2 participants