Skip to content

Emit a better error message when a patch is truncated#693

Merged
ExplodingCabbage merged 2 commits into
masterfrom
better-error-message-on-truncated-patch
Jun 1, 2026
Merged

Emit a better error message when a patch is truncated#693
ExplodingCabbage merged 2 commits into
masterfrom
better-error-message-on-truncated-patch

Conversation

@ExplodingCabbage
Copy link
Copy Markdown
Collaborator

I propose this as a more conservative way to resolve #690. At least this will make clear to the end user what the problem is. Given that the only way we know of so far that one can stumble into generating a truncated patch like this is manually editing it in an editor with trim-on-save behaviour, it should usually be no big deal for an end user to fix the problem after seeing the error message.

Alternatives considered but that I don't really like:

  • Making the default behaviour really liberal, like that of Unix patch. Trouble is this behaviour is arbitrary, surprising, and could easily hide more meaningful corruption of patches.
  • Adding a strict mode controlled by a boolean option. Doesn't seem worth the extra API complexity to solve a problem that will only affect people who are manually editing patches.

The only further angle for improving the state of the world I have considered was to see if anything can be reasonably changed in editors. In an editor that has trim-on-save enabled by default and also supports syntax-specific settings (and has some such settings configured by default), trim-on-save behaviour should probably be disabled by default for patch files.

Except in my current editor, Zed, that's already the case; default settings contain:

    "Diff": {
      "show_edit_predictions": false,
      "remove_trailing_whitespace_on_save": false,
      "ensure_final_newline_on_save": false,
    },

and I think VSCode has trim-on-save off by default globally.

So I reckon the best I can do is just this. Commentary welcome.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR improves the diagnostics emitted by parsePatch when a unified diff hunk ends unexpectedly at end-of-file, so users get a clearer “expected vs got” line-count mismatch error instead of a more opaque parse failure.

Changes:

  • Drop the trailing "" element produced by uniDiff.split(/\n/) when the input ends with a newline, preventing that sentinel from being misinterpreted as a hunk line.
  • Enhance hunk sanity-check errors to include whether the mismatch is for old/new lines and the expected vs actual counts.
  • Update/add tests to assert the new, more informative error messages.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
src/patch/parse.ts Removes the split sentinel trailing empty string and improves line-count mismatch error messages.
test/patch/parse.js Updates expected error messages and adds a regression test for premature EOF in the final hunk.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread test/patch/parse.js
line2
line3
+line4
line5`;
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Good point. Makes sense to test both scenarios (both of which should produce exactly the same error message). Done.

@ExplodingCabbage ExplodingCabbage merged commit bf227c1 into master Jun 1, 2026
@ExplodingCabbage ExplodingCabbage deleted the better-error-message-on-truncated-patch branch June 1, 2026 11:13
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.

2 participants