diff --git a/common/changes/@microsoft/rush/fix-issue-6145-stale-build-cache-restore-folders_2026-11-14-00-00.json b/common/changes/@microsoft/rush/fix-issue-6145-stale-build-cache-restore-folders_2026-11-14-00-00.json new file mode 100644 index 0000000000..b9fa6f8080 --- /dev/null +++ b/common/changes/@microsoft/rush/fix-issue-6145-stale-build-cache-restore-folders_2026-11-14-00-00.json @@ -0,0 +1,11 @@ +{ + "changes": [ + { + "packageName": "@microsoft/rush", + "comment": "Delete the `build-cache-restore-*` staging folders that an interrupted build cache restore left in a project's `.rush/temp` folder.", + "type": "patch" + } + ], + "packageName": "@microsoft/rush", + "email": "178374507+HerambInamke@users.noreply.github.com" +} diff --git a/libraries/rush-lib/src/logic/buildCache/StagedRestore.ts b/libraries/rush-lib/src/logic/buildCache/StagedRestore.ts index dc71289113..b4c5d0acf6 100644 --- a/libraries/rush-lib/src/logic/buildCache/StagedRestore.ts +++ b/libraries/rush-lib/src/logic/buildCache/StagedRestore.ts @@ -13,6 +13,10 @@ import type { ITerminal } from '@rushstack/terminal'; */ export const STAGING_FOLDER_PREFIX: string = 'build-cache-restore-'; +// A staging folder is named `build-cache-restore--`, so that a folder left by a killed process can +// be recognized. +const STAGING_FOLDER_NAME_REGEXP: RegExp = new RegExp(`^${STAGING_FOLDER_PREFIX}(\\d+)-`); + const MOVE_CONCURRENCY: number = 10; /** @@ -77,7 +81,10 @@ export async function tryRestoreThroughStagingFolderAsync( } await mkdir(projectRushTempFolder, { recursive: true }); - stagingFolderPath = await mkdtemp(path.join(projectRushTempFolder, STAGING_FOLDER_PREFIX)); + await deleteStaleStagingFoldersAsync(terminal, projectRushTempFolder); + stagingFolderPath = await mkdtemp( + path.join(projectRushTempFolder, `${STAGING_FOLDER_PREFIX}${process.pid}-`) + ); terminal.writeVerboseLine( `Extracting the cache entry into "${stagingFolderPath}", to move the output folders into place from there.` ); @@ -116,6 +123,54 @@ export async function tryRestoreThroughStagingFolderAsync( } } +// Deletes the staging folders that a process left when it was killed or exited on a signal during a restore, since its +// finally block didn't run. A folder of a process that is still running may be in use, so it is kept: several phases of +// one project can restore at the same time. A folder whose name has no process id is kept for the same reason. +async function deleteStaleStagingFoldersAsync( + terminal: ITerminal, + projectRushTempFolder: string +): Promise { + let names: string[]; + try { + names = await readdir(projectRushTempFolder); + } catch (error) { + terminal.writeVerboseLine( + `Unable to look for stale staging folders in "${projectRushTempFolder}": ${error}` + ); + return; + } + + await Promise.all( + names.map(async (name: string) => { + const match: RegExpExecArray | null = STAGING_FOLDER_NAME_REGEXP.exec(name); + if (!match || isProcessRunning(Number(match[1]))) { + return; + } + const folderPath: string = path.join(projectRushTempFolder, name); + try { + await rm(folderPath, { recursive: true, force: true }); + terminal.writeVerboseLine(`Deleted the stale staging folder "${folderPath}".`); + } catch (error) { + terminal.writeVerboseLine(`Unable to delete the stale staging folder "${folderPath}": ${error}`); + } + }) + ); +} + +function isProcessRunning(pid: number): boolean { + if (pid === process.pid) { + return true; + } + try { + // Signal 0 only checks whether the process exists. + process.kill(pid, 0); + return true; + } catch (error) { + // EPERM means that the process exists but belongs to another user. + return (error as NodeJS.ErrnoException).code !== 'ESRCH'; + } +} + // Returns why the output folders can't be restored through a staging folder, or undefined if they can, in which case // it fills relativeFolderPaths with the normalized project-relative paths of the output folders. function getUnsupportedReason( diff --git a/libraries/rush-lib/src/logic/buildCache/test/StagedRestore.test.ts b/libraries/rush-lib/src/logic/buildCache/test/StagedRestore.test.ts index 698f31b346..f026e2d62b 100644 --- a/libraries/rush-lib/src/logic/buildCache/test/StagedRestore.test.ts +++ b/libraries/rush-lib/src/logic/buildCache/test/StagedRestore.test.ts @@ -1,6 +1,7 @@ // Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license. // See LICENSE in the project root for license information. +import { spawnSync } from 'node:child_process'; import * as fs from 'node:fs'; import * as os from 'node:os'; import * as path from 'node:path'; @@ -566,4 +567,28 @@ describe('OperationBuildCache restores through a staging folder', () => { expect(output).toContain(SKIP_LINE); expect(untarMock).toHaveBeenCalledTimes(1); }); + + it('U8: deletes the staging folders of processes that are no longer running', async () => { + // A process that has exited, like a Rush process that was killed during a restore. + const exitedPid: number = spawnSync(process.execPath, ['-e', '']).pid; + const staleFolder: string = `${STAGING_FOLDER_PREFIX}${exitedPid}-abc123`; + // A process that is still running may be restoring into its folder. + const runningFolder: string = `${STAGING_FOLDER_PREFIX}${process.ppid}-def456`; + // Without a process id, there is no way to tell whether the folder is in use. + const legacyFolder: string = `${STAGING_FOLDER_PREFIX}ghi789`; + const rushTempFolder: string = path.join(projectFolder, '.rush', 'temp'); + for (const folderName of [staleFolder, runningFolder, legacyFolder]) { + writeFiles(path.join(rushTempFolder, folderName), { 'lib/plugin.js': 'stale' }); + } + writeFiles(projectFolder, OLD_OUTPUTS); + seedEntry(); + + const { result } = await restoreAsync(createSubject()); + + expect(result).toBe(true); + expect(path.basename(untarMock.mock.calls[0][0].outputFolderPath)).toMatch( + new RegExp(`^${STAGING_FOLDER_PREFIX}${process.pid}-`) + ); + expect(getStagingFolders().sort()).toEqual([legacyFolder, runningFolder].sort()); + }); });