Skip to content

fix(core): preserve results after partial formatter failure - #61416

Open
qkycir wants to merge 2 commits into
anomalyco:devfrom
qkycir:formatter-result
Open

qkycir wants to merge 2 commits into
anomalyco:devfrom
qkycir:formatter-result

Conversation

@qkycir

@qkycir qkycir commented Nov 3, 2026 •

Copy link
Copy Markdown

Issue for this PR

Closes #61415

Type of change

  • Bug fix

What does this PR do?

A formatter can write corrections before exiting nonzero. Patch currently returns its pre-format diff in that case, and both Patch and FileWrite skip restoring the original BOM.

Always synchronize the BOM and read the final text after formatting. The final text is required on the internal write result, so Edit uses it directly. Formatter success reporting and formatter fallback behavior stay the same; this costs one final read even when no formatter succeeds.

How did you verify your code works?

  • Two regressions run a real configured Bun formatter that writes a file and exits 1. Both fail on the base and pass with this change.
  • Patch, Write, Edit, Formatter and patch parsing suites: 147 passed. Patch/Write repeated in randomized order ten times: 540 passed.
  • After the review cleanup, Patch/Write/Edit pass all 75 tests and Core typecheck:tsc passes again.
  • Core typecheck and typecheck:tsc, changed-file lint, and production Effect rules pass.
  • Root bun run check completes lint but cannot finish workspace typechecking with the filtered install: unrelated packages lack dependencies such as gray-matter and Cloudflare worker types.

Visual

<svg xmlns="http://www.w3.org/2000/svg" width="240" height="72" viewBox="0 0 240 72">
  <g fill="none" stroke="currentColor" stroke-width="2"><rect x="12" y="12" width="64" height="48" rx="5"/><path d="M24 26h40M24 36h32M24 46h40M88 36h58m-8-6 8 6-8 6"/><rect x="158" y="12" width="70" height="48" rx="5"/><path d="m174 36 10 10 27-24"/></g>
  <circle cx="90" cy="36" r="3" fill="currentColor" opacity="0.5"><animate attributeName="cx" values="90;145;90" dur="3s" repeatCount="indefinite"/></circle>
</svg>

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The fix looks correct to me. Two of your new tests (plus the updated read-count test) fail on the base branch and pass with this change: the Patch diff now shows the formatter's final output, and both Patch and Write keep the UTF-8 BOM when the formatter exits with an error after rewriting the file. The tool-patch, tool-write and tool-edit suites pass, and typecheck:tsc passes for packages/core. I couldn't confirm it end to end in the terminal app with the simulated model, so this rests on the tests.

Public API: unchanged. These are internal FileWrite/Patch internals only.

One small follow-up: FileWrite.commit now always sets formatted, so the result.formatted ?? readText(...) fallback in packages/core/src/tool/plugin/edit.ts can never run. You could make formatted required on Written and drop that fallback. It's optional, not a blocker.

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The earlier follow-up is done: Written.formatted is now required, and Edit uses it instead of re-reading the file. tool-patch, tool-write and tool-edit pass and typecheck:tsc passes. The Patch test from the issue's reproduction script fails on the base branch and passes with this change. Public API: unchanged (internal FileWrite/tool code only). No new concerns.

@EgilSandfeld

Copy link
Copy Markdown

I reproduced the two formatter-partial-failure cases on 2.2.7-based native code: the write case lost its BOM, and the patch result reported the pre-formatter content. Both native tests passed after overlaying this PR's three production-file changes on my isolated 2.2.7 + #58324 source.

This was source-level native tool integration testing, not a built-host test of the combined PRs.

Separately, a successful external formatter emitting LF still leaves a previously CRLF file as LF (with BOM restored). I understand that is outside this PR's BOM/result-reporting scope; it is worth keeping separate from a general line-ending-preservation claim.

This branch has not been deployed

No deployments
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.

Patch returns stale diff and writes lose BOM after partial formatter failure

2 participants