Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@rushstack/node-core-library",
"comment": "Retry `FileSystem` folder deletion operations on Windows only for errors reported as `EBUSY`, `ENOTEMPTY` or `EPERM`, with a shared budget of three retries and delays of 100, 200 and 300 ms. Fix `FileSystem.ensureEmptyFolder` and `FileSystem.ensureEmptyFolderAsync` compatibility with Node.js 20.9 and unbound calls, and ensure async child deletions settle before retrying or rejecting. `FileSystem.ensureEmptyFolderAsync` deletes at most 10 top-level children concurrently; `FileSystem.ensureEmptyFolder` and `FileSystem.ensureEmptyFolderAsync` throw on folder read errors other than `ENOENT`.",

@octogonz Pete Gonzalez (octogonz) Nov 26, 2026 •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is summarizing the implementation details. Rewrite to concisely say what the point of this change is, and who's impacted.

"type": "patch"
}
],
"packageName": "@rushstack/node-core-library"
}
169 changes: 128 additions & 41 deletions libraries/node-core-library/src/FileSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -378,18 +378,12 @@ const DELETE_FILE_DEFAULT_OPTIONS: Partial<IFileSystemDeleteFileOptions> = {
throwIfNotExists: false
};

/**
* Default options for fs.rm / fsPromises.rm calls in this file.
*
* @remarks
* The retry settings mitigate transient EPERM/EBUSY/ENOTEMPTY errors on
* Windows (e.g. AV scanners holding a lock right after a delete).
*/
const FS_RM_DEFAULT_OPTIONS: Partial<fs.RmOptions> = {
recursive: true,
force: true,
maxRetries: 3,
retryDelay: 100
// Own the retry budget for the whole operation instead of letting Node retry each recursive entry.
maxRetries: 0,
retryDelay: 0
};

/**
Expand Down Expand Up @@ -740,20 +734,29 @@ export class FileSystem {
* Behind the scenes it uses `fs.rmSync()`.
* @remarks
* Does not throw if the folderPath does not exist.
*
* On Windows, if the operation fails with an error reported as `EBUSY`, `ENOTEMPTY` or `EPERM`,
* it is retried up to three times, with delays of 100, 200 and 300 milliseconds between attempts.
* @param folderPath - The absolute or relative path to the folder which should be deleted.
*/
public static deleteFolder(folderPath: string): void {
_wrapException(() => {
fs.rmSync(folderPath, FS_RM_DEFAULT_OPTIONS);
_retryFolderDeletion(() => {
fs.rmSync(folderPath, FS_RM_DEFAULT_OPTIONS);
});
});
}

/**
* An async version of {@link FileSystem.deleteFolder}.
*
* @remarks
* On Windows, if the operation fails with an error reported as `EBUSY`, `ENOTEMPTY` or `EPERM`,
* it is retried up to three times, with delays of 100, 200 and 300 milliseconds between attempts.
*/
public static async deleteFolderAsync(folderPath: string): Promise<void> {
await _wrapExceptionAsync(() => {
return fsPromises.rm(folderPath, FS_RM_DEFAULT_OPTIONS);
return _retryFolderDeletionAsync(() => fsPromises.rm(folderPath, FS_RM_DEFAULT_OPTIONS));
});
}

