Skip to content

Commit b61050e

Browse files
committed
fix(presets): report corrupt entries, survive an unreadable registry, correct the footer
Addresses review feedback on #4250, plus two issues found while self-reviewing the same code. ExtensionRegistry construction can raise OSError -- _load() recovers from malformed content but deliberately lets OSError through, and is_corrupt() re-reads the file. This check runs after the install has already completed and preset_add handles only preset-domain errors, so an unreadable registry turned a finished install into a traceback over what is only a warning. An unreadable registry now yields no results instead. A corrupted entry was conflated with an absent one. get() returns None for both, but is_installed() still counts the key, so the suggested `extension add <id>` is refused as already installed. It is now a distinct "corrupt" reason remediated with a forced reinstall. The closing note asserted that dependent features "will do nothing" and that the preset is "safe to use". Neither holds for a version mismatch: the extension is installed and enabled, so the preset does invoke it, and the combination is untested against the declared constraint rather than safe. The note is now split by consequence. Found while self-reviewing, in the same two areas this review keeps surfacing: the id pattern used re.match with an anchored `^...$`, but `$` also matches before a trailing newline, so "demo-ext\n" validated here while PresetResolver._is_safe_registry_id (fullmatch) rejects it, and the newline would have reached a printed command. And declaring one dependency twice warned twice; exact repeats now collapse, while two entries for one id with different constraints are still both checked, since both have to hold. Assisted-by: Claude Code (model: Claude Opus 5, autonomous)
1 parent f69a16d commit b61050e

3 files changed

Lines changed: 206 additions & 24 deletions

File tree

‎src/specify_cli/presets/__init__.py‎

Lines changed: 51 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -591,10 +591,15 @@ def _validate_requires_extensions(declared: Any) -> None:
591591
f"got {type(extension_id).__name__}"
592592
)
593593
# Same id shape the extension loader enforces, so a dependency can
594-
# never name something that could not be installed in the first place.
595-
if not re.match(r'^[a-z0-9-]+$', extension_id):
594+
# never name something that could not be installed in the first
595+
# place. fullmatch rather than match with an anchored pattern: `$`
596+
# also matches before a trailing newline, so "demo-ext\n" would
597+
# otherwise validate here while PresetResolver._is_safe_registry_id
598+
# (which uses fullmatch) rejects it, and the newline would land in
599+
# a suggested command.
600+
if not re.fullmatch(r'[a-z0-9-]+', extension_id):
596601
raise PresetValidationError(
597-
f"Invalid {label}.id '{extension_id}': "
602+
f"Invalid {label}.id {extension_id!r}: "
598603
"must be lowercase alphanumeric with hyphens only"
599604
)
600605

