[Bug]: gram format leaves a spaced composite with a no-break space around '<' unglued #269

Closed
opened 2026-11-26 13:13:16 +01:00 by abiwab · 0 comments
Owner

Kitchen flags SPACED_COMPOSITE whatever blank surrounds the < (packages/kitchen/src/spaced-composite.ts trims the text between the two ingredients), but the formatter's repair (rule 6, packages/format/src/index.ts) only glued a space or a tab. With a no-break space (U+00A0) or another Unicode blank, gram format left in place a composite that Gram warns is broken:

Mix @lemon zest{1} < @lemon{2}.   → SPACED_COMPOSITE, left as is by gram format

The fuzz harness (#196) reports it as a change of meaning once an id has a capital letter: rule 6 did not fire, so I6 compared the two compilations, and rule 1 lowercased the parent quoted by data.suggestion (<@Lemon → <@lemon):

Signature: format-preserves-meaning | compilation differs after formatting at $.warnings[].data
Example:   Mix @Juice{1} < @Lemon{2}.

Present on main since SPACED_COMPOSITE shipped in 1.5.2. Not caught by the fixed-seed fuzz test; found by bun run fuzz --iterations 50000 (seed 540879751).

Fix (patch on @gram-lang/format):

  • rule 6 takes every Unicode horizontal blank ([^\S\r\n]), never \s: it runs before the parser finds the steps, and a line break taken there would merge two steps or a comment;
  • fuzz/src/normalize.ts lowercases a SPACED_COMPOSITE's data.suggestion like parent (rule 1), as a guard should a blank kitchen flags ever escape rule 6 again;
  • the minimized input goes to fuzz/regressions/.

A composite cut by a line break (@a{1} <\n@b{2}) is not affected: the parser splits the text at the line break, so kitchen never flags it.

Kitchen flags `SPACED_COMPOSITE` whatever blank surrounds the `<` (`packages/kitchen/src/spaced-composite.ts` trims the text between the two ingredients), but the formatter's repair (rule 6, `packages/format/src/index.ts`) only glued a space or a tab. With a no-break space (U+00A0) or another Unicode blank, `gram format` left in place a composite that Gram warns is broken: ``` Mix @lemon zest{1} < @lemon{2}. → SPACED_COMPOSITE, left as is by gram format ``` The fuzz harness (#196) reports it as a change of meaning once an id has a capital letter: rule 6 did not fire, so I6 compared the two compilations, and rule 1 lowercased the parent quoted by `data.suggestion` (`<@Lemon` → `<@lemon`): ``` Signature: format-preserves-meaning | compilation differs after formatting at $.warnings[].data Example: Mix @Juice{1} < @Lemon{2}. ``` Present on `main` since `SPACED_COMPOSITE` shipped in 1.5.2. Not caught by the fixed-seed fuzz test; found by `bun run fuzz --iterations 50000` (seed 540879751). Fix (patch on `@gram-lang/format`): - rule 6 takes every Unicode horizontal blank (`[^\S\r\n]`), never `\s`: it runs before the parser finds the steps, and a line break taken there would merge two steps or a comment; - `fuzz/src/normalize.ts` lowercases a `SPACED_COMPOSITE`'s `data.suggestion` like `parent` (rule 1), as a guard should a blank kitchen flags ever escape rule 6 again; - the minimized input goes to `fuzz/regressions/`. A composite cut by a line break (`@a{1} <\n@b{2}`) is not affected: the parser splits the text at the line break, so kitchen never flags it.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
gram-lang/gram#269
No description provided.