Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

fix(test runner): prevent esm loader deadlock (#36187)

Dmitry Gozmana0479cb24b96
diff --git a/packages/playwright/src/transform/esmLoader.ts b/packages/playwright/src/transform/esmLoader.ts
index a74b20110d..d32537be5e 100644
--- a/packages/playwright/src/transform/esmLoader.ts
+++ b/packages/playwright/src/transform/esmLoader.ts
@@ -72,8 +72,12 @@ async function load(moduleUrl: string, context: { format?: string }, defaultLoad
const transformed = transformHook(code, filename, moduleUrl);
// Flush the source maps to the main thread, so that errors during import() are source-mapped.
- if (transformed.serializedCache)
- await transport?.send('pushToCompilationCache', { cache: transformed.serializedCache });
+ if (transformed.serializedCache) {
+ if (legacyWaitForSourceMaps)
+ await transport?.send('pushToCompilationCache', { cache: transformed.serializedCache });
+ else
+ transport?.post('pushToCompilationCache', { cache: transformed.serializedCache });
+ }
// Output format is required, so we determine it manually when unknown.
// shortCircuit is required by Node >= 18.6 to designate no more loaders should be called.
@@ -85,9 +89,11 @@ async function load(moduleUrl: string, context: { format?: string }, defaultLoad
}
let transport: PortTransport | undefined;
+let legacyWaitForSourceMaps = false;
function initialize(data: { port: MessagePort }) {
transport = createTransport(data?.port);
+ legacyWaitForSourceMaps = !!process.env.PLAYWRIGHT_WAIT_FOR_SOURCE_MAPS;
}
function createTransport(port: MessagePort) {
diff --git a/packages/playwright/src/transform/portTransport.ts b/packages/playwright/src/transform/portTransport.ts
index f99e39f6ce..a0f4819131 100644
--- a/packages/playwright/src/transform/portTransport.ts
+++ b/packages/playwright/src/transform/portTransport.ts
@@ -24,11 +24,6 @@ export class PortTransport {
port.addEventListener('message', async event => {
const message = event.data;
const { id, ackId, method, params, result } = message;
- if (id) {
- const result = await handler(method, params);
- this._port.postMessage({ ackId: id, result });
- return;
- }
if (ackId) {
const callback = this._callbacks.get(ackId);
@@ -37,12 +32,20 @@ export class PortTransport {
callback?.(result);
return;
}
+
+ const handlerResult = await handler(method, params);
+ if (id)
+ this._port.postMessage({ ackId: id, result: handlerResult });
});
// Make sure to unref **after** adding a 'message' event listener.
// https://nodejs.org/api/worker_threads.html#portref
this._resetRef();
}
+ post(method: string, params: any) {
+ this._port.postMessage({ method, params });
+ }
+
async send(method: string, params: any) {
return await new Promise<any>(f => {
const id = ++this._lastId;
diff --git a/packages/playwright/src/transform/transform.ts b/packages/playwright/src/transform/transform.ts
index 1719fd504c..383180afba 100644
--- a/packages/playwright/src/transform/transform.ts
+++ b/packages/playwright/src/transform/transform.ts
@@ -260,8 +260,16 @@ export async function requireOrImport(file: string) {
installTransformIfNeeded();
const isModule = fileIsModule(file);
const esmImport = () => eval(`import(${JSON.stringify(url.pathToFileURL(file))})`);
- if (isModule)
- return await esmImport();
+ if (isModule) {
+ return await esmImport().finally(async () => {
+ // Compilation cache, which includes source maps, is populated in a post task.
+ // When importing a module results in an error, the very next access to `error.stack`
+ // will need source maps. To make sure source maps have arrived, we insert a task
+ // that will be processed after compilation cache and guarantee that
+ // source maps are available, before `error.stack` is accessed.
+ await new Promise(resolve => setTimeout(resolve, 0));
+ });
+ }
const result = require(file);
const depsCollector = currentFileDepsCollector();
if (depsCollector) {
diff --git a/tests/playwright-test/esm.spec.ts b/tests/playwright-test/esm.spec.ts
index 9f567b0f44..0e1deb9585 100644
--- a/tests/playwright-test/esm.spec.ts
+++ b/tests/playwright-test/esm.spec.ts
@@ -195,6 +195,32 @@ test('should use source maps when importing a file throws an error', async ({ ru
`);
});
+test('should use source maps when importing a file throws an error in legacy mode', {
+ annotation: { type: 'issue', description: 'https://github.com/microsoft/playwright/issues/35706' },
+}, async ({ runInlineTest }) => {
+ const result = await runInlineTest({
+ 'package.json': `{ "type": "module" }`,
+ 'playwright.config.ts': `
+ export default {};
+ `,
+ 'a.test.ts': `
+ import { test, expect } from '@playwright/test';
+
+ throw new Error('Oh my!');
+ `
+ }, {}, { 'PLAYWRIGHT_WAIT_FOR_SOURCE_MAPS': '1' });
+ expect(result.exitCode).toBe(1);
+ expect(result.output).toContain(`Error: Oh my!
+
+ at a.test.ts:4
+
+ 2 | import { test, expect } from '@playwright/test';
+ 3 |
+> 4 | throw new Error('Oh my!');
+ | ^
+ `);
+});
+
test('should show the codeframe in errors', async ({ runInlineTest }) => {
const result = await runInlineTest({
'package.json': `{ "type": "module" }`,
@@ -764,3 +790,69 @@ test('should exit after merge-reports', async ({ runInlineTest, mergeReports })
const { exitCode } = await mergeReports(test.info().outputPath('blob-report'), undefined, { additionalArgs: ['-c', 'merge.config.ts'] });
expect(exitCode).toBe(0);
});
+
+test('should load esm -> cjs -> cjs require tree without stalling', {
+ annotation: { type: 'issue', description: 'https://github.com/microsoft/playwright/issues/35706' },
+}, async ({ runInlineTest }) => {
+ test.setTimeout(10000);
+ const result = await runInlineTest({
+ 'playwright.config.js': `
+ export default { projects: [{name: 'foo'}] };
+ `,
+ 'package.json': JSON.stringify({ type: 'module' }),
+ 'a.esm.test.js': `
+ import { test, expect } from '@playwright/test';
+ import * as root from './nested/root_require.js';
+ test('check project name', ({}, testInfo) => {
+ console.log('root', root);
+ expect(testInfo.project.name).toBe('foo');
+ });
+ `,
+ 'nested/package.json': JSON.stringify({ type: 'commonjs' }),
+ 'nested/root_require.js': `
+ const nested = require('./nested_require.js');
+ console.log(nested);
+ `,
+ 'nested/nested_require.js': `
+ console.log('nested require');
+ `,
+ });
+
+ expect(result.exitCode).toBe(0);
+ expect(result.passed).toBe(1);
+});
+
+test('should have source maps when throwing during nested module import', {
+ annotation: { type: 'issue', description: 'https://github.com/microsoft/playwright/issues/35706' },
+}, async ({ runInlineTest }) => {
+ const result = await runInlineTest({
+ 'package.json': JSON.stringify({ type: 'module' }),
+ 'a.test.ts': `
+ import { test, expect } from '@playwright/test';
+ import { foo } from './import1.ts';
+ test('dummy', () => {
+ expect(foo).toBe('foo');
+ });
+ `,
+ 'import1.ts': `
+ export { foo } from './import2.ts';
+ `,
+ 'import2.ts': `
+ type Foo = { bar: string };
+ const fs = require('fs');
+ throw new Error('Oh no!');
+ export const foo: Foo = { bar: 'foo' };
+ console.log(fs.existsSync('does-not-exist.txt'));
+ `,
+ });
+
+ expect(result.exitCode).toBe(1);
+ expect(result.output).toContain(`require is not defined in ES module scope`);
+ expect(result.output).toContain([
+ ` 2 | type Foo = { bar: string };`,
+ `> 3 | const fs = require('fs');`,
+ ` | ^`,
+ ].join('\n'));
+
+ expect(result.output).toContain(`at import2.ts:3`);
+});