-
Notifications
You must be signed in to change notification settings - Fork 193
fix(tools): preserve CRLF line endings and UTF-8 BOM on write_file overwrite #1064
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| package tools | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "context" | ||
| "fmt" | ||
| "os" | ||
|
|
@@ -117,6 +118,10 @@ func (tool writeFileTool) RunWithOptions(ctx context.Context, args map[string]an | |
| if priorContentKnown { | ||
| expectedContent = &priorContent | ||
| } | ||
| if existed && priorContentKnown { | ||
| hasBOM, isCRLF := detectLineEndingAndBOM([]byte(priorContent)) | ||
| content = normalizeContent(content, hasBOM, isCRLF) | ||
| } | ||
| if err := commitFileContents(absolutePath, priorInfo, expectedContent, content); err != nil { | ||
| return errorResult("Error writing file " + relativePath + ": " + err.Error()) | ||
| } | ||
|
|
@@ -195,3 +200,30 @@ func (tool writeFileTool) RunWithOptions(ctx context.Context, args map[string]an | |
| func fileContentArg(args map[string]any) (string, error) { | ||
| return aliasedStringArg(args, []string{"content", "contents", "text", "body", "data", "file_content"}, "", true, true) | ||
| } | ||
|
|
||
| func detectLineEndingAndBOM(data []byte) (hasBOM bool, crlf bool) { | ||
| if len(data) >= 3 && data[0] == 0xEF && data[1] == 0xBB && data[2] == 0xBF { | ||
| hasBOM = true | ||
| data = data[3:] | ||
| } | ||
| if bytes.IndexByte(data, 0) != -1 { | ||
| return hasBOM, false | ||
| } | ||
| scanLen := len(data) | ||
| if scanLen > 4096 { | ||
| scanLen = 4096 | ||
| } | ||
| crlf = bytes.Contains(data[:scanLen], []byte("\r\n")) | ||
|
Comment on lines
+212
to
+216
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win Scan all prior text before selecting CRLF output. When the first CRLF sequence occurs after byte 4096, this helper reports Based on learnings: new edge-case logic needs focused regression coverage. 🤖 Prompt for AI AgentsSource: Learnings |
||
| return hasBOM, crlf | ||
| } | ||
|
|
||
| func normalizeContent(content string, preserveBOM bool, preserveCRLF bool) string { | ||
| if preserveCRLF { | ||
| content = strings.ReplaceAll(content, "\r\n", "\n") | ||
| content = strings.ReplaceAll(content, "\n", "\r\n") | ||
| } | ||
| if preserveBOM && !strings.HasPrefix(content, "\xef\xbb\xbf") { | ||
| content = "\xef\xbb\xbf" + content | ||
| } | ||
| return content | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.