Skip to content

fix: allow parsing empty context lines at the very end of unified diffs#690

Open
rtritto wants to merge 1 commit into
kpdecker:masterfrom
rtritto:empty-lines
Open

fix: allow parsing empty context lines at the very end of unified diffs#690
rtritto wants to merge 1 commit into
kpdecker:masterfrom
rtritto:empty-lines

Conversation

@rtritto
Copy link
Copy Markdown

@rtritto rtritto commented May 23, 2026

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 (specifically parseHunk) 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 the diffstr array:

// Before
const operation = (diffstr[i].length == 0 && i != (diffstr.length - 1)) ? ' ' : diffstr[i][0];

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, i points to this final "" string. Because of the i != (diffstr.length - 1) check, it falls back to diffstr[i][0], which evaluates to undefined.

Since undefined is not a valid operation (+, -, , \), parseHunk throws an opaque error:
Error: Hunk at line X contained invalid line

Changes 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.

// After
const operation = diffstr[i].length == 0 ? ' ' : diffstr[i][0];

Why this fixes real-world use cases:

This minor tweak prevents runtime exceptions when using applyPatch() on valid patch files generated by package managers (like yarn patch or pnpm patch) or saved via code editors, where a hunk requires an empty contextual line extending to the very end of the file.

Copilot AI review requested due to automatic review settings May 23, 2026 08:31
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

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 thread src/patch/parse.ts
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]);
@rtritto
Copy link
Copy Markdown
Author

rtritto commented May 23, 2026

FYI @ExplodingCabbage

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