fix(core): list a distribution once, however many times sys.path finds it
Found by running, not by reading: `pipdock list` on a venv with one editable install printed the package twice. `importlib.metadata.distributions()` walks sys.path and yields one entry per metadata directory. `pip install -e` puts the project's own src/<name>.egg-info on sys.path via the .pth file, so the package is discovered both there and at the venv's .dist-info. `pip list` shows it once; PipDock showed it twice. Deduplicated by normalized name, first occurrence winning — sys.path precedence, which is the copy an `import` would actually resolve to. This matters more for S2 than for the CLI. The Installed table joins `pkg_list` against `pkg_outdated` on the normalized name, so a duplicate is an ambiguous key, and it renders as two rows a user cannot tell apart. Covered by a new L2 step rather than a unit test: there is no Python test harness, and the condition needs a real editable install to exist at all. The step asserts both of this area's claims — listed once, and reporting no size. **Both halves were exercised**: with the fix reverted the count assertion throws with "got 2", and with it restored the step passes and leaves $LASTEXITCODE at 0, which is the trap the guard step hit in L2's first runs. The pyproject is written as a string array rather than a here-string, because a here-string's closing '@ has to land at column 0 after YAML strips the block indentation. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
e7bc6a5d4d
commit
bd05157a9d
2 files changed
+51
-1
No files matched your search
@@ -188,6 +188,42 @@ jobs:
|
||||
}
|
||||
.\target\debug\pipdock.exe list --env .itest --json | ConvertFrom-Json | Out-Null
|
||||
|
||||
# Two probe claims that only a real editable install can test: it must be listed once
|
||||
# (its own .egg-info joins the venv's .dist-info on sys.path), and it must report no
|
||||
# size (its RECORD lists the import shim, not the sources — a few hundred bytes for a
|
||||
# project of any size, which is worse than reporting nothing).
|
||||
- name: End-to-end — an editable install is listed once, and without a size
|
||||
shell: pwsh
|
||||
run: |
|
||||
$ErrorActionPreference = 'Stop'
|
||||
New-Item -ItemType Directory -Force .itest-editable\src\edmod | Out-Null
|
||||
# An array rather than a here-string: a here-string's closing '@ must sit at column 0
|
||||
# after YAML strips this block's indentation, which makes it break on a re-indent.
|
||||
Set-Content .itest-editable\pyproject.toml @(
|
||||
'[build-system]',
|
||||
'requires = ["setuptools>=64"]',
|
||||
'build-backend = "setuptools.build_meta"',
|
||||
'[project]',
|
||||
'name = "edmod"',
|
||||
'version = "1.0.0"'
|
||||
)
|
||||
Set-Content .itest-editable\src\edmod\__init__.py '"""edmod."""'
|
||||
# 2 MiB of real content, so a wrong size would be unmistakable.
|
||||
Set-Content .itest-editable\src\edmod\payload.py ('DATA = "' + ('y' * 2MB) + '"')
|
||||
.itest\Scripts\python.exe -m pip install --quiet -e .itest-editable
|
||||
|
||||
$dists = .\target\debug\pipdock.exe list --env .itest --json | ConvertFrom-Json
|
||||
$ed = @($dists | Where-Object { $_.name -eq 'edmod' })
|
||||
if ($ed.Count -ne 1) {
|
||||
throw "edmod should be listed once, got $($ed.Count) — probe.py is not deduplicating"
|
||||
}
|
||||
if ($null -ne $ed[0].sizeBytes) {
|
||||
throw "an editable install must report no size, got $($ed[0].sizeBytes) bytes"
|
||||
}
|
||||
$sized = @($dists | Where-Object { $null -ne $_.sizeBytes })
|
||||
if ($sized.Count -lt 1) { throw "no distribution reported a size at all" }
|
||||
Write-Host "$($dists.Count) distributions, $($sized.Count) with a size, edmod correctly without"
|
||||
|
||||
fixture-drift:
|
||||
name: Re-capture against the latest engines
|
||||
# Weekly rather than nightly: it hits PyPI hard enough that daily would be rude, and engine
|
||||
|
||||
@@ -172,10 +172,20 @@ def _dists() -> list[dict[str, object]]:
|
||||
A single unreadable distribution must not fail the whole probe -- broken
|
||||
metadata in one package is common and the user still needs the rest of the
|
||||
list. Such entries are reported with an `error` field instead.
|
||||
|
||||
Distributions are deduplicated by normalized name, **first occurrence winning**.
|
||||
`importlib.metadata.distributions()` walks sys.path in order and yields one entry per
|
||||
metadata directory it finds, so a package discoverable twice is reported twice -- most
|
||||
commonly after `pip install -e`, where the project's own src/<name>.egg-info joins the
|
||||
venv's .dist-info via the .pth file. `pip list` shows such a package once, and so must
|
||||
PipDock: a duplicate row means an ambiguous key when the Installed table joins against
|
||||
the outdated set, and two rows the user cannot tell apart. First-wins matches sys.path
|
||||
precedence, which is the copy an `import` would actually resolve to.
|
||||
"""
|
||||
from importlib.metadata import distributions
|
||||
|
||||
out: list[dict[str, object]] = []
|
||||
seen: set[str] = set()
|
||||
for dist in distributions():
|
||||
try:
|
||||
meta = dist.metadata
|
||||
@@ -188,9 +198,13 @@ def _dists() -> list[dict[str, object]]:
|
||||
raw_name = meta.get("Name")
|
||||
if not raw_name:
|
||||
continue
|
||||
name = _normalize(raw_name)
|
||||
if name in seen:
|
||||
continue
|
||||
seen.add(name)
|
||||
out.append(
|
||||
{
|
||||
"name": _normalize(raw_name),
|
||||
"name": name,
|
||||
"version": dist.version or "",
|
||||
"requires_dist": list(dist.requires or []),
|
||||
"requires_python": meta.get("Requires-Python"),
|
||||
|
||||
Reference in new issue
Block a user