Closed
Description
- 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')
const dest = join(tmpdir(), 'dest.txt')
const file = Buffer.allocUnsafe(1024 * 1024) // 1MB
const count = 100
for (let i = 0; i < count; i++) {
writeFile(`${dest}-${i}.tmp`, file, (err) => {
if (err) {
console.error('Could not write')
throw err
}
rename(`${dest}-${i}.tmp`, dest, (err) => {
if (err) {
console.error('Could not rename')
throw err
}
})
})
}
You will see:
C:\Users\me> node test.js
Could not rename
C:\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