Skip to content

fix: preserve trailing blank line in apply_patch - #42084

Closed
codeAnqiang-ma wants to merge 1 commit into
anomalyco:devfrom
codeAnqiang-ma:patch-trailing-newline
Closed

codeAnqiang-ma wants to merge 1 commit into
anomalyco:devfrom
codeAnqiang-ma:patch-trailing-newline

Conversation

@codeAnqiang-ma

Copy link
Copy Markdown

Issue for this PR

Fixes #42081

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

apply_patch silently deletes the final blank line of any file ending with \n\n, even when the patch doesn't touch it (#42081).

Both patch implementations pop the sentinel "" that split("\n") produces when content ends with a newline, then later re-check "is the last element empty" to decide whether to push the sentinel back. A genuinely blank last line also leaves "" at the end, so the sentinel is never restored and one \n is lost on every edit.

The fix records whether the sentinel was actually popped and reuses that on the way out instead of re-inferring it. Applied to derive() in packages/core/src/patch.ts and deriveNewContentsFromChunks() in packages/opencode/src/patch/index.ts. Files without a trailing newline keep the existing "always add one" behavior; this PR only fixes the unrequested deletion.

Disclosure: developed with AI assistance; I reviewed and verified everything locally.

How did you verify your code works?

Added one regression test per implementation, next to the existing cases; both fail before the fix and pass after:

  • packages/core/test/tool-apply-patch.test.ts: end-to-end through the tool. File alpha\nbeta\n\n, patch changes only alpha; asserts ALPHA\nbeta\n\n on disk and a 1 addition / 1 deletion diff (before the fix: ALPHA\nbeta\n and 1/2).
  • packages/opencode/test/patch/patch.test.ts: same scenario through Patch.applyPatch.

Test runs:

  • packages/core: full bun test — 1081 tests, 1079 pass; the 2 failures are pre-existing flaky Npm.add timeouts unrelated to this change (they pass when the file is run individually, both before and after the fix).
  • packages/opencode: bun test test/patch/patch.test.ts test/tool/apply_patch.test.ts — 48 pass. I did not run this package's full suite.
  • bun typecheck passes in both packages.

Screenshots / recordings

Not a UI change.

Checklist

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

@github-actions

Copy link
Copy Markdown
Contributor

Automated PR Cleanup

Thank you for contributing to opencode.

Due to the high volume of PRs from users and AI agents, we periodically close older PRs using automated criteria so maintainers can focus review time on the most active and community-supported contributions.

This PR was closed because it matched the following cleanup criteria:

  • The PR was created more than 1 month ago
  • The PR had fewer than 2 positive reactions
  • Positive reactions are counted as thumbs-up, heart, celebration, or rocket reactions on the PR

PRs created within the last month are not affected by this cleanup.

If you believe this PR was closed incorrectly, or if you are still actively working on it, please leave a comment explaining why it should be reopened. A maintainer can review and reopen it if appropriate.

Thanks again for taking the time to contribute.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

apply_patch deletes the final blank line of files that end with one

1 participant