Skip to content

fix(poetry-env): preserve venv in project subdirectories - #13932

Merged
robbyrussell merged 2 commits into
ohmyzsh:masterfrom
nrps9909:codex/fix-poetry-env-subdirectories
Sep 5, 2026
Merged

robbyrussell merged 2 commits into
ohmyzsh:masterfrom
nrps9909:codex/fix-poetry-env-subdirectories

Conversation

@nrps9909

@nrps9909 nrps9909 commented Aug 11, 2026 •

Copy link
Copy Markdown
Contributor

Standards checklist:

  • The PR title is descriptive.
  • The PR doesn't replicate another PR which is already open.
  • I have read the contribution guide and followed all the instructions.
  • The code follows the code style guide detailed in the wiki.
  • The code is mine or it's from somewhere with an MIT-compatible license.
  • If I used AI tools (ChatGPT, Claude, Gemini, etc.) to assist with this contribution, I've disclosed it below.
  • The code is efficient, to the best of my ability, and does not waste computer resources.
  • The code is stable and I have tested it myself, to the best of my abilities.
  • If the code introduces new aliases, I provide a valid use case for all plugin users down below.

Changes:

  • Keep the active Poetry environment while navigating ordinary project subdirectories.
  • Deactivate and switch when entering a valid nested Poetry project.
  • Match project paths on a directory boundary, so a sibling such as project2 is not treated as a child of project.
  • Document the two marker files and the navigation behavior.

Fixes #12377.

Testing:

  • Ran the repository's CI syntax command locally: all 579 matched Zsh files passed zsh -n.
  • Ran an isolated zsh -f navigation smoke test covering project activation, ordinary and incomplete nested directories, a valid nested Poetry project, its child, returning to the parent project, a same-prefix sibling project, paths with spaces, and leaving the project tree.
  • Reproduced the original child-directory deactivation against origin/master; all eight navigation scenarios pass with this branch.

Other comments:

No aliases are introduced, so the alias-use-case checklist item is not applicable.

AI-assisted: Codex analyzed the existing state transitions, drafted the condition, and assembled the test matrix. The final condition was cross-checked against the reported regression and edge cases, and the tests above were run locally.

cc @carlosala

Keep the active environment while navigating ordinary project children, but switch when entering a nested Poetry project.

Use a path-component boundary so similarly named sibling projects are not mistaken for subdirectories.

Fixes ohmyzsh#12377
@ohmyzsh ohmyzsh Bot added this to Main project Aug 11, 2026
@github-project-automation github-project-automation Bot moved this to Backlog in Main project Aug 11, 2026
@Morningstar202604

Copy link
Copy Markdown
Contributor

Heads-up while this is in flight: plugins/poetry-env/poetry-env.plugin.zsh:9 has the same string-prefix-vs-path-component slip that #13932 is working around, and it produces a different symptom from the subdirectory one this PR addresses.

if [[ $poetry_active -eq 1 ]] && { [[ $in_poetry_dir -eq 0 ]] || [[ "$PWD" != "$poetry_dir"* ]]; }; then

"$PWD" != "$poetry_dir"* is satisfied by any directory whose path merely starts with the project's text, so an unrelated sibling project is treated as "still inside the project" and the first environment is never released.

Reproduced on current master with two sibling projects proj and proj-extra, each with pyproject.toml + poetry.lock and its own venv, poetry env info --path stubbed to answer per $PWD:

start in proj       -> VIRTUAL_ENV=/tmp/.../venvs/proj
after cd proj-extra -> VIRTUAL_ENV=/tmp/.../venvs/proj     <- expected /tmp/.../venvs/proj-extra
                    -> poetry_dir=/tmp/.../proj

poetry_dir is still the old project and VIRTUAL_ENV still points into project A's venv, so commands run in project B are executed with project A's interpreter and dependencies. in_poetry_dir is 1 there (B has its own pyproject.toml/poetry.lock, just not the same one), so the [[ $in_poetry_dir -eq 0 ]] half does not fire and the prefix test is what lets it through.

Suggesting the component-boundary form rather than a prefix test — the same shape I landed for #14006 and used in #14021:

