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

Need to "cd .." twice to exit a Pipenv folder when pipenv plugin is enabled #12184

Open
yadutaf opened this issue Jan 26, 2024 · 0 comments
Open

Comments

@yadutaf
Copy link

yadutaf commented Jan 26, 2024

When the "pipenv" plugin is enabled, we need to "cd .." twice instead of once to move back to the parent folder.

Under the hood, the auto-activation feature of the pipenv plugin (which I could no longer work without) spawns a subshell on entry and exits it on leave. Therefore the first "cd .." is actually an exit to the parent shell which is still in the previously newly entered folder.

if zstyle -T ':omz:plugins:pipenv' auto-shell; then
# Automatic pipenv shell activation/deactivation
_togglePipenvShell() {
# deactivate shell if Pipfile doesn't exist and not in a subdir
if [[ ! -f "$PWD/Pipfile" ]]; then
if [[ "$PIPENV_ACTIVE" == 1 ]]; then
if [[ "$PWD" != "$pipfile_dir"* ]]; then
exit
fi
fi
fi
# activate the shell if Pipfile exists
if [[ "$PIPENV_ACTIVE" != 1 ]]; then
if [[ -f "$PWD/Pipfile" ]]; then
export pipfile_dir="$PWD"
pipenv shell
fi
fi
}
autoload -U add-zsh-hook
add-zsh-hook chpwd _togglePipenvShell
_togglePipenvShell
fi

Actually, this has some more drawbacks:

  • Any non-exported variable in the parent shell is masked when entering the new directory
  • Any variable set or exported in the subshell will be lost when exiting the env
  • A double "Ctrl+D" would also be needed to exit

and possibly others. Of course, non of them are deal breakers, rather simple day-to-day paper cuts / intuitive behaviors.

Workaround:

# Insert before the plugins declaration
zstyle ':omz:plugins:pipenv' auto-shell no

# Insert after the plugin declaration
# --> Replace 'pipenv shell' by 'source "$(pipenv --venv)/bin/activate"'
# --> Replace 'exit' by 'deactivate'
_togglePipenvShell() {
  # deactivate shell if Pipfile doesn't exist and not in a subdir
  if [[ ! -f "$PWD/Pipfile" ]]; then
    if [[ "$PIPENV_ACTIVE" == 1 ]]; then
      if [[ "$PWD" != "$pipfile_dir"* ]]; then
        unset PIPENV_ACTIVE
        deactivate
      fi
    fi
  fi

  # activate the shell if Pipfile exists
  if [[ "$PIPENV_ACTIVE" != 1 ]]; then
    if [[ -f "$PWD/Pipfile" ]]; then
      export pipfile_dir="$PWD"
      source "$(pipenv --venv)/bin/activate"
      export PIPENV_ACTIVE=1
    fi
  fi
}
autoload -U add-zsh-hook
add-zsh-hook chpwd _togglePipenvShell
_togglePipenvShell

If this is acceptable to you, I'd gladly open a PR with the modified code 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Backlog
Development

No branches or pull requests

1 participant