fix(filesystem): preserve literal \$ in edit_file newText (closes #4157)#4224
Open
adityachilka1 wants to merge 1 commit into
Open
fix(filesystem): preserve literal \$ in edit_file newText (closes #4157)#4224adityachilka1 wants to merge 1 commit into
adityachilka1 wants to merge 1 commit into
Conversation
…lcontextprotocol#4157) The prior code passed `normalizedNew` as String.prototype.replace's replacement argument. JS interprets `$$`, `$&`, `$'`, `$``, and `$n` as replacement patterns in that form, so any actual `$` characters in the user's newText - price tags, regex backreferences in docs, shell examples - get expanded or dropped. The callback form (`replace(..., () => string)`) disables pattern interpretation, so the string is written verbatim. Two regression tests pin the callback form. Closes modelcontextprotocol#4157.
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.
Fixes #4157.
This is the exact fix the issue reporter proposed (and is consistent with the
bug root-cause in the issue body).
The bug
src/filesystem/lib.tspassesnormalizedNewasString.prototype.replace'sreplacement argument. When that argument is a string, JS interprets these
tokens in it:
$$$$&$'$nSo when a caller does
edit_filewithnewText: "$100 USD", the$1tokengets eaten (replaced with the content of capture group 1, which is undefined
for a plain-string search, so it's dropped).
The fix
Use the callback form (
() => normalizedNew). MDN explicitly documentsthis as the way to disable replacement-pattern interpretation:
One-line diff:
Tests
Two regression tests added to
src/filesystem/__tests__/lib.test.ts:$$preservation (literal$)$&,$``,$'` preservation (replacement-pattern tokens)Both tests assert that the exact input string is written verbatim. Without
the fix, both tests fail.
Verification
-> 148 / 148 tests pass across 7 test files (the 2 new tests are in there).
No behavioral regression
The callback always returns a constant (
{n normalizedNew{n unbound}), so forinputs that don't contain
$, the behavior is identical to the string-arg form.The only change is that
$characters now survive the replace.Closes #4157.