Normalize \r\n to \n for file content in LocalFilesystem.edit()#1945
Open
markyuclaw wants to merge 2 commits into
Open
Normalize \r\n to \n for file content in LocalFilesystem.edit()#1945markyuclaw wants to merge 2 commits into
markyuclaw wants to merge 2 commits into
Conversation
Contributor
|
Good fix! Normalizing \r\n to \n before string matching prevents edit failures on Windows. The approach of normalizing both the file content and the search strings is consistent. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
AgentScope-Java Version
2.0.0-RC4
Description
Problem
In LocalFilesystem.edit(), the oldString and newString parameters are normalized from \r\n to \n before performing string replacement, but the file content itself is not normalized. This causes edit() to silently fail (return 0 occurrences) on files with Windows-style (\r\n) line endings, because the replacement target uses \n while the actual content still contains \r\n.
Fix
Apply the same \r\n → \n normalization to the file content read from disk, ensuring consistent line-ending handling across all three strings (content, oldString, newString).
Change
LocalFilesystem.java:362 — chain .replace("\r\n", "\n").replace("\r", "\n") onto the Files.readString() call so that content is normalized identically to oldString and newString.
// Before
String content = Files.readString(resolved, StandardCharsets.UTF_8);
String normalizedOld = oldString.replace("\r\n", "\n").replace("\r", "\n");
String normalizedNew = newString.replace("\r\n", "\n").replace("\r", "\n");
// After
String content = Files.readString(resolved, StandardCharsets.UTF_8)
.replace("\r\n", "\n")
.replace("\r", "\n");
String normalizedOld = oldString.replace("\r\n", "\n").replace("\r", "\n");
String normalizedNew = newString.replace("\r\n", "\n").replace("\r", "\n");
Why This Matters
Cross-platform correctness: files created on Windows often have \r\n endings.
Without this fix, edit() returns "String not found" or 0 occurrences on such files even when the search string is correct.
The read() method already handles this implicitly via content.split("\n", -1), but edit() relied on exact substring matching and therefore needs explicit normalization.
The modified code at LocalFilesystem.java:362 now reads:
String content = Files.readString(resolved, StandardCharsets.UTF_8)
.replace("\r\n", "\n")
.replace("\r", "\n");
Checklist
Please check the following items before code is ready to be reviewed.
mvn spotless:applymvn test)