Expand All @@ -763,50 +766,90 @@ export class FileSystem {
* @remarks
* This is a workaround for a common race condition, where the virus scanner holds a lock on the folder
* for a brief period after it was deleted, causing EBUSY errors for any code that tries to recreate the folder.
*
* On Windows, if the operation fails with an error reported as `EBUSY`, `ENOTEMPTY` or `EPERM`,
* it is retried up to three times, with delays of 100, 200 and 300 milliseconds between attempts.
*
* Throws on folder read errors other than `ENOENT`.
* @param folderPath - The absolute or relative path to the folder which should have its contents deleted.
*/
public static ensureEmptyFolder(folderPath: string): void {
_wrapException(() => {
let items: FolderItem[];
try {
items = this.readFolderItems(folderPath);
} catch (error) {
if (!FileSystem.isNotExistError(error as Error)) {
throw error;
_retryFolderDeletion(() => {
let names: string[];
try {
names = fsx.readdirSync(folderPath);
} catch (error) {
if (!FileSystem.isFileDoesNotExistError(error as Error)) {
throw error;
}

fsx.ensureDirSync(folderPath);
return;
}
fsx.ensureDirSync(folderPath);
return;
}
for (const item of items) {
const itemPath: string = nodeJsPath.join(item.parentPath, item.name);
fs.rmSync(itemPath, FS_RM_DEFAULT_OPTIONS);
}

for (const name of names) {
fs.rmSync(nodeJsPath.join(folderPath, name), FS_RM_DEFAULT_OPTIONS);
}
});
});
}

/**
* An async version of {@link FileSystem.ensureEmptyFolder}.
*
* @remarks
* On Windows, if the operation fails with an error reported as `EBUSY`, `ENOTEMPTY` or `EPERM`,
* it is retried up to three times, with delays of 100, 200 and 300 milliseconds between attempts.
*
* Deletes at most 10 top-level children concurrently and waits for started child deletes to settle
* before retrying or rejecting. Throws on folder read errors other than `ENOENT`.
*/
public static async ensureEmptyFolderAsync(folderPath: string): Promise<void> {
await _wrapExceptionAsync(async () => {
let items: FolderItem[];
try {
items = await this.readFolderItemsAsync(folderPath);
} catch (error) {
if (!FileSystem.isNotExistError(error as Error)) {
throw error;
await _retryFolderDeletionAsync(async () => {
let names: string[];
try {
names = await fsx.readdir(folderPath);
} catch (error) {
if (!FileSystem.isFileDoesNotExistError(error as Error)) {
throw error;
}

await fsx.ensureDir(folderPath);
return;
}
await fsx.ensureDir(folderPath);
return;
}
await Async.forEachAsync(
items,
(item) => {
const itemPath: string = nodeJsPath.join(item.parentPath, item.name);
return fsPromises.rm(itemPath, FS_RM_DEFAULT_OPTIONS);
},
{ concurrency: 10 }
);

let hasError: boolean = false;
let deletionError: unknown;
await Async.forEachAsync(
names,
async (name) => {
if (hasError) {
return;
}

try {
await fsPromises.rm(nodeJsPath.join(folderPath, name), FS_RM_DEFAULT_OPTIONS);
} catch (error) {
// Keep callbacks fulfilled so every started delete settles before the retry decision.
if (
!hasError ||
(_isRetryableFolderDeletionError(deletionError) && !_isRetryableFolderDeletionError(error))
) {
deletionError = error;
}

hasError = true;
}
},
{ concurrency: 10 }
);

if (hasError) {
throw deletionError;
}
});
});
}

Expand Down Expand Up @@ -1758,6 +1801,50 @@ async function _handleLinkAsync(
}
}

function _isRetryableFolderDeletionError(error: unknown): boolean {
if (typeof error !== 'object' || error === null || !('code' in error)) {
return false;
}

const { code } = error;
return code === 'EBUSY' || code === 'ENOTEMPTY' || code === 'EPERM';
}

function _shouldRetryFolderDeletion(error: unknown, retryCount: number): boolean {
// Retry transient deletion failures on Windows, where open file handles can
// temporarily prevent files and folders from being removed. Extend this
// policy to other platforms if the problem is reported there.
return process.platform === 'win32' && retryCount < 3 && _isRetryableFolderDeletionError(error);
}

function _retryFolderDeletion(action: () => void): void {
for (let retryCount: number = 0; ; retryCount++) {
try {
return action();
} catch (error) {
if (!_shouldRetryFolderDeletion(error, retryCount)) {
throw error;
}

Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, (retryCount + 1) * 100);
}
}
}

async function _retryFolderDeletionAsync(action: () => Promise<void>): Promise<void> {
for (let retryCount: number = 0; ; retryCount++) {
try {
return await action();
} catch (error) {
if (!_shouldRetryFolderDeletion(error, retryCount)) {
throw error;
}

await Async.sleepAsync((retryCount + 1) * 100);
}
}
}

function _wrapException<TResult>(fn: () => TResult): TResult {
try {
return fn();
Expand Down
Loading
Loading