Skip to content

fix(filesystem): handle Windows EPERM on file rename when target is locked#3632

Closed
ghost wants to merge 1 commit intomainfrom
unknown repository
Closed

fix(filesystem): handle Windows EPERM on file rename when target is locked#3632
ghost wants to merge 1 commit intomainfrom
unknown repository

Conversation

@ghost
Copy link
Copy Markdown

@ghost ghost commented Mar 18, 2026

Problem

On Windows, fs.rename() throws EPERM when the target file is locked by another process (e.g., open in VS Code, Notepad, or any editor). This breaks both write_file and edit_file operations with:

Error: EPERM: operation not permitted, rename 'C:\test.txt.abc123.tmp' -> 'C:\test.txt'

This works fine on Linux and macOS where rename atomically replaces the target regardless of read locks.

Fix

Added an atomicReplace() helper function that:

  1. Tries fs.rename() first (preserves atomic semantics on Linux/macOS)
  2. On Windows (EPERM + process.platform === 'win32'), falls back to fs.copyFile() + fs.unlink() of the temp file

Applied to both write locations in lib.ts:

  • writeFileSecure() (used by write_file tool)
  • applyFileEdits() (used by edit_file tool)

The fallback is only triggered on Windows and only when the specific EPERM error occurs, so behavior on other platforms is unchanged.

Fixes #3199

…ocked

On Windows, fs.rename() throws EPERM when the target file is locked
by another process (e.g., open in VS Code or Notepad). This breaks
both write_file and edit_file operations.

Added an atomicReplace() helper that falls back to copyFile + unlink
on Windows when rename fails with EPERM. This preserves the atomic
write semantics on Linux/macOS while working around the Windows
file locking limitation.

Fixes #3199
@ghost ghost closed this by deleting the head repository Mar 26, 2026
This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

filesystem edit_file: EPERM error when renaming over locked files on Windows

0 participants