Skip to content

cook recipe -f cooklang corrupts recipes on repeated round-trips #414

Closed

Description

@dubadub

cook recipe -f cooklang is not idempotent: every pass through the formatter adds one leading space to any step line that wraps immediately after an ingredient, cookware or timer. The growth is unbounded, so repeatedly rewriting a .cook file through the formatter corrupts it a little more each time.

Reproduction

cat > p0.cook <<'RECIPE'
---
title: Round Trip
---

Mix @plain flour{200%g} with @whole milk{250%ml} in a #large mixing bowl{} and beat until the batter is completely smooth, then fold in @caster sugar{2%tbsp} and a pinch of @fine sea salt{}.
RECIPE

for i in 1 2 3; do
  cook recipe read p$((i-1)).cook -f cooklang > p$i.cook
done

diff p1.cook p3.cook
8c8
<  and a pinch of @fine sea salt{}.
---
>    and a pinch of @fine sea salt{}.

One leading space after pass 1, two after pass 2, three after pass 3.

Cause

component_word_separator emits the component using Word::from, whose whitespace field is empty. The space that follows the component is therefore handed to the next word rather than being dropped at the line break, so it lands at the start of the wrapped line. On reparse it is ordinary step text, and the next format pass adds another.

Only lines that wrap directly after a component are affected, which is why it is easy to miss — a line wrapping at a plain word is unaffected.

Impact

Any workflow that rewrites recipes through the formatter (normalising a collection, an editor "format document" action, a pre-commit hook) degrades the source on each run.

Notes

There is a failing test proving this, currently marked #[ignore] with the diagnosis in its doc comment, added while extracting the formatters into a library crate: formatting_is_idempotent. Fixing this changes output for every wrapped step, so it wants its own change and a deliberate snapshot review rather than being folded into unrelated work.

Metadata

Metadata

Assignees

No one assigned

    Labels

    help wanted

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions