Skip to content

Commit 609ae12

Browse files
fix(patch): normalize Unicode NFC/NFD differences in apply_patch
When a file contains NFD Unicode (e.g., mac-generated text with combining accents) and the patch uses NFC precomposed characters (or vice versa), the existing seek passes (exact, rstrip, trim, normalizeUnicode) all fail to match. Add a 5th NFC normalization pass that canonicalizes both sides before comparison, so r\u0065\u0301sum\u0065\u0301 (NFD) matches r\u00E9sum\u00E9 (NFC). Closes anomalyco#31651
1 parent 1db5c24 commit 609ae12

2 files changed

Lines changed: 31 additions & 1 deletion

File tree

‎packages/opencode/src/patch/index.ts‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,16 @@ function seekSequence(lines: string[], pattern: string[], startIndex: number, eo
480480
(a, b) => normalizeUnicode(a.trim()) === normalizeUnicode(b.trim()),
481481
eof,
482482
)
483-
return normalized
483+
if (normalized !== -1) return normalized
484+
485+
// Pass 5: NFC normalized (canonical composition, e.g. combining accents)
486+
return tryMatch(
487+
lines,
488+
pattern,
489+
startIndex,
490+
(a, b) => a.trim().normalize("NFC") === b.trim().normalize("NFC"),
491+
eof,
492+
)
484493
}
485494

486495
function generateUnifiedDiff(oldContent: string, newContent: string): string {

‎packages/opencode/test/tool/apply_patch.test.ts‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,4 +526,25 @@ EOF`
526526
expect(yield* readText(target)).toBe(`He said "hi"\nsome${emDash}dash\nend\n`)
527527
}),
528528
)
529+
530+
it.instance("matches with NFC/NFD Unicode differences", () =>
531+
Effect.gen(function* () {
532+
const test = yield* TestInstance
533+
const { ctx } = makeCtx()
534+
const target = path.join(test.directory, "nfc-nfd.txt")
535+
// e with acute accent in NFD form (e + combining acute)
536+
const nfd = "re\u0301sume\u0301"
537+
// Same word in NFC form (single codepoint precomposed)
538+
const nfc = "r\u00E9sum\u00E9"
539+
// File uses NFD form
540+
yield* writeText(target, `before\n${nfd}\nafter\n`)
541+
542+
// Patch uses NFC form — should match via NFC normalized pass
543+
const patchText = `*** Begin Patch\n*** Update File: nfc-nfd.txt\n@@\n-${nfc}\n+caf\u00E9\n*** End Patch`
544+
545+
yield* execute({ patchText }, ctx)
546+
// Result uses the replacement from the patch (NFC)
547+
expect(yield* readText(target)).toBe(`before\ncaf\u00E9\nafter\n`)
548+
}),
549+
)
529550
})

0 commit comments

Comments
 (0)