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

fix(virtualenv): Improve PATH management during virtualenv activation #208

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion autoswitch_virtualenv.plugin.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,18 @@ function _get_venv_name() {
printf "%s" "$venv_name"
}

# Function to ensure virtualenv bin is at the start of PATH
function _ensure_virtualenv_path() {
# Remove the current virtualenv from PATH if it exists
if [[ -n "$VIRTUAL_ENV" ]]; then
PATH=:$PATH:
PATH=${PATH//:$VIRTUAL_ENV\/bin:/:}
PATH=${PATH#:}
PATH=${PATH%:}
fi
# Add the new virtualenv to the start of PATH
PATH="$venv_dir/bin:$PATH"
}

function _maybeworkon() {
local venv_dir="$1"
Expand All @@ -87,7 +99,7 @@ function _maybeworkon() {
fi

# Don't reactivate an already activated virtual environment
if [[ -z "$VIRTUAL_ENV" || "$venv_dir" != "$VIRTUAL_ENV" ]]; then
if [[ -z "$VIRTUAL_ENV" || "$venv_dir" != "$VIRTUAL_ENV" || "$PATH" != "$venv_dir/bin"* ]]; then

if [[ ! -d "$venv_dir" ]]; then
printf "Unable to find ${AUTOSWITCH_PURPLE}$venv_name${AUTOSWITCH_NORMAL} virtualenv\n"
Expand All @@ -112,6 +124,9 @@ function _maybeworkon() {
local activate_script="$venv_dir/bin/activate"

_validated_source "$activate_script"

# Ensure the virtualenv's bin directory is at the start of PATH
_ensure_virtualenv_path
fi
}

Expand Down