diff --git a/CHANGELOG.md b/CHANGELOG.md index 0084d81b5..7dc688a63 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -82,6 +82,12 @@ history. is preserved and reported with manual reinstall guidance. Installer diagnostics stay on stderr in JSON/NDJSON modes, which end with one success or error envelope. +- `comfy node deps` reports malformed installed package versions as `unknown` + consistently across supported `packaging` versions. One metadata probe + preserves raw installed versions with or without pip. Every `===` clause + compares raw strings case-insensitively, including in mixed constraints; + ordinary clauses still require valid versions and use semantic comparison. + - UI→API conversion expands a `COMFY_DYNAMICCOMBO_V3` selector's sub-inputs in the order the workflow was SAVED with, when the saved graph records one, instead of the option's current order. An option that has since gained a diff --git a/comfy_cli/command/node_deps.py b/comfy_cli/command/node_deps.py index e08733aab..0f3e6e233 100644 --- a/comfy_cli/command/node_deps.py +++ b/comfy_cli/command/node_deps.py @@ -8,14 +8,13 @@ ``--registry `` extends the same diff to a pack that is **not yet installed**: the registry's published dependency list for that pack's latest -version, diffed against the very same ``pip list`` map, so conflict risk can be +version, diffed against the same installed-version map, so conflict risk can be assessed *before* installing anything. Read-only by construction: it never installs anything and never shells out to -``cm-cli``. The only subprocess it runs is a single `` -m pip -list --format=json`` for the whole report (not once per pack), plus one -``importlib.metadata`` listing in the same interpreter when that interpreter has -no pip (a ``uv venv`` ships without it). The only network +``cm-cli``. The only subprocess it runs is one guarded ``importlib.metadata`` +listing in the workspace interpreter for the whole report (not once per pack). +It reads raw metadata versions whether or not pip is installed. The only network call is the registry's side-effect-free ``GET /nodes/{id}``, made solely for ``--registry`` ids — never ``install_node``, whose endpoint records an installation and fires an analytics event server-side on every call. @@ -31,9 +30,9 @@ bare VCS/URL line, or anything else :class:`packaging.requirements.Requirement` rejects. Kept in the report rather than dropped silently; - ``unknown`` — the comparison could not be made: either the installed- - packages probe failed (``pip list`` and its ``importlib.metadata`` fallback; - accompanied by an ``installed_versions_unavailable`` warning on the report) - or the installed dist records a version that isn't PEP 440. + packages metadata probe failed (accompanied by an ``installed_versions_unavailable`` warning on the report) + or the installed dist records a version that isn't PEP 440 and the requirement + includes an operator other than literal ``===`` equality. Known caveat: pyproject dependencies are read through the shared registry parser (:func:`comfy_cli.command.pack_scan.read_pyproject`), which drops the @@ -102,24 +101,29 @@ # --------------------------------------------------------------------------- -# Fallback probe for an interpreter without pip (a ``uv venv`` has none): the -# same ``[{"name", "version"}]`` shape as ``pip list --format=json``, read with -# the stdlib inside that interpreter. +# Read raw Name/Version metadata with the workspace interpreter's stdlib. +# `pip list` normalizes valid versions, losing spellings needed by `===`. +# The JSON shape stays [{"name", "version"}], with no pip dependency. # -# - Before anything that could be shadowed is imported, every ``sys.path`` entry -# naming the caller's directory is dropped (the ``-c`` ``''`` entry and e.g. -# ``PYTHONPATH=.``), so a stray ``json.py`` or ``*.dist-info`` there can't leak -# in. ``sys`` is built in and ``os`` is already loaded at startup, so neither -# import reads the path. +# - The probe runs from the caller's directory, so a relative interpreter path +# and relative ``PYTHONPATH`` entries keep the meaning they had there. The +# caller's directory itself never reaches ``sys.path``: ``_probe_env`` drops +# every ``PYTHONPATH`` entry naming it (``.``, an empty entry, its absolute +# path or a symlink to it), and the script's first line drops the ``-c`` +# ``''`` entry, before anything that could be shadowed is imported. So a +# stray ``json.py``, ``sitecustomize.py`` or ``*.dist-info`` where the command +# was run can't leak in. ``sys`` is built in, so importing it reads no path. +# - Absolute entries an installation put on ``sys.path`` stay, even when one is +# the caller's directory: a legacy editable install lists its source folder in +# ``easy-install.pth``, and running from that folder must still find it. # - A dist whose metadata can't be read is skipped, as ``pip list`` skips it, # rather than failing the whole listing. # - ``~``-prefixed dist-info dirs (pip's leftovers from an interrupted # uninstall) are skipped, and the first copy of a name in ``sys.path`` order # wins, as it does for ``import``. _METADATA_LIST_SCRIPT = """\ -import os, sys -cwd = os.path.realpath(os.getcwd()) -sys.path[:] = [p for p in sys.path if p and os.path.realpath(p) != cwd] +import sys +sys.path[:] = [p for p in sys.path if p] import json, re from importlib import metadata seen, rows = set(), [] @@ -139,8 +143,38 @@ """ -def _list_installed(cmd: list[str], label: str) -> tuple[dict[str, str] | None, str | None, bool]: - """Run one installed-packages probe; return ``(versions, error, exited_nonzero)``.""" +def _names_caller_directory(entry: str) -> bool: + """True when a ``PYTHONPATH`` entry is the current directory. + + Python reads an empty entry as the current directory. ``samefile`` sees + through relative spellings, symlinks and case; an entry that can't be + stat'ed names nothing and is kept, as Python keeps it. + """ + if not entry: + return True + try: + return os.path.samefile(entry, os.curdir) + except OSError: + return False + + +def _probe_env() -> dict[str, str]: + """The caller's environment, minus ``PYTHONPATH`` entries naming the caller's directory.""" + env = dict(os.environ) + entries = env.pop("PYTHONPATH", "").split(os.pathsep) + kept = [entry for entry in entries if not _names_caller_directory(entry)] + if kept: + env["PYTHONPATH"] = os.pathsep.join(kept) + return env + + +def _list_installed(cmd: list[str], label: str) -> tuple[dict[str, str] | None, str | None]: + """Run one installed-packages probe; return ``(versions, error)``. + + The probe inherits the caller's directory and environment, except that + ``PYTHONPATH`` entries naming that directory are left out (see + ``_probe_env``). + """ from packaging.utils import canonicalize_name try: @@ -150,19 +184,20 @@ def _list_installed(cmd: list[str], label: str) -> tuple[dict[str, str] | None, text=True, timeout=PIP_LIST_TIMEOUT_SECONDS, check=True, + env=_probe_env(), ) except subprocess.CalledProcessError as e: detail = (e.stderr or "").strip().splitlines() - return None, f"{label} exited {e.returncode}" + (f": {detail[-1]}" if detail else ""), True + return None, f"{label} exited {e.returncode}" + (f": {detail[-1]}" if detail else "") except (subprocess.SubprocessError, OSError) as e: - return None, f"could not run {label}: {e}", False + return None, f"could not run {label}: {e}" try: rows = json.loads(proc.stdout) except ValueError as e: - return None, f"could not parse {label} output: {e}", False + return None, f"could not parse {label} output: {e}" if not isinstance(rows, list): - return None, f"{label} did not return a list", False + return None, f"{label} did not return a list" versions: dict[str, str] = {} for row in rows: @@ -171,27 +206,19 @@ def _list_installed(cmd: list[str], label: str) -> tuple[dict[str, str] | None, name, version = row.get("name"), row.get("version") if isinstance(name, str) and name: versions[canonicalize_name(name)] = str(version) if version is not None else "" - return versions, None, False + return versions, None def pip_list(python: str) -> tuple[dict[str, str] | None, str | None]: - """Return ``(canonical_name -> version, error)`` for the workspace venv. - - Runs `` -m pip list --format=json`` once for the whole report. If - that exits non-zero — typically ``No module named pip`` in a uv-created - venv — the same interpreter is asked once more via ``importlib.metadata``. - A timeout or an interpreter that can't be started is not retried: the - fallback would hit the same wall and only double the wait. On failure the - map is ``None`` with a human-readable reason — a report that can't see the - venv is still a useful report. + """Return ``(canonical_name -> raw metadata version, error)`` for the venv. + + The legacy helper name is retained, but pip is never invoked: its display + output normalizes valid versions. One guarded ``importlib.metadata`` probe + preserves raw values and works in pip-less venvs too. A failed or timed-out + probe is not retried; the map is ``None`` with a human-readable reason so + the report can retain requirements with unknown installed versions. """ - versions, pip_error, exited_nonzero = _list_installed([python, "-m", "pip", "list", "--format=json"], "`pip list`") - if versions is not None or not exited_nonzero: - return versions, pip_error - versions, metadata_error, _ = _list_installed([python, "-c", _METADATA_LIST_SCRIPT], "`importlib.metadata`") - if versions is not None: - return versions, None - return None, f"{pip_error}; {metadata_error}" + return _list_installed([python, "-c", _METADATA_LIST_SCRIPT], "`importlib.metadata`") # --------------------------------------------------------------------------- @@ -228,6 +255,22 @@ def _pyproject_lines(path: Path) -> list[str]: return [d.strip() for d in deps if isinstance(d, str) and d.strip()] +# One ``===`` clause and its literal. The literal stops at whitespace, ``,``, +# ``;`` or ``)``, as packaging splits and parses it. +_ARBITRARY_EQUALITY_RE = re.compile(r"===\s*([^\s,;)]*)") + + +def _arbitrary_equality_literals(raw: str) -> list[str]: + """Return every ``===`` literal in a requirement line, as written. + + ``SpecifierSet`` keeps only one of two clauses whose versions normalize + alike: ``foo===v1.0,===1.0`` holds just ``===v1.0``. Each literal is its + own exact-text test, so they are read from the line itself. The marker + after ``;`` is left out, since a marker can use ``===`` too. + """ + return _ARBITRARY_EQUALITY_RE.findall(raw.split(";", 1)[0]) + + def _classify(raw: str, installed_versions: dict[str, str] | None) -> dict[str, Any]: """Turn one declared requirement line into a report row.""" from packaging.requirements import InvalidRequirement, Requirement @@ -273,10 +316,17 @@ def _classify(raw: str, installed_versions: dict[str, str] | None) -> dict[str, # A bare name is satisfied by any installed version. row["status"] = "satisfied" else: - from packaging.version import InvalidVersion + from packaging.version import InvalidVersion, Version + semantic = [spec for spec in req.specifier if spec.operator != "==="] + literals = _arbitrary_equality_literals(raw) try: - contains = req.specifier.contains(installed, prereleases=True) + # Validate before any clause can short-circuit a mixed constraint. + if semantic: + Version(installed) + contains = all(installed.lower() == literal.lower() for literal in literals) and all( + spec.contains(installed, prereleases=True) for spec in semantic + ) except InvalidVersion: # A dist whose recorded version isn't PEP 440 (rare, but a broken # `.dist-info` will do it) can't be compared. Say so rather than @@ -500,7 +550,7 @@ def _registry_report( row["warning"] = message warnings.append({"code": "registry_partial_dependency_metadata", "message": message}) - # Same per-requirement diff, against the same single ``pip list`` map. + # Same per-requirement diff, against the same single installed-version map. rows: dict[str, dict[str, Any]] = {} for line in declared: if line not in rows: @@ -557,14 +607,14 @@ def build_report( workspace = Path(comfy_path) python = python or resolve_workspace_python(comfy_path) - installed_versions, pip_error = pip_list(python) + installed_versions, probe_error = pip_list(python) if installed_versions is None: warnings.append( { "code": "installed_versions_unavailable", "message": ( f"could not read installed packages from {python}" - + (f" ({pip_error})" if pip_error else "") + + (f" ({probe_error})" if probe_error else "") + " — every parseable requirement is reported with status 'unknown'" ), } diff --git a/comfy_cli/error_codes.py b/comfy_cli/error_codes.py index d402b1332..096cf6b42 100644 --- a/comfy_cli/error_codes.py +++ b/comfy_cli/error_codes.py @@ -1313,9 +1313,9 @@ class ErrorCode: # --- custom node dependency report (`comfy node deps`) -------------------- ErrorCode( "installed_versions_unavailable", - "`comfy node deps` could not read the workspace venv's installed packages (`pip list --format=json` " - "timed out, returned unparseable output, or exited non-zero and its `importlib.metadata` fallback " - "failed too), so every parseable requirement is reported with " + "`comfy node deps` could not read the workspace venv's raw installed-package metadata " + "(the guarded `importlib.metadata` probe timed out, returned unparseable output, or failed), " + "so every parseable requirement is reported with " '`status: "unknown"`. Surfaced in `data.warnings[]` (not as an error envelope) so the declared ' "requirements are still reported.", "check the workspace interpreter runs (`comfy env`), then re-run", diff --git a/comfy_cli/schemas/node_deps.json b/comfy_cli/schemas/node_deps.json index 48d20a1e0..b4cdebc95 100644 --- a/comfy_cli/schemas/node_deps.json +++ b/comfy_cli/schemas/node_deps.json @@ -7,7 +7,7 @@ "workspace": { "type": "string", "description": "Resolved ComfyUI workspace path." }, "python": { "type": "string", - "description": "Interpreter whose site-packages were probed (`pip list --format=json`, run once for the whole report; an `importlib.metadata` listing in the same interpreter when it has no pip)." + "description": "Interpreter whose raw Name/Version metadata was read with one guarded importlib.metadata probe for the whole report, whether or not pip is installed." }, "compiled_lock": { "type": "object", @@ -80,12 +80,12 @@ "source": { "type": "string", "enum": ["requirements.txt", "pyproject.toml", "registry"] }, "installed": { "type": ["string", "null"], - "description": "Version found in the workspace venv, or null when missing/unparseable/unknown." + "description": "Raw metadata version found in the workspace venv, or null when missing/unparseable/unavailable." }, "status": { "type": "string", "enum": ["satisfied", "mismatch", "missing", "unparseable", "unknown"], - "description": "`unparseable` covers pip options (-r, --extra-index-url) and bare VCS/URL lines; `unknown` means the comparison could not be made (the pip probe failed, or the installed dist records a non-PEP-440 version)." + "description": "`unparseable` covers pip options (-r, --extra-index-url) and bare VCS/URL lines; `unknown` means the comparison could not be made (the metadata probe failed, or the installed dist records a non-PEP-440 version and the requirement includes an operator other than ===). Each === clause compares raw strings case-insensitively; any other operator requires a PEP-440-valid installed version." } }, "required": ["raw", "name", "specifier", "source", "installed", "status"] @@ -109,7 +109,7 @@ }, "warnings": { "type": "array", - "description": "Non-fatal problems. `installed_versions_unavailable` means the installed-packages probe (pip, then importlib.metadata) failed, so every requirement is `unknown`. `registry_unavailable` (transient — retry) / `registry_node_not_found` (permanent 404 — do NOT retry) / `registry_no_dependency_metadata` mean a `--registry` candidate could not be diffed; `registry_partial_dependency_metadata` means its `declared` list lost malformed entries and is incomplete; `registry_invalid_node_id` means a `--registry` value was blank or outside `[A-Za-z0-9._-]` and was rejected without a request. Every other row is unaffected.", + "description": "Non-fatal problems. `installed_versions_unavailable` means the installed-packages metadata probe failed, so every requirement is `unknown`. `registry_unavailable` (transient — retry) / `registry_node_not_found` (permanent 404 — do NOT retry) / `registry_no_dependency_metadata` mean a `--registry` candidate could not be diffed; `registry_partial_dependency_metadata` means its `declared` list lost malformed entries and is incomplete; `registry_invalid_node_id` means a `--registry` value was blank or outside `[A-Za-z0-9._-]` and was rejected without a request. Every other row is unaffected.", "items": { "type": "object", "properties": { diff --git a/tests/comfy_cli/command/test_node_deps.py b/tests/comfy_cli/command/test_node_deps.py index bfb256d57..8b1dd6d7e 100644 --- a/tests/comfy_cli/command/test_node_deps.py +++ b/tests/comfy_cli/command/test_node_deps.py @@ -1,8 +1,9 @@ """Tests for ``comfy node deps`` — per-pack declared-vs-installed dependencies. Builds a fixture workspace with fake ``custom_nodes//requirements.txt`` + -``pyproject.toml`` under tmp_path and mocks the single ``pip list --format=json`` -subprocess to a fixed payload, so nothing touches a real venv or the network. +``pyproject.toml`` under tmp_path. Most cases mock the single installed-metadata +probe; integration cases use an isolated interpreter and hand-written metadata. +No package installer or network setup is needed. """ from __future__ import annotations @@ -231,7 +232,7 @@ def test_environment_marker_is_surfaced_not_evaluated(tmp_path, fake_pip): # --------------------------------------------------------------------------- -# pip list behavior +# installed-metadata probe behavior # --------------------------------------------------------------------------- @@ -255,15 +256,294 @@ def test_non_pep440_installed_version_is_unknown_not_a_crash(tmp_path, monkeypat assert reqs["weirdpkg"]["status"] == "satisfied" +@pytest.mark.parametrize( + "specifier, installed, expected", + [ + ("===foobar", "foobar", "satisfied"), + ("===FOOBAR", "foobar", "satisfied"), + ("===foobar", "FOOBAR", "satisfied"), + ("===foobar", "other", "mismatch"), + ("===foo", "foobar", "mismatch"), + ("===foobar,===FOOBAR", "foobar", "satisfied"), + ("===foobar,===other", "foobar", "mismatch"), + ("===v1.0", "v1.0", "satisfied"), + ("===1.0", "v1.0", "mismatch"), + ("===1.0", "1.0.0", "mismatch"), + ("===1.0", "1.0+local", "mismatch"), + ("===1.0.*", "1.0.1", "mismatch"), + # Two literals that normalize alike are still two exact-text tests. + ("===v1.0,===1.0", "v1.0", "mismatch"), + ("===1.0,===v1.0", "v1.0", "mismatch"), + ("===v1.0,===V1.0", "v1.0", "satisfied"), + ], +) +def test_arbitrary_equality_compares_raw_installed_values(specifier, installed, expected): + """Literal pins compare strings, including legacy values, without normalization.""" + row = node_deps_cmd._classify("weirdpkg" + specifier, {"weirdpkg": installed}) + + assert row["status"] == expected + assert row["installed"] == installed + + +@pytest.mark.parametrize("specifier", [">=1", "===foobar,>=1", ">=1,===foobar"]) +def test_non_pep440_installed_version_stays_unknown_with_nonliteral_constraints(specifier): + """A literal clause must not bypass validation required by another operator.""" + row = node_deps_cmd._classify("weirdpkg" + specifier, {"weirdpkg": "foobar"}) + + assert row["status"] == "unknown" + assert row["installed"] == "foobar" + + +@pytest.mark.parametrize( + "specifier, installed, expected", + [ + ("===v1.0,>=1", "v1.0", "satisfied"), + (">=1,===v1.0", "v1.0", "satisfied"), + ("===1.0,>=1", "v1.0", "mismatch"), + (">=1,===1.0", "v1.0", "mismatch"), + ("===v1.0,!=2", "v1.0", "satisfied"), + ("===v1.0,==1.0", "v1.0", "satisfied"), + ("===v1.0,<1", "v1.0", "mismatch"), + ("===1.0rc1,>=0", "1.0rc1", "satisfied"), + ("===1.0rc1,<1.0", "1.0rc1", "mismatch"), + ("===other,>=1", "foobar", "unknown"), + (">=1,===other", "foobar", "unknown"), + ("===v1.0,>=1,===1.0", "v1.0", "mismatch"), + ], +) +def test_mixed_constraints_preserve_raw_literals_and_semantic_clauses(specifier, installed, expected): + """Adding an ordinary clause must neither normalize a literal nor skip validation.""" + row = node_deps_cmd._classify("weirdpkg" + specifier, {"weirdpkg": installed}) + + assert row["status"] == expected + assert row["installed"] == installed + + +@pytest.fixture +def isolated_probe_python(tmp_path): + """A real interpreter with no package installers or network setup.""" + import venv + + root = tmp_path / "probe-venv" + venv.EnvBuilder(with_pip=False, symlinks=os.name != "nt").create(root) + python = str(root / ("Scripts/python.exe" if sys.platform == "win32" else "bin/python")) + site = subprocess.run( + [python, "-c", "import sysconfig; print(sysconfig.get_path('purelib'))"], + capture_output=True, + text=True, + check=True, + ).stdout.strip() + return python, Path(site) + + +def _write_dist_info(site: Path, directory: str, name: str, version: str) -> None: + dist = site / directory + dist.mkdir(parents=True) + (dist / "METADATA").write_text(f"Metadata-Version: 2.1\nName: {name}\nVersion: {version}\n") + + +@pytest.mark.parametrize("pip_present", [False, True]) +def test_report_uses_one_raw_metadata_probe_with_or_without_pip( + tmp_path, monkeypatch, isolated_probe_python, pip_present +): + python, site = isolated_probe_python + _write_dist_info(site, "Foo-1.0.dist-info", "Foo", "v1.0") + if pip_present: + # Pip is importable, but must never be the source of raw version data. + # Its output is deliberately not faked: only the real metadata is read. + pip = site / "pip" + pip.mkdir() + (pip / "__init__.py").write_text("") + (pip / "__main__.py").write_text('raise AssertionError("pip must not be invoked")\n') + workspace = tmp_path / "ComfyUI" + _make_pack(workspace, "pack", requirements="foo===v1.0\nfoo===1.0\nfoo>=1\nfoo\nabsent\n") + monkeypatch.setenv("PYTHONPATH", "") + calls = [] + real_run = subprocess.run + + def record(cmd, **kwargs): + calls.append((cmd, kwargs)) + return real_run(cmd, **kwargs) + + monkeypatch.setattr(node_deps_cmd.subprocess, "run", record) + report, warnings = node_deps_cmd.build_report(str(workspace), python=python) + reqs = _reqs_by_raw(_packs_by_name(report)["pack"]) + + assert warnings == [] + assert reqs["foo===v1.0"]["installed"] == "v1.0" + assert reqs["foo===v1.0"]["status"] == "satisfied" + assert reqs["foo===1.0"]["status"] == "mismatch" + assert reqs["foo>=1"]["status"] == "satisfied" + assert reqs["foo"]["status"] == "satisfied" + assert reqs["absent"]["status"] == "missing" + assert len(calls) == 1 + assert calls[0][0] == [python, "-c", node_deps_cmd._METADATA_LIST_SCRIPT] + assert calls[0][1]["timeout"] == 60 + assert calls[0][1]["check"] is True + # The probe runs where the command ran, so relative paths keep their meaning. + assert "cwd" not in calls[0][1] + assert "PYTHONPATH" not in calls[0][1]["env"] + + +@pytest.mark.parametrize("caller_entry", [".", "", "absolute"]) +def test_metadata_probe_excludes_cwd_and_keeps_first_canonical_copy( + tmp_path, monkeypatch, isolated_probe_python, caller_entry +): + python, site = isolated_probe_python + first, second, leftovers, cwd = [tmp_path / name for name in ("first", "second", "leftovers", "caller")] + _write_dist_info(first, "foo_pack-1.0.dist-info", "Foo.Pack", "v1.0") + _write_dist_info(second, "foo_pack-2.0.dist-info", "foo-pack", "v2.0") + _write_dist_info(site, "foo_pack-3.0.dist-info", "foo_pack", "v3.0") + _write_dist_info(leftovers, "~oo_pack-0.1.dist-info", "foo-pack", "v0.1") + _write_dist_info(cwd, "foo_pack-9.0.dist-info", "foo-pack", "v9.0") + (cwd / "json.py").write_text('raise AssertionError("caller json.py imported")\n') + broken = first / "broken-1.0.dist-info" + broken.mkdir() + (broken / "METADATA").write_bytes(b"Name: broken\nVersion: 1.0\nSummary: \xff\xfe\n") + workspace = tmp_path / "ComfyUI" + _make_pack(workspace, "pack", requirements="foo-pack===v1.0\nfoo-pack>=1\nabsent\n") + monkeypatch.chdir(cwd) + caller_entry = str(cwd) if caller_entry == "absolute" else caller_entry + monkeypatch.setenv("PYTHONPATH", os.pathsep.join([caller_entry, str(leftovers), str(first), str(second)])) + + report, warnings = node_deps_cmd.build_report(str(workspace), python=python) + reqs = _reqs_by_raw(_packs_by_name(report)["pack"]) + + assert warnings == [] + assert reqs["foo-pack===v1.0"]["installed"] == "v1.0" + assert reqs["foo-pack===v1.0"]["status"] == "satisfied" + assert reqs["foo-pack>=1"]["status"] == "satisfied" + assert reqs["absent"]["status"] == "missing" + + +@pytest.mark.parametrize("pythonpath", ["", ".", "absolute"]) +def test_legacy_editable_is_found_when_run_from_its_source_directory( + tmp_path, monkeypatch, isolated_probe_python, pythonpath +): + """An install listed in a ``.pth`` file stays visible even when it is the caller's directory. + + That holds when ``PYTHONPATH`` also names the directory: the ``PYTHONPATH`` + entry is dropped, and the ``.pth`` file still adds the path. + """ + python, site = isolated_probe_python + source = tmp_path / "editable-source" + (source / "foo").mkdir(parents=True) + (source / "foo" / "__init__.py").write_text("") + (source / "foo.egg-info").mkdir() + (source / "foo.egg-info" / "PKG-INFO").write_text("Metadata-Version: 1.2\nName: foo\nVersion: 1.5\n") + (site / "easy-install.pth").write_text(f"{source}\n") + (site / "foo.egg-link").write_text(f"{source}\n.\n") + workspace = tmp_path / "ComfyUI" + _make_pack(workspace, "pack", requirements="foo>=1\n") + monkeypatch.chdir(source) + monkeypatch.setenv("PYTHONPATH", str(source) if pythonpath == "absolute" else pythonpath) + + report, warnings = node_deps_cmd.build_report(str(workspace), python=python) + reqs = _reqs_by_raw(_packs_by_name(report)["pack"]) + + assert warnings == [] + assert reqs["foo>=1"]["installed"] == "1.5" + assert reqs["foo>=1"]["status"] == "satisfied" + + +def test_relative_virtual_env_interpreter_still_runs(tmp_path, monkeypatch, isolated_probe_python): + """``VIRTUAL_ENV=probe-venv`` resolves to a relative interpreter path, which must still start.""" + python, site = isolated_probe_python + _write_dist_info(site, "foo-1.0.dist-info", "foo", "1.0") + workspace = tmp_path / "ComfyUI" + _make_pack(workspace, "pack", requirements="foo>=1\n") + monkeypatch.chdir(tmp_path) + monkeypatch.setenv("VIRTUAL_ENV", os.path.relpath(Path(python).parent.parent, tmp_path)) + monkeypatch.delenv("CONDA_PREFIX", raising=False) + monkeypatch.setenv("PYTHONPATH", "") + + report, warnings = node_deps_cmd.build_report(str(workspace)) + reqs = _reqs_by_raw(_packs_by_name(report)["pack"]) + + assert not os.path.isabs(report["python"]) + assert warnings == [] + assert reqs["foo>=1"]["installed"] == "1.0" + assert reqs["foo>=1"]["status"] == "satisfied" + + +def test_relative_pythonpath_keeps_its_target_under_the_caller_directory(tmp_path, monkeypatch, isolated_probe_python): + """``PYTHONPATH=overlay`` names ``/overlay``; only the caller's directory itself is left out.""" + python, _ = isolated_probe_python + caller = tmp_path / "caller" + _write_dist_info(caller / "overlay", "foo-1.0.dist-info", "foo", "1.0") + workspace = tmp_path / "ComfyUI" + _make_pack(workspace, "pack", requirements="foo>=1\n") + monkeypatch.chdir(caller) + monkeypatch.setenv("PYTHONPATH", "overlay") + + report, warnings = node_deps_cmd.build_report(str(workspace), python=python) + reqs = _reqs_by_raw(_packs_by_name(report)["pack"]) + + assert warnings == [] + assert reqs["foo>=1"]["installed"] == "1.0" + assert reqs["foo>=1"]["status"] == "satisfied" + + +def test_absolute_pythonpath_naming_the_caller_does_not_override_installed_metadata( + tmp_path, monkeypatch, isolated_probe_python +): + """Stray metadata in the caller's directory loses to the installed copy, however the entry is spelled.""" + python, site = isolated_probe_python + _write_dist_info(site, "foo-1.0.dist-info", "foo", "1.0") + caller = tmp_path / "caller" + _write_dist_info(caller, "foo-9.0.dist-info", "foo", "9.0") + workspace = tmp_path / "ComfyUI" + _make_pack(workspace, "pack", requirements="foo<2\n") + monkeypatch.chdir(caller) + monkeypatch.setenv("PYTHONPATH", str(caller)) + + report, warnings = node_deps_cmd.build_report(str(workspace), python=python) + reqs = _reqs_by_raw(_packs_by_name(report)["pack"]) + + assert warnings == [] + assert reqs["foo<2"]["installed"] == "1.0" + assert reqs["foo<2"]["status"] == "satisfied" + + +def test_probe_env_drops_only_pythonpath_entries_naming_the_caller_directory(tmp_path, monkeypatch): + caller, other = tmp_path / "caller", tmp_path / "other" + (caller / "overlay").mkdir(parents=True) + other.mkdir() + # Creating a symlink needs a privilege Windows does not always grant; without + # it, the symlink entry is left out and every other entry is still checked. + link = tmp_path / "caller-link" + try: + link.symlink_to(caller, target_is_directory=True) + except OSError: + link_entries = [] + else: + link_entries = [str(link)] + monkeypatch.chdir(caller) + monkeypatch.setenv( + "PYTHONPATH", os.pathsep.join([".", "", str(caller), *link_entries, "overlay", str(other), "absent"]) + ) + monkeypatch.setenv("COMFY_PROBE_ENV_MARKER", "kept") + + env = node_deps_cmd._probe_env() + + assert env["PYTHONPATH"] == os.pathsep.join(["overlay", str(other), "absent"]) + assert env["COMFY_PROBE_ENV_MARKER"] == "kept" + assert os.environ["PYTHONPATH"].startswith(".") + + monkeypatch.setenv("PYTHONPATH", os.pathsep.join([".", str(caller)])) + assert "PYTHONPATH" not in node_deps_cmd._probe_env() + + def test_pip_list_runs_once_for_the_whole_report(workspace, fake_pip): node_deps_cmd.build_report(str(workspace), python="/fake/python") - assert len(fake_pip.calls) == 1, f"pip list ran {len(fake_pip.calls)}x — must be once per report" - assert fake_pip.calls[0] == ["/fake/python", "-m", "pip", "list", "--format=json"] + assert len(fake_pip.calls) == 1, f"installed probe ran {len(fake_pip.calls)}x — must be once per report" + assert fake_pip.calls[0] == ["/fake/python", "-c", node_deps_cmd._METADATA_LIST_SCRIPT] def test_pip_list_failure_degrades_to_unknown_with_a_warning(workspace, monkeypatch): - monkeypatch.setattr(node_deps_cmd.subprocess, "run", _FakePipList(returncode=2)) + probe = _FakePipList(returncode=2) + monkeypatch.setattr(node_deps_cmd.subprocess, "run", probe) report, warnings = node_deps_cmd.build_report(str(workspace), python="/fake/python") @@ -272,14 +552,16 @@ def test_pip_list_failure_degrades_to_unknown_with_a_warning(workspace, monkeypa reqs = _reqs_by_raw(pack) assert reqs["opencv-python>=4.7"]["status"] == "unknown" assert reqs["opencv-python>=4.7"]["installed"] is None - # unparseable lines stay unparseable — the pip probe can't change that + # unparseable lines stay unparseable — the installed probe can't change that assert reqs["-r other-requirements.txt"]["status"] == "unparseable" assert pack["summary"]["unknown"] == 4 assert pack["summary"]["unparseable"] == 3 + assert len(probe.calls) == 1 -def test_pip_list_unparseable_output_degrades_to_unknown(workspace, monkeypatch): - monkeypatch.setattr(node_deps_cmd.subprocess, "run", _FakePipList(stdout="not json at all")) +@pytest.mark.parametrize("stdout", ["not json at all", "{}"]) +def test_pip_list_unparseable_output_degrades_to_unknown(workspace, monkeypatch, stdout): + monkeypatch.setattr(node_deps_cmd.subprocess, "run", _FakePipList(stdout=stdout)) report, warnings = node_deps_cmd.build_report(str(workspace), python="/fake/python") @@ -336,7 +618,7 @@ def test_interpreter_without_pip_still_reports_installed_versions(tmp_path, monk def test_pip_list_timeout_is_not_retried(workspace, monkeypatch): - """A hung interpreter would hang the fallback too; don't double the wait.""" + """A hung interpreter must not be retried; do not double the wait.""" calls = [] def _hang(cmd, **kwargs): @@ -473,7 +755,7 @@ def test_registry_candidate_is_diffed_against_the_same_pip_list(workspace, fake_ assert all(r["source"] == "registry" for r in row["requirements"]) assert row["summary"] == {"satisfied": 1, "mismatch": 1, "missing": 1, "unparseable": 1, "unknown": 0} - # A single `pip list`, shared with the installed packs — not one per entry. + # One installed-version probe, shared with installed packs — not one per entry. assert len(fake_pip.calls) == 1