Skip to content

Commit bf36b48

Browse files
authored
fix(rimraf): allow 10 retires when removing the profile folder (#5826)
1 parent d1a3a5d commit bf36b48

File tree

6 files changed

+22
-26
lines changed

6 files changed

+22
-26
lines changed

src/install/installer.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,17 @@
1717
import fs from 'fs';
1818
import path from 'path';
1919
import util from 'util';
20-
import removeFolder from 'rimraf';
2120
import lockfile from 'proper-lockfile';
2221
import {Registry, allBrowserNames, isBrowserDirectory, BrowserName, registryDirectory} from '../utils/registry';
2322
import * as browserFetcher from './browserFetcher';
24-
import { getAsBooleanFromENV, calculateSha1 } from '../utils/utils';
23+
import { getAsBooleanFromENV, calculateSha1, removeFolders } from '../utils/utils';
2524

2625
const fsMkdirAsync = util.promisify(fs.mkdir.bind(fs));
2726
const fsReaddirAsync = util.promisify(fs.readdir.bind(fs));
2827
const fsReadFileAsync = util.promisify(fs.readFile.bind(fs));
2928
const fsExistsAsync = (filePath: string) => fsReadFileAsync(filePath).then(() => true).catch(e => false);
3029
const fsUnlinkAsync = util.promisify(fs.unlink.bind(fs));
3130
const fsWriteFileAsync = util.promisify(fs.writeFile.bind(fs));
32-
const removeFolderAsync = util.promisify(removeFolder);
3331

3432
const PACKAGE_PATH = path.join(__dirname, '..', '..');
3533

@@ -101,10 +99,9 @@ async function validateCache(linksDir: string, browserNames: BrowserName[]) {
10199
const directories = new Set<string>(downloadedBrowsers);
102100
for (const browserDirectory of usedBrowserPaths)
103101
directories.delete(browserDirectory);
104-
for (const directory of directories) {
102+
for (const directory of directories)
105103
browserFetcher.logPolitely('Removing unused browser at ' + directory);
106-
await removeFolderAsync(directory).catch(e => {});
107-
}
104+
await removeFolders([...directories]);
108105
}
109106

110107
// 3. Install missing browsers for this package.

src/server/helper.ts

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,10 @@
1616
*/
1717

1818
import { EventEmitter } from 'events';
19-
import removeFolder from 'rimraf';
20-
import util from 'util';
2119
import * as types from './types';
2220
import { Progress } from './progress';
2321
import { debugLogger } from '../utils/debugLogger';
2422

25-
const removeFolderAsync = util.promisify(removeFolder);
26-
2723
export type RegisteredListener = {
2824
emitter: EventEmitter;
2925
eventName: (string | symbol);
@@ -77,12 +73,6 @@ class Helper {
7773
return null;
7874
}
7975

80-
static async removeFolders(dirs: string[]) {
81-
await Promise.all(dirs.map(dir => {
82-
return removeFolderAsync(dir).catch((err: Error) => console.error(err));
83-
}));
84-
}
85-
8676
static waitForEvent(progress: Progress | null, emitter: EventEmitter, event: string | symbol, predicate?: Function): { promise: Promise<any>, dispose: () => void } {
8777
const listeners: RegisteredListener[] = [];
8878
const promise = new Promise((resolve, reject) => {

src/server/processLauncher.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,9 @@
1717

1818
import * as childProcess from 'child_process';
1919
import * as readline from 'readline';
20-
import * as removeFolder from 'rimraf';
2120
import { helper } from './helper';
2221
import * as types from './types';
23-
import { isUnderTest } from '../utils/utils';
22+
import { isUnderTest, removeFolders } from '../utils/utils';
2423

2524
export type Env = {[key: string]: string | number | boolean | undefined};
2625

@@ -62,7 +61,7 @@ if (maxListeners !== 0)
6261
process.setMaxListeners(Math.max(maxListeners || 0, 100));
6362

6463
export async function launchProcess(options: LaunchProcessOptions): Promise<LaunchResult> {
65-
const cleanup = () => helper.removeFolders(options.tempDirectories);
64+
const cleanup = () => removeFolders(options.tempDirectories);
6665

6766
const stdio: ('ignore' | 'pipe')[] = options.stdio === 'pipe' ? ['ignore', 'pipe', 'pipe', 'pipe', 'pipe'] : ['pipe', 'pipe', 'pipe'];
6867
options.log(`<launching> ${options.executablePath} ${options.args.join(' ')}`);
@@ -172,11 +171,7 @@ export async function launchProcess(options: LaunchProcessOptions): Promise<Laun
172171
// the process might have already stopped
173172
}
174173
}
175-
try {
176-
// Attempt to remove temporary directories to avoid littering.
177-
for (const dir of options.tempDirectories)
178-
removeFolder.sync(dir);
179-
} catch (e) { }
174+
cleanup();
180175
}
181176

182177
function killAndWait() {

src/utils/utils.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import path from 'path';
1818
import fs from 'fs';
19+
import removeFolder from 'rimraf';
1920
import * as util from 'util';
2021
import * as crypto from 'crypto';
2122

@@ -145,3 +146,15 @@ export function calculateSha1(buffer: Buffer | string): string {
145146
export function createGuid(): string {
146147
return crypto.randomBytes(16).toString('hex');
147148
}
149+
150+
export async function removeFolders(dirs: string[]) {
151+
await Promise.all(dirs.map((dir: string) => {
152+
return new Promise<void>(fulfill => {
153+
removeFolder(dir, { maxBusyTries: 10 }, error => {
154+
if (error)
155+
console.error(error); // eslint-disable no-console
156+
fulfill();
157+
});
158+
});
159+
}));
160+
}

test/defaultbrowsercontext-2.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,4 +236,5 @@ it('should connect to a browser with the default page', (test, { mode }) => {
236236
const options = { ...browserOptions, __testHookOnConnectToBrowser: () => new Promise(f => setTimeout(f, 3000)) };
237237
const context = await browserType.launchPersistentContext(await createUserDataDir(), options);
238238
expect(context.pages().length).toBe(1);
239+
await context.close();
239240
});

test/fixtures.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ import { folio as httpFolio } from './http.fixtures';
2626
import { folio as playwrightFolio } from './playwright.fixtures';
2727
import { PlaywrightClient } from '../lib/remote/playwrightClient';
2828
import { start } from '../lib/outofprocess';
29+
import { removeFolders } from '../lib/utils/utils';
2930
export { expect, config } from 'folio';
3031

31-
const removeFolderAsync = util.promisify(require('rimraf'));
3232
const mkdtempAsync = util.promisify(fs.mkdtemp);
3333

3434
const getExecutablePath = browserName => {
@@ -69,7 +69,7 @@ fixtures.createUserDataDir.init(async ({ }, run) => {
6969
return dir;
7070
}
7171
await run(createUserDataDir);
72-
await Promise.all(dirs.map(dir => removeFolderAsync(dir).catch(e => { })));
72+
await removeFolders(dirs);
7373
});
7474

7575
fixtures.launchPersistent.init(async ({ createUserDataDir, browserOptions, browserType }, run) => {

0 commit comments

Comments
 (0)