Skip to content

Normalize \r\n to \n for file content in LocalFilesystem.edit()#1945

Open
markyuclaw wants to merge 2 commits into
agentscope-ai:mainfrom
markyuclaw:fix-bug-on-2RC3
Open

Normalize \r\n to \n for file content in LocalFilesystem.edit()#1945
markyuclaw wants to merge 2 commits into
agentscope-ai:mainfrom
markyuclaw:fix-bug-on-2RC3

Conversation

@markyuclaw

Copy link
Copy Markdown

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.

  • Code has been formatted with mvn spotless:apply
  • All tests are passing (mvn test)
  • Javadoc comments are complete and follow project conventions
  • Related documentation has been updated (e.g. links, examples, etc.)
  • Code is ready for review

@markyuclaw markyuclaw requested a review from a team June 27, 2026 06:53
@CLAassistant

CLAassistant commented Jun 27, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@itxaiohanglover

Copy link
Copy Markdown
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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants