From 374cf3de29c0efa473e6f382d9ceeb5349ec17d4 Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 5 Nov 2026 12:03:30 +0000 Subject: [PATCH] Forget a stale namespace 'app' after a failed ComfyUI logger import Fixes the 'cannot import name governance from app' collection error when pytest loads the root package before ComfyUI is on sys.path. Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_01Tw9fjV3UWsHHjJiAXix29B --- core/backend_log.py | 4 ++++ core/tests/test_backend_log_app_import.py | 15 +++++++++++++++ 2 files changed, 19 insertions(+) create mode 100644 core/tests/test_backend_log_app_import.py diff --git a/core/backend_log.py b/core/backend_log.py index b25faec0..4bb4fa9b 100644 --- a/core/backend_log.py +++ b/core/backend_log.py @@ -90,6 +90,10 @@ def _memory(): try: import app.logger as comfy_log except Exception: # noqa: BLE001 + # Without ComfyUI on the path, `app` resolves to our own folder (a namespace package) and stays cached, + # which would later hide ComfyUI's real `app` package. Forget it. + if getattr(sys.modules.get("app"), "__file__", True) is None: + sys.modules.pop("app", None) return None logs = comfy_log.logs if logs is not None and not isinstance(logs, Kept): diff --git a/core/tests/test_backend_log_app_import.py b/core/tests/test_backend_log_app_import.py new file mode 100644 index 00000000..8901766e --- /dev/null +++ b/core/tests/test_backend_log_app_import.py @@ -0,0 +1,15 @@ +import subprocess +import sys +from pathlib import Path + +ROOT = Path(__file__).resolve().parents[2] + + +def test_failed_comfy_logger_import_leaves_no_stale_app_package(): + """Outside ComfyUI, `app` is our own folder; it must not stay cached and hide ComfyUI's real `app` later.""" + code = ( + "import sys; sys.path.insert(0, %r); import core.backend_log; " + "print('app' in sys.modules and getattr(sys.modules['app'], '__file__', 1) is None)" + ) % str(ROOT) + out = subprocess.run([sys.executable, "-I", "-c", code], capture_output=True, text=True, cwd=str(ROOT)) + assert out.stdout.strip().endswith("False"), out.stdout + out.stderr