From 60d09ccabb99eee4d1451e6b0112808f12317c42 Mon Sep 17 00:00:00 2001 From: "eric.sun" Date: Fri, 23 Oct 2026 14:42:17 -0700 Subject: [PATCH 1/6] fix(update): report missing workspace and upgrade uv-tool installs `comfy update` only guarded a None workspace path, so a configured default that was never installed (or was deleted) crashed os.chdir with a raw FileNotFoundError traceback, with no envelope under --json and no hint. Check the directory exists and fail with not_in_workspace instead. `comfy update cli` always ran `python -m pip install -U comfy-cli`, which cannot work in a `uv tool install` env because uv ships those without pip. Detect the uv receipt and run `uv tool upgrade comfy-cli`, and surface any installer failure as an update_cli_failed envelope instead of a traceback. --- CHANGELOG.md | 7 ++ comfy_cli/cmdline.py | 32 ++++- comfy_cli/error_codes.py | 7 ++ comfy_cli/update.py | 33 ++++- .../command/test_update_missing_workspace.py | 113 ++++++++++++++++++ tests/comfy_cli/test_update.py | 39 +++++- 6 files changed, 222 insertions(+), 9 deletions(-) create mode 100644 tests/comfy_cli/command/test_update_missing_workspace.py diff --git a/CHANGELOG.md b/CHANGELOG.md index 7307209c3..ac9b06067 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -169,6 +169,13 @@ history. ### Fixed +- `comfy update` no longer dies with a `FileNotFoundError` traceback when the + configured workspace directory does not exist (never installed, or deleted). + It now fails with a `not_in_workspace` error that points at `comfy install`. +- `comfy update cli` works for a `uv tool install` of comfy-cli. Those envs ship + without pip, so the old `python -m pip install -U` always failed; it now runs + `uv tool upgrade comfy-cli`, and any installer failure is reported as an + `update_cli_failed` error rather than a traceback. - `comfy jobs watch` no longer dies with a traceback when the ComfyUI server accepts a connection and hangs up before answering (a restarting server, a reset connection). That surfaced as a raw `http.client.RemoteDisconnected`, diff --git a/comfy_cli/cmdline.py b/comfy_cli/cmdline.py index 04cf0ea63..8773cb715 100644 --- a/comfy_cli/cmdline.py +++ b/comfy_cli/cmdline.py @@ -1759,10 +1759,22 @@ def update( raise typer.Exit(code=1) if "cli" == target: - from comfy_cli.update import upgrade_cli + from comfy_cli.update import UpgradeUnavailable, upgrade_cli rprint("Updating comfy-cli...") - upgrade_cli() + try: + upgrade_cli() + except UpgradeUnavailable as e: + get_renderer().error(code="update_cli_failed", message=str(e), hint=e.hint) + raise typer.Exit(code=1) from e + except subprocess.CalledProcessError as e: + get_renderer().error( + code="update_cli_failed", + message=f"`{' '.join(map(str, e.cmd))}` failed with exit code {e.returncode}.", + hint="see the installer output above; re-running `comfy update cli` is safe", + details={"returncode": e.returncode}, + ) + raise typer.Exit(code=1) from e return comfy_path = workspace_manager.workspace_path @@ -1798,10 +1810,20 @@ def update( ) raise typer.Exit(code=code) from e else: - rprint(f"Updating ComfyUI in {comfy_path}...") - if comfy_path is None: - rprint("ComfyUI path is not found.") + # The default workspace path comes from config and can name a directory that + # was never installed (or was deleted), so check existence, not just None. + if comfy_path is None or not os.path.isdir(comfy_path): + get_renderer().error( + code="not_in_workspace", + message=( + "ComfyUI workspace not found." + if comfy_path is None + else f"ComfyUI workspace not found: {comfy_path}" + ), + hint="run `comfy install`, or pass `--workspace` to point at an existing install", + ) raise typer.Exit(code=1) + rprint(f"Updating ComfyUI in {comfy_path}...") if version is not None: _switch_comfy_version(comfy_path, version, stash=not no_stash) else: diff --git a/comfy_cli/error_codes.py b/comfy_cli/error_codes.py index d04d43562..088b659e3 100644 --- a/comfy_cli/error_codes.py +++ b/comfy_cli/error_codes.py @@ -1187,6 +1187,13 @@ class ErrorCode: "CLI usage error).", "read the cm-cli output above for the failing pack, then re-run `comfy update all`", ), + ErrorCode( + "update_cli_failed", + "`comfy update cli` could not upgrade comfy-cli: either no usable installer was found " + "(a `uv tool` env without `uv` on PATH, or an env without pip) or the installer exited " + "non-zero, in which case `details.returncode` carries its status.", + "follow the error's hint; for a `uv tool` install, `uv tool upgrade comfy-cli` always works", + ), ErrorCode( "version_switch_unknown_version", "`comfy update comfy --version X` could not resolve X to a ComfyUI tag; the workspace was left untouched.", diff --git a/comfy_cli/update.py b/comfy_cli/update.py index aecea53b7..9dff40cb0 100644 --- a/comfy_cli/update.py +++ b/comfy_cli/update.py @@ -1,3 +1,4 @@ +import importlib.util import json import logging import os @@ -9,6 +10,8 @@ import requests from packaging import version +from comfy_cli._safe_exec import resolve_binary + logger = logging.getLogger(__name__) UPDATE_CHECK_TTL_SECONDS = 24 * 60 * 60 @@ -39,9 +42,35 @@ def check_for_newer_pypi_version(package_name, current_version): return False, current_version +class UpgradeUnavailable(RuntimeError): + """No installer can upgrade the environment comfy-cli is running from.""" + + def __init__(self, message: str, hint: str): + super().__init__(message) + self.hint = hint + + def upgrade_cli(): - """Upgrade the ``comfy-cli`` package itself via pip against the running - interpreter. Raises ``CalledProcessError`` if pip fails — fail fast.""" + """Upgrade the ``comfy-cli`` package itself in the environment it runs from. + + A ``uv tool install`` env ships without pip, so it is upgraded through + ``uv tool upgrade``; anything else goes through pip against the running + interpreter. Raises ``UpgradeUnavailable`` when neither installer is usable + and ``CalledProcessError`` if the installer fails — fail fast.""" + if (Path(sys.prefix) / "uv-receipt.toml").is_file(): + uv = resolve_binary("uv") + if uv is None: + raise UpgradeUnavailable( + "comfy-cli was installed with `uv tool`, but `uv` is not on PATH.", + hint="run `uv tool upgrade comfy-cli` from a shell where uv is available", + ) + subprocess.run([uv, "tool", "upgrade", "comfy-cli"], check=True) + return + if importlib.util.find_spec("pip") is None: + raise UpgradeUnavailable( + f"pip is not installed in the comfy-cli environment ({sys.prefix}).", + hint="upgrade comfy-cli with the tool that installed it (e.g. `pipx upgrade comfy-cli`)", + ) subprocess.run([sys.executable, "-m", "pip", "install", "-U", "comfy-cli"], check=True) diff --git a/tests/comfy_cli/command/test_update_missing_workspace.py b/tests/comfy_cli/command/test_update_missing_workspace.py new file mode 100644 index 000000000..f36302cd9 --- /dev/null +++ b/tests/comfy_cli/command/test_update_missing_workspace.py @@ -0,0 +1,113 @@ +"""Tests for ``comfy update`` when the ComfyUI workspace does not exist. + +The default workspace path is resolved from config even when nothing was ever +installed there, so ``workspace_path`` can be a string naming a missing +directory. ``update`` only guarded against ``None`` and then ``os.chdir``-ed +into the path, crashing with a raw ``FileNotFoundError`` traceback — no +envelope under ``--json``, no hint to run ``comfy install``. +""" + +from __future__ import annotations + +import json +import re +from unittest.mock import patch + +import pytest +from typer.testing import CliRunner + +from comfy_cli import cmdline +from comfy_cli.workspace_manager import WorkspaceType + +runner = CliRunner() + +SUBPROCESS_RUN = "comfy_cli.cmdline.subprocess.run" +SWITCH_VERSION = "comfy_cli.cmdline._switch_comfy_version" + + +def _strip_ansi(text: str) -> str: + return re.sub(r"\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])", "", text) + + +def _resolve_to(path): + # The root callback re-runs ``setup_workspace_manager``, which drops any + # value patched onto ``workspace_path``; pin the resolver underneath instead. + return patch.object(cmdline.workspace_manager, "get_workspace_path", return_value=(path, WorkspaceType.DEFAULT)) + + +@pytest.fixture +def missing_workspace(tmp_path): + path = str(tmp_path / "ComfyUI") + with _resolve_to(path): + yield path + + +class TestMissingWorkspace: + def test_missing_path_is_a_structured_error_in_json_mode(self, missing_workspace): + with patch(SUBPROCESS_RUN) as mock_run: + result = runner.invoke(cmdline.app, ["--json", "update"]) + + assert result.exit_code == 1 + assert result.exception is None or isinstance(result.exception, SystemExit) + envelope = json.loads(result.stdout.strip().splitlines()[-1]) + assert envelope["ok"] is False + assert envelope["error"]["code"] == "not_in_workspace" + assert missing_workspace in envelope["error"]["message"] + assert "comfy install" in envelope["error"]["hint"] + mock_run.assert_not_called() + + def test_missing_path_is_explained_in_pretty_mode(self, missing_workspace): + with patch(SUBPROCESS_RUN) as mock_run: + result = runner.invoke(cmdline.app, ["--no-json", "update", "comfy"]) + + assert result.exit_code == 1 + assert result.exception is None or isinstance(result.exception, SystemExit) + assert "not_in_workspace" in _strip_ansi(result.stdout) + mock_run.assert_not_called() + + def test_missing_path_blocks_a_version_switch(self, missing_workspace): + with patch(SWITCH_VERSION) as mock_switch: + result = runner.invoke(cmdline.app, ["--json", "update", "comfy", "--version", "nightly"]) + + assert result.exit_code == 1 + envelope = json.loads(result.stdout.strip().splitlines()[-1]) + assert envelope["error"]["code"] == "not_in_workspace" + mock_switch.assert_not_called() + + def test_unresolved_path_is_a_structured_error(self): + with _resolve_to(None): + result = runner.invoke(cmdline.app, ["--json", "update"]) + + assert result.exit_code == 1 + envelope = json.loads(result.stdout.strip().splitlines()[-1]) + assert envelope["error"]["code"] == "not_in_workspace" + assert "comfy install" in envelope["error"]["hint"] + + +class TestUpdateCliFailures: + """`comfy update cli` used to let installer failures escape as tracebacks.""" + + def test_unavailable_installer_is_a_structured_error(self): + from comfy_cli import update + + err = update.UpgradeUnavailable("no pip here", hint="run `uv tool upgrade comfy-cli`") + with patch("comfy_cli.update.upgrade_cli", side_effect=err): + result = runner.invoke(cmdline.app, ["--json", "update", "cli"]) + + assert result.exit_code == 1 + envelope = json.loads(result.stdout.strip().splitlines()[-1]) + assert envelope["error"]["code"] == "update_cli_failed" + assert envelope["error"]["hint"] == "run `uv tool upgrade comfy-cli`" + + def test_failed_installer_is_a_structured_error(self): + import subprocess + + err = subprocess.CalledProcessError(1, ["uv", "tool", "upgrade", "comfy-cli"]) + with patch("comfy_cli.update.upgrade_cli", side_effect=err): + result = runner.invoke(cmdline.app, ["--json", "update", "cli"]) + + assert result.exit_code == 1 + assert result.exception is None or isinstance(result.exception, SystemExit) + envelope = json.loads(result.stdout.strip().splitlines()[-1]) + assert envelope["error"]["code"] == "update_cli_failed" + assert envelope["error"]["details"]["returncode"] == 1 diff --git a/tests/comfy_cli/test_update.py b/tests/comfy_cli/test_update.py index 11010b1b1..7ecc3f481 100644 --- a/tests/comfy_cli/test_update.py +++ b/tests/comfy_cli/test_update.py @@ -101,12 +101,47 @@ def test_corrupt_cache_does_not_raise(self, mock_check, tmp_path): class TestUpgradeCli: @patch("comfy_cli.update.subprocess.run") - def test_runs_pip_install_upgrade(self, mock_run): - upgrade_cli() + def test_runs_pip_install_upgrade(self, mock_run, tmp_path): + with patch.object(update.sys, "prefix", str(tmp_path)): + upgrade_cli() args = mock_run.call_args[0][0] assert args[1:] == ["-m", "pip", "install", "-U", "comfy-cli"] assert mock_run.call_args[1]["check"] is True + @patch("comfy_cli.update.subprocess.run") + def test_uv_tool_install_upgrades_through_uv(self, mock_run, tmp_path): + """A `uv tool install` env has no pip, so `python -m pip` always fails there.""" + (tmp_path / "uv-receipt.toml").write_text("[tool]\n") + with ( + patch.object(update.sys, "prefix", str(tmp_path)), + patch("comfy_cli.update.resolve_binary", return_value="/opt/bin/uv"), + ): + upgrade_cli() + assert mock_run.call_args[0][0] == ["/opt/bin/uv", "tool", "upgrade", "comfy-cli"] + assert mock_run.call_args[1]["check"] is True + + @patch("comfy_cli.update.subprocess.run") + def test_uv_tool_install_without_uv_on_path_is_unavailable(self, mock_run, tmp_path): + (tmp_path / "uv-receipt.toml").write_text("[tool]\n") + with ( + patch.object(update.sys, "prefix", str(tmp_path)), + patch("comfy_cli.update.resolve_binary", return_value=None), + pytest.raises(update.UpgradeUnavailable) as excinfo, + ): + upgrade_cli() + assert "uv tool upgrade comfy-cli" in excinfo.value.hint + mock_run.assert_not_called() + + @patch("comfy_cli.update.subprocess.run") + def test_env_without_pip_is_unavailable(self, mock_run, tmp_path): + with ( + patch.object(update.sys, "prefix", str(tmp_path)), + patch("comfy_cli.update.importlib.util.find_spec", return_value=None), + pytest.raises(update.UpgradeUnavailable), + ): + upgrade_cli() + mock_run.assert_not_called() + @pytest.mark.parametrize("payload", ["[]", '"x"', '{"checked_at": "abc"}']) def test_latest_upgrade_version_survives_malformed_cache(tmp_path, payload): From 5aaff9d3a8cba8ff1327a9b0bd5ffdda4b891a79 Mon Sep 17 00:00:00 2001 From: "eric.sun" Date: Wed, 28 Oct 2026 21:40:57 -0700 Subject: [PATCH 2/6] fix(update): bound installer failures and isolate pip tests --- comfy_cli/cmdline.py | 15 +++++++++++ comfy_cli/error_codes.py | 8 +++--- comfy_cli/update.py | 9 ++++--- .../command/test_update_missing_workspace.py | 26 +++++++++++++++++++ tests/comfy_cli/test_update.py | 8 +++++- 5 files changed, 59 insertions(+), 7 deletions(-) diff --git a/comfy_cli/cmdline.py b/comfy_cli/cmdline.py index 8773cb715..8d95103a8 100644 --- a/comfy_cli/cmdline.py +++ b/comfy_cli/cmdline.py @@ -1775,6 +1775,21 @@ def update( details={"returncode": e.returncode}, ) raise typer.Exit(code=1) from e + except subprocess.TimeoutExpired as e: + get_renderer().error( + code="update_cli_failed", + message=f"The comfy-cli installer timed out after {e.timeout} seconds.", + hint="check the installer output, then re-run `comfy update cli`", + details={"timeout": e.timeout}, + ) + raise typer.Exit(code=1) from e + except OSError as e: + get_renderer().error( + code="update_cli_failed", + message=f"Could not start the comfy-cli installer: {e}", + hint="check the installer executable and its permissions, then re-run `comfy update cli`", + ) + raise typer.Exit(code=1) from e return comfy_path = workspace_manager.workspace_path diff --git a/comfy_cli/error_codes.py b/comfy_cli/error_codes.py index 1926df100..c5d050f05 100644 --- a/comfy_cli/error_codes.py +++ b/comfy_cli/error_codes.py @@ -1190,9 +1190,11 @@ class ErrorCode: ErrorCode( "update_cli_failed", "`comfy update cli` could not upgrade comfy-cli: either no usable installer was found " - "(a `uv tool` env without `uv` on PATH, or an env without pip) or the installer exited " - "non-zero, in which case `details.returncode` carries its status.", - "follow the error's hint; for a `uv tool` install, `uv tool upgrade comfy-cli` always works", + "(a `uv tool` env without `uv` on PATH, or an env without pip), or the installer could not start, " + "timed out, or exited non-zero. `details.timeout` carries the expired time budget; " + "`details.returncode` carries a non-zero exit status.", + "follow the error's hint; for a `uv tool` install, ensure `uv` is on PATH before running " + "`uv tool upgrade comfy-cli`", ), ErrorCode( "version_switch_unknown_version", diff --git a/comfy_cli/update.py b/comfy_cli/update.py index 9dff40cb0..37989098d 100644 --- a/comfy_cli/update.py +++ b/comfy_cli/update.py @@ -16,6 +16,7 @@ UPDATE_CHECK_TTL_SECONDS = 24 * 60 * 60 UPDATE_CHECK_DISABLE_ENV = "COMFY_NO_UPDATE_CHECK" +CLI_UPGRADE_TIMEOUT_SECONDS = 600 def check_for_newer_pypi_version(package_name, current_version): @@ -56,7 +57,7 @@ def upgrade_cli(): A ``uv tool install`` env ships without pip, so it is upgraded through ``uv tool upgrade``; anything else goes through pip against the running interpreter. Raises ``UpgradeUnavailable`` when neither installer is usable - and ``CalledProcessError`` if the installer fails — fail fast.""" + and propagates installer exit, start, or timeout failures to the command.""" if (Path(sys.prefix) / "uv-receipt.toml").is_file(): uv = resolve_binary("uv") if uv is None: @@ -64,14 +65,16 @@ def upgrade_cli(): "comfy-cli was installed with `uv tool`, but `uv` is not on PATH.", hint="run `uv tool upgrade comfy-cli` from a shell where uv is available", ) - subprocess.run([uv, "tool", "upgrade", "comfy-cli"], check=True) + subprocess.run([uv, "tool", "upgrade", "comfy-cli"], check=True, timeout=CLI_UPGRADE_TIMEOUT_SECONDS) return if importlib.util.find_spec("pip") is None: raise UpgradeUnavailable( f"pip is not installed in the comfy-cli environment ({sys.prefix}).", hint="upgrade comfy-cli with the tool that installed it (e.g. `pipx upgrade comfy-cli`)", ) - subprocess.run([sys.executable, "-m", "pip", "install", "-U", "comfy-cli"], check=True) + subprocess.run( + [sys.executable, "-m", "pip", "install", "-U", "comfy-cli"], check=True, timeout=CLI_UPGRADE_TIMEOUT_SECONDS + ) def _read_fresh_cache(cache_file, now): diff --git a/tests/comfy_cli/command/test_update_missing_workspace.py b/tests/comfy_cli/command/test_update_missing_workspace.py index f36302cd9..65d368922 100644 --- a/tests/comfy_cli/command/test_update_missing_workspace.py +++ b/tests/comfy_cli/command/test_update_missing_workspace.py @@ -11,6 +11,7 @@ import json import re +import subprocess from unittest.mock import patch import pytest @@ -111,3 +112,28 @@ def test_failed_installer_is_a_structured_error(self): envelope = json.loads(result.stdout.strip().splitlines()[-1]) assert envelope["error"]["code"] == "update_cli_failed" assert envelope["error"]["details"]["returncode"] == 1 + + @pytest.mark.parametrize("output_flag", ["--json", "--no-json"]) + @pytest.mark.parametrize( + "installer_error", + [ + FileNotFoundError("installer disappeared"), + PermissionError("installer is not executable"), + subprocess.TimeoutExpired(["uv", "tool", "upgrade", "comfy-cli"], 600), + ], + ids=["missing-installer", "installer-permissions", "timeout"], + ) + def test_installer_start_and_timeout_failures_are_structured(self, installer_error, output_flag): + with patch("comfy_cli.update.upgrade_cli", side_effect=installer_error): + result = runner.invoke(cmdline.app, [output_flag, "update", "cli"]) + + assert result.exit_code == 1 + assert result.exception is None or isinstance(result.exception, SystemExit) + if output_flag == "--json": + envelope = json.loads(result.stdout.strip().splitlines()[-1]) + assert envelope["error"]["code"] == "update_cli_failed" + assert envelope["error"]["hint"] + if isinstance(installer_error, subprocess.TimeoutExpired): + assert envelope["error"]["details"]["timeout"] == 600 + else: + assert "update_cli_failed" in _strip_ansi(result.stdout) diff --git a/tests/comfy_cli/test_update.py b/tests/comfy_cli/test_update.py index 7ecc3f481..41b7e28c8 100644 --- a/tests/comfy_cli/test_update.py +++ b/tests/comfy_cli/test_update.py @@ -1,5 +1,6 @@ import json import time +from importlib.machinery import ModuleSpec from unittest.mock import MagicMock, patch import pytest @@ -102,11 +103,15 @@ def test_corrupt_cache_does_not_raise(self, mock_check, tmp_path): class TestUpgradeCli: @patch("comfy_cli.update.subprocess.run") def test_runs_pip_install_upgrade(self, mock_run, tmp_path): - with patch.object(update.sys, "prefix", str(tmp_path)): + with ( + patch.object(update.sys, "prefix", str(tmp_path)), + patch("comfy_cli.update.importlib.util.find_spec", return_value=ModuleSpec("pip", loader=None)), + ): upgrade_cli() args = mock_run.call_args[0][0] assert args[1:] == ["-m", "pip", "install", "-U", "comfy-cli"] assert mock_run.call_args[1]["check"] is True + assert mock_run.call_args[1].get("timeout") == 600 @patch("comfy_cli.update.subprocess.run") def test_uv_tool_install_upgrades_through_uv(self, mock_run, tmp_path): @@ -119,6 +124,7 @@ def test_uv_tool_install_upgrades_through_uv(self, mock_run, tmp_path): upgrade_cli() assert mock_run.call_args[0][0] == ["/opt/bin/uv", "tool", "upgrade", "comfy-cli"] assert mock_run.call_args[1]["check"] is True + assert mock_run.call_args[1].get("timeout") == 600 @patch("comfy_cli.update.subprocess.run") def test_uv_tool_install_without_uv_on_path_is_unavailable(self, mock_run, tmp_path): From 2d3f9c9dab77d8b1d92facf163ef153668486695 Mon Sep 17 00:00:00 2001 From: "eric.sun" Date: Thu, 29 Oct 2026 07:31:09 -0700 Subject: [PATCH 3/6] test: reuse hermetic catalog fixtures after main merge --- tests/comfy_cli/command/test_run_json.py | 92 ++++++++++++------- .../output/test_error_envelope_where.py | 8 +- 2 files changed, 65 insertions(+), 35 deletions(-) diff --git a/tests/comfy_cli/command/test_run_json.py b/tests/comfy_cli/command/test_run_json.py index 7fd163f44..32dae6c6c 100644 --- a/tests/comfy_cli/command/test_run_json.py +++ b/tests/comfy_cli/command/test_run_json.py @@ -52,13 +52,30 @@ def ndjson_renderer(): reset_renderer_for_testing() +@pytest.fixture(autouse=True) +def local_preflight_catalog(monkeypatch): + """Keep event/queue tests independent of any real server on localhost. + + Validation still runs against complete schemas. Tests for conversion HTTP + and deliberately invalid catalogs override their specific boundary below. + """ + from copy import deepcopy + + monkeypatch.setattr("comfy_cli.command.run._fetch_object_info", lambda *a, **k: deepcopy(OBJECT_INFO)) + + def unexpected_catalog_request(*args, **kwargs): + pytest.fail("unexpected real catalog request; mock the specific object_info transport") + + monkeypatch.setattr("comfy_cli.http._PLAIN_OPENER.open", unexpected_catalog_request) + + @pytest.fixture def simple_workflow(): return { "1": { - "class_type": "EmptyLatentImage", - "inputs": {"width": 64, "height": 64, "batch_size": 1}, - "_meta": {"title": "Latent"}, + "class_type": "EmptyImage", + "inputs": {"width": 64, "height": 64, "batch_size": 1, "color": 0}, + "_meta": {"title": "Image"}, }, "2": { "class_type": "SaveImage", @@ -524,8 +541,8 @@ def test_queued_nodes_manifest_from_workflow(self, workflow_file, capsys, simple nodes = queued["nodes"] assert len(nodes) == 2 by_id = {n["node_id"]: n for n in nodes} - assert by_id["1"]["class_type"] == "EmptyLatentImage" - assert by_id["1"]["title"] == "Latent" # _meta.title wins + assert by_id["1"]["class_type"] == "EmptyImage" + assert by_id["1"]["title"] == "Image" # _meta.title wins assert by_id["2"]["class_type"] == "SaveImage" assert by_id["2"]["title"] == "Save" @@ -621,7 +638,7 @@ def test_execution_error(self, workflow_file, capsys): "data": { "prompt_id": "p", "node_id": "1", - "node_type": "EmptyLatentImage", + "node_type": "EmptyImage", "exception_type": "RuntimeError", "exception_message": "boom", "traceback": [' File "x.py"\n', " raise RuntimeError\n"], @@ -635,12 +652,12 @@ def test_execution_error(self, workflow_file, capsys): assert any(e["type"] == "execution_error" for e in _events(lines)) env = _envelope(lines) assert env["error"]["code"] == "execution_error" - assert env["error"]["message"] == "EmptyLatentImage (node 1): boom" + assert env["error"]["message"] == "EmptyImage (node 1): boom" details = env["error"]["details"] assert details["node_id"] == "1" - assert details["class_type"] == "EmptyLatentImage" + assert details["class_type"] == "EmptyImage" assert details["exception_type"] == "RuntimeError" - assert details["title"] == "Latent" # from _meta.title + assert details["title"] == "Image" # from _meta.title # The envelope carries only the traceback tail; the full traceback # stays on the execution_error event. assert isinstance(details["traceback_tail"], list) @@ -658,7 +675,7 @@ def test_execution_error_node_id_coerced_to_str(self, workflow_file, capsys): "data": { "prompt_id": "p", "node_id": 7, - "node_type": "EmptyLatentImage", + "node_type": "EmptyImage", "exception_type": "RuntimeError", "exception_message": "boom", "traceback": [], @@ -797,8 +814,8 @@ def test_node_ids_coerced_to_str(self, simple_workflow, capsys): assert isinstance(ev["node"], str), f"{ev['type']} node is {type(ev['node']).__name__}" def test_title_falls_back_to_class_type(self): - ex = _make_workflow_execution({"1": {"class_type": "EmptyLatentImage", "inputs": {}}}) - assert ex.get_node_title("1") == "EmptyLatentImage" + ex = _make_workflow_execution({"1": {"class_type": "EmptyImage", "inputs": {}}}) + assert ex.get_node_title("1") == "EmptyImage" def test_title_falls_back_to_node_id_for_unknown(self): ex = _make_workflow_execution({}) @@ -809,10 +826,10 @@ def test_title_falls_back_to_node_id_for_unknown(self): "nodes": [ { "id": 1, - "type": "EmptyLatentImage", + "type": "EmptyImage", "inputs": [], - "outputs": [{"name": "LATENT", "type": "LATENT", "links": [10]}], - "widgets_values": [64, 64, 1], + "outputs": [{"name": "IMAGE", "type": "IMAGE", "links": [10]}], + "widgets_values": [64, 64, 1, 0], "mode": 0, }, { @@ -827,17 +844,26 @@ def test_title_falls_back_to_node_id_for_unknown(self): } OBJECT_INFO = { - "EmptyLatentImage": { + "EmptyImage": { "input": { "required": { "width": ["INT", {"default": 512}], "height": ["INT", {"default": 512}], "batch_size": ["INT", {"default": 1}], + "color": ["INT", {"default": 0}], } }, - "input_order": {"required": ["width", "height", "batch_size"]}, + "input_order": {"required": ["width", "height", "batch_size", "color"]}, + "output": ["IMAGE"], + "output_name": ["IMAGE"], "output_node": False, - "display_name": "Empty Latent Image", + "display_name": "Empty Image", + }, + "SaveImage": { + "input": {"required": {"filename_prefix": ["STRING", {"default": "ComfyUI"}], "images": ["IMAGE"]}}, + "input_order": {"required": ["filename_prefix", "images"]}, + "output": [], + "output_node": True, }, "PreviewImage": { "input": {"required": {"images": ["IMAGE"]}}, @@ -970,7 +996,7 @@ def test_api_input_emits_prompt_preview_before_queued(self, workflow_file, capsy assert types[0] == "prompt_preview" assert "queued" in types assert types.index("prompt_preview") < types.index("queued") - assert _events(lines)[0]["prompt"]["1"]["class_type"] == "EmptyLatentImage" + assert _events(lines)[0]["prompt"]["1"]["class_type"] == "EmptyImage" def test_ui_input_emits_converted_then_prompt_preview_then_queued(self, ui_workflow_file, capsys): with ( @@ -1035,7 +1061,7 @@ def test_api_input_emits_prompt_preview_and_envelope_only(self, workflow_file, c assert [e["type"] for e in _events(lines)] == ["prompt_preview"] preview = _events(lines)[0] assert preview["schema"] == "event/1" - assert preview["prompt"]["1"]["class_type"] == "EmptyLatentImage" + assert preview["prompt"]["1"]["class_type"] == "EmptyImage" env = _envelope(lines) assert env["ok"] is True assert env["data"]["status"] == "preview" @@ -1092,7 +1118,7 @@ def test_print_prompt_text_mode_pretty_prints_json(self, workflow_file, capsys): out, _err = capsys.readouterr() parsed = json.loads(out) assert "1" in parsed - assert parsed["1"]["class_type"] == "EmptyLatentImage" + assert parsed["1"]["class_type"] == "EmptyImage" def test_print_prompt_does_not_post_when_workflow_invalid(self, capsys): # Pre-flight failures (workflow_not_found, workflow_not_api_format) @@ -1240,10 +1266,10 @@ def test_execution_cached_event_shape(self, workflow_file, capsys): cached_events = [e for e in _events(lines) if e["type"] == "execution_cached"] assert len(cached_events) == 2 - # Node 1 has _meta.title="Latent"; class_type=EmptyLatentImage + # Node 1 has _meta.title="Image"; class_type=EmptyImage n1 = next(e for e in cached_events if e["node"] == "1") - assert n1["class_type"] == "EmptyLatentImage" - assert n1["title"] == "Latent" + assert n1["class_type"] == "EmptyImage" + assert n1["title"] == "Image" # Node 2 has _meta.title="Save"; class_type=SaveImage n2 = next(e for e in cached_events if e["node"] == "2") assert n2["class_type"] == "SaveImage" @@ -1380,9 +1406,9 @@ class TestErrorPathCoverage: def _make_workflow(self): return { "1": { - "class_type": "EmptyLatentImage", - "inputs": {"width": 64, "height": 64, "batch_size": 1}, - "_meta": {"title": "Latent"}, + "class_type": "EmptyImage", + "inputs": {"width": 64, "height": 64, "batch_size": 1, "color": 0}, + "_meta": {"title": "Image"}, }, "2": { "class_type": "SaveImage", @@ -1399,10 +1425,10 @@ def test_object_info_timeout_routes_to_connection_error(self, capsys): "nodes": [ { "id": 1, - "type": "EmptyLatentImage", + "type": "EmptyImage", "inputs": [], "outputs": [], - "widgets_values": [64, 64, 1], + "widgets_values": [64, 64, 1, 0], "mode": 0, } ], @@ -1414,7 +1440,7 @@ def test_object_info_timeout_routes_to_connection_error(self, capsys): try: with ( patch("comfy_cli.command.run.check_comfy_server_running", return_value=True), - patch("comfy_cli.http._AUTHED_OPENER.open", side_effect=TimeoutError("timed out")), + patch("comfy_cli.http._PLAIN_OPENER.open", side_effect=TimeoutError("timed out")), ): lines, exit_code = _run_execute_capture(path, capsys) assert _envelope(lines)["error"]["code"] == "connection_error" @@ -1427,10 +1453,10 @@ def test_object_info_non_json_body_routes_to_object_info_unavailable(self, capsy "nodes": [ { "id": 1, - "type": "EmptyLatentImage", + "type": "EmptyImage", "inputs": [], "outputs": [], - "widgets_values": [64, 64, 1], + "widgets_values": [64, 64, 1, 0], "mode": 0, } ], @@ -1876,7 +1902,7 @@ def test_api_workflow_async_stream_matches_documented_archetype( assert isinstance(queued["client_id"], str) and queued["client_id"] assert queued["validation_warnings"] == [] assert queued["nodes"] == [ - {"node_id": "1", "class_type": "EmptyLatentImage", "title": "Latent"}, + {"node_id": "1", "class_type": "EmptyImage", "title": "Image"}, {"node_id": "2", "class_type": "SaveImage", "title": "Save"}, ] assert queued["base_url"] == "https://api.comfy.org" diff --git a/tests/comfy_cli/output/test_error_envelope_where.py b/tests/comfy_cli/output/test_error_envelope_where.py index e84ae3a06..ed0ae837f 100644 --- a/tests/comfy_cli/output/test_error_envelope_where.py +++ b/tests/comfy_cli/output/test_error_envelope_where.py @@ -324,15 +324,19 @@ def _workflow(tmp_path): def test_cloud(self, tmp_path): err = LoadError("no object_info", details={"hint": "sign in"}) - with patch("comfy_cli.cql.engine.Graph.load", side_effect=err): + with patch("comfy_cli.cql.loader.resilient_load_object_info", side_effect=err) as load: env = _run(["--json", "validate", "--workflow", self._workflow(tmp_path), "--where", "cloud"]) + load.assert_called_once() + assert load.call_args.kwargs["mode"] == "cloud" assert env["error"]["code"] == "cql_no_graph" assert env["where"] == "cloud" def test_local(self, tmp_path): err = LoadError("no object_info", details={"hint": "comfy launch"}) - with patch("comfy_cli.cql.engine.Graph.load", side_effect=err): + with patch("comfy_cli.cql.loader.resilient_load_object_info", side_effect=err) as load: env = _run(["--json", "validate", "--workflow", self._workflow(tmp_path), "--where", "local"]) + load.assert_called_once() + assert load.call_args.kwargs["mode"] == "local" assert env["error"]["code"] == "cql_no_graph" assert env["where"] == "local" From cca48d84a820b009d2a5f36d823e1859ef1d92fe Mon Sep 17 00:00:00 2001 From: "eric.sun" Date: Thu, 29 Oct 2026 10:41:33 -0700 Subject: [PATCH 4/6] Fix self-upgrade pins, target selection, and machine output --- CHANGELOG.md | 10 +- comfy_cli/cmdline.py | 14 +- comfy_cli/error_codes.py | 10 +- comfy_cli/update.py | 87 ++++++++++- .../command/test_update_missing_workspace.py | 131 ++++++++++++++++- tests/comfy_cli/test_update.py | 135 ++++++++++++++++++ 6 files changed, 370 insertions(+), 17 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c749b2781..7b3f9b7c3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -205,11 +205,17 @@ history. - `comfy update` no longer dies with a `FileNotFoundError` traceback when the configured workspace directory does not exist (never installed, or deleted). - It now fails with a `not_in_workspace` error that points at `comfy install`. + It now fails with a `not_in_workspace` error that points at `comfy install`, + including when `--recent` cannot resolve a workspace, keeping the resolver's + exit status. - `comfy update cli` works for a `uv tool install` of comfy-cli. Those envs ship without pip, so the old `python -m pip install -U` always failed; it now runs `uv tool upgrade comfy-cli`, and any installer failure is reported as an - `update_cli_failed` error rather than a traceback. + `update_cli_failed` error rather than a traceback. The upgrade targets the + running installation's tool directory even when `UV_TOOL_DIR` is absent or + selects a different directory in the current shell. An exact uv version pin + 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. - Editing a `Load3DAdvanced` node saved with the old LOAD_3D button values (`upload3dmodel`, `uploadExtraResources`, `clear`) now drops them, so the written `widgets_values` is the frontend's four-widget shape diff --git a/comfy_cli/cmdline.py b/comfy_cli/cmdline.py index 78e26c398..1f0a11508 100644 --- a/comfy_cli/cmdline.py +++ b/comfy_cli/cmdline.py @@ -1790,9 +1790,21 @@ def update( hint="check the installer executable and its permissions, then re-run `comfy update cli`", ) raise typer.Exit(code=1) from e + get_renderer().emit({"target": "cli"}, command="update") return - comfy_path = workspace_manager.workspace_path + try: + comfy_path = workspace_manager.workspace_path + except typer.Exit as error: + if target != "comfy": + raise + get_renderer().error( + code="not_in_workspace", + message="ComfyUI workspace could not be resolved.", + hint="run `comfy install`, or pass `--workspace` to point at an existing install", + exit_code=error.exit_code, + ) + raise if "all" == target: # Without raise_on_error, execute_cm_cli swallows a cm-cli exit 1 and returns None, diff --git a/comfy_cli/error_codes.py b/comfy_cli/error_codes.py index 756dd5688..67eb799c3 100644 --- a/comfy_cli/error_codes.py +++ b/comfy_cli/error_codes.py @@ -1206,11 +1206,11 @@ class ErrorCode: ErrorCode( "update_cli_failed", "`comfy update cli` could not upgrade comfy-cli: either no usable installer was found " - "(a `uv tool` env without `uv` on PATH, or an env without pip), or the installer could not start, " - "timed out, or exited non-zero. `details.timeout` carries the expired time budget; " - "`details.returncode` carries a non-zero exit status.", - "follow the error's hint; for a `uv tool` install, ensure `uv` is on PATH before running " - "`uv tool upgrade comfy-cli`", + "(a `uv tool` env without `uv` on PATH, or an env without pip), the uv receipt is unreadable " + "or pins the CLI to an exact version, or the installer could not start, timed out, or exited non-zero. " + "`details.timeout` carries the expired time budget; `details.returncode` carries a non-zero exit status.", + "follow the error's hint; for a `uv tool` install, ensure `uv` is on PATH and use the stated " + "UV_TOOL_DIR; change an exact version pin only through an explicit reinstall", ), ErrorCode( "version_switch_unknown_version", diff --git a/comfy_cli/update.py b/comfy_cli/update.py index 37989098d..0230f8bdb 100644 --- a/comfy_cli/update.py +++ b/comfy_cli/update.py @@ -44,28 +44,100 @@ def check_for_newer_pypi_version(package_name, current_version): class UpgradeUnavailable(RuntimeError): - """No installer can upgrade the environment comfy-cli is running from.""" + """The running installation cannot be upgraded automatically.""" def __init__(self, message: str, hint: str): super().__init__(message) self.hint = hint +def _check_uv_tool_pin(receipt: Path) -> None: + """Preserve exact primary-tool constraints rather than reporting a no-op upgrade.""" + from packaging.requirements import Requirement + from packaging.specifiers import SpecifierSet + from packaging.utils import canonicalize_name + from tomlkit import parse + from tomlkit.exceptions import ParseError + + tool_root = receipt.parent.parent + hint = ( + "Review the receipt. To change or remove a pin, explicitly reinstall using your chosen requirement " + "(for example `uv tool install --force comfy-cli` to remove it); " + f"first set UV_TOOL_DIR to {tool_root} to target this installation." + ) + pinned = False + try: + tool = parse(receipt.read_text(encoding="utf-8")).unwrap().get("tool") + if not isinstance(tool, dict): + raise ValueError("missing tool table") + for field in ("requirements", "constraints"): + rows = tool.get(field, []) + if not isinstance(rows, list): + raise ValueError("invalid requirement list") + for row in rows: + if isinstance(row, str): + requirement = Requirement(row) + name, specifiers = requirement.name, requirement.specifier + elif isinstance(row, dict): + name = row.get("name") + if not isinstance(name, str): + raise ValueError("missing requirement name") + parsed_name = Requirement(name) + if parsed_name.extras or parsed_name.url or parsed_name.specifier or parsed_name.marker: + raise ValueError("invalid package name") + if canonicalize_name(name) != "comfy-cli": + continue + specifier = row.get("specifier", "") + if not isinstance(specifier, str): + raise ValueError("invalid primary specifier") + specifiers = SpecifierSet(specifier) + else: + raise ValueError("invalid requirement row") + if canonicalize_name(name) == "comfy-cli" and any( + spec.operator == "===" or (spec.operator == "==" and not spec.version.endswith(".*")) + for spec in specifiers + ): + pinned = True + except (OSError, ValueError, ParseError) as error: + raise UpgradeUnavailable("The running uv tool receipt could not be read or understood.", hint=hint) from error + if pinned: + raise UpgradeUnavailable( + "The running comfy-cli installation has an exact uv version pin; this command will not remove it.", + hint=hint, + ) + + def upgrade_cli(): """Upgrade the ``comfy-cli`` package itself in the environment it runs from. A ``uv tool install`` env ships without pip, so it is upgraded through ``uv tool upgrade``; anything else goes through pip against the running - interpreter. Raises ``UpgradeUnavailable`` when neither installer is usable - and propagates installer exit, start, or timeout failures to the command.""" - if (Path(sys.prefix) / "uv-receipt.toml").is_file(): + interpreter. Exact uv pins remain unchanged. Raises ``UpgradeUnavailable`` + for unusable installers or receipts, and propagates installer exit, start, + or timeout failures to the command.""" + from comfy_cli.output import get_renderer + + installer_stdout = sys.stderr if get_renderer().is_json() else None + receipt = Path(sys.prefix) / "uv-receipt.toml" + if receipt.is_file(): uv = resolve_binary("uv") if uv is None: raise UpgradeUnavailable( "comfy-cli was installed with `uv tool`, but `uv` is not on PATH.", hint="run `uv tool upgrade comfy-cli` from a shell where uv is available", ) - subprocess.run([uv, "tool", "upgrade", "comfy-cli"], check=True, timeout=CLI_UPGRADE_TIMEOUT_SECONDS) + _check_uv_tool_pin(receipt) + # Upgrade the running installation even if its original tool-dir override + # is absent now, or the current shell selects another tools directory. + installer_env = os.environ.copy() + installer_env["UV_TOOL_DIR"] = str(Path(sys.prefix).parent) + subprocess.run( + [uv, "tool", "upgrade", "comfy-cli"], + check=True, + timeout=CLI_UPGRADE_TIMEOUT_SECONDS, + env=installer_env, + stdout=installer_stdout, + ) return if importlib.util.find_spec("pip") is None: raise UpgradeUnavailable( @@ -73,7 +145,10 @@ def upgrade_cli(): hint="upgrade comfy-cli with the tool that installed it (e.g. `pipx upgrade comfy-cli`)", ) subprocess.run( - [sys.executable, "-m", "pip", "install", "-U", "comfy-cli"], check=True, timeout=CLI_UPGRADE_TIMEOUT_SECONDS + [sys.executable, "-m", "pip", "install", "-U", "comfy-cli"], + check=True, + timeout=CLI_UPGRADE_TIMEOUT_SECONDS, + stdout=installer_stdout, ) diff --git a/tests/comfy_cli/command/test_update_missing_workspace.py b/tests/comfy_cli/command/test_update_missing_workspace.py index 65d368922..10c296eb6 100644 --- a/tests/comfy_cli/command/test_update_missing_workspace.py +++ b/tests/comfy_cli/command/test_update_missing_workspace.py @@ -12,12 +12,15 @@ import json import re import subprocess +import sys +from importlib.machinery import ModuleSpec from unittest.mock import patch import pytest +import typer from typer.testing import CliRunner -from comfy_cli import cmdline +from comfy_cli import cmdline, constants from comfy_cli.workspace_manager import WorkspaceType runner = CliRunner() @@ -85,6 +88,58 @@ def test_unresolved_path_is_a_structured_error(self): assert "comfy install" in envelope["error"]["hint"] +class TestRecentWorkspaceResolution: + @pytest.mark.parametrize("output_flag", ["--json", "--no-json"]) + @pytest.mark.parametrize("target_args", [[], ["comfy"], ["comfy", "--version", "nightly"]]) + def test_missing_recent_has_structured_comfy_failure(self, monkeypatch, output_flag, target_args): + manager_config = cmdline.workspace_manager.config_manager + original_get = manager_config.get + monkeypatch.setattr( + manager_config, + "get", + lambda key: None if key == constants.CONFIG_KEY_RECENT_WORKSPACE else original_get(key), + ) + with patch(SUBPROCESS_RUN) as installer, patch(SWITCH_VERSION) as switch: + result = runner.invoke(cmdline.app, [output_flag, "--recent", "update", *target_args]) + assert result.exit_code == 1 + assert result.exception is None or isinstance(result.exception, SystemExit) + if output_flag == "--json": + lines = [line for line in result.stdout.splitlines() if line.strip()] + assert len(lines) == 1 + envelope = json.loads(lines[0]) + assert envelope["ok"] is False + assert envelope["error"]["code"] == "not_in_workspace" + assert "comfy install" in envelope["error"]["hint"] + else: + assert "not_in_workspace" in _strip_ansi(result.stdout) + installer.assert_not_called() + switch.assert_not_called() + + @pytest.mark.parametrize("exit_code", [1, 7]) + def test_comfy_resolution_keeps_resolver_exit_status(self, exit_code): + with ( + patch.object(cmdline.workspace_manager, "get_workspace_path", side_effect=typer.Exit(code=exit_code)), + patch(SUBPROCESS_RUN) as installer, + ): + result = runner.invoke(cmdline.app, ["--json", "update", "comfy"]) + assert result.exit_code == exit_code + lines = [line for line in result.stdout.splitlines() if line.strip()] + assert len(lines) == 1 + assert json.loads(lines[0])["error"]["code"] == "not_in_workspace" + installer.assert_not_called() + + @pytest.mark.parametrize("exit_code", [1, 7]) + def test_all_resolution_keeps_existing_exit_without_manager_execution(self, exit_code): + with ( + patch.object(cmdline.workspace_manager, "get_workspace_path", side_effect=typer.Exit(code=exit_code)), + patch("comfy_cli.command.custom_nodes.command.execute_cm_cli") as manager, + ): + result = runner.invoke(cmdline.app, ["--json", "update", "all"]) + assert result.exit_code == exit_code + assert "not_in_workspace" not in result.stdout + manager.assert_not_called() + + class TestUpdateCliFailures: """`comfy update cli` used to let installer failures escape as tracebacks.""" @@ -101,8 +156,6 @@ def test_unavailable_installer_is_a_structured_error(self): assert envelope["error"]["hint"] == "run `uv tool upgrade comfy-cli`" def test_failed_installer_is_a_structured_error(self): - import subprocess - err = subprocess.CalledProcessError(1, ["uv", "tool", "upgrade", "comfy-cli"]) with patch("comfy_cli.update.upgrade_cli", side_effect=err): result = runner.invoke(cmdline.app, ["--json", "update", "cli"]) @@ -137,3 +190,75 @@ def test_installer_start_and_timeout_failures_are_structured(self, installer_err assert envelope["error"]["details"]["timeout"] == 600 else: assert "update_cli_failed" in _strip_ansi(result.stdout) + + +class TestUpdateCliInstallerOutput: + @pytest.mark.parametrize("installer", ["pip", "uv"]) + @pytest.mark.parametrize("output_flag", ["--json", "--json-stream", "--no-json"]) + @pytest.mark.parametrize("fails", [False, True]) + def test_installer_diagnostics_follow_output_mode(self, tmp_path, monkeypatch, installer, output_flag, fails): + from comfy_cli import update + + prefix = tmp_path / "tools" / "comfy-cli" + prefix.mkdir(parents=True) + if installer == "uv": + (prefix / "uv-receipt.toml").write_text('[tool]\nrequirements = [{ name = "comfy-cli" }]\n') + monkeypatch.setattr(update.sys, "prefix", str(prefix)) + monkeypatch.setattr(update, "resolve_binary", lambda name: "/opt/bin/uv") + monkeypatch.setattr(update.importlib.util, "find_spec", lambda name: ModuleSpec("pip", loader=None)) + + def simulated_installer(argv, **kwargs): + print("installer diagnostic sentinel", file=kwargs.get("stdout") or sys.stdout) + if fails: + raise subprocess.CalledProcessError(3, argv) + return subprocess.CompletedProcess(argv, 0) + + monkeypatch.setattr(update.subprocess, "run", simulated_installer) + result = runner.invoke(cmdline.app, [output_flag, "update", "cli"]) + assert result.exit_code == (1 if fails else 0) + if output_flag == "--no-json": + assert "installer diagnostic sentinel" in result.stdout + assert "installer diagnostic sentinel" not in result.stderr + else: + assert "installer diagnostic sentinel" not in result.stdout + assert "installer diagnostic sentinel" in result.stderr + lines = [line for line in result.stdout.splitlines() if line.strip()] + assert len(lines) == 1 + envelope = json.loads(lines[0]) + assert envelope["type"] == "envelope" + assert envelope["ok"] is not fails + if fails: + assert envelope["error"]["code"] == "update_cli_failed" + assert envelope["error"]["details"]["returncode"] == 3 + else: + assert envelope["data"] == {"target": "cli"} + + +@pytest.mark.parametrize("output_flag", ["--json", "--json-stream", "--no-json"]) +def test_pinned_uv_receipt_is_a_structured_cli_failure(tmp_path, monkeypatch, output_flag): + from comfy_cli import update + + prefix = tmp_path / "custom tools" / "comfy-cli" + prefix.mkdir(parents=True) + path = prefix / "uv-receipt.toml" + receipt = '[tool]\nrequirements = [{ name = "comfy-cli", specifier = "==1.2.3" }]\n' + path.write_text(receipt) + monkeypatch.setattr(update.sys, "prefix", str(prefix)) + monkeypatch.setattr(update, "resolve_binary", lambda name: "/opt/bin/uv") + with patch("comfy_cli.update.subprocess.run") as installer: + result = runner.invoke(cmdline.app, [output_flag, "update", "cli"]) + assert result.exit_code == 1 + assert result.exception is None or isinstance(result.exception, SystemExit) + if output_flag == "--no-json": + assert "update_cli_failed" in _strip_ansi(result.stdout) + assert "reinstall" in _strip_ansi(result.stdout).lower() + else: + lines = [line for line in result.stdout.splitlines() if line.strip()] + assert len(lines) == 1 + error = json.loads(lines[0])["error"] + assert error["code"] == "update_cli_failed" + assert "reinstall" in error["hint"].lower() + assert str(prefix.parent) in error["hint"] + assert "UV_TOOL_DIR" in error["hint"] + assert path.read_text() == receipt + installer.assert_not_called() diff --git a/tests/comfy_cli/test_update.py b/tests/comfy_cli/test_update.py index 41b7e28c8..de60e617d 100644 --- a/tests/comfy_cli/test_update.py +++ b/tests/comfy_cli/test_update.py @@ -126,6 +126,37 @@ def test_uv_tool_install_upgrades_through_uv(self, mock_run, tmp_path): assert mock_run.call_args[1]["check"] is True assert mock_run.call_args[1].get("timeout") == 600 + @pytest.mark.parametrize("ambient_tool_dir", [None, "other", "current"]) + @patch("comfy_cli.update.subprocess.run") + def test_uv_upgrade_targets_running_install_not_ambient_tool_dir( + self, mock_run, tmp_path, monkeypatch, ambient_tool_dir + ): + tool_root = tmp_path / "custom-tools" + prefix = tool_root / "comfy-cli" + prefix.mkdir(parents=True) + (prefix / "uv-receipt.toml").write_text("[tool]\n") + previous = ( + None + if ambient_tool_dir is None + else str(tool_root if ambient_tool_dir == "current" else tmp_path / "other-tools") + ) + if previous is None: + monkeypatch.delenv("UV_TOOL_DIR", raising=False) + else: + monkeypatch.setenv("UV_TOOL_DIR", previous) + monkeypatch.setenv("UV_SELF_UPGRADE_TEST_SENTINEL", "preserved") + with ( + patch.object(update.sys, "prefix", str(prefix)), + patch("comfy_cli.update.resolve_binary", return_value="/opt/bin/uv"), + ): + upgrade_cli() + installer_env = mock_run.call_args.kwargs.get("env", {}) + assert installer_env.get("UV_TOOL_DIR") == str(tool_root) + assert installer_env.get("UV_SELF_UPGRADE_TEST_SENTINEL") == "preserved" + assert update.os.environ.get("UV_TOOL_DIR") == previous + assert mock_run.call_args.args[0] == ["/opt/bin/uv", "tool", "upgrade", "comfy-cli"] + assert mock_run.call_args.kwargs["timeout"] == 600 + @patch("comfy_cli.update.subprocess.run") def test_uv_tool_install_without_uv_on_path_is_unavailable(self, mock_run, tmp_path): (tmp_path / "uv-receipt.toml").write_text("[tool]\n") @@ -167,3 +198,107 @@ def test_latest_upgrade_version_survives_missing_latest_key(mock_check, tmp_path def test_latest_upgrade_version_survives_unparseable_latest(tmp_path): (tmp_path / "update-check.json").write_text(json.dumps({"checked_at": time.time(), "latest": "unknown"})) assert update.latest_upgrade_version("1.0.0", str(tmp_path)) is None + + +@pytest.mark.parametrize( + "receipt", + [ + '[tool]\nrequirements = [{ name = "comfy-cli", specifier = "==1.2.3" }]\n', + '[tool]\nrequirements = [{ name = "comfy-cli", specifier = "===1.2.3" }]\n', + '[tool]\nrequirements = [{ name = "Comfy_Cli", specifier = ">=1,==1.2.3" }]\n', + '[tool]\nrequirements = ["comfy-cli==1.2.3"]\n', + '[tool]\nrequirements = [{ name = "comfy-cli" }]\nconstraints = [{ name = "comfy-cli", specifier = "==1.2.3" }]\n', + ], + ids=["exact", "arbitrary-exact", "mixed-normalized", "legacy", "constraint"], +) +def test_uv_exact_pin_blocks_installer_and_preserves_receipt(tmp_path, monkeypatch, receipt): + prefix = tmp_path / "original tools" / "comfy-cli" + prefix.mkdir(parents=True) + path = prefix / "uv-receipt.toml" + path.write_text(receipt) + monkeypatch.setattr(update.sys, "prefix", str(prefix)) + monkeypatch.setattr(update, "resolve_binary", lambda name: "/opt/bin/uv") + with patch("comfy_cli.update.subprocess.run") as installer, pytest.raises(update.UpgradeUnavailable) as failure: + upgrade_cli() + assert "pin" in str(failure.value).lower() + assert "UV_TOOL_DIR" in failure.value.hint + assert str(prefix.parent) in failure.value.hint + assert "reinstall" in failure.value.hint.lower() + assert path.read_text() == receipt + installer.assert_not_called() + + +@pytest.mark.parametrize( + "receipt", + [ + "[tool]\n", + '[tool]\nrequirements = [{ name = "comfy-cli" }]\n', + '[tool]\nrequirements = [{ name = "comfy-cli", specifier = ">=1,<2" }]\n', + '[tool]\nrequirements = [{ name = "comfy-cli", specifier = "==1.2.*" }]\n', + '[tool]\nrequirements = [{ name = "comfy-cli", specifier = ">=1" }, { name = "helper", specifier = "==2" }]\nconstraints = [{ name = "helper", specifier = "==2" }]\n', + '[tool]\nrequirements = ["comfy-cli>=1", "helper==2"]\n', + ], + ids=["legacy-empty", "unbounded", "range", "wildcard", "other-dependency", "legacy-range"], +) +def test_uv_unpinned_or_ranged_receipt_keeps_existing_upgrade(tmp_path, monkeypatch, receipt): + prefix = tmp_path / "tools" / "comfy-cli" + prefix.mkdir(parents=True) + path = prefix / "uv-receipt.toml" + path.write_text(receipt) + monkeypatch.setattr(update.sys, "prefix", str(prefix)) + monkeypatch.setattr(update, "resolve_binary", lambda name: "/opt/bin/uv") + with patch("comfy_cli.update.subprocess.run") as installer: + upgrade_cli() + assert installer.call_count == 1 + assert installer.call_args.args[0] == ["/opt/bin/uv", "tool", "upgrade", "comfy-cli"] + assert installer.call_args.kwargs["env"]["UV_TOOL_DIR"] == str(prefix.parent) + assert installer.call_args.kwargs["check"] is True + assert installer.call_args.kwargs["timeout"] == 600 + assert path.read_text() == receipt + + +@pytest.mark.parametrize( + "receipt", + [ + "[tool", + 'other = "invalid"\n', + '[tool]\nrequirements = "invalid"\n', + "[tool]\nrequirements = [7]\n", + '[tool]\nrequirements = [{name="comfy-cli", specifier="token=secret"}]\n', + ], +) +def test_malformed_uv_receipt_is_structured_and_never_runs_installer(tmp_path, monkeypatch, receipt): + prefix = tmp_path / "tools" / "comfy-cli" + prefix.mkdir(parents=True) + path = prefix / "uv-receipt.toml" + path.write_text(receipt) + monkeypatch.setattr(update.sys, "prefix", str(prefix)) + monkeypatch.setattr(update, "resolve_binary", lambda name: "/opt/bin/uv") + with patch("comfy_cli.update.subprocess.run") as installer, pytest.raises(update.UpgradeUnavailable) as failure: + upgrade_cli() + assert "receipt" in str(failure.value).lower() + assert "token=secret" not in str(failure.value) + assert "UV_TOOL_DIR" in failure.value.hint + installer.assert_not_called() + + +def test_unreadable_uv_receipt_is_unavailable_without_exposing_details(tmp_path, monkeypatch): + prefix = tmp_path / "tools" / "comfy-cli" + prefix.mkdir(parents=True) + receipt = prefix / "uv-receipt.toml" + receipt.write_text("[tool]\n") + monkeypatch.setattr(update.sys, "prefix", str(prefix)) + monkeypatch.setattr(update, "resolve_binary", lambda name: "/opt/bin/uv") + original_read = update.Path.read_text + + def guarded_read(path, *args, **kwargs): + if path == receipt: + raise PermissionError("sensitive-parser-details") + return original_read(path, *args, **kwargs) + + monkeypatch.setattr(update.Path, "read_text", guarded_read) + with patch("comfy_cli.update.subprocess.run") as installer, pytest.raises(update.UpgradeUnavailable) as failure: + upgrade_cli() + assert "receipt" in str(failure.value).lower() + assert "sensitive-parser-details" not in str(failure.value) + installer.assert_not_called() From e9b6f211943196bbb46724edba18773c802a2581 Mon Sep 17 00:00:00 2001 From: "eric.sun" Date: Thu, 29 Oct 2026 10:41:43 -0700 Subject: [PATCH 5/6] Isolate credentials and platform fixtures in the full test suite --- .../comfy_cli/command/test_deploy_progress.py | 9 +++++++- .../command/test_pretty_print_sanitize.py | 3 ++- tests/comfy_cli/command/test_run.py | 21 +++++++++++-------- tests/comfy_cli/command/test_run_prompt.py | 16 +++++++++----- tests/comfy_cli/conftest.py | 4 ++++ tests/comfy_cli/test_http.py | 5 +++-- 6 files changed, 40 insertions(+), 18 deletions(-) diff --git a/tests/comfy_cli/command/test_deploy_progress.py b/tests/comfy_cli/command/test_deploy_progress.py index 5aaaf1927..051c62da3 100644 --- a/tests/comfy_cli/command/test_deploy_progress.py +++ b/tests/comfy_cli/command/test_deploy_progress.py @@ -683,7 +683,14 @@ class _TerminalRenderer: def __init__(self, width: int) -> None: from rich.console import Console - self._console = Console(file=io.StringIO(), width=width, force_terminal=True, color_system=None) + # Emulate the promised terminal even when the caller exports TERM=dumb. + self._console = Console( + file=io.StringIO(), + width=width, + force_terminal=True, + color_system=None, + _environ={"TERM": "xterm-256color"}, + ) def is_pretty(self) -> bool: return True diff --git a/tests/comfy_cli/command/test_pretty_print_sanitize.py b/tests/comfy_cli/command/test_pretty_print_sanitize.py index ed8f70792..7e1b00407 100644 --- a/tests/comfy_cli/command/test_pretty_print_sanitize.py +++ b/tests/comfy_cli/command/test_pretty_print_sanitize.py @@ -708,7 +708,8 @@ def test_status_glyph_keeps_its_own_tags_live(pretty): from comfy_cli.output.glyphs import status_glyph - console = Console(file=pretty, force_terminal=True, width=80) + # This tests authored styling, independently of the caller's terminal type. + console = Console(file=pretty, force_terminal=True, width=80, _environ={"TERM": "xterm-256color"}) console.print(status_glyph("success")) out = pretty.getvalue() diff --git a/tests/comfy_cli/command/test_run.py b/tests/comfy_cli/command/test_run.py index 9731c0233..e5c93051d 100644 --- a/tests/comfy_cli/command/test_run.py +++ b/tests/comfy_cli/command/test_run.py @@ -2363,14 +2363,16 @@ def test_markup_in_node_name_does_not_crash_confirm(self, monkeypatch): class TestExecuteUiWorkflow: + # The conversion success fixture must also be an executable graph: + # PreviewImage consumes IMAGE, not an EmptyLatentImage's LATENT output. UI = { "nodes": [ { "id": 1, - "type": "EmptyLatentImage", + "type": "EmptyImage", "inputs": [], - "outputs": [{"name": "LATENT", "type": "LATENT", "links": [10]}], - "widgets_values": [512, 512, 1], + "outputs": [{"name": "IMAGE", "type": "IMAGE", "links": [10]}], + "widgets_values": [512, 512, 1, 0], "mode": 0, }, { @@ -2384,19 +2386,20 @@ class TestExecuteUiWorkflow: "links": [[10, 1, 0, 2, 0, "IMAGE"]], } OBJECT_INFO = { - "EmptyLatentImage": { + "EmptyImage": { "input": { "required": { "width": ["INT", {"default": 512}], "height": ["INT", {"default": 512}], "batch_size": ["INT", {"default": 1}], + "color": ["INT", {"default": 0}], } }, - "input_order": {"required": ["width", "height", "batch_size"]}, - "output": ["LATENT"], - "output_name": ["LATENT"], + "input_order": {"required": ["width", "height", "batch_size", "color"]}, + "output": ["IMAGE"], + "output_name": ["IMAGE"], "output_node": False, - "display_name": "Empty Latent Image", + "display_name": "Empty Image", }, "PreviewImage": { "input": {"required": {"images": ["IMAGE"]}}, @@ -2435,7 +2438,7 @@ def test_ui_workflow_is_converted_then_executed(self, ui_workflow_file): assert mock_fetch.call_args.args == ("127.0.0.1", 8188, 30) api_workflow = MockExec.call_args.args[0] assert set(api_workflow) == {"1", "2"} - assert api_workflow["1"]["class_type"] == "EmptyLatentImage" + assert api_workflow["1"]["class_type"] == "EmptyImage" assert api_workflow["2"]["inputs"]["images"] == ["1", 0] mock_exec.queue.assert_called_once() diff --git a/tests/comfy_cli/command/test_run_prompt.py b/tests/comfy_cli/command/test_run_prompt.py index c67139558..da6f3c557 100644 --- a/tests/comfy_cli/command/test_run_prompt.py +++ b/tests/comfy_cli/command/test_run_prompt.py @@ -9,6 +9,8 @@ from __future__ import annotations +import json +from pathlib import Path from unittest.mock import MagicMock, patch import pytest @@ -32,11 +34,15 @@ class TestExecuteSubmitsPreloadedGraph: """`preloaded` short-circuits file loading; the in-memory graph is what reaches the submit path (WorkflowExecution).""" + @pytest.fixture(autouse=True) + def preloaded_catalog(self): + """Validate the injected graph against a frozen catalog, without a server.""" + catalog = json.loads((Path(__file__).resolve().parents[1] / "fixtures/sd15_object_info.json").read_text()) + with patch("comfy_cli.command.run._fetch_object_info", return_value=catalog): + yield + def test_preloaded_graph_is_submitted(self): - injected = { - "6": {"class_type": "CLIPTextEncode", "inputs": {"text": "a red fox in snow"}}, - "9": {"class_type": "SaveImage", "inputs": {"images": ["8", 0]}}, - } + injected = build_default_workflow(prompt="a red fox in snow") with ( patch("comfy_cli.command.run.check_comfy_server_running", return_value=True), patch("comfy_cli.command.run.ExecutionProgress"), @@ -63,7 +69,7 @@ def test_preloaded_graph_is_submitted(self): def test_preloaded_skips_file_loading(self): """A non-existent path never triggers workflow_not_found when preloaded.""" - injected = {"9": {"class_type": "SaveImage", "inputs": {}}} + injected = build_default_workflow(prompt="fox") with ( patch("comfy_cli.command.run.check_comfy_server_running", return_value=True), patch("comfy_cli.command.run.ExecutionProgress"), diff --git a/tests/comfy_cli/conftest.py b/tests/comfy_cli/conftest.py index 522edcf38..2fa85f4b7 100644 --- a/tests/comfy_cli/conftest.py +++ b/tests/comfy_cli/conftest.py @@ -72,6 +72,10 @@ def _isolate_config_path(tmp_path, monkeypatch): # whichever ``get_os()`` resolves to lands in our tmp dir. for k in list(constants.DEFAULT_CONFIG.keys()): monkeypatch.setitem(constants.DEFAULT_CONFIG, k, str(fake_root)) + # Constants patched in this process do not reach CLI subprocesses. Give + # each test its own supported store override, also replacing any ambient + # override so credential writes cannot leak between tests or reach a user. + monkeypatch.setenv("COMFY_SECRETS_PATH", str(fake_root / "secrets.json")) # ConfigManager is a @singleton that reads the config dir once, at # construction — so repointing the dir alone leaves the FIRST test's # in-memory config serving every later one. Any test that runs the CLI diff --git a/tests/comfy_cli/test_http.py b/tests/comfy_cli/test_http.py index 703199d85..20adb7027 100644 --- a/tests/comfy_cli/test_http.py +++ b/tests/comfy_cli/test_http.py @@ -819,7 +819,6 @@ def test_an_unloadable_supplement_falls_through_to_the_platform_roots(tmp_path, monkeypatch.setitem(sys.modules, "certifi", types.SimpleNamespace(where=lambda: str(broken))) real = ssl.create_default_context - populated = len(pathlib.Path(certifi.where()).read_text().split("-----BEGIN CERTIFICATE-----")) - 1 def platform_with_roots(*args, **kwargs): context = real(*args, **kwargs) @@ -827,13 +826,15 @@ def platform_with_roots(*args, **kwargs): context.load_verify_locations(cafile=certifi.where()) return context + expected_roots = set(platform_with_roots().get_ca_certs(binary_form=True)) + assert expected_roots, "the platform fixture must contain trust roots" monkeypatch.setattr(ssl, "create_default_context", platform_with_roots) # When context = http_mod.ssl_context() # Then - assert context.cert_store_stats()["x509_ca"] == populated, ( + assert set(context.get_ca_certs(binary_form=True)) == expected_roots, ( "the platform roots must survive a supplement that would not load" ) From 7aa10dc980fd45b1d2913e81b01d5f6427047cf7 Mon Sep 17 00:00:00 2001 From: "eric.sun" Date: Sat, 31 Oct 2026 11:54:24 -0700 Subject: [PATCH 6/6] test: isolate conversion preflight catalog --- tests/comfy_cli/command/test_run.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/tests/comfy_cli/command/test_run.py b/tests/comfy_cli/command/test_run.py index e5c93051d..3af335823 100644 --- a/tests/comfy_cli/command/test_run.py +++ b/tests/comfy_cli/command/test_run.py @@ -2363,6 +2363,19 @@ def test_markup_in_node_name_does_not_crash_confirm(self, monkeypatch): class TestExecuteUiWorkflow: + @pytest.fixture(autouse=True) + def local_preflight_catalog(self, monkeypatch): + """Validate against the complete conversion fixture without a live server.""" + from copy import deepcopy + + monkeypatch.setattr("comfy_cli.command.run._fetch_object_info", lambda *a, **k: deepcopy(self.OBJECT_INFO)) + + def unexpected_catalog_request(*args, **kwargs): + pytest.fail("unexpected real catalog request; mock the specific object_info transport") + + monkeypatch.setattr("comfy_cli.http._PLAIN_OPENER.open", unexpected_catalog_request) + monkeypatch.setattr("comfy_cli.cql.engine._load_from_target", unexpected_catalog_request) + # The conversion success fixture must also be an executable graph: # PreviewImage consumes IMAGE, not an EmptyLatentImage's LATENT output. UI = {