local project_dir="${poetry_dir%/}"
if [[ "$PWD" != "$project_dir" && "$PWD" != "$project_dir"/* ]]; then

Truth table, stay = keep the environment:

poetry_dir PWD current with the above
/x/proj /x/proj stay stay
/x/proj /x/proj/sub/dir stay stay
/x/proj /x/proj-extra stay (bug) deactivate
/ /tmp stay stay

Not posting a competing patch since this PR is already open on the file — just flagging it so the fix can cover both cases if you want to fold it in.

@nrps9909

Copy link
Copy Markdown
Contributor Author

Thanks for the detailed boundary table. The existing PR head already used the component-aware "$PWD" == "$poetry_dir"/* check, and a fresh zsh probe confirmed that /x/proj-extra deactivates while /x/proj/sub stays active. Your / case exposed a remaining edge, though: an unnormalized root produced //*, which did not match /tmp. Signed follow-up a7248eac now normalizes the trailing slash before both exact and descendant comparisons. Revalidated states: sibling project -> deactivated once; ordinary child -> active with zero deactivations; root child /tmp -> active with zero deactivations. zsh -n and git diff --check pass.

@Morningstar202604

Copy link
Copy Markdown
Contributor

You are right about the scope, and thanks for chasing it down instead of letting it slide — my note mis-attributed the bug. The string-prefix slip is in master's _togglePoetryShell; I verified the reproduction against master and framed the note as if this PR's head also had it, which it did not. Sorry for the noise.

I have re-run the same stubbed-poetry probe against your current head (a7248ea), and all three behaviours you listed check out independently:

  • proj -> proj/subdir, where the child has no pyproject.toml/poetry.lock: the environment stays active on this branch; on master the same cd drops it, which is the case this PR fixes.
  • proj -> sibling proj-extra with its own project files and venv: the branch switches to the sibling's own environment rather than keeping the old one.
  • leaving the project tree for an unrelated directory: deactivates.

I also exercised the containment condition from a7248ea directly rather than only through the transitions, including the unnormalized-root case you fixed: with poetry_dir=/, the normalized project_dir keeps /, /tmp and /usr/local inside the project; with poetry_dir=/x/proj, the exact path and children stay and a sibling like /x/proj-docs deactivates. That matches your commit message.

Nothing outstanding from me on this one.

@Morningstar202604

Copy link
Copy Markdown
Contributor

Clarification: The string-prefix bug I initially mentioned is in master's _togglePoetryShell, not in this PR. This PR correctly uses component-aware path matching ("$PWD" == "$poetry_dir"/*). The reproduction and fix scope are accurate — ready for review.

@robbyrussell robbyrussell left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nice catch, and thanks for the writeup.

This one's more annoying than it looks in the diff... on master, walking into any ordinary subdirectory of your own project killed the venv, because in_poetry_dir only goes to 1 when the current directory itself has both marker files. So the fix isn't really about sibling directories, it's about the most normal thing you can do in a project.

I ran the decision table against both branches. Subdirectories stay active now, nested projects switch properly, and a project at / still behaves. Three of those cases were wrong on master, so this fixes more than it advertises.

One tiny thing, not for this PR: venv_dir on line 17 still isn't local, so it leaks into the global namespace. Pre-existing, and I'll pick it up separately.

Merging. Thanks!

🤖 Co-authored with Claude Code

@robbyrussell
robbyrussell merged commit 593e8ed into ohmyzsh:master Sep 5, 2026
1 check passed
@github-project-automation github-project-automation Bot moved this from Backlog to In Progress in Main project Sep 5, 2026
@github-project-automation github-project-automation Bot moved this from In Progress to Done in Main project Sep 5, 2026
@robbyrussell

robbyrussell commented Sep 5, 2026 •

Copy link
Copy Markdown
Member

Follow-up on the venv_dir note above: #14043.

robbyrussell added a commit that referenced this pull request Sep 5, 2026
venv_dir was assigned without `local`, so every activation left it in the
caller's scope. The sibling uv-env plugin already declares it `local`.

Noticed while reviewing #13932.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
RH203 pushed a commit to RH203/ohmyzsh that referenced this pull request Sep 7, 2026
* fix(poetry-env): preserve venv in project subdirectories

Keep the active environment while navigating ordinary project children, but switch when entering a nested Poetry project.

Use a path-component boundary so similarly named sibling projects are not mistaken for subdirectories.

Fixes ohmyzsh#12377

* fix(poetry-env): normalize active project root
RajibRashid pushed a commit to RajibRashid/ohmyzsh that referenced this pull request Sep 11, 2026
* fix(poetry-env): preserve venv in project subdirectories

Keep the active environment while navigating ordinary project children, but switch when entering a nested Poetry project.

Use a path-component boundary so similarly named sibling projects are not mistaken for subdirectories.

Fixes ohmyzsh#12377

* fix(poetry-env): normalize active project root
ksaynice pushed a commit to ksaynice/oh-my-zsh that referenced this pull request Sep 16, 2026
* fix(poetry-env): preserve venv in project subdirectories

Keep the active environment while navigating ordinary project children, but switch when entering a nested Poetry project.

Use a path-component boundary so similarly named sibling projects are not mistaken for subdirectories.

Fixes ohmyzsh#12377

* fix(poetry-env): normalize active project root
tutkun pushed a commit to tutkun/ohmyzsh that referenced this pull request Oct 29, 2026
* fix(poetry-env): preserve venv in project subdirectories

Keep the active environment while navigating ordinary project children, but switch when entering a nested Poetry project.

Use a path-component boundary so similarly named sibling projects are not mistaken for subdirectories.

Fixes ohmyzsh#12377

* fix(poetry-env): normalize active project root
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

poetry-env gets deactivated if changing to a sub directory

3 participants