From bb1102f8d0aba730b0a15cf4343df84c037d2b62 Mon Sep 17 00:00:00 2001 From: Lenin Mookiah <2639584+leninworld@users.noreply.github.com> Date: Wed, 25 Nov 2026 07:52:53 -0800 Subject: [PATCH 1/2] fix(presets): detect command frontmatter after a UTF-8 BOM PresetResolver only recognised command frontmatter when the first line was exactly "---". A preset command saved as UTF-8 with BOM therefore had its frontmatter `strategy:` ignored (an append/prepend/wrap command replaced the core command), and with a manifest strategy the BOM and the raw frontmatter block were composed into the command body. Ignore one leading BOM when looking for the opening delimiter in the legacy strategy fallback and in resolve_content's frontmatter split, as #5562 did for CommandRegistrar.parse_frontmatter. Content without a complete frontmatter block is still composed unchanged. Assisted-by: OpenAI Codex CLI (model: gpt-5.6-sol, autonomous) Assisted-by: Claude Code (model: Claude Opus 5.5, autonomous) --- src/specify_cli/presets/_resolver.py | 5 +- tests/specify_cli/presets/test_resolver.py | 130 +++++++++++++++++++++ 2 files changed, 133 insertions(+), 2 deletions(-) diff --git a/src/specify_cli/presets/_resolver.py b/src/specify_cli/presets/_resolver.py index 2d07ebd4b9..f6c94700e6 100644 --- a/src/specify_cli/presets/_resolver.py +++ b/src/specify_cli/presets/_resolver.py @@ -604,7 +604,7 @@ def _find_in_subdirs(base_dir: Path) -> Optional[Path]: try: cmd_content = candidate.read_text(encoding="utf-8") lines = cmd_content.splitlines(keepends=True) - if lines and lines[0].rstrip("\r\n") == "---": + if lines and lines[0].removeprefix("\ufeff").rstrip("\r\n") == "---": fence_end = -1 for fi, fline in enumerate(lines[1:], start=1): if fline.rstrip("\r\n") == "---": @@ -883,7 +883,8 @@ def _split_frontmatter(text: str) -> tuple: Uses line-based fence detection (fence must be ``---`` on its own line) to avoid false matches on ``---`` inside YAML values. """ - lines = text.splitlines(keepends=True) + frontmatter_text = text.removeprefix("\ufeff") + lines = frontmatter_text.splitlines(keepends=True) if not lines or lines[0].rstrip("\r\n") != "---": return None, text diff --git a/tests/specify_cli/presets/test_resolver.py b/tests/specify_cli/presets/test_resolver.py index f052361c76..7ebbd530d9 100644 --- a/tests/specify_cli/presets/test_resolver.py +++ b/tests/specify_cli/presets/test_resolver.py @@ -13,6 +13,7 @@ PresetResolver, PresetValidationError, ) +from specify_cli.presets._manifest import PresetSectionValidationError from tests.specify_cli.presets._helpers import ( CORE_TEMPLATE_NAMES, install_self_test_preset, @@ -1670,6 +1671,135 @@ def test_resolve_content_command_frontmatter_stripping(self, project_dir, temp_d # Core frontmatter should NOT appear in the body assert content.count("---") == 2 # only one frontmatter block (opening + closing) + @pytest.mark.parametrize("newline", ["\n", "\r\n"], ids=["lf", "crlf"]) + def test_resolve_content_legacy_bom_frontmatter_strategy( + self, project_dir, temp_dir, valid_pack_data, newline + ): + """A frontmatter strategy after a UTF-8 BOM is still honoured.""" + commands_dir = project_dir / ".specify" / "templates" / "commands" + commands_dir.mkdir(parents=True, exist_ok=True) + (commands_dir / "bom-legacy.md").write_text( + "Core body content\n", encoding="utf-8" + ) + preset_content = ( + "\ufeff---\ndescription: BOM preset\nstrategy: append\n---\n" + "Appended body content\n" + ).replace("\n", newline) + pack_dir = _create_pack( + temp_dir, + valid_pack_data, + "bom-legacy-pack", + preset_content, + template_type="command", + template_name="speckit.bom-legacy", + ) + PresetManager(project_dir).install_from_directory(pack_dir, "0.1.5") + + resolver = PresetResolver(project_dir) + layers = resolver.collect_all_layers("speckit.bom-legacy", "command") + content = resolver.resolve_content("speckit.bom-legacy", "command") + + assert layers[0]["strategy"] == "append" + assert content is not None + assert "Core body content" in content + assert "Appended body content" in content + + def test_resolve_content_strips_bom_frontmatter_from_composed_command( + self, project_dir, temp_dir, valid_pack_data + ): + """A BOM-prefixed layer's frontmatter must not leak into the body.""" + commands_dir = project_dir / ".specify" / "templates" / "commands" + commands_dir.mkdir(parents=True, exist_ok=True) + (commands_dir / "bom-compose.md").write_text( + "---\ndescription: Core command\n---\nCore body content\n", + encoding="utf-8", + ) + pack_dir = _create_pack( + temp_dir, + valid_pack_data, + "bom-compose-pack", + "\ufeff---\ndescription: BOM preset\nstrategy: append\n---\n" + "Appended body content\n", + strategy="append", + template_type="command", + template_name="speckit.bom-compose", + ) + PresetManager(project_dir).install_from_directory(pack_dir, "0.1.5") + + content = PresetResolver(project_dir).resolve_content( + "speckit.bom-compose", "command" + ) + + assert content is not None + assert "\ufeff" not in content + assert "strategy: append" not in content + assert "Appended body content" in content + frontmatter = yaml.safe_load(content.split("---", 2)[1]) + assert frontmatter == {"description": "BOM preset"} + + @pytest.mark.parametrize( + "preset_content", + [ + "\ufeffPlain appended content\n", + "\ufeff---\ndescription: Unterminated frontmatter\nAppended body content\n", + ], + ids=["no-frontmatter", "unterminated-frontmatter"], + ) + def test_resolve_content_keeps_bom_without_complete_frontmatter( + self, project_dir, temp_dir, valid_pack_data, preset_content + ): + """Without complete frontmatter the layer is appended unchanged.""" + commands_dir = project_dir / ".specify" / "templates" / "commands" + commands_dir.mkdir(parents=True, exist_ok=True) + core_content = "Core body content\n" + (commands_dir / "bom-guard.md").write_text( + core_content, encoding="utf-8" + ) + pack_dir = _create_pack( + temp_dir, + valid_pack_data, + "bom-guard-pack", + preset_content, + strategy="append", + template_type="command", + template_name="speckit.bom-guard", + ) + PresetManager(project_dir).install_from_directory(pack_dir, "0.1.5") + + content = PresetResolver(project_dir).resolve_content( + "speckit.bom-guard", "command" + ) + + # No complete frontmatter means content stays unchanged, matching #5562. + assert content == core_content + "\n\n" + preset_content + + @pytest.mark.parametrize("prefix", ["", "\ufeff"], ids=["plain", "bom"]) + def test_legacy_frontmatter_replace_section_requires_manifest_declaration( + self, project_dir, valid_pack_data, prefix + ): + """replace-section in command frontmatter is rejected, BOM or not.""" + presets_dir = project_dir / ".specify" / "presets" + presets_dir.mkdir(parents=True) + _create_pack( + presets_dir, + valid_pack_data, + "replace-section-pack", + prefix + "---\nstrategy: replace-section\n---\nReplacement body\n", + template_type="command", + template_name="speckit.replace-section", + ) + PresetRegistry(presets_dir).add( + "replace-section-pack", {"version": "1.0.0", "priority": 10} + ) + + with pytest.raises( + PresetSectionValidationError, + match="Declare replace-section and selector", + ): + PresetResolver(project_dir).collect_all_layers( + "speckit.replace-section", "command" + ) + def test_resolve_content_blank_line_separator(self, project_dir, temp_dir, valid_pack_data): """Test that prepend/append use blank line separator.""" pack_data = {**valid_pack_data} From 2aebbedb7eb8e1922a9a4bfed6c4f555a74c8a9e Mon Sep 17 00:00:00 2001 From: Lenin Mookiah <2639584+leninworld@users.noreply.github.com> Date: Wed, 25 Nov 2026 08:36:47 -0800 Subject: [PATCH 2/2] test(presets): write BOM command fixtures as UTF-8 _create_pack wrote command files with the platform default encoding, so the new BOM cases raised UnicodeEncodeError before reaching the resolver where the default is cp1252 (e.g. Windows). Assisted-by: Claude Code (model: Claude Opus 5.5, autonomous) Assisted-by: OpenAI Codex CLI (model: gpt-5.6-sol, autonomous) --- tests/specify_cli/presets/test_resolver.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/specify_cli/presets/test_resolver.py b/tests/specify_cli/presets/test_resolver.py index 7ebbd530d9..56463a38b3 100644 --- a/tests/specify_cli/presets/test_resolver.py +++ b/tests/specify_cli/presets/test_resolver.py @@ -2082,7 +2082,7 @@ def _create_pack(temp_dir, valid_pack_data, pack_id, content, elif template_type == "command": subdir = pack_dir / "commands" subdir.mkdir(exist_ok=True) - (subdir / f"{template_name}.md").write_text(content) + (subdir / f"{template_name}.md").write_text(content, encoding="utf-8") else: subdir = pack_dir / "templates" subdir.mkdir(exist_ok=True)