Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions src/filesystem/__tests__/bug_repro.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@

import { describe, it, expect, beforeEach, afterEach } from 'vitest';
import * as fs from 'fs/promises';
import * as path from 'path';
import { applyFileEdits } from '../lib.js';

describe('Bug #4157 Reproduction', () => {
const testFile = path.join(process.cwd(), 'test_repro.txt');

beforeEach(async () => {
await fs.writeFile(testFile, 'Value: OLD_VALUE\n');
});

afterEach(async () => {
try {
await fs.unlink(testFile);
} catch {}
});

it('should preserve literal $ characters in newText', async () => {
const edits = [
{
oldText: 'OLD_VALUE',
newText: '$100'
}
];

await applyFileEdits(testFile, edits, false);
const result = await fs.readFile(testFile, 'utf-8');
expect(result).toContain('$100');
});

it('should preserve complex $ patterns in newText', async () => {
const edits = [
{
oldText: 'OLD_VALUE',
newText: '$& $1 $` $\''
}
];

await applyFileEdits(testFile, edits, false);
const result = await fs.readFile(testFile, 'utf-8');
expect(result).toContain('$& $1 $` $\'');
});
});
2 changes: 1 addition & 1 deletion src/filesystem/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ export async function applyFileEdits(

// If exact match exists, use it
if (modifiedContent.includes(normalizedOld)) {
modifiedContent = modifiedContent.replace(normalizedOld, normalizedNew);
modifiedContent = modifiedContent.replace(normalizedOld, () => normalizedNew);
continue;
}

Expand Down
37 changes: 37 additions & 0 deletions src/filesystem/reproduce_bug_4157.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@

import * as fs from 'fs/promises';
import * as path from 'path';
import { applyFileEdits } from './lib.js';

async function reproduce() {
const testFile = path.join(process.cwd(), 'test_dollar.txt');
const initialContent = 'Value: OLD_VALUE\n';
await fs.writeFile(testFile, initialContent);

console.log('Initial content:', initialContent);

const edits = [
{
oldText: 'OLD_VALUE',
newText: '$100'
}
];

try {
await applyFileEdits(testFile, edits, false);
const result = await fs.readFile(testFile, 'utf-8');
console.log('Resulting content:', result);

if (result.includes('$100')) {
console.log('SUCCESS: $100 preserved');
} else {
console.log('FAILURE: $100 corrupted. Found:', result);
}
} catch (error) {
console.error('Error during reproduction:', error);
} finally {
await fs.unlink(testFile);
}
}

reproduce();
Loading