Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
40 changes: 40 additions & 0 deletions bin/pyenv-virtualenv-init
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,31 @@ fish )
cat <<EOS
function _pyenv_virtualenv_hook --on-event fish_prompt;
set -l ret \$status
set -l pvh_local ""
if test -f "\$PWD/.python-version"
read -z pvh_local < "\$PWD/.python-version" 2>/dev/null; or true
end
set -l pvh_global ""
if test -f "\$PYENV_ROOT/version"
read -z pvh_global < "\$PYENV_ROOT/version" 2>/dev/null; or true
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As you probably guessed, this duplicates pyenv version logic.
If we leave it as a separate logic, we must somehow make sure that it remains in lockstep with the subcommand proper.

The most realistic way I see is to create a shared sourced file in Pyenv with a function that checks "if the active version has changed". It can very well read and write the cache as well. Then we add some tests to Pyenv to make sure that it properly resets the cache whenever we change the active version via any of the real subcommands (in an opaque way so as to not depend on their implementation details).

  • This way, whenever anything in the real subcommands change, we'll automatically verify that the caching logic remains sound!
  • Moreover, pyenv version proper can make use of this shared function, too, thus also taking advantage of the caching!!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The most realistic way I see is to create a shared sourced file in Pyenv with a function that checks "if the active version has changed".

To optimize the effort, we can leave this logic here until we have at least some working solution -- and refactor it out afterwards as a separate step.
So you can save yourself from worrying about this for now!

