Skip to content

fix: preserve literal dollars in filesystem edits#4236

Closed
he-yufeng wants to merge 1 commit into
modelcontextprotocol:mainfrom
he-yufeng:fix/filesystem-literal-dollar-edits
Closed

fix: preserve literal dollars in filesystem edits#4236
he-yufeng wants to merge 1 commit into
modelcontextprotocol:mainfrom
he-yufeng:fix/filesystem-literal-dollar-edits

Conversation

@he-yufeng

Copy link
Copy Markdown

Summary

edit_file currently passes newText as the string replacement argument to String.prototype.replace. That makes literal $ sequences behave like replacement patterns, so values such as $$100 and $& are silently rewritten before the file is saved.

This switches the exact-match path to the callback form of replace, which preserves the existing first-match semantics while treating the replacement as literal text.

Changes

  • Use modifiedContent.replace(normalizedOld, () => normalizedNew) for exact text edits.
  • Add a regression test covering $$ and $& in replacement text.

To verify

  • npm exec -w src/filesystem -- vitest run __tests__/lib.test.ts --testNamePattern "dollar signs"
  • npm exec -w src/filesystem -- vitest run __tests__/lib.test.ts
  • npm run build -w src/filesystem
  • git diff --check origin/main...HEAD

Fixes #4157

@olaservo

Copy link
Copy Markdown
Member

Thanks for the PR! We ended up going with #4225 as the canonical fix for #4157. Closing this as a duplicate. Thank you for taking the time to submit this.

@olaservo olaservo closed this May 30, 2026
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.

filesystem: edit_file newText interpreted as String.prototype.replace replacement-pattern (literal $ corrupted)

2 participants