on main help sign in
fix(tool): fix `edit` and `multi-edit` tools on windows

We should convert the content to LF to apply the patch while still respecting the original line endings when saving the file on disk. Co-authored-by: theguy000 <istiakm30@gmail.com>

commita3efafc93058aacbec64d5f3ac3328fd71ef1288 authorAndrey Nering <andreynering@users.noreply.github.com> dateMon 11 Aug 2025, 15:39 (1 year ago) parentbb94be2
3 files changed+32−3
internal/fsext/fileutil.go+16 internal/llm/tools/edit.go+11−2 internal/llm/tools/multiedit.go+5−1
internal/fsext/fileutil.go+16
@@ -233,3 +233,19 @@ func HasPrefix(path, prefix string) bool {
233233 // If path is within prefix, Rel will not return a path starting with ".."
234234 return !strings.HasPrefix(rel, "..")
235235}
236+
237+// ToUnixLineEndings converts Windows line endings (CRLF) to Unix line endings (LF).
238+func ToUnixLineEndings(content string) (string, bool) {
239+ if strings.Contains(content, "\r\n") {
240+ return strings.ReplaceAll(content, "\r\n", "\n"), true
241+ }
242+ return content, false
243+}
244+
245+// ToWindowsLineEndings converts Unix line endings (LF) to Windows line endings (CRLF).
246+func ToWindowsLineEndings(content string) (string, bool) {
247+ if !strings.Contains(content, "\r\n") {
248+ return strings.ReplaceAll(content, "\n", "\r\n"), true
249+ }
250+ return content, false
251+}
internal/llm/tools/edit.go+11−2
@@ -99,6 +99,7 @@ WINDOWS NOTES:
9999- File paths should use forward slashes (/) for cross-platform compatibility
100100- On Windows, absolute paths start with drive letters (C:/) but forward slashes work throughout
101101- File permissions are handled automatically by the Go runtime
102+- Always assumes \n for line endings. The tool will handle \r\n conversion automatically if needed.
102103
103104Remember: when making multiple file edits in a row to the same file, you should prefer to send all edits in a single message with multiple calls to this tool, rather than multiple messages with a single call each.`
104105)
@@ -299,7 +300,7 @@ func (e *editTool) deleteContent(ctx context.Context, filePath, oldString string
299300 return ToolResponse{}, fmt.Errorf("failed to read file: %w", err)
300301 }
301302
302− oldContent := string(content)
303+ oldContent, isCrlf := fsext.ToUnixLineEndings(string(content))
303304
304305 var newContent string
305306 var deletionCount int
@@ -356,6 +357,10 @@ func (e *editTool) deleteContent(ctx context.Context, filePath, oldString string
356357 return ToolResponse{}, permission.ErrorPermissionDenied
357358 }
358359
360+ if isCrlf {
361+ newContent, _ = fsext.ToWindowsLineEndings(newContent)
362+ }
363+
359364 err = os.WriteFile(filePath, []byte(newContent), 0o644)
360365 if err != nil {
361366 return ToolResponse{}, fmt.Errorf("failed to write file: %w", err)
@@ -428,7 +433,7 @@ func (e *editTool) replaceContent(ctx context.Context, filePath, oldString, newS
428433 return ToolResponse{}, fmt.Errorf("failed to read file: %w", err)
429434 }
430435
431− oldContent := string(content)
436+ oldContent, isCrlf := fsext.ToUnixLineEndings(string(content))
432437
433438 var newContent string
434439 var replacementCount int
@@ -487,6 +492,10 @@ func (e *editTool) replaceContent(ctx context.Context, filePath, oldString, newS
487492 return ToolResponse{}, permission.ErrorPermissionDenied
488493 }
489494
495+ if isCrlf {
496+ newContent, _ = fsext.ToWindowsLineEndings(newContent)
497+ }
498+
490499 err = os.WriteFile(filePath, []byte(newContent), 0o644)
491500 if err != nil {
492501 return ToolResponse{}, fmt.Errorf("failed to write file: %w", err)
internal/llm/tools/multiedit.go+5−1
@@ -335,7 +335,7 @@ func (m *multiEditTool) processMultiEditExistingFile(ctx context.Context, params
335335 return ToolResponse{}, fmt.Errorf("failed to read file: %w", err)
336336 }
337337
338− oldContent := string(content)
338+ oldContent, isCrlf := fsext.ToUnixLineEndings(string(content))
339339 currentContent := oldContent
340340
341341 // Apply all edits sequentially
@@ -377,6 +377,10 @@ func (m *multiEditTool) processMultiEditExistingFile(ctx context.Context, params
377377 return ToolResponse{}, permission.ErrorPermissionDenied
378378 }
379379
380+ if isCrlf {
381+ currentContent, _ = fsext.ToWindowsLineEndings(currentContent)
382+ }
383+
380384 // Write the updated content
381385 err = os.WriteFile(params.FilePath, []byte(currentContent), 0o644)
382386 if err != nil {