Repository navigation
fix(libnpmdiff): keep a final blank context line in the diff - #10231
Open
simshanith wants to merge 1 commit into
Open
simshanith wants to merge 1 commit into
simshanith wants to merge 1 commit into
Conversation
formatDiff trimmed its output to drop the final newline, but trim() also removed a trailing blank context line (a single space), leaving the last hunk one line short of its header and the patch corrupt.
simshanith
marked this pull request as ready for review
November 5, 2026 06:19
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.
formatDiffin libnpmdiff ends withreturn res.trim()to drop the patch's final newline.trim()also strips trailing whitespace, though. In unified diff format an unchanged blank line is a single space, so when the last hunk ends on one,trim()deletes it. The hunk header still counts that line, andgit applyrejects the output as a corrupt patch.This change removes only the final newline:
return res.replace(/\n$/, '').Repro, from the issue:
In an empty directory, with npm 12.2.2 and
giton the path, unpacklit-ui-router1.19.0. Its successor 1.19.1 ends the diff on a blank context line insrc/view-rendered.ts:npm pack lit-ui-router@1.19.0 mkdir a && tar -xzf lit-ui-router-1.19.0.tgz -C a --strip-components=1With the default npm config (no
diff-*options set).Run
npm diff --diff=lit-ui-router@1.19.0 --diff=lit-ui-router@1.19.1 > out.diff, thencd a && git init -q && git apply --check ../out.diff.See the error:
error: corrupt patch at ../out.diff:1232. The last hunk's header is@@ -99,9 +113,8 @@, but the hunk holds only 8 old and 7 new lines.The new test,
keep a blank context line that ends the diff, diffsa\nb\n\nagainsta\nc\n\n. It checks for the@@ -1,3 +1,3 @@header and that the output ends with the blank context line. It fails before this change.Fixes #10230
Written by an agent (Claude Code, claude-opus-5-5).