Open-source SEO, GEO, and marketing skills for AI agents.
fix(browser): ship playwright-core browsers.json in the standalone package (#127)
Deepanshu Pald48eb249c0c5
diff --git a/notfair/next.config.ts b/notfair/next.config.tsindex cf983a1e..a37f4ad7 100644--- a/notfair/next.config.ts+++ b/notfair/next.config.ts@@ -16,6 +16,10 @@ const config: NextConfig = {"*": ["./node_modules/@swc/helpers/**/*","./node_modules/@next/env/**/*",+ // playwright-core reads browsers.json at runtime via require(); file+ // tracing only follows code, so the workspace browser fails to launch+ // from the published tarball without it.+ "./node_modules/playwright-core/browsers.json",],},// typedRoutes intentionally disabled in V1 — our nav table builds Link hrefsdiff --git a/notfair/scripts/copy-standalone-assets.mjs b/notfair/scripts/copy-standalone-assets.mjsindex 16c8c276..0f68ba5d 100644--- a/notfair/scripts/copy-standalone-assets.mjs+++ b/notfair/scripts/copy-standalone-assets.mjs@@ -15,6 +15,7 @@ import { existsSync, cpSync, lstatSync, readdirSync, realpathSync, rmSync, renamimport { execSync } from "node:child_process";import { basename, dirname, join } from "node:path";import { fileURLToPath } from "node:url";+import { findPlaywrightDirsMissingBrowsersJson } from "./lib/standalone-integrity.mjs";const __dirname = dirname(fileURLToPath(import.meta.url));const ROOT = dirname(__dirname);@@ -113,3 +114,16 @@ function countSymlinks(dir) {}return count;}++// playwright-core requires its own browsers.json at runtime. next.config.ts+// force-includes it; fail the build if any copy in the tree is still missing+// it, since the workspace browser cannot launch from such a package.+const missingBrowsersJson = findPlaywrightDirsMissingBrowsersJson(STANDALONE);+if (missingBrowsersJson.length > 0) {+ console.error(+ `playwright-core is missing browsers.json in the standalone tree:\n${missingBrowsersJson+ .map((dir) => ` ${dir.replace(ROOT + "/", "")}`)+ .join("\n")}\nCheck outputFileTracingIncludes in next.config.ts.`,+ );+ process.exit(1);+}diff --git a/notfair/scripts/lib/standalone-integrity.mjs b/notfair/scripts/lib/standalone-integrity.mjsnew file mode 100644index 00000000..1b00f4df--- /dev/null+++ b/notfair/scripts/lib/standalone-integrity.mjs@@ -0,0 +1,38 @@+// Runtime files that Next.js output file tracing cannot see (they are read via+// require()/fs at runtime, not imported), but that the standalone tarball+// needs. Checked after scripts/copy-standalone-assets.mjs builds the tree so a+// broken package fails the build instead of failing on a user's machine.++import { existsSync, readdirSync } from "node:fs";+import { join } from "node:path";++/**+ * Return every playwright-core package directory under `standaloneRoot`+ * (including pnpm store copies and Turbopack's hashed `.next/node_modules`+ * aliases) that is missing `browsers.json`.+ */+export function findPlaywrightDirsMissingBrowsersJson(standaloneRoot) {+ const missing = [];+ const walk = (dir, depth) => {+ if (depth > 8 || !existsSync(dir)) return;+ let entries;+ try {+ entries = readdirSync(dir, { withFileTypes: true });+ } catch {+ return;+ }+ for (const entry of entries) {+ if (!entry.isDirectory()) continue;+ const full = join(dir, entry.name);+ if (entry.name === "playwright-core" || entry.name.startsWith("playwright-core-")) {+ if (existsSync(join(full, "package.json")) && !existsSync(join(full, "browsers.json"))) {+ missing.push(full);+ }+ continue;+ }+ walk(full, depth + 1);+ }+ };+ walk(standaloneRoot, 0);+ return missing;+}diff --git a/notfair/src/server/browser/session.test.ts b/notfair/src/server/browser/session.test.tsindex 29135dc4..e74c09ac 100644--- a/notfair/src/server/browser/session.test.ts+++ b/notfair/src/server/browser/session.test.ts@@ -165,6 +165,22 @@ describe("getOrLaunchBrowser", () => {expect(mocks.stopChrome).toHaveBeenCalledWith(chrome);});+ it("reports an incomplete package when playwright-core is missing browsers.json", async () => {+ const chrome = launched();+ await expect(+ getOrLaunchBrowser("acme", {+ launch: vi.fn(async () => chrome as never),+ connectOverCDP: vi.fn(async () => {+ throw new Error(+ "Cannot find module '/pkg/.next/node_modules/playwright-core-b643089f39648130/browsers.json'",+ );+ }),+ }),+ ).rejects.toThrow(/NotFair package is incomplete.*browsers\.json/);+ expect(mocks.stopChrome).toHaveBeenCalledWith(chrome);+ expect(_sessionsByProject.has("acme")).toBe(false);+ });+it("closes both handles when CDP has no default context", async () => {const chrome = launched();const attached = browser([]);diff --git a/notfair/src/server/browser/session.ts b/notfair/src/server/browser/session.tsindex 3d4e93c5..f366e394 100644--- a/notfair/src/server/browser/session.ts+++ b/notfair/src/server/browser/session.ts@@ -139,8 +139,14 @@ async function launchSession(browser = await connect(launched.cdpHttpUrl);} catch (err) {await stopChrome(launched).catch(() => {});+ const message = (err as Error).message ?? String(err);+ if (/playwright-core[^'"]*[\\/]browsers\.json/.test(message)) {+ throw new Error(+ "The installed NotFair package is incomplete: playwright-core is missing browsers.json, so the workspace browser cannot start. Reinstall or upgrade NotFair to repair it.",+ );+ }throw new Error(- `Failed to attach Playwright to Chrome CDP at ${launched.cdpHttpUrl}: ${(err as Error).message}`,+ `Failed to attach Playwright to Chrome CDP at ${launched.cdpHttpUrl}: ${message}`,);}diff --git a/notfair/src/server/browser/standalone-integrity.test.ts b/notfair/src/server/browser/standalone-integrity.test.tsnew file mode 100644index 00000000..d88f7d38--- /dev/null+++ b/notfair/src/server/browser/standalone-integrity.test.ts@@ -0,0 +1,35 @@+import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";+import { tmpdir } from "node:os";+import { join } from "node:path";+import { afterEach, describe, expect, it } from "vitest";++import { findPlaywrightDirsMissingBrowsersJson } from "../../../scripts/lib/standalone-integrity.mjs";++function pkg(root: string, rel: string, withBrowsersJson: boolean) {+ const dir = join(root, rel);+ mkdirSync(dir, { recursive: true });+ writeFileSync(join(dir, "package.json"), "{}");+ if (withBrowsersJson) writeFileSync(join(dir, "browsers.json"), "{}");+ return dir;+}++describe("standalone package integrity", () => {+ let root: string;+ afterEach(() => rmSync(root, { recursive: true, force: true }));++ it("flags playwright-core copies without browsers.json, including hashed Turbopack aliases", () => {+ root = mkdtempSync(join(tmpdir(), "notfair-standalone-"));+ pkg(root, "node_modules/.pnpm/[email protected]/node_modules/playwright-core", true);+ const alias = pkg(root, ".next/node_modules/playwright-core-b643089f39648130", false);++ expect(findPlaywrightDirsMissingBrowsersJson(root)).toEqual([alias]);+ });++ it("passes a tree where every playwright-core copy ships browsers.json", () => {+ root = mkdtempSync(join(tmpdir(), "notfair-standalone-"));+ pkg(root, "node_modules/playwright-core", true);+ pkg(root, ".next/node_modules/playwright-core-b643089f39648130", true);++ expect(findPlaywrightDirsMissingBrowsersJson(root)).toEqual([]);+ });+});