forked from DonJayamanne/pythonVSCode
-
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
Support deactivating virtual environments without user intervention #22405
Merged
Merged
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
db038f6
z
150445b
YAY
7408e42
Changes
8e20ff9
lint
5a47445
only build vsix
c06ec22
xz
cc38abb
Remove old way and install new way
785fa16
yes
c162dcb
Update how to parse vars
03b4567
Restore all env vars directly for powershell (to keep things simple)
4b44449
filter global interpreters the right way
17c79ca
z
362827f
Check it
4efa830
Do not add deactivate if its not a virtual env
a121f5b
restore ci
f7d13d1
Merge branch 'main' of https://github.com/microsoft/vscode-python int…
8dbf907
Rebase
af145b3
Edit message a bit
d8c7b10
Fix
69679a5
fix test
eae73f6
Add tests
f01e998
Do not import from test module
4948912
Track if shell integration is not working
657a8e1
Separate names from shell types
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# Same as deactivate in "<venv>/bin/activate" | ||
deactivate () { | ||
if [ -n "${_OLD_VIRTUAL_PATH:-}" ] ; then | ||
PATH="${_OLD_VIRTUAL_PATH:-}" | ||
export PATH | ||
unset _OLD_VIRTUAL_PATH | ||
fi | ||
if [ -n "${_OLD_VIRTUAL_PYTHONHOME:-}" ] ; then | ||
PYTHONHOME="${_OLD_VIRTUAL_PYTHONHOME:-}" | ||
export PYTHONHOME | ||
unset _OLD_VIRTUAL_PYTHONHOME | ||
fi | ||
if [ -n "${BASH:-}" -o -n "${ZSH_VERSION:-}" ] ; then | ||
hash -r 2> /dev/null | ||
fi | ||
if [ -n "${_OLD_VIRTUAL_PS1:-}" ] ; then | ||
PS1="${_OLD_VIRTUAL_PS1:-}" | ||
export PS1 | ||
unset _OLD_VIRTUAL_PS1 | ||
fi | ||
unset VIRTUAL_ENV | ||
unset VIRTUAL_ENV_PROMPT | ||
if [ ! "${1:-}" = "nondestructive" ] ; then | ||
unset -f deactivate | ||
fi | ||
} | ||
|
||
# Get the directory of the current script | ||
SCRIPT_DIR=$(dirname "$0") | ||
# Construct the path to envVars.txt relative to the script directory | ||
ENV_FILE="$SCRIPT_DIR/envVars.txt" | ||
|
||
# Read the JSON file and set the variables | ||
TEMP_PS1=$(grep '^PS1=' $ENV_FILE | cut -d '=' -f 2) | ||
TEMP_PATH=$(grep '^PATH=' $ENV_FILE | cut -d '=' -f 2) | ||
TEMP_PYTHONHOME=$(grep '^PYTHONHOME=' $ENV_FILE | cut -d '=' -f 2) | ||
# Initialize the variables required by deactivate function | ||
_OLD_VIRTUAL_PS1="${TEMP_PS1:-}" | ||
_OLD_VIRTUAL_PATH="$TEMP_PATH" | ||
if [ -n "${PYTHONHOME:-}" ] ; then | ||
_OLD_VIRTUAL_PYTHONHOME="${TEMP_PYTHONHOME:-}" | ||
fi | ||
deactivate | ||
bash |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# Same as deactivate in "<venv>/bin/activate" | ||
deactivate () { | ||
if [ -n "${_OLD_VIRTUAL_PATH:-}" ] ; then | ||
PATH="${_OLD_VIRTUAL_PATH:-}" | ||
export PATH | ||
unset _OLD_VIRTUAL_PATH | ||
fi | ||
if [ -n "${_OLD_VIRTUAL_PYTHONHOME:-}" ] ; then | ||
PYTHONHOME="${_OLD_VIRTUAL_PYTHONHOME:-}" | ||
export PYTHONHOME | ||
unset _OLD_VIRTUAL_PYTHONHOME | ||
fi | ||
if [ -n "${BASH:-}" -o -n "${ZSH_VERSION:-}" ] ; then | ||
hash -r 2> /dev/null | ||
fi | ||
if [ -n "${_OLD_VIRTUAL_PS1:-}" ] ; then | ||
PS1="${_OLD_VIRTUAL_PS1:-}" | ||
export PS1 | ||
unset _OLD_VIRTUAL_PS1 | ||
fi | ||
unset VIRTUAL_ENV | ||
unset VIRTUAL_ENV_PROMPT | ||
if [ ! "${1:-}" = "nondestructive" ] ; then | ||
unset -f deactivate | ||
fi | ||
} | ||
|
||
# Get the directory of the current script | ||
SCRIPT_DIR=$(dirname "$0") | ||
# Construct the path to envVars.txt relative to the script directory | ||
ENV_FILE="$SCRIPT_DIR/envVars.txt" | ||
|
||
# Read the JSON file and set the variables | ||
TEMP_PS1=$(grep '^PS1=' $ENV_FILE | cut -d '=' -f 2) | ||
TEMP_PATH=$(grep '^PATH=' $ENV_FILE | cut -d '=' -f 2) | ||
TEMP_PYTHONHOME=$(grep '^PYTHONHOME=' $ENV_FILE | cut -d '=' -f 2) | ||
# Initialize the variables required by deactivate function | ||
_OLD_VIRTUAL_PS1="${TEMP_PS1:-}" | ||
_OLD_VIRTUAL_PATH="$TEMP_PATH" | ||
if [ -n "${PYTHONHOME:-}" ] ; then | ||
_OLD_VIRTUAL_PYTHONHOME="${TEMP_PYTHONHOME:-}" | ||
fi | ||
deactivate | ||
fish |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# Load dotenv-style file and restore environment variables | ||
Get-Content -Path "$PSScriptRoot\envVars.txt" | ForEach-Object { | ||
# Split each line into key and value at the first '=' | ||
$parts = $_ -split '=', 2 | ||
if ($parts.Count -eq 2) { | ||
$key = $parts[0].Trim() | ||
$value = $parts[1].Trim() | ||
# Set the environment variable | ||
Set-Item -Path "env:$key" -Value $value | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why run a new shell? Doesn't this stop the deactivate script from returning until the new shell is exited?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know it's been a while, but I just dug up this PR wondering the exact same thing. In my case I was trying to figure out a conflict between pyenv and a virtualenv when opening a shell in vscode, so I tried manually deactivating the virtualenv, and was really confused by the shell behaviour until I found this.
If anybody still happens to be watching this PR, it would be great to know why this is needed, partly for my (and apparently covracer's curiosity) but also because it seems non-obvious (to me anyway), and if it's important it might be useful to record the reason for posterity in case in the future someone wants to delete it.