From b518d1f6da60dbf1e1e4fc811bfb5839768dab4a Mon Sep 17 00:00:00 2001 From: Dmitry Gozman Date: Mon, 5 Jun 2023 09:51:08 -0700 Subject: [PATCH] chore: update error message when using wrong cli (#23512) Now recommends uninstalling other packages. Fixes #23314. Example messages: ``` Please install @playwright/test package before running "npx playwright show-report" npm uninstall playwright playwright-firefox npm install -D @playwright/test ``` ``` Please install @playwright/test package before running "yarn playwright show-report" yarn remove playwright playwright-chromium playwright-firefox playwright-webkit yarn add -D @playwright/test ``` ``` Please install @playwright/test package before running "pnpm exec playwright show-report" pnpm remove playwright pnpm add -D @playwright/test ``` --- packages/playwright-core/src/cli/cli.ts | 44 +++++++++++++------ .../src/server/registry/index.ts | 1 - packages/playwright-core/src/utils/env.ts | 9 ++++ ...laywright-cdn-failover-should-work.spec.ts | 2 +- .../playwright-should-work.spec.ts | 2 +- 5 files changed, 41 insertions(+), 17 deletions(-) diff --git a/packages/playwright-core/src/cli/cli.ts b/packages/playwright-core/src/cli/cli.ts index 67003e63fe..600f544ac9 100755 --- a/packages/playwright-core/src/cli/cli.ts +++ b/packages/playwright-core/src/cli/cli.ts @@ -18,14 +18,41 @@ /* eslint-disable no-console */ +import { getPackageManager } from '../utils'; import program from './program'; +function printPlaywrightTestError(command: string) { + const packages: string[] = []; + for (const pkg of ['playwright', 'playwright-chromium', 'playwright-firefox', 'playwright-webkit']) { + try { + require.resolve(pkg); + packages.push(pkg); + } catch (e) { + } + } + if (!packages.length) + packages.push('playwright'); + const packageManager = getPackageManager(); + if (packageManager === 'yarn') { + console.error(`Please install @playwright/test package before running "yarn playwright ${command}"`); + console.error(` yarn remove ${packages.join(' ')}`); + console.error(' yarn add -D @playwright/test'); + } else if (packageManager === 'pnpm') { + console.error(`Please install @playwright/test package before running "pnpm exec playwright ${command}"`); + console.error(` pnpm remove ${packages.join(' ')}`); + console.error(' pnpm add -D @playwright/test'); + } else { + console.error(`Please install @playwright/test package before running "npx playwright ${command}"`); + console.error(` npm uninstall ${packages.join(' ')}`); + console.error(' npm install -D @playwright/test'); + } +} + { const command = program.command('test').allowUnknownOption(true); command.description('Run tests with Playwright Test. Available in @playwright/test package.'); command.action(async () => { - console.error('Please install @playwright/test package to use Playwright Test.'); - console.error(' npm install -D @playwright/test'); + printPlaywrightTestError('test'); process.exit(1); }); } @@ -34,18 +61,7 @@ import program from './program'; const command = program.command('show-report').allowUnknownOption(true); command.description('Show Playwright Test HTML report. Available in @playwright/test package.'); command.action(async () => { - console.error('Please install @playwright/test package to use Playwright Test.'); - console.error(' npm install -D @playwright/test'); - process.exit(1); - }); -} - -{ - const command = program.command('show-trace').allowUnknownOption(true); - command.description('Show Playwright Trace. Available in @playwright/test package.'); - command.action(async () => { - console.error('Please install @playwright/test package to use Playwright Test.'); - console.error(' npm install -D @playwright/test'); + printPlaywrightTestError('show-report'); process.exit(1); }); } diff --git a/packages/playwright-core/src/server/registry/index.ts b/packages/playwright-core/src/server/registry/index.ts index d6f822c119..0d0ea0f064 100644 --- a/packages/playwright-core/src/server/registry/index.ts +++ b/packages/playwright-core/src/server/registry/index.ts @@ -617,7 +617,6 @@ export class Registry { return undefined; const location = prefixes.length ? ` at ${path.join(prefixes[0], suffix)}` : ``; - // TODO: language-specific error message const installation = install ? `\nRun "${buildPlaywrightCLICommand(sdkLanguage, 'install ' + name)}"` : ''; throw new Error(`Chromium distribution '${name}' is not found${location}${installation}`); }; diff --git a/packages/playwright-core/src/utils/env.ts b/packages/playwright-core/src/utils/env.ts index d9a553896b..27f10f384b 100644 --- a/packages/playwright-core/src/utils/env.ts +++ b/packages/playwright-core/src/utils/env.ts @@ -25,3 +25,12 @@ export function getAsBooleanFromENV(name: string): boolean { const value = getFromENV(name); return !!value && value !== 'false' && value !== '0'; } + +export function getPackageManager() { + const env = process.env.npm_config_user_agent || ''; + if (env.includes('yarn')) + return 'yarn'; + if (env.includes('pnpm')) + return 'pnpm'; + return 'npm'; +} diff --git a/tests/installation/playwright-cdn-failover-should-work.spec.ts b/tests/installation/playwright-cdn-failover-should-work.spec.ts index 5e2ae07d6f..49ff405a90 100644 --- a/tests/installation/playwright-cdn-failover-should-work.spec.ts +++ b/tests/installation/playwright-cdn-failover-should-work.spec.ts @@ -45,6 +45,6 @@ for (const cdn of CDNS) { if (nodeMajorVersion >= 14) await exec('node esm-playwright.mjs'); const stdio = await exec('npx playwright', 'test', '-c', '.', { expectToExitWithError: true }); - expect(stdio).toContain(`Please install @playwright/test package to use Playwright Test.`); + expect(stdio).toContain(`Please install @playwright/test package`); }); } diff --git a/tests/installation/playwright-should-work.spec.ts b/tests/installation/playwright-should-work.spec.ts index 80a4376941..72d5bcedd2 100755 --- a/tests/installation/playwright-should-work.spec.ts +++ b/tests/installation/playwright-should-work.spec.ts @@ -23,5 +23,5 @@ test(`playwright should work`, async ({ exec, nodeMajorVersion, installedSoftwar if (nodeMajorVersion >= 14) await exec('node esm-playwright.mjs'); const stdio = await exec('npx playwright', 'test', '-c', '.', { expectToExitWithError: true }); - expect(stdio).toContain(`Please install @playwright/test package to use Playwright Test.`); + expect(stdio).toContain(`Please install @playwright/test package`); });