Repository navigation
Scan only a no-venv Python project's own packages, not the whole machine's - #2170
Conversation
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>
PR SummaryMedium Risk Overview
Docs ( Reviewed by Cursor Bugbot for commit e05041a. Configure here. |
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes using high effort and found 1 potential issue.
❌ 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>
…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>

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 onPATH, 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.txtwith comments and no requirement:scan --mode hostedSwitched 0 packages, 9 ×Warning: No requirements.txt entry for <pkg>No packages found.scan --mode vendored --dry-runPatches to vendorNo packages found.scan --mode agent --dry-runPatches to applyNo packages found.scan --globalThe 9 packages were Poetry's own (installed as a uv tool), Homebrew's Python packages, and the
pip/setuptoolsthat 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)python3(elsepython, elsepy) onPATH, asked from the project directory so a version manager's shim follows its.python-version, and only the directories that interpreter reports.--global, which is unchanged.PATHthat sits in a venv (a Docker image'sENV PATH=/opt/venv/bin:$PATH, with noVIRTUAL_ENV) is the project's own environment and is read like an activated one: the venv's directories, then what itspyvenv.cfglets 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)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.*requirements*.txt/*requirements*.infile at the top level and one directory down (docs/requirements.txt), every.txt/.infile in a top-levelrequirements/directory, their-rincludes, and the PEP 621[project]dependencies/optional-dependenciesand PEP 735[dependency-groups]ofpyproject.toml..socket/manifest.jsonrecords 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.pyproject.tomlthat configures tools, and no requirements file is found. A requirements file with no requirement in it is a statement that there are none.setup.pyorsetup.cfg; apoetry.lock,pdm.lock,uv.lockor PEP 751 / PEP 723 lock;dynamicdependencies; 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
apply,rollback,vex): a recorded patch names its package.get <name>andshow <name>: a name resolves among every package installed where the project installs, as its PURL would.scan --prunekeeps a record whose copy is still patched (scan/gc.rs)pypi_installed_patch_keptwarning naming therollbackthat 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.--global/--global-prefixkeeps every entry whose package is installed where the project installs, declared or not, as before.A clearer empty result (
scan/render.rs)PATHwas left out as undeclared ends withNo 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
pipandsetuptools, are no longer offered by a project scan unless the project declares or requires them.--globalstill reaches them, and so doesget <name>.PATH(saypython3.11 -m pip install …whilepython3is 3.14) is no longer found by a project scan. Use a virtualenv, put that interpreter first onPATH, or use--global.rollback <purl> --global. Its record is not pruned while that copy is patched.Code review
Two
/code-review highpasses. The first, on the first commit, reported nine findings. All nine are fixed in the second commit:scan --prune/--syncdropped 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.PATHwas treated as a shared interpreter and scoped. It is now the project's own environment.…/archive/release-1.0.zip) was read as declaring a package calledrelease. A name is now taken from an archive only when it is a wheel.[tool.rye] dev-dependencieswere neither read nor treated as unread. Both now leave the crawl unscoped.pyproject.tomlthat is a link leaving the repository read as absent. It now leaves the crawl unscoped.requirements/were not read. Files one directory down are read, and a linkedrequirements/directory is followed.get <name>resolved names against the scoped crawl. It now lists every installed package.Requires-Distwas 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.The second pass, on the first two commits, reported eight. Seven are fixed in the third commit:
--global/--global-prefixjudged 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.basewas treated as the project's own environment. It is shared.PATHalso 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 whileget <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:
rollback --global. The skipped prune now reportspypi_installed_patch_keptfor 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_lockfilewire 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 underHOME. The fallback does not read that when an interpreter answers, so the test now installs the declared package in a stub interpreter onPATH. 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 warningsandcargo check --workspace --all-targets --all-features: clean.cargo test -p socket-patch-core --lib: 8336 passed.--test crawler_python_e2e: 68 passed.cargo test -p socket-patch-cli --lib --binsand the 56 CLI test targets that touch Python projects: all passing, includingin_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 thein_process_redirect_*suites.PATH, the no-interpreter stand-ins and the scoped crawl (stub interpreter onPATH); in-processscantests (all three modes, and a recorded package), aget <name>test and ashow <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 newscantest and the fallback integration tests fail.requirements.txtnamingcertifi, which thePATHinterpreter has installed, is found and would be switched to the hosted patch.Not in this PR
==whose installed copy is another version still counts as the project's by name.redirect_requirements_entry_not_foundstill names no remedy when a real transitive dependency has a patch and no line to pin it on.🤖 Generated with Claude Code