Skip to content

fix(poetry-env): handle Poetry returning "." for venv path #13183

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

stephanschielke
Copy link

Took me a while to find and reproduce the issue. The fix itself was simple.
I'm not a bot, I only enriched this PR summary with ALL context information.

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.
  • 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:

  • Fixed poetry-env plugin failure with in-project virtual environments

    • Handle case where poetry env info --path returns "." instead of full path when virtualenvs.in-project = true
    • Convert "." to "$PWD/.venv" to locate the correct virtual environment directory
    • Add safety check to ensure activate script exists before attempting to source it
    • Maintain backward compatibility with existing Poetry setups (centralized virtual environments)
  • Root cause: When Poetry is configured with virtualenvs.in-project = true, the command poetry env info --path returns "." instead of the full path to the virtual environment. This causes the plugin to attempt sourcing "./bin/activate" which fails with "no such file or directory".

  • Solution: Check if the returned path is "." and convert it to the proper in-project virtual environment path "$PWD/.venv", then verify the activate script exists before sourcing.

Comprehensive Issue Analysis:

When This Issue Occurs:

Poetry Version Impact:

  • Poetry 1.8.5 & 2.0.1: ❌ Return "." (affected by bug)
  • Poetry 2.1.3+: ✅ Return full paths (bug fixed)

Specific Scenarios That Trigger the Bug:

  1. Documented Poetry -C Bug (GitHub Issue #9247)

    • Using poetry -C <directory> returns relative paths instead of absolute
    • Affects monorepo workflows and automation tools
  2. Broken/Transitional Virtual Environment States

    • Empty .venv directory exists but is incomplete
    • Interrupted Poetry operations (e.g., during poetry install)
    • Race conditions in complex development environments
  3. Sophisticated Development Setups

    • Monorepos with git worktrees
    • Runtime managers like mise/asdf with nested configurations
    • Concurrent operations between automation tools and shell hooks

How to Reproduce:

Method 1: Create broken venv state

cd your-poetry-project
rm -rf .venv && mkdir .venv
poetry env info --path  # Returns "." instead of full path

Method 2: Monorepo + git worktree setup

# In sophisticated setups with mise, git worktrees, and nested configs
# The issue occurs during directory navigation when:
# 1. Mise enter hooks run poetry commands
# 2. Oh My Zsh chpwd hooks trigger simultaneously
# 3. Race conditions cause Poetry to return "." temporarily

Error Reproduced:

_togglePoetryShell:source:20: no such file or directory: ./bin/activate

Why This Fix "Can't Hurt" Even Though It's an Edge Case:

Defensive Programming Benefits:

  1. Zero Risk: The fix only activates when Poetry returns exactly "." - a clearly broken state
  2. Backward Compatible: No impact on working Poetry setups (centralized or in-project)
  3. Future-Proof: Protects against Poetry regressions or edge cases in newer versions
  4. Graceful Degradation: Adds safety checks that benefit all users

Real-World Impact:

  • Affects Advanced Users: Those using sophisticated development environments
  • Critical When It Happens: Completely breaks virtual environment activation
  • Hard to Debug: Users get cryptic error messages without clear solutions
  • Growing Relevance: More developers using monorepos, worktrees, and automation tools

Maintenance Perspective:

  • Low Maintenance: Simple, focused fix with clear logic
  • Self-Documenting: Code comments explain the exact scenario being handled
  • Testable: Easy to verify the fix works and doesn't break existing functionality
  • Educational: Helps other developers understand Poetry's edge cases

Other comments:

Related PRs:

Why This Approach is Better:

Technical Details:

This issue commonly affects users who:

  • Use Poetry's in-project virtual environments (virtualenvs.in-project = true) - a recommended setup
  • Work with monorepos or git worktrees
  • Have migrated from centralized to in-project virtual environments
  • Use automation tools (mise, asdf, etc.) with complex project structures

Testing:

  • ✅ Tested with Poetry 1.8.5, 2.0.1 (reproduces issue), and 2.1.3 (issue fixed)
  • ✅ Verified fix works with both in-project (virtualenvs.in-project = true) and centralized virtual environments
  • ✅ Confirmed backward compatibility with existing setups
  • ✅ Successfully reproduced original error and verified fix resolves it
  • ✅ Added safety checks to prevent similar issues in the future

Poetry Version Analysis:

While Poetry 2.1.3+ fixes the underlying bug, this defensive programming approach is valuable because:

  • Many users still run older Poetry versions
  • The fix protects against potential regressions
  • It handles edge cases that might still exist in complex environments
  • Zero cost for users with working Poetry installations

The fix is minimal, backward-compatible, and follows the existing code style and patterns used in the plugin.

…urns "."

When Poetry is configured with virtualenvs.in-project = true,
poetry env info --path returns "." instead of the full path.
This causes the plugin to fail with "no such file or directory: ./bin/activate".

Changes:
- Handle case where poetry returns "." by converting to $PWD/.venv
- Add safety check to ensure activate script exists before sourcing
- Maintain backward compatibility with existing setups

Fixes issue with in-project virtual environments used in monorepos and git worktrees.
@ohmyzsh ohmyzsh bot added the Area: plugin Issue or PR related to a plugin label Jun 25, 2025
@ohmyzsh ohmyzsh bot added this to Main project Jun 25, 2025
@github-project-automation github-project-automation bot moved this to Backlog in Main project Jun 25, 2025
@stephanschielke stephanschielke changed the title fix(poetry-env): handle in-project virtual environments when path ret… fix(poetry-env): handle Poetry returning "." for venv path Jun 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: plugin Issue or PR related to a plugin
Projects
Status: Backlog
Development

Successfully merging this pull request may close these issues.

1 participant