fix: don't misclassify deletion-only hunks as already applied - #22
Merged
Merged
Conversation
A hunk that only deletes lines, with its deletions at the hunk's edge (e.g. leading context and no trailing context), has a post-image that consists solely of its context lines - which the *un*patched file also contains contiguously. The post-image search from prefix-dev#20 therefore reported such hunks as already applied, and apply_bytes_reporting silently skipped them: exit code 0, no diagnostic, no change. This is how php-feedstock's 0001-win-iconv-compat.patch broke under rattler-build 0.70+: the hunk emptying ext/iconv/php_iconv.def (one EXPORTS context line followed by deletions to EOF) vanished while the other hunks applied, leaving stale .def exports and an LNK2001 on _libiconv_version. For a hunk with no insertions, now additionally require that its pre-image (context plus deleted lines) is absent from the file before declaring it already applied. Hunks with insertions are unaffected: their post-image contains lines the unpatched file doesn't have. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Merged
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.
The bug
Since #20, a hunk containing only context and deletion lines, with ≥1 leading context line and 0 trailing context lines, is silently dropped by
apply_bytes_reporting— exit code 0, no diagnostic, the patch just doesn't happen:→ file unchanged.
Root cause:
is_diff_applied_with_configdeclares a hunk already applied when its post-image (context + inserted lines) occurs contiguously in the file. For a deletion-only hunk whose deletions sit at the hunk's edge, the post-image is just its context lines — which the unpatched file also contains contiguously. So the hunk is classifiedAlreadyAppliedand skipped. Any trailing context, or any insertion, makes the post-image unambiguous, which is why only this shape was affected. The mirror shape (deletion at the start of the hunk with only trailing context) had the same latent bug.Real-world impact: this is how php-feedstock's
0001-win-iconv-compat.patchbroke under rattler-build ≥ 0.70 (which bumped flickzeug to 0.5.3). The hunk emptyingext/iconv/php_iconv.def(oneEXPORTScontext line followed by deletions to EOF) vanished while the patch's other 5 hunks applied, leaving stale exports inphp8ts.dll.defand anLNK2001on_libiconv_version.The fix
For a hunk with no insertions, a post-image match alone is no longer sufficient evidence: additionally require that its pre-image (context plus deleted lines) is absent from the file — i.e. the deleted lines are actually gone. Hunks with insertions keep the existing behavior, since their post-image contains lines the unpatched file doesn't have.
find_post_image_positionis refactored into a genericfind_lines_positionused for both searches.Testing
Appliedon the pre-image andAlreadyAppliedon the post-image, so already-applied detection still works for these hunks.test_is_diff_applied_pure_deletionandtest_deletion_patch_with_stale_context_is_not_already_appliedfrom fix: detect already-applied diffs via post-image matching #20.🤖 Generated with Claude Code