From bf36b487fc66341e25aed9dd1a2b2c842ac4f2a9 Mon Sep 17 00:00:00 2001 From: Pavel Feldman Date: Wed, 17 Mar 2021 10:31:35 +0800 Subject: [PATCH] fix(rimraf): allow 10 retires when removing the profile folder (#5826) --- src/install/installer.ts | 9 +++------ src/server/helper.ts | 10 ---------- src/server/processLauncher.ts | 11 +++-------- src/utils/utils.ts | 13 +++++++++++++ test/defaultbrowsercontext-2.spec.ts | 1 + test/fixtures.ts | 4 ++-- 6 files changed, 22 insertions(+), 26 deletions(-) diff --git a/src/install/installer.ts b/src/install/installer.ts index a0a86e414e..186c01c1f1 100644 --- a/src/install/installer.ts +++ b/src/install/installer.ts @@ -17,11 +17,10 @@ import fs from 'fs'; import path from 'path'; import util from 'util'; -import removeFolder from 'rimraf'; import lockfile from 'proper-lockfile'; import {Registry, allBrowserNames, isBrowserDirectory, BrowserName, registryDirectory} from '../utils/registry'; import * as browserFetcher from './browserFetcher'; -import { getAsBooleanFromENV, calculateSha1 } from '../utils/utils'; +import { getAsBooleanFromENV, calculateSha1, removeFolders } from '../utils/utils'; const fsMkdirAsync = util.promisify(fs.mkdir.bind(fs)); const fsReaddirAsync = util.promisify(fs.readdir.bind(fs)); @@ -29,7 +28,6 @@ const fsReadFileAsync = util.promisify(fs.readFile.bind(fs)); const fsExistsAsync = (filePath: string) => fsReadFileAsync(filePath).then(() => true).catch(e => false); const fsUnlinkAsync = util.promisify(fs.unlink.bind(fs)); const fsWriteFileAsync = util.promisify(fs.writeFile.bind(fs)); -const removeFolderAsync = util.promisify(removeFolder); const PACKAGE_PATH = path.join(__dirname, '..', '..'); @@ -101,10 +99,9 @@ async function validateCache(linksDir: string, browserNames: BrowserName[]) { const directories = new Set(downloadedBrowsers); for (const browserDirectory of usedBrowserPaths) directories.delete(browserDirectory); - for (const directory of directories) { + for (const directory of directories) browserFetcher.logPolitely('Removing unused browser at ' + directory); - await removeFolderAsync(directory).catch(e => {}); - } + await removeFolders([...directories]); } // 3. Install missing browsers for this package. diff --git a/src/server/helper.ts b/src/server/helper.ts index 4012a759d5..fb34204536 100644 --- a/src/server/helper.ts +++ b/src/server/helper.ts @@ -16,14 +16,10 @@ */ import { EventEmitter } from 'events'; -import removeFolder from 'rimraf'; -import util from 'util'; import * as types from './types'; import { Progress } from './progress'; import { debugLogger } from '../utils/debugLogger'; -const removeFolderAsync = util.promisify(removeFolder); - export type RegisteredListener = { emitter: EventEmitter; eventName: (string | symbol); @@ -77,12 +73,6 @@ class Helper { return null; } - static async removeFolders(dirs: string[]) { - await Promise.all(dirs.map(dir => { - return removeFolderAsync(dir).catch((err: Error) => console.error(err)); - })); - } - static waitForEvent(progress: Progress | null, emitter: EventEmitter, event: string | symbol, predicate?: Function): { promise: Promise, dispose: () => void } { const listeners: RegisteredListener[] = []; const promise = new Promise((resolve, reject) => { diff --git a/src/server/processLauncher.ts b/src/server/processLauncher.ts index 361cf15d96..60835c374c 100644 --- a/src/server/processLauncher.ts +++ b/src/server/processLauncher.ts @@ -17,10 +17,9 @@ import * as childProcess from 'child_process'; import * as readline from 'readline'; -import * as removeFolder from 'rimraf'; import { helper } from './helper'; import * as types from './types'; -import { isUnderTest } from '../utils/utils'; +import { isUnderTest, removeFolders } from '../utils/utils'; export type Env = {[key: string]: string | number | boolean | undefined}; @@ -62,7 +61,7 @@ if (maxListeners !== 0) process.setMaxListeners(Math.max(maxListeners || 0, 100)); export async function launchProcess(options: LaunchProcessOptions): Promise { - const cleanup = () => helper.removeFolders(options.tempDirectories); + const cleanup = () => removeFolders(options.tempDirectories); const stdio: ('ignore' | 'pipe')[] = options.stdio === 'pipe' ? ['ignore', 'pipe', 'pipe', 'pipe', 'pipe'] : ['pipe', 'pipe', 'pipe']; options.log(` ${options.executablePath} ${options.args.join(' ')}`); @@ -172,11 +171,7 @@ export async function launchProcess(options: LaunchProcessOptions): Promise { + return new Promise(fulfill => { + removeFolder(dir, { maxBusyTries: 10 }, error => { + if (error) + console.error(error); // eslint-disable no-console + fulfill(); + }); + }); + })); +} diff --git a/test/defaultbrowsercontext-2.spec.ts b/test/defaultbrowsercontext-2.spec.ts index f7e873c9ad..e35857446a 100644 --- a/test/defaultbrowsercontext-2.spec.ts +++ b/test/defaultbrowsercontext-2.spec.ts @@ -236,4 +236,5 @@ it('should connect to a browser with the default page', (test, { mode }) => { const options = { ...browserOptions, __testHookOnConnectToBrowser: () => new Promise(f => setTimeout(f, 3000)) }; const context = await browserType.launchPersistentContext(await createUserDataDir(), options); expect(context.pages().length).toBe(1); + await context.close(); }); diff --git a/test/fixtures.ts b/test/fixtures.ts index fd7dde40fa..44f5c1537f 100644 --- a/test/fixtures.ts +++ b/test/fixtures.ts @@ -26,9 +26,9 @@ import { folio as httpFolio } from './http.fixtures'; import { folio as playwrightFolio } from './playwright.fixtures'; import { PlaywrightClient } from '../lib/remote/playwrightClient'; import { start } from '../lib/outofprocess'; +import { removeFolders } from '../lib/utils/utils'; export { expect, config } from 'folio'; -const removeFolderAsync = util.promisify(require('rimraf')); const mkdtempAsync = util.promisify(fs.mkdtemp); const getExecutablePath = browserName => { @@ -69,7 +69,7 @@ fixtures.createUserDataDir.init(async ({ }, run) => { return dir; } await run(createUserDataDir); - await Promise.all(dirs.map(dir => removeFolderAsync(dir).catch(e => { }))); + await removeFolders(dirs); }); fixtures.launchPersistent.init(async ({ createUserDataDir, browserOptions, browserType }, run) => {