Compare commits

...
Author SHA1 Message Date
Dr.Lt.Data 15b6684939 v4: Install the dependencies of a nodepack from a Git URL when the locale cannot read its requirements.txt (#3520)
The module-level execute_install_script() of both cores opened
requirements.txt in the locale's encoding. On a Windows code page, a
UTF-8 file with non-ASCII comments raised UnicodeDecodeError, and the
nodepack stayed without its dependencies.

It now reads with read_requirements_lines(). That reads in the locale's
encoding first. When that fails, it takes the encoding chardet names
only if that codec reads ASCII as ASCII, or is UTF-16 or UTF-32, and
every line is printable ASCII outside its comment. Otherwise it raises
the locale's UnicodeDecodeError, as before, and nothing is installed.

robust_readlines() and its other callers are unchanged.
2026-12-02 01:29:04 +09:00
5 changed files with 241 additions and 10 deletions
+13
View File
@@ -36,6 +36,19 @@ and this project adheres to [Semantic Versioning 2.0.0](https://semver.org/spec/
### Fixed
- An install from a Git URL installs the dependencies when Windows cannot
read `requirements.txt`. An install of a nodepack from a Git URL
(**Install via Git URL** of the legacy Custom Nodes Manager,
`cm-cli install <url>`, `cm-cli install-deps`) read its `requirements.txt`
in the encoding of the locale, which on Windows is the ANSI code page. A
UTF-8 file with a Chinese or Korean comment failed with
`UnicodeDecodeError`, and the nodepack was cloned without its dependencies
(#3520). When the locale cannot read the file, the install now reads it in
the encoding that `chardet` names, if every line is printable ASCII outside
its comment. Otherwise, as for a file that is not text or a requirement
with a non-ASCII path, it fails with the same error as before. Such a
failure no longer installs the lines in front of the byte it fails on.
- A nodepack that only the registry lists shows Update in the legacy Custom
Nodes Manager when the registry has a newer version. A nodepack that is in
the registry and not in the node list never showed **Update** there, and
+47
View File
@@ -18,6 +18,7 @@ import subprocess
import sys
import tempfile
import re
import codecs
import logging
import platform
import shlex
@@ -1536,6 +1537,52 @@ def robust_readlines(fullpath):
return []
_PRINTABLE_ASCII = re.compile(r'[\t-\r -~]*')
_PRINTABLE_ASCII_BYTES = bytes(range(0x09, 0x0e)) + bytes(range(0x20, 0x7f))
_WIDE_UNICODE_CODECS = ('utf-16', 'utf-16-le', 'utf-16-be', 'utf-32', 'utf-32-le', 'utf-32-be')
def _is_ascii_compatible_or_wide_unicode(encoding):
try:
codec = codecs.lookup(encoding)
if codec.name in _WIDE_UNICODE_CODECS:
return True
return codec.decode(_PRINTABLE_ASCII_BYTES)[0] == _PRINTABLE_ASCII_BYTES.decode('ascii')
except (LookupError, UnicodeDecodeError):
return False
def _hands_pip_only_ascii(line):
before_comment = _PIP_COMMENT_PATTERN.sub('', line)
return before_comment.isspace() or _PRINTABLE_ASCII.fullmatch(before_comment) is not None
def read_requirements_lines(fullpath):
try:
with open(fullpath, "r") as f:
return f.readlines()
except UnicodeDecodeError:
import chardet
with open(fullpath, "rb") as f:
encoding = chardet.detect(f.read())['encoding']
lines = None
if encoding is not None and _is_ascii_compatible_or_wide_unicode(encoding):
try:
with open(fullpath, "r", encoding=encoding) as f:
lines = f.readlines()
except UnicodeDecodeError:
pass
if lines:
# chardet 7.1.0 names UTF-16-LE for a UTF-16 file with a byte order mark, which then stays in the text
lines[0] = lines[0].lstrip('\ufeff')
if lines is None or not all(_hands_pip_only_ascii(line) for line in lines):
raise
return lines
def build_restart_command(argv, drop_args=()):
"""
Rebuild the command line used to re-exec the current ComfyUI process.
+4 -5
View File
@@ -2196,11 +2196,10 @@ def execute_install_script(url, repo_path, lazy_mode=False, instant_execution=Fa
elif os.path.exists(requirements_path) and not no_deps:
print("Install: pip packages")
pip_fixer = manager_util.PIPFixer(manager_util.get_installed_packages(), context.comfy_path, context.manager_files_path)
with open(requirements_path, "r") as requirements_file:
for line in requirements_file:
install_args = manager_util.pip_install_args(line, cm_global.pip_overrides)
if install_args:
try_install_script(url, repo_path, manager_util.make_pip_cmd(["install", *install_args]), instant_execution=instant_execution)
for line in manager_util.read_requirements_lines(requirements_path):
install_args = manager_util.pip_install_args(line, cm_global.pip_overrides)
if install_args:
try_install_script(url, repo_path, manager_util.make_pip_cmd(["install", *install_args]), instant_execution=instant_execution)
pip_fixer.fix_broken()
if os.path.exists(install_script_path):
+4 -5
View File
@@ -2121,11 +2121,10 @@ def execute_install_script(url, repo_path, lazy_mode=False, instant_execution=Fa
if os.path.exists(requirements_path) and not no_deps:
print("Install: pip packages")
pip_fixer = manager_util.PIPFixer(manager_util.get_installed_packages(), context.comfy_path, context.manager_files_path)
with open(requirements_path, "r") as requirements_file:
for line in requirements_file:
install_args = manager_util.pip_install_args(line, cm_global.pip_overrides)
if install_args:
try_install_script(url, repo_path, manager_util.make_pip_cmd(["install", *install_args]), instant_execution=instant_execution)
for line in manager_util.read_requirements_lines(requirements_path):
install_args = manager_util.pip_install_args(line, cm_global.pip_overrides)
if install_args:
try_install_script(url, repo_path, manager_util.make_pip_cmd(["install", *install_args]), instant_execution=instant_execution)
pip_fixer.fix_broken()
if os.path.exists(install_script_path):
+173
View File
@@ -387,6 +387,179 @@ def test_requirements_file_lines_are_checked_before_install(set_lists, monkeypat
assert requirements == ['scipy --index-url https://example.invalid/simple', 'pyyaml>=6']
def ansi_open(file, mode='r', *args, encoding=None, **kwargs):
if 'b' not in mode and encoding is None:
encoding = 'cp1252'
return open(file, mode, *args, encoding=encoding, **kwargs)
def ansi_core(monkeypatch, tree, executed):
core = import_reader(tree)
monkeypatch.setattr(core.manager_funcs, 'run_script', lambda cmd, cwd='.': executed.append(cmd) or 0)
monkeypatch.setattr(core.manager_util, 'PIPFixer', lambda *args: SimpleNamespace(fix_broken=lambda: None))
monkeypatch.setattr(core.manager_util, 'use_unified_resolver', False)
for module in (core, core.manager_util):
monkeypatch.setattr(module, 'open', ansi_open, raising=False)
return core
def chardet_names(monkeypatch, encoding):
import chardet
monkeypatch.setattr(chardet, 'detect', lambda data: {'encoding': encoding, 'confidence': 0.06, 'language': None})
NOT_TEXT = bytes(range(128, 256)) * 4 + b'\nnumpy\n'
MAC_ROMAN = '# Dependências do nó – instalação\nrich\n'.encode('mac_roman')
SHIFT_JIS = 'numpy\nrich # 画像処理用\n'.encode('shift_jis')
EBCDIC = 'pandas\r'.encode('cp500')
@pytest.mark.parametrize('tree', ['glob', 'legacy'])
def test_utf8_requirements_file_with_non_ascii_comments_is_installed_in_a_windows_code_page(
set_lists, monkeypatch, tmp_path, tree):
(tmp_path / 'requirements.txt').write_text('# Łukasz ✨ 中文\nnumpy\nrich # Łukasz\n', encoding='utf-8')
executed = []
core = ansi_core(monkeypatch, tree, executed)
assert core.execute_install_script(None, str(tmp_path), instant_execution=True) is True
assert [cmd[cmd.index('install') + 1:] for cmd in executed] == [['numpy'], ['rich']]
@pytest.mark.parametrize('content, named, installed', [
(MAC_ROMAN, 'ISO-8859-5', ['rich']),
(SHIFT_JIS, 'cp1006', ['numpy', 'rich']),
], ids=['mac-roman-chardet-7.1.0', 'shift-jis-chardet-7.1.0'])
@pytest.mark.parametrize('tree', ['glob', 'legacy'])
def test_requirements_file_whose_comment_chardet_misreads_is_installed(
set_lists, monkeypatch, tmp_path, tree, content, named, installed):
(tmp_path / 'requirements.txt').write_bytes(content)
executed = []
core = ansi_core(monkeypatch, tree, executed)
chardet_names(monkeypatch, named)
assert core.execute_install_script(None, str(tmp_path), instant_execution=True) is True
assert [cmd[cmd.index('install') + 1:] for cmd in executed] == [[name] for name in installed]
@pytest.mark.parametrize('text, installed', [
('\u3000# 依存関係\nnumpy\n', ['numpy']),
('# 依存関係\nnumpy\n\u00a0\u00a0\nrich\n', ['numpy', 'rich']),
], ids=['comment-indented-with-an-ideographic-space', 'line-of-no-break-spaces'])
@pytest.mark.parametrize('tree', ['glob', 'legacy'])
def test_utf8_requirements_file_with_non_ascii_blanks_is_installed(
set_lists, monkeypatch, tmp_path, tree, text, installed):
(tmp_path / 'requirements.txt').write_text(text, encoding='utf-8')
executed = []
core = ansi_core(monkeypatch, tree, executed)
chardet_names(monkeypatch, 'utf-8')
assert core.execute_install_script(None, str(tmp_path), instant_execution=True) is True
assert [cmd[cmd.index('install') + 1:] for cmd in executed] == [[name] for name in installed]
@pytest.mark.parametrize('tree', ['glob', 'legacy'])
def test_requirement_line_with_a_no_break_space_fails_the_install(set_lists, monkeypatch, tmp_path, tree):
(tmp_path / 'requirements.txt').write_text('numpy\u00a0 # 依存関係\nrich\n', encoding='utf-8')
executed = []
core = ansi_core(monkeypatch, tree, executed)
chardet_names(monkeypatch, 'utf-8')
with pytest.raises(UnicodeDecodeError) as error:
core.execute_install_script(None, str(tmp_path), instant_execution=True)
assert str(error.value) == "'charmap' codec can't decode byte 0x9d in position 12: character maps to <undefined>"
assert executed == []
@pytest.mark.parametrize('tree', ['glob', 'legacy'])
def test_requirement_with_a_non_ascii_url_fragment_fails_the_install(set_lists, monkeypatch, tmp_path, tree):
(tmp_path / 'requirements.txt').write_text('git+https://host/repo#egg=依存\nrich\n', encoding='utf-8')
executed = []
core = ansi_core(monkeypatch, tree, executed)
chardet_names(monkeypatch, 'utf-8')
with pytest.raises(UnicodeDecodeError):
core.execute_install_script(None, str(tmp_path), instant_execution=True)
assert executed == []
@pytest.mark.parametrize('tree', ['glob', 'legacy'])
def test_utf16_requirements_file_with_byte_order_mark_installs_only_its_requirements(
set_lists, monkeypatch, tmp_path, tree):
(tmp_path / 'requirements.txt').write_bytes('# 依赖列表\nrich\n'.encode('utf-16'))
executed = []
core = ansi_core(monkeypatch, tree, executed)
chardet_names(monkeypatch, 'utf-16-le')
assert core.execute_install_script(None, str(tmp_path), instant_execution=True) is True
assert [cmd[cmd.index('install') + 1:] for cmd in executed] == [['rich']]
@pytest.mark.parametrize('named', ['ISO-8859-5', 'MacCyrillic', None],
ids=['chardet-7.7.0', 'chardet-7.1.0', 'no-encoding'])
@pytest.mark.parametrize('tree', ['glob', 'legacy'])
def test_requirements_file_that_is_not_text_fails_the_install(set_lists, monkeypatch, tmp_path, tree, named):
(tmp_path / 'requirements.txt').write_bytes(NOT_TEXT)
executed = []
core = ansi_core(monkeypatch, tree, executed)
chardet_names(monkeypatch, named)
with pytest.raises(UnicodeDecodeError) as error:
core.execute_install_script(None, str(tmp_path), instant_execution=True)
assert str(error.value) == "'charmap' codec can't decode byte 0x81 in position 1: character maps to <undefined>"
assert executed == []
@pytest.mark.parametrize('named', ['cp500', 'cp1140'])
@pytest.mark.parametrize('tree', ['glob', 'legacy'])
def test_requirements_file_that_an_ebcdic_guess_reads_as_ascii_fails_the_install(
set_lists, monkeypatch, tmp_path, tree, named):
(tmp_path / 'requirements.txt').write_bytes(EBCDIC)
executed = []
core = ansi_core(monkeypatch, tree, executed)
chardet_names(monkeypatch, named)
with pytest.raises(UnicodeDecodeError) as error:
core.execute_install_script(None, str(tmp_path), instant_execution=True)
assert str(error.value) == "'charmap' codec can't decode byte 0x81 in position 1: character maps to <undefined>"
assert executed == []
@pytest.mark.parametrize('tree', ['glob', 'legacy'])
def test_requirements_file_with_carriage_return_line_ends_is_installed(set_lists, monkeypatch, tmp_path, tree):
(tmp_path / 'requirements.txt').write_bytes('pandas\r# 依存関係\r'.encode('utf-8'))
executed = []
core = ansi_core(monkeypatch, tree, executed)
chardet_names(monkeypatch, 'utf-8')
assert core.execute_install_script(None, str(tmp_path), instant_execution=True) is True
assert [cmd[cmd.index('install') + 1:] for cmd in executed] == [['pandas']]
@pytest.mark.parametrize('entry_point', ['glob manager', 'legacy manager', 'startup', 'resolver'])
def test_the_other_installers_take_the_requirements_chardet_reads(
set_lists, monkeypatch, tmp_path, entry_point):
(tmp_path / 'requirements.txt').write_bytes(MAC_ROMAN)
executed = []
core = ansi_core(monkeypatch, 'legacy' if entry_point == 'legacy manager' else 'glob', executed)
chardet_names(monkeypatch, 'ISO-8859-5')
if entry_point == 'startup':
startup = load_prestartup(
process_wrap=lambda cmd, cwd=None, **kwargs: executed.append(cmd) or 0,
processed_install=set(), _unified_resolver_succeeded=False)
startup['execute_lazy_install_script'](str(tmp_path), 'python')
elif entry_point == 'resolver':
from comfyui_manager.common import unified_dep_resolver
monkeypatch.setattr(unified_dep_resolver, 'manager_util', core.manager_util)
collected = unified_dep_resolver.UnifiedDepResolver(node_pack_paths=[str(tmp_path)]).collect_requirements()
executed.extend(['install', r.spec] for r in collected.requirements)
else:
monkeypatch.setattr(core.unified_manager, 'processed_install', set())
core.unified_manager.execute_install_script(None, str(tmp_path), instant_execution=True)
assert [cmd[cmd.index('install') + 1:] for cmd in executed] == [['rich']]
# Every base command make_pip_cmd() can produce.
PIP_COMMANDS = [
['python', '-m', 'pip'], ['python', '-s', '-m', 'pip'],