Skip to content

EPERM when renaming files on Windows #29481

Description

@achingbrain
  • Version: 10.16.3
  • Platform: Windows 10
  • Subsystem: fs

The docs for fs.rename(oldPath, newPath, callback) say:

In the case that newPath already exists, it will be overwritten.

This happens on Linux/Mac OS X but doesn't appear to happen with Windows. If you run the following script:

const{ tmpdir }=require('os')const{ join }=require('path')const{ writeFile, rename }=require('fs')constdest=join(tmpdir(),'dest.txt')constfile=Buffer.allocUnsafe(1024*1024)// 1MBconstcount=100for(leti=0;i<count;i++){writeFile(`${dest}-${i}.tmp`,file,(err)=>{if(err){console.error('Could not write')throwerr}rename(`${dest}-${i}.tmp`,dest,(err)=>{if(err){console.error('Could not rename')throwerr}})})}

You will see:

C:\Users\me> node test.jsCould not renameC:\Users\me\test.js:21 throw err ^Error: EPERM: operation not permitted, rename 'C:\Users\me\AppData\Local\Temp\dest.txt-0.tmp' -> 'C:\Users\me\AppData\Local\Temp\dest.txt'

The same script runs to completion on Linux & Mac OS X without an error.

Setting count to a low value (e.g. 2) doesn't trigger the bug for me, so if it works for you on Windows, your computer is likely faster than mine - try setting it to something unreasonable like 1000.

This could be what was happening in #6335 and appears at least tangentially related to #21957

Metadata

Metadata

Assignees

No one assigned

    Labels

    fsIssues and PRs related to file-system APIs and the fs module.windowsIssues and PRs related to the Windows platform.wontfixIssues that will not be fixed.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions