From 946b4efa3b55dd96396b1c34f1f159735beaaaea Mon Sep 17 00:00:00 2001 From: Andrey Lushnikov Date: Mon, 8 Jun 2020 12:11:16 -0700 Subject: [PATCH] fix(installer): create tmp directory inside `browserPath` (#2498) `fs.rename` doesn't work across partitions, so we have to have tmp folder next to our final destination. Fixes #2494 --- src/install/browserFetcher.ts | 7 ++----- src/install/browserPaths.ts | 26 ++++++++++++++++++++++++-- src/install/installer.ts | 17 +++++++++++++---- 3 files changed, 39 insertions(+), 11 deletions(-) diff --git a/src/install/browserFetcher.ts b/src/install/browserFetcher.ts index 5c260dbe9d..1b6173b4c1 100644 --- a/src/install/browserFetcher.ts +++ b/src/install/browserFetcher.ts @@ -18,8 +18,6 @@ import * as extract from 'extract-zip'; import * as fs from 'fs'; import * as ProxyAgent from 'https-proxy-agent'; -import * as os from 'os'; -import * as path from 'path'; import * as ProgressBar from 'progress'; import { getProxyForUrl } from 'proxy-from-env'; import * as URL from 'url'; @@ -30,7 +28,6 @@ import { BrowserName, BrowserPlatform, BrowserDescriptor } from './browserPaths' const unlinkAsync = util.promisify(fs.unlink.bind(fs)); const chmodAsync = util.promisify(fs.chmod.bind(fs)); -const mkdtempAsync = util.promisify(fs.mkdtemp.bind(fs)); const renameAsync = util.promisify(fs.rename.bind(fs)); const existsAsync = (path: string): Promise => new Promise(resolve => fs.stat(path, err => resolve(!err))); @@ -109,10 +106,10 @@ export async function downloadBrowserWithProgressBar(browserPath: string, browse } const url = revisionURL(browser); - const zipPath = path.join(os.tmpdir(), `playwright-download-${browser.name}-${browserPaths.hostPlatform}-${browser.revision}.zip`); + const zipPath = browserPaths.browserZipFile(browserPath, browser); try { await downloadFile(url, zipPath, progress); - const extractPath = await mkdtempAsync(path.join(os.tmpdir(), `playwright-extract-${browser.name}-${browserPaths.hostPlatform}-${browser.revision}-`)); + const extractPath = browserPaths.browserExtractDirectory(browserPath, browser); await extract(zipPath, { dir: extractPath}); await chmodAsync(browserPaths.executablePath(extractPath, browser)!, 0o755); await renameAsync(extractPath, browserPath); diff --git a/src/install/browserPaths.ts b/src/install/browserPaths.ts index c48073d643..6d3314f9bb 100644 --- a/src/install/browserPaths.ts +++ b/src/install/browserPaths.ts @@ -104,7 +104,29 @@ export function browserDirectory(browsersPath: string, browser: BrowserDescripto return path.join(browsersPath, `${browser.name}-${browser.revision}`); } -export function isBrowserDirectory(browserPath: string): boolean { - const baseName = path.basename(browserPath); +export function isBrowserDirectory(aPath: string): boolean { + const baseName = path.basename(aPath); return baseName.startsWith('chromium-') || baseName.startsWith('firefox-') || baseName.startsWith('webkit-'); } + +const BROWSER_EXTRACT_DIRECTORY_PREFIX = 'playwright-extract-'; + +export function isBrowserExtractDirectory(aPath: string): boolean { + const baseName = path.basename(aPath); + return baseName.startsWith(BROWSER_EXTRACT_DIRECTORY_PREFIX); +} + +export function browserExtractDirectory(browserPath: string, browser: BrowserDescriptor): string { + return (path.join(path.dirname(browserPath), `${BROWSER_EXTRACT_DIRECTORY_PREFIX}${browser.name}-${hostPlatform}-${browser.revision}`)); +} + +const BROWSER_ZIP_FILE_PREFIX = 'playwright-download-'; + +export function isBrowserZipFile(aPath: string): boolean { + const baseName = path.basename(aPath); + return baseName.startsWith(BROWSER_ZIP_FILE_PREFIX) && baseName.endsWith('.zip'); +} + +export function browserZipFile(browserPath: string, browser: BrowserDescriptor): string { + return path.join(path.dirname(browserPath), `${BROWSER_ZIP_FILE_PREFIX}${browser.name}-${hostPlatform}-${browser.revision}.zip`); +} diff --git a/src/install/installer.ts b/src/install/installer.ts index 51049c7897..0ae43d1fbd 100644 --- a/src/install/installer.ts +++ b/src/install/installer.ts @@ -28,7 +28,7 @@ const fsReaddirAsync = util.promisify(fs.readdir.bind(fs)); const fsReadFileAsync = util.promisify(fs.readFile.bind(fs)); const fsUnlinkAsync = util.promisify(fs.unlink.bind(fs)); const fsWriteFileAsync = util.promisify(fs.writeFile.bind(fs)); -const removeFolderAsync = util.promisify(removeFolder); +const rmAsync = util.promisify(removeFolder); export async function installBrowsersWithProgressBar(packagePath: string) { const browsersPath = browserPaths.browsersPath(packagePath); @@ -60,7 +60,16 @@ async function validateCache(packagePath: string, browsersPath: string, linksDir } } - // 2. Delete all unused browsers. + // 2. Delete all stale browser extract directories and .zip files. + // NOTE: this must not run concurrently with other installations. + let staleFiles = (await fsReaddirAsync(browsersPath)).map(file => path.join(browsersPath, file)); + staleFiles = staleFiles.filter(file => browserPaths.isBrowserZipFile(file) || browserPaths.isBrowserExtractDirectory(file)); + for (const staleFile of staleFiles) { + logPolitely('Removing leftover from interrupted installation ' + staleFile); + await rmAsync(staleFile).catch(e => {}); + } + + // 3. Delete all unused browsers. let downloadedBrowsers = (await fsReaddirAsync(browsersPath)).map(file => path.join(browsersPath, file)); downloadedBrowsers = downloadedBrowsers.filter(file => browserPaths.isBrowserDirectory(file)); const directories = new Set(downloadedBrowsers); @@ -68,10 +77,10 @@ async function validateCache(packagePath: string, browsersPath: string, linksDir directories.delete(browserPaths.browserDirectory(browsersPath, browser)); for (const directory of directories) { logPolitely('Removing unused browser at ' + directory); - await removeFolderAsync(directory).catch(e => {}); + await rmAsync(directory).catch(e => {}); } - // 3. Install missing browsers for this package. + // 4. Install missing browsers for this package. const myBrowsers = JSON.parse((await fsReadFileAsync(path.join(packagePath, 'browsers.json'))).toString())['browsers'] as browserPaths.BrowserDescriptor[]; for (const browser of myBrowsers) { const browserPath = browserPaths.browserDirectory(browsersPath, browser);