Skip to content

Commit 4939d66

Browse files
author
Your Name
committed
Fix modelcontextprotocol#3199: filesystem edit_file: EPERM error when renaming over locked
1 parent a83b145 commit 4939d66

2 files changed

Lines changed: 65 additions & 57 deletions

File tree

‎src/filesystem/__tests__/lib.test.ts‎

Lines changed: 53 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -422,21 +422,22 @@ describe('Lib Functions', () => {
422422
const edits = [
423423
{ oldText: 'line2', newText: 'modified line2' }
424424
];
425-
426-
mockFs.rename.mockResolvedValueOnce(undefined);
427-
425+
426+
mockFs.cp.mockResolvedValueOnce(undefined);
427+
428428
const result = await applyFileEdits('/test/file.txt', edits, false);
429-
429+
430430
expect(result).toContain('modified line2');
431-
// Should write to temporary file then rename
431+
// Should write to temporary file then copy with force
432432
expect(mockFs.writeFile).toHaveBeenCalledWith(
433433
expect.stringMatching(/\/test\/file\.txt\.[a-f0-9]+\.tmp$/),
434434
'line1\nmodified line2\nline3\n',
435435
'utf-8'
436436
);
437-
expect(mockFs.rename).toHaveBeenCalledWith(
437+
expect(mockFs.cp).toHaveBeenCalledWith(
438438
expect.stringMatching(/\/test\/file\.txt\.[a-f0-9]+\.tmp$/),
439-
'/test/file.txt'
439+
'/test/file.txt',
440+
{ force: true }
440441
);
441442
});
442443

@@ -456,41 +457,43 @@ describe('Lib Functions', () => {
456457
{ oldText: 'line1', newText: 'first line' },
457458
{ oldText: 'line3', newText: 'third line' }
458459
];
459-
460-
mockFs.rename.mockResolvedValueOnce(undefined);
461-
460+
461+
mockFs.cp.mockResolvedValueOnce(undefined);
462+
462463
await applyFileEdits('/test/file.txt', edits, false);
463-
464+
464465
expect(mockFs.writeFile).toHaveBeenCalledWith(
465466
expect.stringMatching(/\/test\/file\.txt\.[a-f0-9]+\.tmp$/),
466467
'first line\nline2\nthird line\n',
467468
'utf-8'
468469
);
469-
expect(mockFs.rename).toHaveBeenCalledWith(
470+
expect(mockFs.cp).toHaveBeenCalledWith(
470471
expect.stringMatching(/\/test\/file\.txt\.[a-f0-9]+\.tmp$/),
471-
'/test/file.txt'
472+
'/test/file.txt',
473+
{ force: true }
472474
);
473475
});
474476

475477
it('handles whitespace-flexible matching', async () => {
476478
mockFs.readFile.mockResolvedValue(' line1\n line2\n line3\n');
477-
479+
478480
const edits = [
479481
{ oldText: 'line2', newText: 'modified line2' }
480482
];
481-
482-
mockFs.rename.mockResolvedValueOnce(undefined);
483-
483+
484+
mockFs.cp.mockResolvedValueOnce(undefined);
485+
484486
await applyFileEdits('/test/file.txt', edits, false);
485-
487+
486488
expect(mockFs.writeFile).toHaveBeenCalledWith(
487489
expect.stringMatching(/\/test\/file\.txt\.[a-f0-9]+\.tmp$/),
488490
' line1\n modified line2\n line3\n',
489491
'utf-8'
490492
);
491-
expect(mockFs.rename).toHaveBeenCalledWith(
493+
expect(mockFs.cp).toHaveBeenCalledWith(
492494
expect.stringMatching(/\/test\/file\.txt\.[a-f0-9]+\.tmp$/),
493-
'/test/file.txt'
495+
'/test/file.txt',
496+
{ force: true }
494497
);
495498
});
496499

@@ -505,73 +508,76 @@ describe('Lib Functions', () => {
505508

506509
it('handles complex multi-line edits with indentation', async () => {
507510
mockFs.readFile.mockResolvedValue('function test() {\n console.log("hello");\n return true;\n}');
508-
511+
509512
const edits = [
510-
{
511-
oldText: ' console.log("hello");\n return true;',
512-
newText: ' console.log("world");\n console.log("test");\n return false;'
513+
{
514+
oldText: ' console.log("hello");\n return true;',
515+
newText: ' console.log("world");\n console.log("test");\n return false;'
513516
}
514517
];
515-
516-
mockFs.rename.mockResolvedValueOnce(undefined);
517-
518+
519+
mockFs.cp.mockResolvedValueOnce(undefined);
520+
518521
await applyFileEdits('/test/file.js', edits, false);
519-
522+
520523
expect(mockFs.writeFile).toHaveBeenCalledWith(
521524
expect.stringMatching(/\/test\/file\.js\.[a-f0-9]+\.tmp$/),
522525
'function test() {\n console.log("world");\n console.log("test");\n return false;\n}',
523526
'utf-8'
524527
);
525-
expect(mockFs.rename).toHaveBeenCalledWith(
528+
expect(mockFs.cp).toHaveBeenCalledWith(
526529
expect.stringMatching(/\/test\/file\.js\.[a-f0-9]+\.tmp$/),
527-
'/test/file.js'
530+
'/test/file.js',
531+
{ force: true }
528532
);
529533
});
530534

531535
it('handles edits with different indentation patterns', async () => {
532536
mockFs.readFile.mockResolvedValue(' if (condition) {\n doSomething();\n }');
533-
537+
534538
const edits = [
535-
{
536-
oldText: 'doSomething();',
537-
newText: 'doSomethingElse();\n doAnotherThing();'
539+
{
540+
oldText: 'doSomething();',
541+
newText: 'doSomethingElse();\n doAnotherThing();'
538542
}
539543
];
540-
541-
mockFs.rename.mockResolvedValueOnce(undefined);
542-
544+
545+
mockFs.cp.mockResolvedValueOnce(undefined);
546+
543547
await applyFileEdits('/test/file.js', edits, false);
544-
548+
545549
expect(mockFs.writeFile).toHaveBeenCalledWith(
546550
expect.stringMatching(/\/test\/file\.js\.[a-f0-9]+\.tmp$/),
547551
' if (condition) {\n doSomethingElse();\n doAnotherThing();\n }',
548552
'utf-8'
549553
);
550-
expect(mockFs.rename).toHaveBeenCalledWith(
554+
expect(mockFs.cp).toHaveBeenCalledWith(
551555
expect.stringMatching(/\/test\/file\.js\.[a-f0-9]+\.tmp$/),
552-
'/test/file.js'
556+
'/test/file.js',
557+
{ force: true }
553558
);
554559
});
555560

556561
it('handles CRLF line endings in file content', async () => {
557562
mockFs.readFile.mockResolvedValue('line1\r\nline2\r\nline3\r\n');
558-
563+
559564
const edits = [
560565
{ oldText: 'line2', newText: 'modified line2' }
561566
];
562-
563-
mockFs.rename.mockResolvedValueOnce(undefined);
564-
567+
568+
mockFs.cp.mockResolvedValueOnce(undefined);
569+
565570
await applyFileEdits('/test/file.txt', edits, false);
566-
571+
567572
expect(mockFs.writeFile).toHaveBeenCalledWith(
568573
expect.stringMatching(/\/test\/file\.txt\.[a-f0-9]+\.tmp$/),
569574
'line1\nmodified line2\nline3\n',
570575
'utf-8'
571576
);
572-
expect(mockFs.rename).toHaveBeenCalledWith(
577+
expect(mockFs.cp).toHaveBeenCalledWith(
573578
expect.stringMatching(/\/test\/file\.txt\.[a-f0-9]+\.tmp$/),
574-
'/test/file.txt'
579+
'/test/file.txt',
580+
{ force: true }
575581
);
576582
});
577583
});

‎src/filesystem/lib.ts‎

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -165,18 +165,19 @@ export async function writeFileContent(filePath: string, content: string): Promi
165165
await fs.writeFile(filePath, content, { encoding: "utf-8", flag: 'wx' });
166166
} catch (error) {
167167
if ((error as NodeJS.ErrnoException).code === 'EEXIST') {
168-
// Security: Use atomic rename to prevent race conditions where symlinks
169-
// could be created between validation and write. Rename operations
170-
// replace the target file atomically and don't follow symlinks.
168+
// Security: Use cp with force option to prevent race conditions where symlinks
169+
// could be created between validation and write. cp operations replace the target
170+
// file atomically and don't follow symlinks. On Windows, cp handles file locking
171+
// better than rename.
171172
const tempPath = `${filePath}.${randomBytes(16).toString('hex')}.tmp`;
172173
try {
173174
await fs.writeFile(tempPath, content, 'utf-8');
174-
await fs.rename(tempPath, filePath);
175-
} catch (renameError) {
175+
await fs.cp(tempPath, filePath, { force: true });
176+
} catch (cpError) {
176177
try {
177178
await fs.unlink(tempPath);
178179
} catch {}
179-
throw renameError;
180+
throw cpError;
180181
}
181182
} else {
182183
throw error;
@@ -263,13 +264,14 @@ export async function applyFileEdits(
263264
const formattedDiff = `${'`'.repeat(numBackticks)}diff\n${diff}${'`'.repeat(numBackticks)}\n\n`;
264265

265266
if (!dryRun) {
266-
// Security: Use atomic rename to prevent race conditions where symlinks
267-
// could be created between validation and write. Rename operations
268-
// replace the target file atomically and don't follow symlinks.
267+
// Security: Use cp with force option to prevent race conditions where symlinks
268+
// could be created between validation and write. cp operations replace the target
269+
// file atomically and don't follow symlinks. On Windows, cp handles file locking
270+
// better than rename.
269271
const tempPath = `${filePath}.${randomBytes(16).toString('hex')}.tmp`;
270272
try {
271273
await fs.writeFile(tempPath, modifiedContent, 'utf-8');
272-
await fs.rename(tempPath, filePath);
274+
await fs.cp(tempPath, filePath, { force: true });
273275
} catch (error) {
274276
try {
275277
await fs.unlink(tempPath);

0 commit comments

Comments
 (0)