denoland/deno · error

Formatting succeeded initially, but failed when ensuring a s

Error message

Formatting succeeded initially, but failed when ensuring a stable format. This indicates a bug in the formatter where the text it produces is not syntactically correct. As a temporary workaround you can ignore this file.

{:#}

What it means

After formatting a file, `deno fmt` re-parses and re-formats its own output to verify stability. This error means the first formatting pass succeeded but the produced text failed to parse on the verification pass — the formatter emitted syntactically invalid output. This is a bug in the formatter (dprint plugin) for that input, not a problem with your file.

Source

Thrown at cli/tools/fmt.rs:1214

        match fmt_func(
          file_path,
          &FileContents {
            had_bom: false,
            text: (&current_text).into(),
          },
        ) {
          Ok(Some(next_pass_text)) => {
            // just in case
            if next_pass_text == current_text {
              return Ok(Some(next_pass_text));
            }
            current_text = next_pass_text;
          }
          Ok(None) => {
            return Ok(Some(current_text));
          }
          Err(err) => {
            bail!(
              concat!(
                "Formatting succeeded initially, but failed when ensuring a ",
                "stable format. This indicates a bug in the formatter where ",
                "the text it produces is not syntactically correct. As a temporary ",
                "workaround you can ignore this file.\n\n{:#}"
              ),
              err,
            )
          }
        }
        count += 1;
        if count == 5 {
          bail!(
            concat!(
              "Formatting not stable. Bailed after {} tries. This indicates a bug ",
              "in the formatter where it formats the file differently each time. As a ",
              "temporary workaround you can ignore this file."
            ),

View on GitHub (pinned to 9ad36f7a2c)

Solutions

  1. Temporarily exclude the offending file: add `// deno-fmt-ignore-file` as the first line, or add it to "exclude" under "fmt" in deno.json
  2. Upgrade (or downgrade) Deno — such regressions are usually fixed quickly in a patch release
  3. Minimize the file to the smallest snippet that reproduces it and report it at github.com/denoland/deno/issues

Example fix

// before — deno.json
{}

// after — skip the file that trips the formatter bug
{
  "fmt": {
    "exclude": ["src/tricky_file.ts"]
  }
}

// or inline, first line of the file:
// deno-fmt-ignore-file
Defensive patterns

Strategy: fallback

Validate before calling

# smoke-test formattability of the exact file set before CI commits to fmt:
deno fmt --check . || deno fmt .
# per-file loop lets one bad file be excluded instead of failing the job:
for f in $(git ls-files '*.ts'); do deno fmt "$f" || echo "fmt-bug: $f" >> fmt-skip.txt; done

Try / catch

When invoking deno fmt programmatically (Node/Bun): spawn it, on non-zero exit scan stderr for 'Formatting succeeded initially, but failed when ensuring a stable format'; if found, add the named file to fmt exclude and re-run instead of failing the build.

Prevention

When it happens

Trigger: Formatting a file whose syntax hits a formatter bug: exotic TypeScript/JavaScript constructs, markdown files with unusual embedded code blocks, or edge cases in a specific Deno version's dprint plugin. The verification loop at cli/tools/fmt.rs:1214 calls format_file again on the formatted text and the second parse returns Err.

Common situations: Appearing right after a Deno upgrade that introduced a formatter regression; files mixing languages (markdown with code fences, inline expressions in template literals); CI formatting jobs suddenly failing on files that used to format fine.

Related errors


AI-assisted analysis of denoland/deno@9ad36f7a2c (2026-08-20). Data as JSON: /api/errors/ee30d8695114da07. Report an issue: GitHub.