Emit a better error message when a patch is truncated#693
Merged
Conversation
Contributor
There was a problem hiding this comment.
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 byuniDiff.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.
| line2 | ||
| line3 | ||
| +line4 | ||
| line5`; |
Collaborator
Author
There was a problem hiding this comment.
Good point. Makes sense to test both scenarios (both of which should produce exactly the same error message). Done.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:
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:
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.