From f68e5ec3926331b7d97df99d89cd695faf11c73e Mon Sep 17 00:00:00 2001 From: Srinivas Chippagiri Date: Tue, 24 Nov 2026 11:37:22 -0800 Subject: [PATCH] fix(poetry): make psh and psync work with Poetry 2 (#14283) --- plugins/poetry/README.md | 4 ++-- plugins/poetry/poetry.plugin.zsh | 21 +++++++++++++++++++-- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/plugins/poetry/README.md b/plugins/poetry/README.md index 3e7ada439..a794120e1 100644 --- a/plugins/poetry/README.md +++ b/plugins/poetry/README.md @@ -28,11 +28,11 @@ plugins=(... poetry) | prm | `poetry remove` | Remove packages from `pyproject.toml` and uninstall them | | prun | `poetry run` | Executes the given command inside the project’s virtualenv | | psad | `poetry self add` | Add the Poetry plugin and install dependencies to make it work | -| psh | `poetry shell` | Spawns a shell within the virtual environment. If one doesn’t exist, it will be created | +| psh | `poetry shell` | Spawns a shell within the virtual environment. With Poetry 2.0+ and no shell plugin, activates it with `poetry env activate` | | pshw | `poetry show` | List all the available dependencies | | pslt | `poetry show --latest` | List latest version of the dependencies | | psup | `poetry self update` | Update Poetry to the latest version (default) or to the specified version | -| psync | `poetry install --sync` | Synchronize your environment with the `poetry.lock` | +| psync | `poetry sync` | Synchronize your environment with the `poetry.lock` (`poetry install --sync` before Poetry 2.0) | | ptree | `poetry show --tree` | List the dependencies as tree | | pup | `poetry update` | Get the latest versions of the dependencies and to update the `poetry.lock` | | pvinf | `poetry env info` | Get basic information about the currently activated virtualenv | diff --git a/plugins/poetry/poetry.plugin.zsh b/plugins/poetry/poetry.plugin.zsh index 0bef4cdeb..66a2b2563 100644 --- a/plugins/poetry/poetry.plugin.zsh +++ b/plugins/poetry/poetry.plugin.zsh @@ -14,11 +14,9 @@ alias ppub='poetry publish' alias prm='poetry remove' alias prun='poetry run' alias psad='poetry self add' -alias psh='poetry shell' alias pshw='poetry show' alias pslt='poetry show --latest' alias psup='poetry self update' -alias psync='poetry install --sync' alias ptree='poetry show --tree' alias pup='poetry update' alias pvinf='poetry env info' @@ -26,6 +24,25 @@ alias pvoff='poetry config virtualenvs.create false' alias pvrm='poetry env remove' alias pvu='poetry env use' +# Poetry 2.0 moved `poetry shell` to the poetry-plugin-shell plugin, so fall +# back to activating the virtualenv in the current shell when it's missing. +function psh { + if poetry shell --help &>/dev/null; then + poetry shell "$@" + else + eval "$(poetry env activate)" + fi +} + +# Poetry 2.0 replaced `poetry install --sync` with `poetry sync`. +function psync { + if poetry sync --help &>/dev/null; then + poetry sync "$@" + else + poetry install --sync "$@" + fi +} + # Return immediately if poetry is not found if (( ! $+commands[poetry] )); then return