Skip to content

Commit 9468976

Browse files
Merge pull request #7179 from comfyanonymous/ignore_fe_package
Only check frontend package if using default frontend
2 parents ca8efab + cfbe4b4 commit 9468976

File tree

3 files changed

+85
-37
lines changed

3 files changed

+85
-37
lines changed

app/frontend_management.py

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,33 +11,43 @@
1111
from functools import cached_property
1212
from pathlib import Path
1313
from typing import TypedDict, Optional
14+
from importlib.metadata import version
1415

1516
import requests
1617
from typing_extensions import NotRequired
1718

1819
from comfy.cli_args import DEFAULT_VERSION_STRING
1920

21+
# The path to the requirements.txt file
22+
req_path = Path(__file__).parents[1] / "requirements.txt"
2023

2124
def frontend_install_warning_message():
22-
req_path = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'requirements.txt'))
25+
"""The warning message to display when the frontend version is not up to date."""
26+
2327
extra = ""
2428
if sys.flags.no_user_site:
2529
extra = "-s "
2630
return f"Please install the updated requirements.txt file by running:\n{sys.executable} {extra}-m pip install -r {req_path}\n\nThis error is happening because the ComfyUI frontend is no longer shipped as part of the main repo but as a pip package instead.\n\nIf you are on the portable package you can run: update\\update_comfyui.bat to solve this problem"
2731

28-
try:
29-
import comfyui_frontend_package
30-
except ImportError:
31-
# TODO: Remove the check after roll out of 0.3.16
32-
logging.error(f"\n\n********** ERROR ***********\n\ncomfyui-frontend-package is not installed. {frontend_install_warning_message()}\n********** ERROR **********\n")
33-
exit(-1)
3432

33+
def check_frontend_version():
34+
"""Check if the frontend version is up to date."""
35+
36+
def parse_version(version: str) -> tuple[int, int, int]:
37+
return tuple(map(int, version.split(".")))
38+
39+
try:
40+
frontend_version_str = version("comfyui-frontend-package")
41+
frontend_version = parse_version(frontend_version_str)
42+
with open(req_path, "r", encoding="utf-8") as f:
43+
required_frontend = parse_version(f.readline().split("=")[-1])
44+
if frontend_version < required_frontend:
45+
logging.warning("________________________________________________________________________\nWARNING WARNING WARNING WARNING WARNING\n\nInstalled frontend version {} is lower than the recommended version {}.\n\n{}\n________________________________________________________________________".format('.'.join(map(str, frontend_version)), '.'.join(map(str, required_frontend)), frontend_install_warning_message()))
46+
else:
47+
logging.info("ComfyUI frontend version: {}".format(frontend_version_str))
48+
except Exception as e:
49+
logging.error(f"Failed to check frontend version: {e}")
3550

36-
try:
37-
frontend_version = tuple(map(int, comfyui_frontend_package.__version__.split(".")))
38-
except:
39-
frontend_version = (0,)
40-
pass
4151

4252
REQUEST_TIMEOUT = 10 # seconds
4353

@@ -133,9 +143,17 @@ def download_release_asset_zip(release: Release, destination_path: str) -> None:
133143

134144

135145
class FrontendManager:
136-
DEFAULT_FRONTEND_PATH = str(importlib.resources.files(comfyui_frontend_package) / "static")
137146
CUSTOM_FRONTENDS_ROOT = str(Path(__file__).parents[1] / "web_custom_versions")
138147

148+
@classmethod
149+
def default_frontend_path(cls) -> str:
150+
try:
151+
import comfyui_frontend_package
152+
return str(importlib.resources.files(comfyui_frontend_package) / "static")
153+
except ImportError:
154+
logging.error(f"\n\n********** ERROR ***********\n\ncomfyui-frontend-package is not installed. {frontend_install_warning_message()}\n********** ERROR **********\n")
155+
sys.exit(-1)
156+
139157
@classmethod
140158
def parse_version_string(cls, value: str) -> tuple[str, str, str]:
141159
"""
@@ -172,7 +190,8 @@ def init_frontend_unsafe(cls, version_string: str, provider: Optional[FrontEndPr
172190
main error source might be request timeout or invalid URL.
173191
"""
174192
if version_string == DEFAULT_VERSION_STRING:
175-
return cls.DEFAULT_FRONTEND_PATH
193+
check_frontend_version()
194+
return cls.default_frontend_path()
176195

177196
repo_owner, repo_name, version = cls.parse_version_string(version_string)
178197

@@ -225,4 +244,5 @@ def init_frontend(cls, version_string: str) -> str:
225244
except Exception as e:
226245
logging.error("Failed to initialize frontend: %s", e)
227246
logging.info("Falling back to the default frontend.")
228-
return cls.DEFAULT_FRONTEND_PATH
247+
check_frontend_version()
248+
return cls.default_frontend_path()

main.py

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,6 @@ def execute_script(script_path):
139139
import nodes
140140
import comfy.model_management
141141
import comfyui_version
142-
import app.frontend_management
143142

144143

