Skip to content

fs: support removing read-only files in rmSync on Windows#64453

Open
SparshGarg999 wants to merge 1 commit into
nodejs:mainfrom
SparshGarg999:fix/fs-rmSync-readonly-win
Open

fs: support removing read-only files in rmSync on Windows#64453
SparshGarg999 wants to merge 1 commit into
nodejs:mainfrom
SparshGarg999:fix/fs-rmSync-readonly-win

Conversation

@SparshGarg999

Copy link
Copy Markdown

Summary

Ensure fs.rmSync can delete read-only files and directories containing them on Windows when Node is built with clang libc++ (e.g. inside Electron).

Description

Since the migration to std::filesystem::remove and std::filesystem::remove_all on Windows, Node's fs.rmSync behaves differently depending on whether it was built with MSVC STL or clang libc++.

MSVC STL's implementation of std::filesystem::remove automatically clears the read-only attribute of a file before attempting to delete it. However, libc++'s implementation on Windows does not clear the read-only attribute, which causes the deletion to fail with operation_not_permitted (EPERM). This issue directly impacts downstream platforms like Electron that compile Node.js with libc++ on Windows (see electron/electron#52253).

This commit introduces a Windows-specific helper ClearReadOnlyAttributeW in src/node_file.cc that is called if operation_not_permitted is encountered. It manually clears the FILE_ATTRIBUTE_READONLY attribute from the file (or recursively from files inside a directory) and retries the removal operation, bringing libc++ builds to parity with MSVC STL builds.

Verification

Added a new test in test/parallel/test-fs-rm.js that makes files read-only using fs.chmodSync(file, 0o444) (which sets the file attribute on Windows) and validates that fs.rmSync can delete them both directly and recursively as part of a directory.

Fixes: #64374

@nodejs-github-bot nodejs-github-bot added c++ Issues and PRs that require attention from people who are familiar with C++. fs Issues and PRs related to the fs subsystem / file system. needs-ci PRs that need a full CI run. labels Jul 12, 2026
On Windows, libc++ std::filesystem::remove and remove_all do not
automatically clear the read-only attribute before deleting a file
(unlike MSVC STL). This causes s.rmSync to fail with EPERM`n(operation_not_permitted) when trying to remove read-only files
in environments where Node.js is built using clang libc++ (such as Electron).

This commit introduces a Windows-specific helper ClearReadOnlyAttributeW`nwhich clears the FILE_ATTRIBUTE_READONLY attribute recursively
(or for a single file) when operation_not_permitted is returned,
allowing 
mSync to successfully delete read-only files/folders.

Fixes: nodejs#64374

Signed-off-by: SparshGarg999 <sparshgarg999@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

c++ Issues and PRs that require attention from people who are familiar with C++. fs Issues and PRs related to the fs subsystem / file system. needs-ci PRs that need a full CI run.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fs.rmSync cannot remove read-only files on Windows when Node is built with libc++

2 participants