Skip to content

Fix Poetry venv selection ignoring envs.toml (#476, #526) - #527

Merged
Mikola Lysenko (mikolalysenko) merged 2 commits into
mainfrom
agent/fix-poetry-env-selection
Oct 2, 2026
Merged

Mikola Lysenko (mikolalysenko) merged 2 commits into
mainfrom
agent/fix-poetry-env-selection

Conversation

@mikolalysenko

@mikolalysenko Mikola Lysenko (mikolalysenko) commented Oct 2, 2026 •

Copy link
Copy Markdown
Collaborator

LLM Description written by Claude Code:claude-opus-5-5

Fixes #476
Fixes #526

Summary

Agent mode, hosted mode's stale-install check, and VEX now use the Poetry virtualenv that Poetry itself uses. Before this change, two common Poetry setups led socket-patch to patch (or inspect) the wrong environment. The env behind poetry run stayed unpatched while the run reported success, and vex attested not_affected.

Root cause

Poetry venv discovery in crates/socket-patch-core/src/crawlers/python_crawler.rs approximated Poetry's EnvManager.get() instead of following it:

Fix

Follow EnvManager.get() for Poetry projects:

  • load_poetry_project reads envs.toml under virtualenvs.path ([<name>-<hash>] minor = "X.Y"). When the project has an entry, only <name>-<hash>-pyX.Y is probed.
  • The VIRTUAL_ENV shortcut is gated the way Poetry gates it: it's skipped when envs.toml has an entry, CONDA_PREFIX is honoured like VIRTUAL_ENV, and conda's base env doesn't count as an activated venv. This mirrors the existing Pipenv gate (pipenv_uses_virtual_env).
  • ./.venv is the env only when it exists and in-project isn't an explicit false. virtualenvs.path is resolved whatever in-project says, and create = false still opts out.
  • load_poetry_project now takes the injected environment instead of reading the process env. This also removes a race between the new tests and the existing serial env-mutating test.
  • docs/testing/poetry-compatibility.md "Mode notes" are updated to match.

Hosted mode's redirect_pypi_stale_install check and vex both go through find_local_venv_site_packages, so they pick up the fix with no further changes. There are no wrapper changes: npm/, pypi/ and gem/ only dispatch to the binary.

Per-issue checklist

Issue Regression test main this PR
#526 activated env (3.12/3.9/3.11 among 3.9–3.12; 3.10 vs 3.9 lexicographic trap) crawlers::python_crawler::tests::poetry_envs_toml_activated_env_is_the_only_one_probed ❌ all 4 envs returned ✅
#526 envs.toml beats VIRTUAL_ENV; conda prefix / base crawlers::python_crawler::tests::poetry_envs_toml_entry_overrides_virtual_env ❌ VIRTUAL_ENV returned ✅
#476 in-project = true (poetry.toml and POETRY_VIRTUALENVS_IN_PROJECT) with no ./.venv crawlers::python_crawler::tests::poetry_in_project_true_without_dot_venv_keeps_the_out_of_tree_env ❌ [] ✅

Two existing assertions encoded the #476 behaviour (in-project = true ⇒ nothing probed, and a None virtualenvs root). They now assert Poetry's behaviour instead.

Test evidence (Linux)

Follow-ups (not in scope)

🤖 Generated with Claude Code

https://claude.ai/code/session_01Rh6wkfBxavb2aj1JWKbSxM


Note

Medium Risk
Changes which site-packages paths agent mode patches—a wrong choice previously left the real Poetry env unpatched; the new logic is more complex but targets documented Poetry behavior with regression tests.

Overview
Agent-mode Python crawling now picks the same virtualenv Poetry uses, instead of approximating it.

envs.toml (#526): The crawler reads <virtualenvs.path>/envs.toml for the env poetry env use recorded and probes only that <name>-<hash>-py<minor> tree. When that entry exists, an unrelated VIRTUAL_ENV is ignored; otherwise VIRTUAL_ENV or a non-base CONDA_PREFIX can still win, matching Poetry’s EnvManager.get().

In-project setting (#476): virtualenvs.in-project = true no longer blocks out-of-tree discovery when ./.venv is missing—only an existing ./.venv (and not an explicit in-project = false) counts as the in-project env. virtualenvs.path / envs.toml resolution is unchanged by in-project.

load_poetry_project takes an injected env lookup for tests; regression tests and docs/testing/poetry-compatibility.md agent-mode notes are updated. Anything that already uses find_local_venv_site_packages (hosted stale-install checks, VEX, etc.) inherits the fix.

Reviewed by Cursor Bugbot for commit 0b4a2df. Configure here.


Generated by Claude Code

Assisted-by: Claude Code:claude-opus-5-5
Agent mode, hosted mode's stale-install check and VEX picked the wrong
Poetry virtualenv in two common setups, so the env `poetry run` uses
stayed unpatched while the run reported success and VEX attested
not_affected:

- After `poetry env use 3.12` on a project that also has a 3.11 env,
  the alphabetically first env was patched. An unrelated activated
  VIRTUAL_ENV also won, although Poetry ignores it once envs.toml names
  the project (#526).
- With `virtualenvs.in-project = true` but no ./.venv, the existing
  out-of-tree env Poetry keeps using was never probed (#476).

Venv discovery now follows Poetry's EnvManager.get(): it reads
envs.toml under virtualenvs.path, takes ./.venv only when it exists and
in-project isn't false, and gates VIRTUAL_ENV / CONDA_PREFIX the way
Poetry does.

Assisted-by: Claude Code:claude-opus-5-5
@mikolalysenko
Mikola Lysenko (mikolalysenko) marked this pull request as ready for review October 2, 2026 03:55
@mikolalysenko

Copy link
Copy Markdown
Collaborator Author

BugBot review


Generated by Claude Code

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

✅ Bugbot reviewed your changes and found no new issues!

Comment @cursor review or bugbot run to trigger another review on this PR

Reviewed by Cursor Bugbot for commit 0b4a2df. Configure here.

@mikolalysenko

Copy link
Copy Markdown
Collaborator Author

[agent] Ready for review at 0b4a2df.

  • CI: 333/333 non-skipped check runs green, including the real-Poetry e2e_vex_build legs and the Pipenv workflow.
  • Bugbot: no findings on the current head.
  • Mergeable, 0 commits behind main.

Generated by Claude Code

@mikolalysenko

Copy link
Copy Markdown
Collaborator Author

[burn-down agent] Labeled Ready for review at 0b4a2dffb2.

  • CI: 340/340 check runs finished on the head, none failed (success, skipped, or neutral).
  • Bugbot: reviewed 0b4a2dffb2, no new issues. No unresolved review threads.
  • Mergeable against main. Still needs a human approval.

Generated by Claude Code

@mikolalysenko

Copy link
Copy Markdown
Collaborator Author

Reviewed 0b4a2dffb2654381f11426e23469d84a21cd7772. Recommendation: ready to merge as-is from code review.

No actionable correctness/security regressions found. Checked envs.toml selection, activated virtualenv/conda precedence, in-project fallback, and the shared stale-install/VEX discovery path against Poetry's environment manager.

Validation: cargo test -p socket-patch-core --lib crawlers::python_crawler — 57 passed on macOS. Full workspace/platform matrix not rerun.

@mikolalysenko
Mikola Lysenko (mikolalysenko) merged commit 8eec03a into main Oct 2, 2026
340 checks passed
@mikolalysenko
Mikola Lysenko (mikolalysenko) deleted the agent/fix-poetry-env-selection branch October 2, 2026 14:28
Mikola Lysenko (mikolalysenko) pushed a commit that referenced this pull request Oct 2, 2026
Resolve the python_crawler.rs conflict with the Poetry env selection
fix (#527): keep main's poetry_active_prefix for VIRTUAL_ENV and add
the PDM_IGNORE_ACTIVE_VENV gate on top.

Assisted-by: Claude Code:claude-opus-5-5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Ready for review Agent-verified: mergeable, CI green, Bugbot clean — awaiting human review

Projects

None yet

3 participants