145144
def cuda_malloc_warning():
@@ -293,28 +292,13 @@ async def start_all():
293292
return asyncio_loop, prompt_server, start_all
294293

295294

296-
def warn_frontend_version(frontend_version):
297-
try:
298-
required_frontend = (0,)
299-
req_path = os.path.join(os.path.dirname(__file__), 'requirements.txt')
300-
with open(req_path, 'r') as f:
301-
required_frontend = tuple(map(int, f.readline().split('=')[-1].split('.')))
302-
if frontend_version < required_frontend:
303-
logging.warning("________________________________________________________________________\nWARNING WARNING WARNING WARNING WARNING\n\nInstalled frontend version {} is lower than the recommended version {}.\n\n{}\n________________________________________________________________________".format('.'.join(map(str, frontend_version)), '.'.join(map(str, required_frontend)), app.frontend_management.frontend_install_warning_message()))
304-
except:
305-
pass
306-
307-
308295
if __name__ == "__main__":
309296
# Running directly, just start ComfyUI.
310297
logging.info("ComfyUI version: {}".format(comfyui_version.__version__))
311-
frontend_version = app.frontend_management.frontend_version
312-
logging.info("ComfyUI frontend version: {}".format('.'.join(map(str, frontend_version))))
313298

314299
event_loop, _, start_all_func = start_comfyui()
315300
try:
316301
x = start_all_func()
317-
warn_frontend_version(frontend_version)
318302
event_loop.run_until_complete(x)
319303
except KeyboardInterrupt:
320304
logging.info("\nStopped server")

tests-unit/app_test/frontend_manager_test.py

Lines changed: 50 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ def test_get_release_invalid_version(mock_provider):
7070
def test_init_frontend_default():
7171
version_string = DEFAULT_VERSION_STRING
7272
frontend_path = FrontendManager.init_frontend(version_string)
73-
assert frontend_path == FrontendManager.DEFAULT_FRONTEND_PATH
73+
assert frontend_path == FrontendManager.default_frontend_path()
7474

7575

7676
def test_init_frontend_invalid_version():
@@ -84,24 +84,29 @@ def test_init_frontend_invalid_provider():
8484
with pytest.raises(HTTPError):
8585
FrontendManager.init_frontend_unsafe(version_string)
8686

87+
8788
@pytest.fixture
8889
def mock_os_functions():
89-
with patch('app.frontend_management.os.makedirs') as mock_makedirs, \
90-
patch('app.frontend_management.os.listdir') as mock_listdir, \
91-
patch('app.frontend_management.os.rmdir') as mock_rmdir:
90+
with (
91+
patch("app.frontend_management.os.makedirs") as mock_makedirs,
92+
patch("app.frontend_management.os.listdir") as mock_listdir,
93+
patch("app.frontend_management.os.rmdir") as mock_rmdir,
94+
):
9295
mock_listdir.return_value = [] # Simulate empty directory
9396
yield mock_makedirs, mock_listdir, mock_rmdir
9497

98+
9599
@pytest.fixture
96100
def mock_download():
97-
with patch('app.frontend_management.download_release_asset_zip') as mock:
101+
with patch("app.frontend_management.download_release_asset_zip") as mock:
98102
mock.side_effect = Exception("Download failed") # Simulate download failure
99103
yield mock
100104

105+
101106
def test_finally_block(mock_os_functions, mock_download, mock_provider):
102107
# Arrange
103108
mock_makedirs, mock_listdir, mock_rmdir = mock_os_functions
104-
version_string = 'test-owner/[email protected]'
109+
version_string = "test-owner/[email protected]"
105110

106111
# Act & Assert
107112
with pytest.raises(Exception):
@@ -128,3 +133,42 @@ def test_parse_version_string_invalid():
128133
version_string = "invalid"
129134
with pytest.raises(argparse.ArgumentTypeError):
130135
FrontendManager.parse_version_string(version_string)
136+
137+
138+
def test_init_frontend_default_with_mocks():
139+
# Arrange
140+
version_string = DEFAULT_VERSION_STRING
141+
142+
# Act
143+
with (
144+
patch("app.frontend_management.check_frontend_version") as mock_check,
145+
patch.object(
146+
FrontendManager, "default_frontend_path", return_value="/mocked/path"
147+
),
148+
):
149+
frontend_path = FrontendManager.init_frontend(version_string)
150+
151+
# Assert
152+
assert frontend_path == "/mocked/path"
153+
mock_check.assert_called_once()
154+
155+
156+
def test_init_frontend_fallback_on_error():
157+
# Arrange
158+
version_string = "test-owner/[email protected]"
159+
160+
# Act
161+
with (
162+
patch.object(
163+
FrontendManager, "init_frontend_unsafe", side_effect=Exception("Test error")
164+
),
165+
patch("app.frontend_management.check_frontend_version") as mock_check,
166+
patch.object(
167+
FrontendManager, "default_frontend_path", return_value="/default/path"
168+
),
169+
):
170+
frontend_path = FrontendManager.init_frontend(version_string)
171+
172+
# Assert
173+
assert frontend_path == "/default/path"
174+
mock_check.assert_called_once()

0 commit comments

Comments
 (0)