Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/core/tools/SearchAndReplaceTool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ export class SearchAndReplaceTool extends BaseTool<"search_and_replace"> {
let fileContent: string
try {
fileContent = await fs.readFile(absolutePath, "utf8")
// Normalize line endings to LF for consistent matching
fileContent = fileContent.replace(/\r\n/g, "\n")
} catch (error) {
task.consecutiveMistakeCount++
task.recordToolError("search_and_replace")
Expand All @@ -125,7 +127,9 @@ export class SearchAndReplaceTool extends BaseTool<"search_and_replace"> {
const errors: string[] = []

for (let i = 0; i < operations.length; i++) {
const { search, replace } = operations[i]
// Normalize line endings in search/replace strings to match file content
const search = operations[i].search.replace(/\r\n/g, "\n")
const replace = operations[i].replace.replace(/\r\n/g, "\n")
const searchPattern = new RegExp(escapeRegExp(search), "g")

const matchCount = newContent.match(searchPattern)?.length ?? 0
Expand Down
10 changes: 8 additions & 2 deletions src/core/tools/SearchReplaceTool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ export class SearchReplaceTool extends BaseTool<"search_replace"> {
let fileContent: string
try {
fileContent = await fs.readFile(absolutePath, "utf8")
// Normalize line endings to LF for consistent matching
fileContent = fileContent.replace(/\r\n/g, "\n")
} catch (error) {
task.consecutiveMistakeCount++
task.recordToolError("search_replace")
Expand All @@ -114,8 +116,12 @@ export class SearchReplaceTool extends BaseTool<"search_replace"> {
return
}

// Normalize line endings in search/replace strings to match file content
const normalizedOldString = old_string.replace(/\r\n/g, "\n")
const normalizedNewString = new_string.replace(/\r\n/g, "\n")

// Check for exact match (literal string, not regex)
const matchCount = fileContent.split(old_string).length - 1
const matchCount = fileContent.split(normalizedOldString).length - 1

if (matchCount === 0) {
task.consecutiveMistakeCount++
Expand All @@ -142,7 +148,7 @@ export class SearchReplaceTool extends BaseTool<"search_replace"> {
}

// Apply the single replacement
const newContent = fileContent.replace(old_string, new_string)
const newContent = fileContent.replace(normalizedOldString, normalizedNewString)

// Check if any changes were made
if (newContent === fileContent) {
Expand Down
Loading
Loading