Skip to content

Commit 26833aa

Browse files
committed
fix bug with str replace on windows newlines
1 parent 7310075 commit 26833aa

File tree

2 files changed

+12
-12
lines changed

2 files changed

+12
-12
lines changed

backend/src/__tests__/process-str-replace.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ describe('processStrReplace', () => {
2626

2727
it('should handle Windows line endings', async () => {
2828
const initialContent = 'const x = 1;\r\nconst y = 2;\r\n'
29-
const oldStr = 'const y = 2;'
30-
const newStr = 'const y = 3;'
29+
const oldStr = 'const y = 2;\r\n'
30+
const newStr = 'const y = 3;\r\n'
3131

3232
const result = await processStrReplace(
3333
'test.ts',

backend/src/process-str-replace.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ import { createPatch } from 'diff'
33

44
import { tryToDoStringReplacementWithExtraIndentation } from './generate-diffs-prompt'
55

6+
function normalizeLineEndings(str: string): string {
7+
return str.replace(/\r\n/g, '\n')
8+
}
9+
610
export async function processStrReplace(
711
path: string,
812
replacements: { old: string; new: string; allowMultiple: boolean }[],
@@ -29,7 +33,6 @@ export async function processStrReplace(
2933

3034
// Process each old/new string pair
3135
let currentContent = initialContent
32-
let allPatches: string[] = []
3336
let messages: string[] = []
3437
const lineEnding = currentContent.includes('\r\n') ? '\r\n' : '\n'
3538

@@ -42,14 +45,14 @@ export async function processStrReplace(
4245
continue
4346
}
4447

45-
const normalizeLineEndings = (str: string) => str.replace(/\r\n/g, '\n')
4648
const normalizedCurrentContent = normalizeLineEndings(currentContent)
4749
const normalizedOldStr = normalizeLineEndings(oldStr)
50+
const normalizedNewStr = normalizeLineEndings(newStr)
4851

4952
const match = tryMatchOldStr(
5053
normalizedCurrentContent,
5154
normalizedOldStr,
52-
newStr,
55+
normalizedNewStr,
5356
allowMultiple,
5457
)
5558
let updatedOldStr: string | null
@@ -61,15 +64,14 @@ export async function processStrReplace(
6164
updatedOldStr = null
6265
}
6366

64-
const updatedContent =
67+
currentContent =
6568
updatedOldStr === null
6669
? normalizedCurrentContent
67-
: normalizedCurrentContent.replaceAll(updatedOldStr, newStr)
68-
69-
// Update current content for next iteration
70-
currentContent = updatedContent.replaceAll('\n', lineEnding)
70+
: normalizedCurrentContent.replaceAll(updatedOldStr, normalizedNewStr)
7171
}
7272

73+
currentContent = currentContent.replaceAll('\n', lineEnding)
74+
7375
if (initialContent === currentContent) {
7476
logger.debug(
7577
{
@@ -91,8 +93,6 @@ export async function processStrReplace(
9193
const hunkStartIndex = lines.findIndex((line) => line.startsWith('@@'))
9294
if (hunkStartIndex !== -1) {
9395
patch = lines.slice(hunkStartIndex).join('\n')
94-
patch = patch.replaceAll('\n', lineEnding)
95-
allPatches.push(patch)
9696
}
9797
const finalPatch = patch
9898

0 commit comments

Comments
 (0)