mirror of
https://github.com/github/spec-kit.git
synced 2026-08-15 14:02:04 +02:00
fix(presets): return None for an unreadable layer in resolve_content (#3959)
* fix(presets): return None for an unreadable layer in resolve_content PresetResolver.resolve_content() reads the winning layer (and each composition layer) with a bare read_text(), so a layer file that cannot be read or decoded crashed command registration with a raw OSError/UnicodeDecodeError. The docstring already promises 'Composed content string, or None if not found', and since #3896 collect_all_layers() deliberately tolerates a non-UTF-8 legacy layer — moving the crash here, where both callers (_register_commands and _reconcile_composed_commands) are unguarded. Return None when the winning or base layer cannot be read, treating an unreadable layer like a missing one per the documented contract. Assisted-by: GitHub Copilot (model: claude-fable-5, autonomous) Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * test: cover the base guard and composing-layer read Review follow-up: add an unreadable replace base beneath a valid composing layer, and a mocked-PermissionError composing layer over a valid base, so every new boundary and both exception types are covered. Assisted-by: GitHub Copilot (model: claude-fable-5, autonomous) Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --------- Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
@@ -5576,7 +5576,7 @@ class PresetResolver:
|
||||
if not layers:
|
||||
return None
|
||||
|
||||
def _read_layer_content(layer: Dict[str, Any]) -> str:
|
||||
def _read_layer_content(layer: Dict[str, Any]) -> Optional[str]:
|
||||
"""Read a layer's raw text, rewriting extension-relative subdir
|
||||
references (agents/, knowledge-base/, etc.) to their installed
|
||||
location when the layer is extension-provided (#2101).
|
||||
@@ -5586,8 +5586,18 @@ class PresetResolver:
|
||||
rewrite when it wins outright above or serves as the
|
||||
composition base below — never as a mid-stack composing
|
||||
(append/prepend/wrap) layer.
|
||||
|
||||
Returns None when the layer cannot be read or decoded:
|
||||
collect_all_layers deliberately keeps a non-UTF-8 legacy layer
|
||||
(with its "replace" default) so unrelated commands still
|
||||
resolve, so the same tolerance must apply here — the documented
|
||||
contract is "Composed content string, or None if not found",
|
||||
not a raw UnicodeDecodeError at composition time.
|
||||
"""
|
||||
text = layer["path"].read_text(encoding="utf-8")
|
||||
try:
|
||||
text = layer["path"].read_text(encoding="utf-8")
|
||||
except (OSError, UnicodeDecodeError):
|
||||
return None
|
||||
extension_id = layer.get("extension_id")
|
||||
extension_dir = layer.get("extension_dir")
|
||||
if extension_id and extension_dir:
|
||||
@@ -5625,6 +5635,8 @@ class PresetResolver:
|
||||
# Convert to reversed_layers index
|
||||
base_reversed_idx = len(layers) - 1 - base_layer_idx
|
||||
content = _read_layer_content(layers[base_layer_idx])
|
||||
if content is None:
|
||||
return None
|
||||
# Compose only the layers above the base (higher priority = lower index in layers,
|
||||
# higher index in reversed_layers). Process bottom-up from base+1.
|
||||
start_idx = base_reversed_idx + 1
|
||||
@@ -5668,7 +5680,12 @@ class PresetResolver:
|
||||
|
||||
# Apply composition layers from bottom to top
|
||||
for layer in reversed_layers[start_idx:]:
|
||||
layer_content = layer["path"].read_text(encoding="utf-8")
|
||||
try:
|
||||
layer_content = layer["path"].read_text(encoding="utf-8")
|
||||
except (OSError, UnicodeDecodeError):
|
||||
# Same tolerance as _read_layer_content: an unreadable layer
|
||||
# means the composed result cannot be produced.
|
||||
return None
|
||||
strategy = layer["strategy"]
|
||||
|
||||
if is_command:
|
||||
|
||||
Reference in New Issue
Block a user