Repository navigation
fix(fmt): keep embedded CSS custom property indentation stable - #35949
bartlomieju merged 3 commits into
Conversation
`deno fmt` was not idempotent when it formatted CSS embedded in a `css` tagged template literal whose custom property value is a `var(...)` call wrapped across several lines. Every formatting pass added the template literal's indentation to the wrapped argument lines again, without first removing the indentation the previous pass had added, so the indentation grew without bound. Deno's stability check then failed with "Formatting not stable. Bailed after 5 tries", which meant these files could not be formatted at all and always failed `deno fmt --check`. The problem first appeared in 2.9.0; 2.8.3 formatted the same input stably. The CSS formatter prints custom property values (names starting with `--`) verbatim, preserving the leading whitespace on their wrapped continuation lines. When the TypeScript formatter embeds the formatted CSS back into the template literal, it reindents every line to the template's indentation level by reading each line's leading whitespace and re-emitting it on top of the template's base indent. Because the verbatim continuation lines already carried the base indent that the previous pass had added, each pass stacked another base indent on top of them, so they drifted further right every time. The embedded HTML path already avoids this by dedenting the text before formatting so that the reindent step is a fixed point. Apply the same dedent to the embedded CSS path. Values that the CSS formatter reflows are unaffected because their indentation is recomputed from scratch; only the verbatim custom property values, which previously grew, are now anchored to the template's indentation and stay put. Add a spec test that formats a wrapped `var(...)` custom property inside a `css` template and asserts both that stdin formatting is idempotent and that file formatting does not hit the stability bailout. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
e53e620 to
17b2973
Compare
bartlomieju
left a comment
There was a problem hiding this comment.
Nice fix, and thank you for the thorough write-up in the description. The core dedent approach is right and matches the embedded-HTML path.
Two things in the new None => dedented fallback branch, both about content that lax-css hands back unchanged (i.e. deno-fmt-ignore-file, empty, or already-canonical blocks). For ordinary, reflowed CSS the dedent is a no-op since lax-css reindents from scratch, so these only bite the passthrough path — but one of them can re-trigger the exact stability bailout this PR is fixing. Details inline.
| &lax_css_config, | ||
| )? { | ||
| Some(formatted) => formatted, | ||
| None => dedented, |
There was a problem hiding this comment.
Worth a conscious decision here: previously, when lax-css returned None, format_embedded_css returned Ok(None), and the TS plugin then emitted the template quasi verbatim, preserving the user's exact indentation. With None => dedented, an indented block now becomes Some(dedented) and the TS reindent loop re-quantizes its indentation — so a deno-fmt-ignore-file (or node-level deno-fmt-ignore) block inside a css template loses the indentation it was explicitly told to keep, e.g.:
css`
/* deno-fmt-ignore-file */
.a{color:red}
`;
The embedded-HTML path has the same behavior, so this may be acceptable and just needs a note — but since it's newly introduced for CSS, it'd be good to either accept it explicitly or special-case the None return to fall back to Ok(None) (leave the block untouched) rather than Some(dedented). Combined with the trim_matches fix above, a spec test with a deno-fmt-ignore-file'd css block asserting stdin idempotency would lock this down.
There was a problem hiding this comment.
Do you have an opinion one way or the other? I really don't know enough about the codebase to have an opinion. I can pick one at random if you like. :-)
Restore the pre-existing None semantics for the embedded CSS fallback: when lax-css returns None, return Ok(None) so the TypeScript formatter emits the template verbatim. This is inherently a fixed point (no risk of the leading-newline growth) and preserves any intentional formatting, while the dedent fix stays on the Some path where the actual instability lived. Add a spec case asserting a deno-fmt-ignore block inside a css template is idempotent.
|
I have an opinion now — I dug into The That also means my comment-1 repro doesn't actually bail out — I couldn't reproduce the "Formatting not stable" growth, because it needs the Given that, rather than patch I verified both versions against a matrix (the original fix, both of my repros, node-level The one trade-off: the CSS Sorry for sending you on a slightly wrong trail with the original comments. Thanks for the nice fix! |
Signed-off-by: Bartek Iwańczuk <biwanczuk@gmail.com>
(Written by Opus 4.8, sanity-checked by a human.)
Fixes #35948.
deno fmtwas not idempotent when it formatted CSS embedded in acsstagged template literal whose custom property value is avar(...)call wrapped across several lines. Every formatting pass added the template literal's indentation to the wrapped argument lines again, without first removing the indentation the previous pass had added, so the indentation grew without bound. Deno's stability check then failed with "Formatting not stable. Bailed after 5 tries", which meant these files could not be formatted at all and always faileddeno fmt --check. The problem first appeared in 2.9.0; 2.8.3 formatted the same input stably.The CSS formatter prints custom property values (names starting with
--) verbatim, preserving the leading whitespace on their wrapped continuation lines. When the TypeScript formatter embeds the formatted CSS back into the template literal, it reindents every line to the template's indentation level by reading each line's leading whitespace and re-emitting it on top of the template's base indent. Because the verbatim continuation lines already carried the base indent that the previous pass had added, each pass stacked another base indent on top of them, so they drifted further right every time.The embedded HTML path already avoids this by dedenting the text before formatting so that the reindent step is a fixed point. Apply the same dedent to the embedded CSS path. Values that the CSS formatter reflows are unaffected because their indentation is recomputed from scratch; only the verbatim custom property values, which previously grew, are now anchored to the template's indentation and stay put.
Add a spec test that formats a wrapped
var(...)custom property inside acsstemplate and asserts both that stdin formatting is idempotent and that file formatting does not hit the stability bailout.