shots / console: resolve playwright only from an installed node_modules copy; in a project without playwright Bun auto-installed the newest one into its global cache, whose browsers were never downloaded (capture-web on Linux CI: 'headless shell: not installed'; macOS hid it with Google Chrome)

This commit is contained in:
CRTkafa 2026-10-13 02:55:12 +08:00
parent 56591f4118
commit b9579e2a2e
2 changed files with 26 additions and 1 deletions

View file

@ -29,6 +29,10 @@ async function chromium(root: string): Promise<any> {
for (const base of [root, join(import.meta.dir, '../..')]) {
try {
const p = Bun.resolveSync('playwright', base);
// only an installed copy (node_modules): outside a project Bun auto-installs the newest playwright into its global
// cache (~/.bun/install/cache), whose browser build was never downloaded ("headless shell: not installed" on
// Linux CI, where no Google Chrome hides it); storebound's own copy is next
if (!isInstalledPackage(p)) continue;
return (await import(p)).chromium;
} catch { /* next */ }
}
@ -37,6 +41,9 @@ async function chromium(root: string): Promise<any> {
throw new Error('playwright not found: in your project run `bun add -d playwright` (and `bunx playwright install chromium` if Google Chrome is not installed)');
}
/** A resolved module path inside a node_modules folder (not one of Bun's auto-installs from its global cache). */
export const isInstalledPackage = (resolved: string): boolean => /[\\/]node_modules[\\/]/.test(resolved);
/** Playwright's chromium (same resolution as `launch`), for connectOverCDP. */
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const chromiumFor = (root: string): Promise<any> => chromium(root);

View file

@ -8,7 +8,7 @@ import { existsSync, mkdirSync, mkdtempSync, rmSync, symlinkSync, writeFileSync
import { hostname, tmpdir } from 'node:os';
import { join } from 'node:path';
import { openConsoleBrowser } from '../cli/console/session';
import { installedBrowsers, launch, launchCandidates, launchPersistent, persistentExecutable, profileHolder } from '../cli/shots/browser';
import { chromiumFor, installedBrowsers, isInstalledPackage, launch, launchCandidates, launchPersistent, persistentExecutable, profileHolder } from '../cli/shots/browser';
/* eslint-disable @typescript-eslint/no-explicit-any */
@ -139,3 +139,21 @@ describe('console fill', () => {
expect(f.calls).toEqual([]);
});
});
describe('playwright resolution', () => {
test('a project without playwright uses storebound\'s installed copy, never a Bun auto-install of the newest one', async () => {
// regression (Linux CI): `shots capture` in a temp project resolved playwright through Bun's auto-install (the
// newest version, in ~/.bun/install/cache) whose browser build was not downloaded: "headless shell: not installed"
expect(isInstalledPackage('/p/node_modules/playwright/index.mjs')).toBe(true);
expect(isInstalledPackage('C:\\p\\node_modules\\playwright\\index.mjs')).toBe(true);
expect(isInstalledPackage('/root/.bun/install/cache/playwright@1.64.0@@@1/index.mjs')).toBe(false);
const bare = mkdtempSync(join(tmpdir(), 'sb-no-playwright-'));
try {
// in a child whose cwd has no node_modules, like a real run in such a project (auto-install is on there)
const mod = JSON.stringify(join(import.meta.dir, '..', 'cli', 'shots', 'browser.ts'));
const r = Bun.spawnSync(['bun', '-e', `const { chromiumFor } = await import(${mod}); console.log((await chromiumFor(process.cwd())).executablePath());`], { cwd: bare });
const own = await chromiumFor(join(import.meta.dir, '..'));
expect(r.stdout.toString().trim()).toBe(own.executablePath());
} finally { rmSync(bare, { recursive: true, force: true }); }
});
});