@@ -986,9 +991,12 @@ def find_unmet_extension_dependencies(
986991
One entry per unsatisfied dependency, each with ``id``, the
987992
requested ``version`` specifier (``None`` when unconstrained), the
988993
``installed`` version (``None`` when absent or unusable), and a
989-
``reason`` of ``"missing"``, ``"stale"``, ``"disabled"``, or
990-
``"version"``. Optional dependencies (``required: false``) are
991-
never reported.
994+
``reason`` of ``"missing"``, ``"corrupt"``, ``"stale"``,
995+
``"disabled"``, or ``"version"``. Optional dependencies
996+
(``required: false``) are never reported.
997+
998+
An unreadable registry yields no results rather than raising, since
999+
this runs after the install has already succeeded.
9921000
9931001
A registry version that cannot be parsed is treated as
9941002
uncomparable, not as a mismatch: the extension is installed and
@@ -1003,15 +1011,36 @@ def find_unmet_extension_dependencies(
10031011
if not isinstance(candidates, list):
10041012
return []
10051013

1006-
declared = [
1007-
dep for dep in candidates
1008-
if isinstance(dep, dict) and dep.get("required", True)
1009-
]
1014+
# Collapse exact repeats so a manifest naming the same dependency twice
1015+
# warns once. Two entries for one id with *different* constraints are
1016+
# kept, since both genuinely have to hold.
1017+
declared: List[Dict[str, Any]] = []
1018+
seen: Set[tuple] = set()
1019+
for dep in candidates:
1020+
if not isinstance(dep, dict) or not dep.get("required", True):
1021+
continue
1022+
key = (dep.get("id"), dep.get("version"))
1023+
if key in seen:
1024+
continue
1025+
seen.add(key)
1026+
declared.append(dep)
10101027
if not declared:
10111028
return []
10121029

10131030
extensions_dir = self.project_root / ".specify" / "extensions"
1014-
registry = ExtensionRegistry(extensions_dir)
1031+
try:
1032+
registry = ExtensionRegistry(extensions_dir)
1033+
registered_ids = registry.keys()
1034+
registry_corrupt = registry.is_corrupt()
1035+
except OSError:
1036+
# Both reads can raise: _load() recovers from malformed content but
1037+
# deliberately lets OSError through, and is_corrupt() re-reads the
1038+
# file. This check runs *after* the install has completed, and
1039+
# preset_add only handles preset-domain errors, so letting that
1040+
# escape would turn a finished install into a traceback over a
1041+
# warning. An unreadable registry simply cannot be inspected.
1042+
return []
1043+
10151044
unmet: List[Dict[str, Any]] = []
10161045

10171046
for dep in declared:
@@ -1025,15 +1054,19 @@ def find_unmet_extension_dependencies(
10251054
# path fail closed and contribute nothing.
10261055
#
10271056
# get() returns None for a corrupted (non-dict) entry as well as
1028-
# an absent one, and keys() deliberately retains corrupted ids so
1029-
# resolution does *not* re-admit their directories as
1030-
# unregistered. Requiring absence from keys() keeps this fallback
1031-
# from reviving exactly what resolution excludes.
1057+
# an absent one, but keys() retains corrupted ids -- both so
1058+
# resolution does not re-admit their directories as
1059+
# unregistered, and because is_installed() still counts them, so
1060+
# a plain `extension add` would be refused as already installed.
1061+
# That is a different state from absent, and needs a different
1062+
# remedy.
1063+
if dep["id"] in registered_ids:
1064+
unmet.append({**dep, "installed": None, "reason": "corrupt"})
1065+
continue
10321066
if (
1033-
dep["id"] not in registry.keys()
1034-
and (extensions_dir / dep["id"]).is_dir()
1067+
(extensions_dir / dep["id"]).is_dir()
10351068
and PresetResolver._is_safe_registry_id(dep["id"])
1036-
and not registry.is_corrupt()
1069+
and not registry_corrupt
10371070
):
10381071
# Unregistered means no recorded version, so a constraint
10391072
# cannot be evaluated -- uncomparable, not unsatisfied.

‎src/specify_cli/presets/_commands.py‎

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,13 @@ def _warn_unmet_extension_dependencies(manager, manifest) -> None:
7474
if reason == "missing":
7575
console.print(f" [yellow]{extension_id}[/yellow] is not installed")
7676
remedy = f"specify extension add {command_id}"
77+
elif reason == "corrupt":
78+
console.print(
79+
f" [yellow]{extension_id}[/yellow] has an unreadable "
80+
"registry entry"
81+
)
82+
# is_installed() still counts the key, so a plain add is refused.
83+
remedy = f"specify extension add {command_id} --force"
7784
elif reason == "stale":
7885
console.print(
7986
f" [yellow]{extension_id}[/yellow] is registered but its "
@@ -95,11 +102,26 @@ def _warn_unmet_extension_dependencies(manager, manifest) -> None:
95102
)
96103
console.print(f" Fix with: {remedy}")
97104
console.print()
98-
console.print(
99-
"[dim]The preset is installed and safe to use; the parts that rely on "
100-
"these extensions will do nothing until this is resolved.[/dim]"
101-
)
102-
if any(dep["reason"] in ("missing", "stale") for dep in unmet):
105+
# The consequence differs by reason and must not be overstated. An
106+
# unavailable extension contributes nothing, so those features are simply
107+
# inert. A version mismatch is the opposite: the extension is installed and
108+
# enabled, so the preset does invoke it -- the combination is just untested
109+
# against the declared constraint, which is not the same as "safe".
110+
console.print("[dim]The preset is installed.[/dim]")
111+
if any(
112+
dep["reason"] in ("missing", "corrupt", "stale", "disabled")
113+
for dep in unmet
114+
):
115+
console.print(
116+
"[dim]Anything relying on an unavailable extension does nothing "
117+
"until that is resolved.[/dim]"
118+
)
119+
if any(dep["reason"] == "version" for dep in unmet):
120+
console.print(
121+
"[dim]Where only a version constraint is unmet the extension is "
122+
"still used, so it may not behave as the preset expects.[/dim]"
123+
)
124+
if any(dep["reason"] in ("missing", "corrupt", "stale") for dep in unmet):
103125
# `extension add <id>` resolves through the catalogs, and the default
104126
# community catalog is discovery-only, so installing by id alone is
105127
# rejected for anything listed only there. Say so once rather than

‎tests/test_presets.py‎

Lines changed: 128 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -577,6 +577,12 @@ def test_requires_extensions_accepts_both_forms(self, temp_dir, valid_pack_data)
577577
([{"id": "x", "version": " "}], r"Invalid requires\.extensions\[0\]\.version"),
578578
([{"id": "x", "version": "nonsense"}], r"Invalid requires\.extensions\[0\]\.version"),
579579
([{"id": "x", "required": "yes"}], r"Invalid requires\.extensions\[0\]\.required"),
580+
# `$` also matches before a trailing newline, so an anchored
581+
# re.match would admit these while the resolver's fullmatch-based
582+
# safe-id check rejects them.
583+
(["demo-ext\n"], r"Invalid requires\.extensions\[0\]\.id"),
584+
([{"id": "demo-ext\n"}], r"Invalid requires\.extensions\[0\]\.id"),
585+
(["demo\next"], r"Invalid requires\.extensions\[0\]\.id"),
580586
],
581587
)
582588
def test_requires_extensions_rejects_malformed(
@@ -1397,7 +1403,9 @@ def test_corrupted_registry_entry_with_directory_is_not_satisfied(
13971403

13981404
unmet = PresetManager(project_dir).find_unmet_extension_dependencies(manifest)
13991405

1400-
assert [dep["reason"] for dep in unmet] == ["missing"]
1406+
# Reported as corrupt rather than missing: the id is still registered,
1407+
# so a plain `extension add` would be refused as already installed.
1408+
assert [dep["reason"] for dep in unmet] == ["corrupt"]
14011409

14021410
def test_missing_and_stale_warnings_mention_discovery_only_catalogs(self):
14031411
"""`extension add <id>` is rejected for discovery-only entries, so say so."""
@@ -1473,6 +1481,125 @@ def test_unregistered_extension_with_corrupt_registry_is_missing(
14731481

14741482
assert [dep["reason"] for dep in unmet] == ["missing"]
14751483

1484+
def test_corrupted_entry_gets_a_forced_reinstall_remedy(
1485+
self, project_dir, temp_dir, valid_pack_data
1486+
):
1487+
"""A corrupted entry is not simply absent: `add <id>` would be refused.
1488+
1489+
``get()`` returns None for it, but ``is_installed()`` still counts the
1490+
key, so a plain add reports "already installed". It needs --force.
1491+
"""
1492+
extensions_dir = project_dir / ".specify" / "extensions"
1493+
extensions_dir.mkdir(parents=True)
1494+
(extensions_dir / ".registry").write_text(
1495+
json.dumps(
1496+
{"schema_version": "1.0", "extensions": {"speckit-inventory": "bad"}}
1497+
),
1498+
encoding="utf-8",
1499+
)
1500+
manifest = self._manifest(temp_dir, valid_pack_data, ["speckit-inventory"])
1501+
1502+
unmet = PresetManager(project_dir).find_unmet_extension_dependencies(manifest)
1503+
1504+
assert [dep["reason"] for dep in unmet] == ["corrupt"]
1505+
1506+
def test_corrupt_warning_suggests_forced_reinstall(self):
1507+
"""The corrupt remedy must use --force, since the id is still registered."""
1508+
manager = MagicMock()
1509+
manager.find_unmet_extension_dependencies.return_value = [
1510+
{"id": "speckit-inventory", "reason": "corrupt",
1511+
"installed": None, "version": None}
1512+
]
1513+
1514+
with console.capture() as capture:
1515+
_warn_unmet_extension_dependencies(manager, MagicMock())
1516+
1517+
output = strip_ansi(capture.get())
1518+
assert "unreadable registry entry" in output
1519+
assert "specify extension add speckit-inventory --force" in output
1520+
1521+
def test_unreadable_registry_does_not_raise(
1522+
self, project_dir, temp_dir, valid_pack_data, monkeypatch
1523+
):
1524+
"""An OSError from the registry must not crash an already-completed install.
1525+
1526+
``_load()`` lets OSError through, and ``preset_add`` only handles
1527+
preset-domain errors, so raising here would turn a finished install
1528+
into a traceback over what is only a warning.
1529+
"""
1530+
manifest = self._manifest(temp_dir, valid_pack_data, ["speckit-inventory"])
1531+
1532+
import specify_cli.presets as presets_mod
1533+
1534+
def _boom(*args, **kwargs):
1535+
raise PermissionError("registry unreadable")
1536+
1537+
monkeypatch.setattr(presets_mod, "ExtensionRegistry", _boom)
1538+
1539+
assert PresetManager(project_dir).find_unmet_extension_dependencies(
1540+
manifest
1541+
) == []
1542+
1543+
def test_version_only_footer_does_not_claim_the_feature_is_inert(self):
1544+
"""A version mismatch still invokes the extension, so wording differs."""
1545+
manager = MagicMock()
1546+
manager.find_unmet_extension_dependencies.return_value = [
1547+
{"id": "speckit-inventory", "reason": "version",
1548+
"installed": "0.1.0", "version": ">=9.0.0"}
1549+
]
1550+
1551+
with console.capture() as capture:
1552+
_warn_unmet_extension_dependencies(manager, MagicMock())
1553+
1554+
output = " ".join(strip_ansi(capture.get()).split())
1555+
assert "may not behave as the preset expects" in output
1556+
assert "does nothing" not in output
1557+
assert "safe to use" not in output
1558+
1559+
def test_unavailable_footer_states_the_feature_is_inert(self):
1560+
"""An unavailable extension genuinely contributes nothing."""
1561+
manager = MagicMock()
1562+
manager.find_unmet_extension_dependencies.return_value = [
1563+
{"id": "speckit-inventory", "reason": "missing",
1564+
"installed": None, "version": None}
1565+
]
1566+
1567+
with console.capture() as capture:
1568+
_warn_unmet_extension_dependencies(manager, MagicMock())
1569+
1570+
output = " ".join(strip_ansi(capture.get()).split())
1571+
assert "does nothing" in output
1572+
assert "may not behave as the preset expects" not in output
1573+
1574+
def test_exact_duplicate_declarations_warn_once(
1575+
self, project_dir, temp_dir, valid_pack_data
1576+
):
1577+
"""Naming the same dependency twice must not print the warning twice."""
1578+
manifest = self._manifest(
1579+
temp_dir, valid_pack_data, ["speckit-inventory", "speckit-inventory"]
1580+
)
1581+
1582+
unmet = PresetManager(project_dir).find_unmet_extension_dependencies(manifest)
1583+
1584+
assert [dep["id"] for dep in unmet] == ["speckit-inventory"]
1585+
1586+
def test_same_id_with_different_constraints_is_checked_twice(
1587+
self, project_dir, temp_dir, valid_pack_data
1588+
):
1589+
"""Distinct constraints on one id both have to hold, so both are checked."""
1590+
self._install_extension(project_dir, "speckit-inventory", "1.0.0")
1591+
manifest = self._manifest(
1592+
temp_dir, valid_pack_data,
1593+
[
1594+
{"id": "speckit-inventory", "version": ">=9.0.0"},
1595+
{"id": "speckit-inventory", "version": "<0.5"},
1596+
],
1597+
)
1598+
1599+
unmet = PresetManager(project_dir).find_unmet_extension_dependencies(manifest)
1600+
1601+
assert [dep["version"] for dep in unmet] == [">=9.0.0", "<0.5"]
1602+
14761603
def test_stale_registry_entry_is_reported(
14771604
self, project_dir, temp_dir, valid_pack_data
14781605
):

0 commit comments

Comments
 (0)