if test "\$PWD" = "\$_PYENV_VH_PWD" \\
-a "\$PYENV_VERSION" = "\$_PYENV_VH_VERSION" \\
-a "\$pvh_local" = "\$_PYENV_VH_LOCAL" \\
-a "\$pvh_global" = "\$_PYENV_VH_GLOBAL" \\
-a "\$VIRTUAL_ENV" = "\$_PYENV_VH_VENV"
return \$ret
end
if [ -n "\$VIRTUAL_ENV" ]
pyenv activate --quiet; or pyenv deactivate --quiet; or true
else
pyenv activate --quiet; or true
end
set -g _PYENV_VH_PWD "\$PWD"
set -g _PYENV_VH_VERSION "\$PYENV_VERSION"
set -g _PYENV_VH_LOCAL "\$pvh_local"
set -g _PYENV_VH_GLOBAL "\$pvh_global"
set -g _PYENV_VH_VENV "\$VIRTUAL_ENV"
return \$ret
end
EOS
Expand All @@ -130,11 +150,31 @@ esac
if [[ "$shell" != "fish" ]]; then
cat <<EOS
local ret=\$?
local pvh_local=""
if [ -f "\${PWD}/.python-version" ]; then
pvh_local=\$(< "\${PWD}/.python-version") 2>/dev/null || true
fi
local pvh_global=""
if [ -f "\${PYENV_ROOT}/version" ]; then
pvh_global=\$(< "\${PYENV_ROOT}/version") 2>/dev/null || true
fi
if [ "\${PWD}" = "\${_PYENV_VH_PWD-}" ] \\
&& [ "\${PYENV_VERSION-}" = "\${_PYENV_VH_VERSION-}" ] \\
&& [ "\${pvh_local}" = "\${_PYENV_VH_LOCAL-}" ] \\
&& [ "\${pvh_global}" = "\${_PYENV_VH_GLOBAL-}" ] \\
&& [ "\${VIRTUAL_ENV-}" = "\${_PYENV_VH_VENV-}" ]; then
return \$ret
fi
if [ -n "\${VIRTUAL_ENV-}" ]; then
eval "\$(pyenv sh-activate --quiet || pyenv sh-deactivate --quiet || true)" || true
else
eval "\$(pyenv sh-activate --quiet || true)" || true
fi
_PYENV_VH_PWD="\${PWD}"
_PYENV_VH_VERSION="\${PYENV_VERSION-}"
_PYENV_VH_LOCAL="\${pvh_local}"
_PYENV_VH_GLOBAL="\${pvh_global}"
_PYENV_VH_VENV="\${VIRTUAL_ENV-}"
return \$ret
};
EOS
Expand Down
60 changes: 60 additions & 0 deletions test/init.bats
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,31 @@ export PATH="${TMP}/pyenv/plugins/pyenv-virtualenv/shims:\${PATH}";
export PYENV_VIRTUALENV_INIT=1;
_pyenv_virtualenv_hook() {
local ret=\$?
local pvh_local=""
if [ -f "\${PWD}/.python-version" ]; then
pvh_local=\$(< "\${PWD}/.python-version") 2>/dev/null || true
fi
local pvh_global=""
if [ -f "\${PYENV_ROOT}/version" ]; then
pvh_global=\$(< "\${PYENV_ROOT}/version") 2>/dev/null || true
fi
if [ "\${PWD}" = "\${_PYENV_VH_PWD-}" ] \\
&& [ "\${PYENV_VERSION-}" = "\${_PYENV_VH_VERSION-}" ] \\
&& [ "\${pvh_local}" = "\${_PYENV_VH_LOCAL-}" ] \\
&& [ "\${pvh_global}" = "\${_PYENV_VH_GLOBAL-}" ] \\
&& [ "\${VIRTUAL_ENV-}" = "\${_PYENV_VH_VENV-}" ]; then
return \$ret
fi
if [ -n "\${VIRTUAL_ENV-}" ]; then
eval "\$(pyenv sh-activate --quiet || pyenv sh-deactivate --quiet || true)" || true
else
eval "\$(pyenv sh-activate --quiet || true)" || true
fi
_PYENV_VH_PWD="\${PWD}"
_PYENV_VH_VERSION="\${PYENV_VERSION-}"
_PYENV_VH_LOCAL="\${pvh_local}"
_PYENV_VH_GLOBAL="\${pvh_global}"
_PYENV_VH_VENV="\${VIRTUAL_ENV-}"
return \$ret
};
if ! [[ "\${PROMPT_COMMAND-}" =~ _pyenv_virtualenv_hook ]]; then
Expand All @@ -78,11 +98,31 @@ set -gx PATH '${TMP}/pyenv/plugins/pyenv-virtualenv/shims' \$PATH;
set -gx PYENV_VIRTUALENV_INIT 1;
function _pyenv_virtualenv_hook --on-event fish_prompt;
set -l ret \$status
set -l pvh_local ""
if test -f "\$PWD/.python-version"
read -z pvh_local < "\$PWD/.python-version" 2>/dev/null; or true
end
set -l pvh_global ""
if test -f "\$PYENV_ROOT/version"
read -z pvh_global < "\$PYENV_ROOT/version" 2>/dev/null; or true
end
if test "\$PWD" = "\$_PYENV_VH_PWD" \\
-a "\$PYENV_VERSION" = "\$_PYENV_VH_VERSION" \\
-a "\$pvh_local" = "\$_PYENV_VH_LOCAL" \\
-a "\$pvh_global" = "\$_PYENV_VH_GLOBAL" \\
-a "\$VIRTUAL_ENV" = "\$_PYENV_VH_VENV"
return \$ret
end
if [ -n "\$VIRTUAL_ENV" ]
pyenv activate --quiet; or pyenv deactivate --quiet; or true
else
pyenv activate --quiet; or true
end
set -g _PYENV_VH_PWD "\$PWD"
set -g _PYENV_VH_VERSION "\$PYENV_VERSION"
set -g _PYENV_VH_LOCAL "\$pvh_local"
set -g _PYENV_VH_GLOBAL "\$pvh_global"
set -g _PYENV_VH_VENV "\$VIRTUAL_ENV"
return \$ret
end
EOS
Expand All @@ -97,11 +137,31 @@ export PATH="${TMP}/pyenv/plugins/pyenv-virtualenv/shims:\${PATH}";
export PYENV_VIRTUALENV_INIT=1;
_pyenv_virtualenv_hook() {
local ret=\$?
local pvh_local=""
if [ -f "\${PWD}/.python-version" ]; then
pvh_local=\$(< "\${PWD}/.python-version") 2>/dev/null || true
fi
local pvh_global=""
if [ -f "\${PYENV_ROOT}/version" ]; then
pvh_global=\$(< "\${PYENV_ROOT}/version") 2>/dev/null || true
fi
if [ "\${PWD}" = "\${_PYENV_VH_PWD-}" ] \\
&& [ "\${PYENV_VERSION-}" = "\${_PYENV_VH_VERSION-}" ] \\
&& [ "\${pvh_local}" = "\${_PYENV_VH_LOCAL-}" ] \\
&& [ "\${pvh_global}" = "\${_PYENV_VH_GLOBAL-}" ] \\
&& [ "\${VIRTUAL_ENV-}" = "\${_PYENV_VH_VENV-}" ]; then
return \$ret
fi
if [ -n "\${VIRTUAL_ENV-}" ]; then
eval "\$(pyenv sh-activate --quiet || pyenv sh-deactivate --quiet || true)" || true
else
eval "\$(pyenv sh-activate --quiet || true)" || true
fi
_PYENV_VH_PWD="\${PWD}"
_PYENV_VH_VERSION="\${PYENV_VERSION-}"
_PYENV_VH_LOCAL="\${pvh_local}"
_PYENV_VH_GLOBAL="\${pvh_global}"
_PYENV_VH_VENV="\${VIRTUAL_ENV-}"
return \$ret
};
typeset -g -a precmd_functions
Expand Down
Loading