Skip to content

fix(fmt): keep embedded CSS custom property indentation stable - #35949

Merged
bartlomieju merged 3 commits into
denoland:mainfrom
Hixie:fix/fmt-embedded-css-wrapped-custom-property-idempotent
Jul 20, 2026
Merged

bartlomieju merged 3 commits into
denoland:mainfrom
Hixie:fix/fmt-embedded-css-wrapped-custom-property-idempotent

Conversation

@Hixie

@Hixie Hixie commented Jul 10, 2026

Copy link
Copy Markdown
Contributor

(Written by Opus 4.8, sanity-checked by a human.)

Fixes #35948.

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.

`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>
@Hixie
Hixie force-pushed the fix/fmt-embedded-css-wrapped-custom-property-idempotent branch from e53e620 to 17b2973 Compare July 10, 2026 20:44

@bartlomieju bartlomieju left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.

Comment thread cli/tools/fmt.rs Resolved
Comment thread cli/tools/fmt.rs Outdated
&lax_css_config,
)? {
Some(formatted) => formatted,
None => dedented,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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.
@bartlomieju

Copy link
Copy Markdown
Member

I have an opinion now — I dug into lax-css to check my assumptions, and it turns out both of my review comments rest on a premise that doesn't actually hold for embedded CSS. I pushed a commit taking the safe route; details below.

The None branch is effectively unreachable for embedded CSS. lax-css::format_text returns None only when its output equals its input, but its generated output never carries a leading newline while an embedded css`\n…`template always starts with one. Soresult == dedented` basically never holds here.

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 None => dedented path with a surviving leading \n, which isn't reached. And deno-fmt-ignore-file inside embedded CSS is a no-op regardless of this PR: lax_core::has_ignore_file_comment returns false when the first token is a newline, which an embedded template always has.

Given that, rather than patch None => dedented with trim_matches, I went with the other option from my second comment: restore None => return Ok(None) (the pre-PR semantics). The dedent fix stays exactly where the real instability lived — the Some path. When lax-css declines to format, the TS formatter now emits the template verbatim, which is inherently a fixed point (no trim band-aid needed) and preserves any intentional formatting in an ignored block.

I verified both versions against a matrix (the original fix, both of my repros, node-level deno-fmt-ignore, over-indented ignore, non-standard base indent): identical output on every input, all stable. Existing spec passes, full fmt suite (100 tests) passes. I also added an ignored_block_is_idempotent spec case covering a deno-fmt-ignore node — the reachable analog of what I asked for.

The one trade-off: the CSS None handling now diverges from the HTML path's None => dedented. I think that's fine — the paths already diverge (the HTML code even notes "unlike lax-css…"), and verbatim-preserving is the more principled fallback for "the formatter chose not to touch this."

Sorry for sending you on a slightly wrong trail with the original comments. Thanks for the nice fix!

@bartlomieju bartlomieju left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks!

Signed-off-by: Bartek Iwańczuk <biwanczuk@gmail.com>
@bartlomieju
bartlomieju enabled auto-merge (squash) July 20, 2026 11:58
@bartlomieju
bartlomieju merged commit 13ce29b into denoland:main Jul 20, 2026
136 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.

fmt: not idempotent for wrapped var() in a CSS custom property inside a css`` template (regression in 2.9.0)

2 participants