From 01ca6cc7caaabffaa4bd8a5f4edf6753c7f69e0d Mon Sep 17 00:00:00 2001 From: Lenin Mookiah <2639584+leninworld@users.noreply.github.com> Date: Tue, 24 Nov 2026 19:20:07 -0800 Subject: [PATCH] fix(extensions): parse command frontmatter after a UTF-8 BOM parse_frontmatter only looked for "---" at offset 0, so a command file saved as UTF-8 with BOM lost its frontmatter (description, scripts) and the BOM plus the raw frontmatter block ended up in the generated agent command. Ignore one leading BOM when looking for the opening delimiter; content without complete frontmatter is still returned 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/agents.py | 7 ++- tests/test_extensions.py | 92 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 97 insertions(+), 2 deletions(-) diff --git a/src/specify_cli/agents.py b/src/specify_cli/agents.py index 4376c192af..7538e04579 100644 --- a/src/specify_cli/agents.py +++ b/src/specify_cli/agents.py @@ -102,7 +102,10 @@ def parse_frontmatter(content: str) -> tuple[dict, str]: Returns: Tuple of (frontmatter_dict, body_content) """ - if not content.startswith("---"): + # A UTF-8 BOM before the opening delimiter is an encoding marker, not + # content. Inputs without frontmatter are still returned unchanged. + text = content.removeprefix("\ufeff") + if not text.startswith("---"): return {}, content # The closing delimiter is a line that is exactly ``---`` (a YAML @@ -113,7 +116,7 @@ def parse_frontmatter(content: str) -> tuple[dict, str]: # which truncates the frontmatter and spills the remainder into the # body. Match on line boundaries instead, mirroring the line-anchored # scan in ``VibeIntegration._inject_frontmatter_flag``. - lines = content.splitlines(keepends=True) + lines = text.splitlines(keepends=True) end_line = next( (i for i in range(1, len(lines)) if lines[i].rstrip() == "---"), None, diff --git a/tests/test_extensions.py b/tests/test_extensions.py index 9c6f3ba649..530ad0bf7e 100644 --- a/tests/test_extensions.py +++ b/tests/test_extensions.py @@ -3467,6 +3467,60 @@ def test_parse_frontmatter_no_frontmatter(self): assert frontmatter == {} assert body == content + @pytest.mark.parametrize( + ("content", "expected_frontmatter", "expected_body"), + [ + ( + "\ufeff---\ndescription: Test command\n---\n\n# Command body\n", + {"description": "Test command"}, + "# Command body", + ), + ( + "\ufeff---\r\ndescription: Test command\r\n---\r\n\r\n# Command body\r\n", + {"description": "Test command"}, + "# Command body", + ), + ( + "\ufeff# Just a command\n$ARGUMENTS", + {}, + "\ufeff# Just a command\n$ARGUMENTS", + ), + ( + "\ufeff\ufeff---\ndescription: Test command\n---\n\n# Command body\n", + {}, + "\ufeff\ufeff---\ndescription: Test command\n---\n\n# Command body\n", + ), + ( + "\ufeff---\ndescription: Test command\n", + {}, + "\ufeff---\ndescription: Test command\n", + ), + ( + "# Command body\n\ufeffKeep this marker", + {}, + "# Command body\n\ufeffKeep this marker", + ), + ], + ids=( + "lf", + "crlf", + "bom-without-frontmatter", + "double-bom", + "bom-unterminated-frontmatter", + "non-leading-bom", + ), + ) + def test_parse_frontmatter_handles_leading_bom( + self, content, expected_frontmatter, expected_body + ): + """Strip one leading BOM only when YAML frontmatter follows it.""" + registrar = CommandRegistrar() + + frontmatter, body = registrar.parse_frontmatter(content) + + assert frontmatter == expected_frontmatter + assert body == expected_body + def test_parse_frontmatter_non_mapping_returns_empty_dict(self): """Non-mapping YAML frontmatter should not crash downstream renderers.""" content = """--- @@ -3948,6 +4002,44 @@ def test_register_commands_for_claude(self, extension_dir, project_dir): assert "description: Test hello command" in content assert "test-ext" in content + def test_register_commands_handles_bom_prefixed_frontmatter( + self, extension_dir, project_dir + ): + """A BOM-prefixed command keeps its metadata when registered.""" + from specify_cli.agents import CommandRegistrar as AgentCommandRegistrar + + source_file = extension_dir / "commands" / "hello.md" + source_file.write_text( + "\ufeff---\ndescription: BOM command\n---\n\n# Command body\n", + encoding="utf-8", + ) + manifest = ExtensionManifest(extension_dir / "extension.yml") + registrar = AgentCommandRegistrar() + + registered = registrar.register_commands( + "claude", + manifest.commands, + manifest.id, + extension_dir, + project_dir, + extension_id=manifest.id, + author=manifest.data["extension"].get("author"), + version=manifest.version, + ) + + assert registered == ["speckit.test-ext.hello"] + output_file = ( + project_dir + / ".claude" + / "skills" + / "speckit-test-ext-hello" + / "SKILL.md" + ) + output = output_file.read_text(encoding="utf-8") + rendered_frontmatter, _ = registrar.parse_frontmatter(output) + assert rendered_frontmatter["description"] == "BOM command" + assert "\ufeff" not in output + def test_command_with_aliases(self, project_dir, temp_dir): """Test registering a command with aliases.""" import yaml