@@ -308,6 +308,61 @@ describe('Lib Functions', () => {
308308
309309 expect ( mockFs . writeFile ) . toHaveBeenCalledWith ( '/test/file.txt' , 'new content' , { encoding : "utf-8" , flag : 'wx' } ) ;
310310 } ) ;
311+
312+ it ( 'falls back to fs.cp when fs.rename fails with EPERM' , async ( ) => {
313+ const eexistError = new Error ( 'EEXIST' ) as NodeJS . ErrnoException ;
314+ eexistError . code = 'EEXIST' ;
315+ const epermError = new Error ( 'EPERM' ) as NodeJS . ErrnoException ;
316+ epermError . code = 'EPERM' ;
317+
318+ mockFs . writeFile
319+ . mockRejectedValueOnce ( eexistError ) // First write fails (file exists)
320+ . mockResolvedValueOnce ( undefined ) ; // Temp file write succeeds
321+ mockFs . rename . mockRejectedValueOnce ( epermError ) ; // Rename fails (locked)
322+ mockFs . cp . mockResolvedValueOnce ( undefined ) ; // cp succeeds
323+ mockFs . unlink . mockResolvedValueOnce ( undefined ) ; // Temp cleanup succeeds
324+
325+ await writeFileContent ( '/test/file.txt' , 'new content' ) ;
326+
327+ expect ( mockFs . rename ) . toHaveBeenCalledWith (
328+ expect . stringMatching ( / \/ t e s t \/ f i l e \. t x t \. [ a - f 0 - 9 ] + \. t m p $ / ) ,
329+ '/test/file.txt'
330+ ) ;
331+ expect ( mockFs . cp ) . toHaveBeenCalledWith (
332+ expect . stringMatching ( / \/ t e s t \/ f i l e \. t x t \. [ a - f 0 - 9 ] + \. t m p $ / ) ,
333+ '/test/file.txt' ,
334+ { force : true }
335+ ) ;
336+ expect ( mockFs . unlink ) . toHaveBeenCalledWith (
337+ expect . stringMatching ( / \/ t e s t \/ f i l e \. t x t \. [ a - f 0 - 9 ] + \. t m p $ / )
338+ ) ;
339+ } ) ;
340+
341+ it ( 'succeeds when fs.cp works but temp file unlink fails' , async ( ) => {
342+ const eexistError = new Error ( 'EEXIST' ) as NodeJS . ErrnoException ;
343+ eexistError . code = 'EEXIST' ;
344+ const epermError = new Error ( 'EPERM' ) as NodeJS . ErrnoException ;
345+ epermError . code = 'EPERM' ;
346+ const ebusyError = new Error ( 'EBUSY' ) as NodeJS . ErrnoException ;
347+ ebusyError . code = 'EBUSY' ;
348+
349+ mockFs . writeFile
350+ . mockRejectedValueOnce ( eexistError )
351+ . mockResolvedValueOnce ( undefined ) ;
352+ mockFs . rename . mockRejectedValueOnce ( epermError ) ;
353+ mockFs . cp . mockResolvedValueOnce ( undefined ) ;
354+ mockFs . unlink . mockRejectedValueOnce ( ebusyError ) ; // Temp cleanup fails (e.g. antivirus)
355+
356+ // Should NOT throw — the target file was written successfully
357+ await expect ( writeFileContent ( '/test/file.txt' , 'new content' ) )
358+ . resolves . toBeUndefined ( ) ;
359+
360+ expect ( mockFs . cp ) . toHaveBeenCalledWith (
361+ expect . stringMatching ( / \/ t e s t \/ f i l e \. t x t \. [ a - f 0 - 9 ] + \. t m p $ / ) ,
362+ '/test/file.txt' ,
363+ { force : true }
364+ ) ;
365+ } ) ;
311366 } ) ;
312367
313368 } ) ;
@@ -553,6 +608,61 @@ describe('Lib Functions', () => {
553608 ) ;
554609 } ) ;
555610
611+ it ( 'falls back to fs.cp when fs.rename fails with EPERM during file edit' , async ( ) => {
612+ const epermError = new Error ( 'EPERM' ) as NodeJS . ErrnoException ;
613+ epermError . code = 'EPERM' ;
614+
615+ mockFs . readFile . mockResolvedValue ( 'line1\nline2\nline3\n' ) ;
616+ mockFs . writeFile . mockResolvedValue ( undefined ) ;
617+ mockFs . rename . mockRejectedValueOnce ( epermError ) ;
618+ mockFs . cp . mockResolvedValueOnce ( undefined ) ;
619+ mockFs . unlink . mockResolvedValueOnce ( undefined ) ;
620+
621+ const edits = [ { oldText : 'line2' , newText : 'modified line2' } ] ;
622+ const result = await applyFileEdits ( '/test/file.txt' , edits , false ) ;
623+
624+ // Should have tried rename first, then fallen back to cp + unlink
625+ expect ( mockFs . rename ) . toHaveBeenCalledWith (
626+ expect . stringMatching ( / \/ t e s t \/ f i l e \. t x t \. [ a - f 0 - 9 ] + \. t m p $ / ) ,
627+ '/test/file.txt'
628+ ) ;
629+ expect ( mockFs . cp ) . toHaveBeenCalledWith (
630+ expect . stringMatching ( / \/ t e s t \/ f i l e \. t x t \. [ a - f 0 - 9 ] + \. t m p $ / ) ,
631+ '/test/file.txt' ,
632+ { force : true }
633+ ) ;
634+ expect ( mockFs . unlink ) . toHaveBeenCalledWith (
635+ expect . stringMatching ( / \/ t e s t \/ f i l e \. t x t \. [ a - f 0 - 9 ] + \. t m p $ / )
636+ ) ;
637+ // Edit should still produce a valid diff
638+ expect ( result ) . toContain ( 'modified line2' ) ;
639+ } ) ;
640+
641+ it ( 'succeeds when fs.cp works but temp file unlink fails during file edit' , async ( ) => {
642+ const epermError = new Error ( 'EPERM' ) as NodeJS . ErrnoException ;
643+ epermError . code = 'EPERM' ;
644+ const ebusyError = new Error ( 'EBUSY' ) as NodeJS . ErrnoException ;
645+ ebusyError . code = 'EBUSY' ;
646+
647+ mockFs . readFile . mockResolvedValue ( 'line1\nline2\nline3\n' ) ;
648+ mockFs . writeFile . mockResolvedValue ( undefined ) ;
649+ mockFs . rename . mockRejectedValueOnce ( epermError ) ;
650+ mockFs . cp . mockResolvedValueOnce ( undefined ) ;
651+ mockFs . unlink . mockRejectedValueOnce ( ebusyError ) ; // Temp cleanup fails
652+
653+ const edits = [ { oldText : 'line2' , newText : 'modified line2' } ] ;
654+
655+ // Should NOT throw — the target file was written successfully
656+ const result = await applyFileEdits ( '/test/file.txt' , edits , false ) ;
657+
658+ expect ( result ) . toContain ( 'modified line2' ) ;
659+ expect ( mockFs . cp ) . toHaveBeenCalledWith (
660+ expect . stringMatching ( / \/ t e s t \/ f i l e \. t x t \. [ a - f 0 - 9 ] + \. t m p $ / ) ,
661+ '/test/file.txt' ,
662+ { force : true }
663+ ) ;
664+ } ) ;
665+
556666 it ( 'handles CRLF line endings in file content' , async ( ) => {
557667 mockFs . readFile . mockResolvedValue ( 'line1\r\nline2\r\nline3\r\n' ) ;
558668
0 commit comments