ohmyzsh
ohmyzsh copied to clipboard
Need to "cd .." twice to exit a Pipenv folder when pipenv plugin is enabled
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.
https://github.com/ohmyzsh/ohmyzsh/blob/80c114cb3a64044ea50b623f96a35bc022db5e8d/plugins/pipenv/pipenv.plugin.zsh#L15-L38
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 :slightly_smiling_face: