From 4ec02800801d441238d6160a933f88f98c5f7165 Mon Sep 17 00:00:00 2001 From: Alex Rudenko Date: Thu, 4 Apr 2024 12:46:35 +0200 Subject: [PATCH] fix: check if executablePath exists (#12201) --- .../puppeteer-core/src/node/ProductLauncher.ts | 6 ++++++ .../assets/puppeteer-core/launch.js | 6 +++++- test/src/launcher.spec.ts | 18 +++++++++++++++++- 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/packages/puppeteer-core/src/node/ProductLauncher.ts b/packages/puppeteer-core/src/node/ProductLauncher.ts index 2da07e8f7c8..6f4008f23d5 100644 --- a/packages/puppeteer-core/src/node/ProductLauncher.ts +++ b/packages/puppeteer-core/src/node/ProductLauncher.ts @@ -98,6 +98,12 @@ export abstract class ProductLauncher { const launchArgs = await this.computeLaunchArguments(options); + if (!existsSync(launchArgs.executablePath)) { + throw new Error( + `Browser was not found at the configured executablePath (${launchArgs.executablePath})` + ); + } + const usePipe = launchArgs.args.includes('--remote-debugging-pipe'); const onProcessExit = async () => { diff --git a/test/installation/assets/puppeteer-core/launch.js b/test/installation/assets/puppeteer-core/launch.js index 4776d7e2611..b0982cdd90f 100644 --- a/test/installation/assets/puppeteer-core/launch.js +++ b/test/installation/assets/puppeteer-core/launch.js @@ -13,7 +13,11 @@ import puppeteer from 'puppeteer-core'; executablePath: 'node', }); } catch (error) { - if (error.message.includes('Failed to launch the browser process')) { + if ( + error.message.includes( + 'Browser was not found at the configured executablePath (node)' + ) + ) { process.exit(0); } console.error(error); diff --git a/test/src/launcher.spec.ts b/test/src/launcher.spec.ts index cac2aaaee1d..e1eefaceb19 100644 --- a/test/src/launcher.spec.ts +++ b/test/src/launcher.spec.ts @@ -190,7 +190,9 @@ describe('Launcher specs', function () { }).catch(error => { return (waitError = error); }); - expect(waitError.message).toContain('Failed to launch'); + expect(waitError.message).toBe( + 'Browser was not found at the configured executablePath (random-invalid-path)' + ); }); it('userDataDir option', async () => { const userDataDir = await mkdtemp(TMP_FOLDER); @@ -615,6 +617,20 @@ describe('Launcher specs', function () { }); expect(error.message).toContain('either pipe or debugging port'); }); + + it('throws an error if executable path is not valid with pipe=true', async () => { + const options = { + executablePath: '/tmp/does-not-exist', + pipe: true, + }; + let error!: Error; + await launch(options).catch(error_ => { + return (error = error_); + }); + expect(error.message).toContain( + 'Browser was not found at the configured executablePath (/tmp/does-not-exist)' + ); + }); }); describe('Puppeteer.launch', function () {