Skip to content

Scan only a no-venv Python project's own packages, not the whole machine's - #2170

Merged
Mikola Lysenko (mikolalysenko) merged 8 commits into
mainfrom
fix/python-system-fallback-scope
Nov 20, 2026
Merged

Mikola Lysenko (mikolalysenko) merged 8 commits into
mainfrom
fix/python-system-fallback-scope

Conversation

@mikolalysenko

@mikolalysenko Mikola Lysenko (mikolalysenko) commented Nov 19, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

A project with a Python marker (requirements.txt, pyproject.toml, …) and no environment of its own fell back to the same site-packages list as --global: the interpreter on PATH, every other interpreter on the machine, and the environments tools keep for themselves (uv tool install, pipx apps, the Poetry installer, uv- and PDM-managed interpreters). Every package found there was treated as the project's.

Measured with 5.1.0 on a developer Mac, in a directory holding only a requirements.txt with comments and no requirement:

Command 5.1.0 This branch
scan --mode hosted 60 packages found, 9 with patches, Switched 0 packages, 9 × Warning: No requirements.txt entry for <pkg> No packages found.
scan --mode vendored --dry-run the same 9 listed under Patches to vendor No packages found.
scan --mode agent --dry-run the same 9 listed under Patches to apply No packages found.
scan --global 60 Python packages 60 Python packages

The 9 packages were Poetry's own (installed as a uv tool), Homebrew's Python packages, and the pip / setuptools that uv-managed interpreters ship. None was the project's. Hosted mode wrote nothing, so there the result was only noise. Vendored and agent mode list the same packages as work to do.

What changes

