Skip to content

fix(parser): strip a UTF-8 BOM before parsing recipes - #742

Merged
LeMyst merged 1 commit into
cooklang:mainfrom
drawmeanelephant:fix/712-strip-utf8-bom
Oct 17, 2026
Merged

LeMyst merged 1 commit into
cooklang:mainfrom
drawmeanelephant:fix/712-strip-utf8-bom

Conversation

@drawmeanelephant

@drawmeanelephant drawmeanelephant commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

Closes #712

A recipe file starting with a UTF-8 BOM never matches cooklang's ---
frontmatter fence (upstream bug, filed as cooklang/cooklang-rs#134), so
the whole frontmatter became a phantom step and the title was lost.
Menus already strip a BOM in util/menu.rs; recipes did not.

Adds cookcli_core::strip_bom/bom_len and strips on every file-content
parse entry point: parse_unscaled (which backs parse_recipe /
parse_recipe_at), doctor's validate_entry, and
util::parse_unscaled_recipe_from_entry.

Diagnostics: the report is still rendered against the stripped text so
ariadne's output stays aligned, but Diagnostic.location.span — public
serialized API — is shifted back by the BOM length via shift_spans, so
a caller indexing the original buffer is not off by three bytes. The
textual-timer warnings validate_entry computes itself are shifted too.

Known upstream gaps (filed, not fixable here): cooklang/cooklang-find#35
(metadata.rs front-matter scan) and cooklang-reports / the LSP buffer
path, where stripping would desync editor coordinates — the parser fix
in cooklang-rs#134 is the right home for those.

Test plan

  • a_utf8_bom_does_not_break_frontmatter — BOM'd file parses with
    its title, no phantom step
  • a_utf8_bom_keeps_spans_in_file_coordinates — spans differ by
    exactly +3 vs the same recipe without BOM
  • validate_a_recipe_starting_with_a_bom — doctor validate on a
    BOM'd file reports its timer warning
  • cargo test --all-features green

Generated with Devin

@LeMyst LeMyst left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks. This covers the main parse paths:

  • parse_unscaled (crates/core/src/parser.rs:86), which serves recipe, the core shopping list and find::parse_or_skip;
  • util::parse_unscaled_recipe_from_entry (server render, menus, build);
  • doctor's validate_entry.

A few places still choke on the BOM, and spans have shifted.

Still not covered

  • cooklang-find front matter. metadata.rs compares first_line.trim() with ---, and Rust's trim() doesn't remove U+FEFF. So entry.metadata() stays empty for files with a BOM. That affects feed titles (src/build/feed.rs:526), image_url (src/server/handlers/recipe_image.rs:386), and search metadata and tag filters.
  • cook report. cooklang-reports parses the file with its own parser.
  • LSP / editor. The language server parses the raw buffer, so the editor still shows the phantom step.

Spans are now 3 bytes off

Diagnostic spans are now relative to the stripped text, 3 bytes before the real file offsets. The CLI is consistent, because render_report renders against the stripped text. But Diagnostic.location.span is serialized public API, and a caller indexing the original buffer would be off by 3 bytes (1 UTF-16 unit). Either add the BOM length back in collect_diagnostics/convert_diagnostic, or document the offset.

Tests

The only test goes through parse_recipe; one through doctor or the util path would help.

The root cause is upstream (cooklang-rs's front-matter fence and cooklang-find's metadata.rs). This is a fine stopgap if issues get filed there.

This conflicts with #743 in all four shared files (adjacent lines). Both PRs patch three copies of the same parse call, so a shared helper (BOM strip plus options) would let them stop colliding.


🤖 Analysis and recommendations made by Claude (Anthropic's AI assistant), posted on behalf of @LeMyst.

@drawmeanelephant

Copy link
Copy Markdown
Contributor Author

Reworked along the review:

  • Spans restored to file coordinates. bom_len + shift_spans (core parser) put the BOM's three bytes back into every Diagnostic.location.span, applied in parse_unscaled (parse errors and warnings alike) and in validate_entry — including the textual-timer warnings it computes itself. render_report still renders against the stripped text, so CLI output stays consistent with the report's own offsets.
  • Coverage added: a_utf8_bom_keeps_spans_in_file_coordinates (spans differ by exactly +3 vs the same recipe un-BOM'd) and a doctor validate integration test on a BOM'd file that finds its timer warning.
  • Upstream gaps filed: UTF-8 BOM defeats the frontmatter fence cooklang-rs#134 (BOM defeats the frontmatter fence — the real fix belongs there, which also unblocks cooklang-reports and the LSP path, where stripping would desync buffer coordinates) and Front-matter metadata missed on files with a UTF-8 BOM cooklang-find#35 (metadata.rs first-line --- check misses BOM'd files — feed titles, image_url, search metadata).

On the shared-helper collision with #743: strip_bom/bom_len are now the single entry point — parse_unscaled owns the strip for parse_recipe, so when #743 lands the conflict surface should be down to imports rather than three parallel parse calls. I left validate_entry doing its own strip because its report rendering and timer-warning spans need the same shifted coordinates; it reuses the helpers rather than re-implementing them.

@LeMyst

LeMyst commented Oct 17, 2026 •

Copy link
Copy Markdown
Collaborator

Thanks. Moving the spans back to file coordinates is the right fix, and most paths now handle a BOM. I checked on 6517d97 with a BOM-prefixed recipe that has YAML front matter:

  • cook recipe in all 8 formats, shopping-list, search, the server recipe page and /api/recipes/... all show the front-matter title and metadata.
  • doctor validate reports parse errors on line 1 and on line 3 at the right line.
  • The editor's raw endpoint and save keep the BOM. A curl PUT writes the body back verbatim.

One regression came in with the rework, plus two smaller points:

1. doctor validate panics on a textual timer in a BOM'd file

validate_entry shifts the timer warnings into file coordinates (crates/core/src/doctor.rs:330) before passing them to render_findings. That function then slices the stripped text with them (content[..span.start], :463), so every timer span is 3 bytes too far:

$ printf '\xef\xbb\xbfCook ~{a}' > t.cook && cook doctor validate
thread 'main' panicked at crates/core/src/doctor.rs:463:35:
end byte index 10 is out of bounds for string of length 9
  • It exits with 101.
  • Cook ~{a€} now. panics too: end byte index 10 is not a char boundary; it is inside '€'.
  • Cook ~{a}\nthen stir. doesn't panic, but says Timer value is text: a (t.cook:2) while the parser's own warning just above it points at line 1.
  • Because it's a panic, one such file stops validation of the whole collection.

On main, all three report line 1 without a panic. Rendering first and shifting timer_warnings after render_findings (or passing the unshifted copy to it) fixes it. validate_a_recipe_starting_with_a_bom does go through this path, but the extra 3 bytes land harmlessly there. The slice ends inside medium heat, still on the same line, so it prints bom.cook:5 on both this PR and main, and the test only checks that "Timer value is text" appears. The offset only shows when the timer's text is within 3 bytes of a newline or the end of the file. A test with a BOM in front of Cook ~{a}, asserting :1, fails on this PR and passes once the shift moves after rendering.

2. cook report still renders an empty title

render reads the recipe text itself (crates/core/src/report.rs:111, recipe_text) and hands it to cooklang-reports with the BOM still in front. So the front matter isn't found and {{ title }} comes out empty. A strip_bom there fixes this locally, without waiting for upstream.

3. Upstream, as you noted

The recipe listing (/ and /api/recipes) still shows a BOM'd file by its file name with empty metadata, and the LSP still sees a phantom first step, the same as main. Thanks for filing cooklang-rs#134 and cooklang-find#35.

This still conflicts with #743 in all four files: the imports and the three PARSER.parse call sites. That's mechanical for whichever lands second. Folding the BOM strip and parse_options() into one helper, as you offered on #743, would make it trivial.

fmt, clippy, cargo test --workspace and CI are green.


🤖 Analysis and recommendations made by Claude (Anthropic's AI assistant), posted on behalf of @LeMyst.

A BOM'd recipe's front-matter fence is not at the start of the text, so
the parser read it as a step and the title and metadata were silently
lost (#712). parse_file now strips it, applies CookCLI's parse_options,
and hands back the stripped text alongside the result, so parsing and
the rendered report stay in the same coordinates while Diagnostic spans
are shifted back onto the file by bom_len.

cook report reads the file itself and hands it to cooklang-reports with
the BOM still on it, so {{ title }} rendered empty; it strips too.

Upstream: cooklang/cooklang-rs#134, cooklang/cooklang-find#35.
@drawmeanelephant

Copy link
Copy Markdown
Contributor Author

On 695c796, rebased onto main with #743 in:

  1. The panic: right — render_findings slices the stripped text, so shifting timer_warnings first ran the span 3 bytes past it. Warnings now go to render_findings unshifted and move to file coordinates only for the stored diagnostics. The new a_bom_and_a_timer_near_the_edge_of_the_line test (Cook ~{a%min} at EOF and with a multi-byte char) panics on the old order and prints :1 on this one; the existing BOM test now also pins bom.cook:5.
  2. cook report: recipe_text strips the BOM before handing the text to cooklang-reports, so {{ title }} sees the front matter.
  3. The fix(parser): suppress spurious type warning on >> numeric metadata #743 overlap is gone: both concerns now live in one place — parse_file(text) strips the BOM, applies parse_options(), and returns the stripped text with the result. validate_entry, parse_unscaled and parse_unscaled_recipe_from_entry all go through it.

@LeMyst

LeMyst commented Oct 17, 2026

Copy link
Copy Markdown
Collaborator

Thanks. I checked 695c796:

  • doctor validate no longer panics on a BOM'd file with a textual timer, whether the timer is at the end of the file, before a newline or next to a multi-byte character, and it reports the right line. With the old shift order put back, a_bom_and_a_timer_near_the_edge_of_the_line fails on the € case, so it guards the fix.
  • cook report now sees a BOM'd file's front matter (metadata.title, metadata.servings).
  • Nothing else changed: cook recipe gives byte-identical output with and without the BOM in all 8 formats, and shopping-list, search and the server recipe page and API all work.

With parse_file as the one place that strips the BOM and sets the parse options, this looks good to merge. fmt, clippy, tests and CI are green, and it merges cleanly with main. One heads-up: it conflicts with #734 in tests/doctor_characterization_test.rs, where both add tests, so whichever lands second needs a small rebase.


🤖 Analysis and recommendations made by Claude (Anthropic's AI assistant), posted on behalf of @LeMyst.

@LeMyst
LeMyst merged commit c93fd51 into cooklang:main Oct 17, 2026
5 checks passed
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.

UTF-8 BOM breaks YAML frontmatter parsing

2 participants