|
27 | 27 | * errors are collected and printed as diagnostics, and only fail the run when |
28 | 28 | * they carry the externalized-module signature. |
29 | 29 | * |
30 | | - * Run from the clients/web directory (`npm run smoke:web:browser` does |
31 | | - * `cd clients/web` first) so `import("playwright")` resolves against that |
32 | | - * client's node_modules. Repo-root paths are derived inside the shared helper |
33 | | - * from import.meta.url, so the cwd change doesn't affect which build tree runs. |
| 30 | + * Playwright lives in clients/web's node_modules, so it's resolved with a |
| 31 | + * `createRequire` based at clients/web/package.json rather than a bare |
| 32 | + * `import("playwright")`. A bare ESM specifier resolves relative to *this |
| 33 | + * script's* directory (scripts/), not the cwd — so `cd clients/web` in the npm |
| 34 | + * script would NOT make it resolvable, and it only appeared to work locally |
| 35 | + * when an ancestor node_modules happened to carry playwright (it fails in CI, |
| 36 | + * which has none). createRequire pins resolution to clients/web regardless of |
| 37 | + * cwd or ancestor dirs. |
34 | 38 | * |
35 | 39 | * Expects `clients/web/dist` and `clients/launcher/build` to be built first — |
36 | 40 | * the validate / CI ordering guarantees this. |
37 | 41 | */ |
38 | 42 |
|
| 43 | +import { createRequire } from "node:module"; |
39 | 44 | import { setTimeout as delay } from "node:timers/promises"; |
| 45 | +import { fileURLToPath } from "node:url"; |
| 46 | +import { dirname, resolve } from "node:path"; |
40 | 47 | import { startProdWebServer } from "./lib/prod-web-server.mjs"; |
41 | 48 |
|
| 49 | +const scriptDir = dirname(fileURLToPath(import.meta.url)); |
| 50 | +// Resolve playwright from clients/web (where it's installed) no matter the cwd. |
| 51 | +const requireFromWeb = createRequire( |
| 52 | + resolve(scriptDir, "..", "clients/web/package.json"), |
| 53 | +); |
| 54 | + |
42 | 55 | const HOST = "127.0.0.1"; |
43 | 56 | // Distinct from smoke:web's SMOKE_WEB_PORT so overriding one doesn't make both |
44 | 57 | // back-to-back smokes bind the same port (→ EADDRINUSE on the second). |
@@ -78,16 +91,16 @@ async function fail(message) { |
78 | 91 | } |
79 | 92 |
|
80 | 93 | async function loadChromium() { |
81 | | - let playwright; |
| 94 | + let chromium; |
82 | 95 | try { |
83 | | - playwright = await import("playwright"); |
| 96 | + ({ chromium } = requireFromWeb("playwright")); |
84 | 97 | } catch (err) { |
85 | 98 | throw new Error( |
86 | | - `could not load Playwright — run \`npx playwright install --with-deps chromium\` (${err instanceof Error ? err.message : String(err)})`, |
| 99 | + `could not load Playwright from clients/web — run \`npx playwright install --with-deps chromium\` (${err instanceof Error ? err.message : String(err)})`, |
87 | 100 | ); |
88 | 101 | } |
89 | 102 | try { |
90 | | - return await playwright.chromium.launch({ headless: true }); |
| 103 | + return await chromium.launch({ headless: true }); |
91 | 104 | } catch (err) { |
92 | 105 | throw new Error( |
93 | 106 | `chromium failed to launch — on a bare Linux box run \`npx playwright install --with-deps chromium\` for the system libraries (${err instanceof Error ? err.message : String(err)})`, |
|
0 commit comments