One interpreter (python_crawler.rs)

  • The no-venv fallback reads the interpreter the project installs into: python3 (else python, else py) on PATH, asked from the project directory so a version manager's shim follows its .python-version, and only the directories that interpreter reports.
  • When no interpreter answers, the well-known interpreter locations stand in for it.
  • The other interpreters on the machine and the tool environments are read only under --global, which is unchanged.
  • When no interpreter answers, the stand-in locations are shared whatever environments sit among them.
  • An interpreter on PATH that sits in a venv (a Docker image's ENV PATH=/opt/venv/bin:$PATH, with no VIRTUAL_ENV) is the project's own environment and is read like an activated one: the venv's directories, then what its pyvenv.cfg lets in. So is one in a conda environment other than the root. Everything in such an environment is the project's. The conda root (base) is shared: it is the machine's default interpreter and carries conda's own dependencies.

Only the project's packages (new python_crawler/scope.rs)

  • Any other interpreter is shared with everything else installed there, so discovery keeps the packages the project declares and the installed packages those require through Requires-Dist, transitively. A requirement behind an extra counts only when that extra is asked for, by a declaration (requests[socks]) or by a requiring package. No other marker is evaluated. This is the Python counterpart of the Maven local-repository scope.
  • Declarations read: every *requirements*.txt / *requirements*.in file at the top level and one directory down (docs/requirements.txt), every .txt / .in file in a top-level requirements/ directory, their -r includes, and the PEP 621 [project] dependencies / optional-dependencies and PEP 735 [dependency-groups] of pyproject.toml.
  • A package .socket/manifest.json records a patch for is the project's too, declared or not (the package itself, not what it requires): get <name> patched it on purpose, and it keeps getting newer patches.
  • The crawl also stays unscoped when nothing states the project's dependencies: its only marker is a pyproject.toml that configures tools, and no requirements file is found. A requirements file with no requirement in it is a statement that there are none.
  • The crawl stays unscoped, as before, when a declaration is not read in full: a setup.py or setup.cfg; a poetry.lock, pdm.lock, uv.lock or PEP 751 / PEP 723 lock; dynamic dependencies; dependencies in a [tool.poetry], [tool.pdm], [tool.uv], [tool.rye] or [tool.flit] table; Hatch environments (hatch.toml, [tool.hatch.envs]); a requirement on a local directory other than the project itself, on an archive that is not a wheel, or on a url that names no package; a ${VAR} in a requirements line; a declaration that cannot be read (a missing include, a link leaving the repository).

What is not scoped

  • Lookups by PURL (apply, rollback, vex): a recorded patch names its package.
  • get <name> and show <name>: a name resolves among every package installed where the project installs, as its PURL would.

scan --prune keeps a record whose copy is still patched (scan/gc.rs)

  • The prune judges manifest entries against the crawl, and the crawl now reports fewer packages. A PyPI entry the crawl does not report is kept while an installed copy still carries the patch, with a pypi_installed_patch_kept warning naming the rollback that restores the copy and drops the record. This is the rule Cargo entries already have (cargo_cache_patch_kept). Copies outside the project's interpreter are looked for only when the project installs into a shared one.
  • A prune under --global / --global-prefix keeps every entry whose package is installed where the project installs, declared or not, as before.
  • A prune that an empty crawl skips drops no record, and reports the same warning for each recorded PyPI patch that is still on disk.

A clearer empty result (scan/render.rs)

  • A human-mode scan that finds nothing because every package in the interpreter on PATH was left out as undeclared ends with No packages found: the N packages installed in the Python on PATH are not ones this project declares… in place of "install first".

A project with a venv, or whose manager owns its environment (uv, Pipenv, Poetry), is not affected.

Behaviour changes to know about

  • A project installed straight into a system Python keeps its declared packages and their requirements. Packages that are only pre-installed there, such as pip and setuptools, are no longer offered by a project scan unless the project declares or requires them. --global still reaches them, and so does get <name>.
  • A project installed with an interpreter other than the one on PATH (say python3.11 -m pip install … while python3 is 3.14) is no longer found by a project scan. Use a virtualenv, put that interpreter first on PATH, or use --global.
  • A patch that an earlier release applied to a copy in another interpreter or a tool environment, from a project scan, is reached by rollback <purl> --global. Its record is not pruned while that copy is patched.

Code review

Two /code-review high passes. The first, on the first commit, reported nine findings. All nine are fixed in the second commit:

  1. scan --prune / --sync dropped the record and before-blobs of a patch whose package the narrowed crawl no longer reports, leaving the copy patched with nothing to restore it from. Fixed with the keep rule above.
  2. A venv or conda environment reached only through PATH was treated as a shared interpreter and scoped. It is now the project's own environment.
  3. A url ending in a forge archive (…/archive/release-1.0.zip) was read as declaring a package called release. A name is now taken from an archive only when it is a wheel.
  4. Hatch environment dependencies and [tool.rye] dev-dependencies were neither read nor treated as unread. Both now leave the crawl unscoped.
  5. A pyproject.toml that is a link leaving the repository read as absent. It now leaves the crawl unscoped.
  6. Requirements files outside the top level and requirements/ were not read. Files one directory down are read, and a linked requirements/ directory is followed.
  7. get <name> resolved names against the scoped crawl. It now lists every installed package.
  8. Every Requires-Dist was followed, extras included, so one declared package with a large optional extra pulled unrelated installed packages back in. Extras are followed only when asked for.
  9. The Hatch test fixture (below) passed on a package the scan never asked about. It now installs the fixture's own package.

The second pass, on the first two commits, reported eight. Seven are fixed in the third commit:

  1. A prune under --global / --global-prefix judged the project's entries against the scoped crawl and could drop the record of an undeclared, still-patched package. It uses the unscoped listing again.
  2. With no interpreter to ask, one conda root or pyenv-virtualenv among the stand-in locations switched the scope off for all of them. The stand-ins are always shared.
  3. A package patched on purpose but not declared got no newer patch and a roll-back warning on every prune. A recorded package is in the scope.
  4. The prune's machine-wide lookup ran for every project, so a venv project's stale record was kept by another project's patched copy. It runs only for a project that installs into a shared interpreter.
  5. A project whose requirements file is not in a place the scope reads was treated as declaring nothing. The crawl stays unscoped when nothing states dependencies.
  6. The auto-activated conda base was treated as the project's own environment. It is shared.
  7. A venv found through PATH also reported the interpreter's user site, which it does not import from. It is read like an activated venv.

Bugbot then reported that show <name> still resolved names against the scoped crawl while get <name> did not. Fixed in the fourth commit: both use the same unscoped listing.

The last finding of the second pass is fixed in the fifth commit:

  1. When the crawl is empty the prune is skipped, so a project whose only records are for copies in other interpreters got no pointer to rollback --global. The skipped prune now reports pypi_installed_patch_kept for each recorded patch still on disk. The same review asked for a notice when the scope leaves installed packages out: a scan that finds nothing for that reason now says so in its closing line. A scan that does find the project's packages prints nothing about the others, since that line would appear on every scan of a project that shares Homebrew's or a distro's Python.

A test fixture that depended on the old behaviour

The Hatch cells of e2e_vex_lockfile wire a checkout that has no lockfile and nothing installed. They passed only because the host's own Python packages made the scan send a batch query, which the mock API answers with the fixture's patch whatever is asked. With the scope in place the host's packages are not the project's, so those 11 tests failed. The scan helper (vex_pdm_hatch_common::run_scan) now activates, for a hosted scan of a project with no lock and no environment, an environment where the fixture's own package is installed at its patched bytes, so the scan asks the API about the right package.

Hatch pins are not part of the PyPI lock inventory, so a fresh Hatch checkout has no candidates in hosted mode against the real API. That was already the case and is not changed here.

pixi_project_that_pip_also_installs_keeps_the_interpreter_crawl, which came in with #2166, staged its package in a conda root under HOME. The fallback does not read that when an interpreter answers, so the test now installs the declared package in a stub interpreter on PATH. A pixi project that pip also installs still falls back to the interpreter; a project only pixi installs still does not.

Verification

  • cargo clippy --locked --workspace --all-features -- -D warnings and cargo check --workspace --all-targets --all-features: clean.
  • After merging main (with Patch pixi projects: find .pixi/envs without activation #2166): cargo test -p socket-patch-core --lib: 8336 passed. --test crawler_python_e2e: 68 passed.
  • cargo test -p socket-patch-cli --lib --bins and the 56 CLI test targets that touch Python projects: all passing, including in_process_python_envs, in_process_scan, in_process_get, scan, apply, vex, rollback, remove, vendor, e2e_cargo, mode_migration_pypi, scan_requirements_lock_only, e2e_vex_lockfile (356) and the in_process_redirect_* suites.
  • New tests: 14 unit tests for the scope; five integration tests for the fallback's reach, an environment on PATH, the no-interpreter stand-ins and the scoped crawl (stub interpreter on PATH); in-process scan tests (all three modes, and a recorded package), a get <name> test and a show <name> test; a prune test for the keep rule; binary tests for the skipped prune's warning and the empty-result line. With the fallback put back the way it was, the new scan test and the fallback integration tests fail.
  • The table above, re-run with a build of this branch. A requirements.txt naming certifi, which the PATH interpreter has installed, is found and would be switched to the hosted patch.

Not in this PR

  • A requirement pinned with == whose installed copy is another version still counts as the project's by name.
  • redirect_requirements_entry_not_found still names no remedy when a real transitive dependency has a patch and no line to pin it on.

🤖 Generated with Claude Code

A project with a Python marker (requirements.txt, pyproject.toml, ...) and
no environment of its own fell back to the same site-packages list as
`--global`: the interpreter on PATH, every other interpreter on the
machine, and the environments tools keep for themselves (`uv tool
install`, pipx apps, the Poetry installer, uv- and PDM-managed
interpreters). Every package found there was offered as the project's.
Hosted mode then warned `No requirements.txt entry for <pkg>` once per
patch, vendored mode listed the same packages to vendor into the project,
and agent mode listed them to patch in place.

Two changes to the Python crawler:

- The fallback reads one interpreter: the `python3` (else `python`, `py`)
  on PATH, asked from the project directory, and only the directories it
  reports. The well-known interpreter locations stand in when no
  interpreter answers. Other interpreters and tool environments are read
  only under `--global`.
- That interpreter is shared, so discovery (`crawl_all`) keeps only the
  packages the project declares and the installed packages those require
  through `Requires-Dist`. Declarations read: `*requirements*.txt` / `.in`
  files, a `requirements/` directory, their `-r` includes, and the PEP 621
  and PEP 735 tables of pyproject.toml. A declaration that is not read in
  full (setup.py, setup.cfg, another manager's lock, dynamic dependencies,
  a tool's own dependency table, a requirement on a local directory, a
  `${VAR}`, an unreadable include) leaves the crawl unscoped. Lookups by
  PURL are not scoped: a recorded patch names its package.

The Hatch cells of the lockfile VEX suite wire a checkout with no lockfile
and nothing installed. They passed only because the host's own Python
packages made the scan query the mock API, which answers any query with
the fixture's patch. The scan helper now activates a placeholder
environment for those cells.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
- `scan --prune` no longer drops the record of a PyPI patch whose package
  the crawl does not report while an installed copy still carries the
  patch. The record holds the only before-blobs that can restore the
  copy. It is kept with a `pypi_installed_patch_kept` warning naming the
  `rollback` that restores the copy, as Cargo entries already are.
- An interpreter on PATH that sits in a venv or a conda environment is
  the project's own environment, like an activated one, and is not
  scoped.
- A package name is taken from an archive requirement only when it is a
  wheel. An sdist or a forge archive (`.../archive/release-1.0.zip`) is
  named however its author chose, so it leaves the crawl unscoped.
- Hatch environments (`hatch.toml`, `[tool.hatch.envs]`) and
  `[tool.rye] dev-dependencies` leave the crawl unscoped.
- A `pyproject.toml` that is there but cannot be read (a link leaving
  the repository) leaves the crawl unscoped instead of reading as absent.
- Requirements files one directory down (`docs/requirements.txt`) are
  read, a linked `requirements/` directory is followed, and a path that
  reaches the project root from the file's directory (`-e ..`) is the
  project itself.
- `get <name>` resolves a name among every package installed where the
  project installs, as its PURL would.
- A requirement behind an extra is followed only when that extra is
  asked for, by a declaration or by a requiring package. One declared
  package with a large optional extra no longer pulls unrelated installed
  packages into the scope.
- The Hatch scan helper of the lockfile VEX suite installs the fixture's
  own package, at its patched bytes, in the environment it activates, so
  the scan asks the API about the right package.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…lback-scope

# Conflicts:
#	crates/socket-patch-cli/src/commands/get.rs
- A package the project's patch manifest records stays one of its
  packages when no requirements file names it (`get <name>` patched it on
  purpose). Left out of the scope, it got no newer patch and every
  `scan --prune` reported its record as one to roll back.
- With no interpreter to ask, the well-known locations that stand in for
  it are always shared. A conda root or a pyenv-virtualenv among them no
  longer made the whole list the project's own environment.
- A venv reached through PATH is read like an activated one: its own
  directories, then what its `pyvenv.cfg` lets in. The interpreter's user
  site, which such a venv does not import from, is no longer crawled.
- The conda root (`base`) is the machine's default interpreter and
  carries conda's own dependencies, so it is shared and scoped. A conda
  environment other than the root is still the project's own.
- The crawl stays unscoped when nothing states the project's
  dependencies: its only marker is a `pyproject.toml` that configures
  tools and no requirements file is found. Its requirements are then kept
  somewhere the scope does not look.
- `scan --prune` looks for still-patched copies outside the project's
  interpreter only for a project that installs into a shared one. A
  project with an environment of its own never patched a copy outside
  it, so another project's patched copy no longer keeps its stale record.
- A prune under `--global` / `--global-prefix` keeps every entry whose
  package is installed where the project installs, declared or not, as
  it did before the scope.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@mikolalysenko
Mikola Lysenko (mikolalysenko) marked this pull request as ready for review November 20, 2026 13:19
@cursor

cursor Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

PR Summary

Medium Risk
Touches Python discovery, scan batching, prune/GC, and get name resolution across many project shapes; wrong scoping could miss packages or retain stale manifest entries, though fail-open unscoped paths and extensive tests reduce that risk.

Overview
Narrows PyPI discovery for projects with no dedicated environment (e.g. only requirements.txt on the system interpreter on PATH). Those installs no longer reuse the full --global package set; site paths come from that interpreter (or well-known stand-ins), and scan sees only declared dependencies (requirements / PEP 621 / PEP 735), their transitive Requires-Dist (with extra-aware filtering), and packages already in .socket/manifest.json. When declarations can’t be read fully (setup.py, Poetry/uv locks, Hatch env tables, etc.) or nothing states deps, behavior stays unscoped like before.

get <name> uses a separate crawl (crawl_ecosystems_for_name / crawl_all_installed) so name resolution still searches every package in the project’s interpreter, not just declared ones. PURL-based apply / rollback / vex are unchanged (unscoped).

scan --prune mirrors Cargo: PyPI manifest rows the crawl no longer lists are kept while an on-disk copy still has the patch, with a new pypi_installed_patch_kept warning; global prune still accounts for all installed packages in the project interpreter via the unscoped crawl for “project” scope.

Docs (CLI_CONTRACT.md, ecosystems.md) and tests cover shared-interpreter edge cases (venv/conda on PATH, empty requirements, recorded packages).

Reviewed by Cursor Bugbot for commit e05041a. Configure here.

@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.

Cursor Bugbot has reviewed your changes using high effort and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Want fixes drafted automatically? Bugbot Autofix can create code changes for findings. A team admin can enable Autofix in the Cursor dashboard.

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

Reviewed by Cursor Bugbot for commit e05041a. Configure here.

`show <name>` looked the name up in the scoped crawl, so in a Python
project with no environment of its own it reported an installed package
the project does not declare as not installed. `get <name>` already
used the unscoped listing; `show` now shares it, as the two share the
name matcher.

The plain `crawl_ecosystems` has no caller left outside the tests, so it
is compiled for them only.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@mikolalysenko Mikola Lysenko (mikolalysenko) added the Ready for review Agent-verified: mergeable, CI green, Bugbot clean — awaiting human review label Nov 20, 2026
…lback-scope

# Conflicts:
#	crates/socket-patch-core/src/crawlers/python_crawler.rs
#	docs/ecosystems.md
…ges were left out

- When the crawl is empty the manifest half of `scan --prune` is skipped,
  so a project whose only records are for copies outside its interpreter
  got no pointer to the `rollback --global` that restores them. The
  skipped prune now reports `pypi_installed_patch_kept` for each recorded
  PyPI patch that is still on disk. No record is dropped on that path.
- A scan that finds nothing because every package in the interpreter on
  PATH was left out as undeclared now ends with a line saying so, in
  place of "install first", which sent the user to install what is
  already there.
- A pixi project that pip also installs keeps the interpreter crawl. Its
  test staged the package in a conda root under HOME, which the fallback
  does not read when an interpreter answers; it now installs the declared
  package in a stub interpreter on PATH, and the three tests that use one
  share the stub.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Merged via the queue into main with commit fc18188 Nov 20, 2026
60 checks passed
@mikolalysenko
Mikola Lysenko (mikolalysenko) deleted the fix/python-system-fallback-scope branch November 20, 2026 17:27
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

Development

Successfully merging this pull request may close these issues.

3 participants