fix: allow parsing empty context lines at the very end of unified diffs#690
Open
rtritto wants to merge 1 commit into
Open
fix: allow parsing empty context lines at the very end of unified diffs#690rtritto wants to merge 1 commit into
rtritto wants to merge 1 commit into
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
Adjusts how parsePatch determines the hunk line “operation” for empty strings while iterating through a unified diff.
Changes:
- Simplifies operation detection by treating empty lines as context (
' ') unconditionally.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+470
to
472
| const operation = diffstr[i].length == 0 ? ' ' : diffstr[i][0]; | ||
| if (operation === '+' || operation === '-' || operation === ' ' || operation === '\\') { | ||
| hunk.lines.push(diffstr[i]); |
Author
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.
When parsing unified diff files, empty context lines (which should technically consist of a single space
" ") are often inadvertently truncated to completely empty strings ("") due to code editors trimming trailing whitespace or formatting tools removing extraneous spaces.Currently, the
parsePatch(specificallyparseHunk) function successfully forgives and maps these empty strings back to a space token (' '), except when the empty string happens to be the last line in thediffstrarray:If a patch file ends with a trailing newline,
uniDiff.split(/\n/)yields""as the last element of the array. If a hunk still expects one last context line at that exact end-of-file position,ipoints to this final""string. Because of thei != (diffstr.length - 1)check, it falls back todiffstr[i][0], which evaluates toundefined.Since
undefinedis not a valid operation (+,-,,\),parseHunkthrows an opaque error:Error: Hunk at line X contained invalid lineChanges Made:
Removed the boundary exception
&& i != (diffstr.length - 1)condition. Empty strings are now globally and safely interpreted as empty context lines regardless of their index in the diff string.Why this fixes real-world use cases:
This minor tweak prevents runtime exceptions when using
applyPatch()on valid patch files generated by package managers (likeyarn patchorpnpm patch) or saved via code editors, where a hunk requires an empty contextual line extending to the very end of the file.