Skip to content

fix(presets): detect command frontmatter after a UTF-8 BOM - #5568

Merged
mnriem merged 2 commits into
github:mainfrom
leninworld:fix/preset-command-frontmatter-bom
Nov 25, 2026
Merged

mnriem merged 2 commits into
github:mainfrom
leninworld:fix/preset-command-frontmatter-bom

Conversation

@leninworld

Copy link
Copy Markdown
Contributor

Description

PresetResolver only recognizes command frontmatter when the first line is exactly ---. Preset command files are read with read_text(encoding="utf-8"), which keeps a UTF-8 BOM, so a command saved as "UTF-8 with BOM" (for example by Windows PowerShell 5.1 Set-Content -Encoding UTF8, or an editor set to that encoding) breaks composition in two places in src/specify_cli/presets/_resolver.py:

  1. Frontmatter strategy ignored (collect_all_layers, legacy fallback when the manifest entry has no strategy): strategy: append (or prepend/wrap) is not seen, so the layer is treated as replace and the core command is dropped. The check that rejects strategy: replace-section in command frontmatter is skipped the same way.
  2. Frontmatter leaks into the body (resolve_content, frontmatter split for command composition): with strategy: append in preset.yml, the BOM and the raw --- / description / strategy / --- block are appended into the composed command.

Reproduction on main: specify init --here --integration claude --ignore-agent-tools, then specify preset add --dev ./preset where commands/speckit.specify.md is ---\ndescription: Appended by preset\nstrategy: append\n---\n\nAPPENDED-BY-PRESET\n and preset.yml lists it as a command template:

.claude/skills/speckit-specify/SKILL.md no BOM BOM, main BOM, this PR
strategy only in frontmatter 354 lines, core body + appended text 16 lines, core command gone 354 lines, same as no BOM
strategy: append in preset.yml 354 lines 359 lines, BOM + raw frontmatter block in the body 354 lines, same as no BOM

The fix ignores one leading U+FEFF when looking for the opening --- at both sites, the same approach as #5562 for CommandRegistrar.parse_frontmatter. When no complete frontmatter follows (BOM without frontmatter, unterminated frontmatter), the layer is composed unchanged, BOM included, as before. A U+FEFF anywhere else is left alone.

Testing

Set up this checkout's .venv and editable install using the local development guide. Run commands from the checkout root.

  • Tested locally with .venv/bin/specify --help (Windows PowerShell: .\.venv\Scripts\specify.exe --help)

  • Ran relevant existing tests with .venv/bin/python -m pytest tests -q (Windows PowerShell: .\.venv\Scripts\python.exe -m pytest tests -q); use affected test paths for focused checks and LC_ALL=en_US.UTF-8 on macOS

  • Tested with a sample project (if applicable)

  • New tests in tests/specify_cli/presets/test_resolver.py: frontmatter strategy: append after a BOM (LF and CRLF) is honoured; a BOM-prefixed layer with a manifest append strategy composes without the BOM or its frontmatter; strategy: replace-section in command frontmatter is rejected with and without a BOM; and two negative cases (BOM without frontmatter, BOM with unterminated frontmatter) that must be appended unchanged. On main the 4 BOM positive cases fail and the 3 others pass; with the fix all 7 pass.

  • tests/specify_cli/presets: 856 passed, 1 skipped. Full suite with -n auto on macOS: 17255 passed, 635 skipped, 1 failed: test_version_execution.py::TestWindowsUvToolEnvironment::test_non_windows_uv_tool_dry_run_still_previews_installer, which fails on main in my environment too.

  • uvx ruff@0.15.0 check src tests: all checks passed.

  • Sample project: the specify init / specify preset add --dev reproduction above, before and after.

AI Disclosure

  • I did not use AI assistance for this contribution
  • I did use AI assistance (fill in the disclosure below)

AI disclosure: Claude Code (model: Claude Opus 5.5, autonomous) found these sites while checking the rest of the BOM class after #5562. OpenAI Codex CLI (model: gpt-5.6-sol, medium reasoning, autonomous) wrote the fix and tests (code generation); Claude Code made small test edits (docstrings, an unused variable), reproduced the bug end to end, reviewed the change, ran the tests and lint, and wrote this description. The commit carries Assisted-by: trailers.

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 github#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)
@leninworld
leninworld requested a review from mnriem as a code owner November 25, 2026 16:14
Copilot AI adaptive review requested due to automatic review settings November 25, 2026 16:14

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The new BOM tests use a helper that omits an explicit UTF-8 encoding when writing command files, causing UnicodeEncodeError on Windows configurations such as cp1252 before the resolver is exercised.

1 open issue
What changed in this PR

Extends the BOM handling from #5562 to preset command composition, allowing UTF-8 BOM-prefixed frontmatter to be recognized without changing incomplete or absent frontmatter.

Changes:

  • Recognizes one leading BOM during legacy strategy detection and frontmatter splitting.
  • Adds regression coverage for composition, prohibited frontmatter strategies, and unchanged fallback content.
File Description
tests/​specify_cli/​presets/​test_resolver.py Adds BOM regression tests and preservation checks.
src/​specify_cli/​presets/​_resolver.py Handles a leading BOM at both frontmatter detection sites.

Review effort: Balanced (adaptive)

_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)
Copilot AI adaptive review requested due to automatic review settings November 25, 2026 16:36

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues are recorded for the BOM-prefixed frontmatter fix, although tests were not independently run during this review.

No open issues 🎉

1 resolved since last review

Review effort: Balanced (adaptive)

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues remain for the UTF-8 BOM handling change.

No open issues 🎉

Review effort: Balanced

@mnriem
mnriem merged commit f432bd2 into github:main Nov 25, 2026
14 checks passed
@mnriem

mnriem commented Nov 25, 2026

Copy link
Copy Markdown
Collaborator

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants