Repository navigation
fix(parser): strip a UTF-8 BOM before parsing recipes - #742
Conversation
LeMyst
left a comment
There was a problem hiding this comment.
Thanks. This covers the main parse paths:
parse_unscaled(crates/core/src/parser.rs:86), which servesrecipe, the core shopping list andfind::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.rscomparesfirst_line.trim()with---, and Rust'strim()doesn't remove U+FEFF. Soentry.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.
2a6a8a4 to
6517d97
Compare
|
Reworked along the review:
On the shared-helper collision with #743: |
|
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:
One regression came in with the rework, plus two smaller points: 1.
|
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.
6517d97 to
695c796
Compare
|
On 695c796, rebased onto main with #743 in:
|
|
Thanks. I checked 695c796:
With 🤖 Analysis and recommendations made by Claude (Anthropic's AI assistant), posted on behalf of @LeMyst. |
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_lenand strips on every file-contentparse entry point:
parse_unscaled(which backsparse_recipe/parse_recipe_at),doctor'svalidate_entry, andutil::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— publicserialized API — is shifted back by the BOM length via
shift_spans, soa caller indexing the original buffer is not off by three bytes. The
textual-timer warnings
validate_entrycomputes itself are shifted too.Known upstream gaps (filed, not fixable here): cooklang/cooklang-find#35
(
metadata.rsfront-matter scan) and cooklang-reports / the LSP bufferpath, 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 withits title, no phantom step
a_utf8_bom_keeps_spans_in_file_coordinates— spans differ byexactly +3 vs the same recipe without BOM
validate_a_recipe_starting_with_a_bom—doctor validateon aBOM'd file reports its timer warning
cargo test --all-featuresgreenGenerated with Devin