Skip to content

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

chore: do not resource source maps under test (#40199)

Pavel Feldmanbee0454d4fce
diff --git a/packages/playwright/src/transform/compilationCache.ts b/packages/playwright/src/transform/compilationCache.ts
index 345ee9d1a6..492d0ed090 100644
--- a/packages/playwright/src/transform/compilationCache.ts
+++ b/packages/playwright/src/transform/compilationCache.ts
@@ -18,8 +18,10 @@ import fs from 'fs';
import os from 'os';
import path from 'path';
-import { calculateSha1 } from '@utils/crypto';
import sourceMapSupport from 'source-map-support';
+import { calculateSha1 } from '@utils/crypto';
+import { isUnderTest } from '@utils/debug';
+
import { isWorkerProcess } from '../globals';
import { packageRoot } from '../package';
@@ -67,6 +69,8 @@ const fileDependencies = new Map<string, Set<string>>();
// Dependencies resolved by the external bundler.
const externalDependencies = new Map<string, Set<string>>();
+const devSourceInfix = path.sep + 'playwright' + path.sep + 'packages' + path.sep;
+
export function installSourceMapSupport() {
Error.stackTraceLimit = 200;
@@ -74,6 +78,8 @@ export function installSourceMapSupport() {
environment: 'node',
handleUncaughtExceptions: false,
retrieveSourceMap(source) {
+ if (!process.env.PWDEBUGIMPL && isUnderTest() && source.includes(devSourceInfix))
+ return { map: identitySourceMap(source), url: source };
if (!sourceMaps.has(source))
return null;
const sourceMapPath = sourceMaps.get(source)!;
@@ -89,6 +95,15 @@ export function installSourceMapSupport() {
});
}
+function identitySourceMap(source: string) {
+ const lineCount = fs.readFileSync(source, 'utf8').split('\n').length;
+ return {
+ version: 3,
+ sources: [source],
+ mappings: lineCount ? 'AAAA' + ';AACA'.repeat(lineCount - 1) : '',
+ };
+}
+
function _innerAddToCompilationCacheAndSerialize(filename: string, entry: MemoryCache) {
sourceMaps.set(entry.moduleUrl || filename, entry.sourceMapPath);
memoryCache.set(filename, entry);
diff --git a/packages/playwright/src/util.ts b/packages/playwright/src/util.ts
index 10eee9f9d1..ff1cacb6cd 100644
--- a/packages/playwright/src/util.ts
+++ b/packages/playwright/src/util.ts
@@ -36,7 +36,6 @@ import type { TestCase } from './common/test';
const PLAYWRIGHT_TEST_PATH = path.join(__dirname, '..');
const PLAYWRIGHT_CORE_PATH = path.dirname(require.resolve('playwright-core/package.json'));
-const PLAYWRIGHT_SRC_INFIX = ['', 'playwright', 'packages', ''].join(path.sep);
export function filterStackTrace(e: Error): { message: string, stack: string, cause?: ReturnType<typeof filterStackTrace> } {
const name = e.name ? e.name + ': ' : '';
@@ -59,8 +58,6 @@ export function filterStackFile(file: string) {
return false;
if (file.startsWith(PLAYWRIGHT_CORE_PATH))
return false;
- if (file.includes(PLAYWRIGHT_SRC_INFIX))
- return false;
return true;
}
diff --git a/tests/mcp/playwright.config.ts b/tests/mcp/playwright.config.ts
index 9087f29a48..bbc2671891 100644
--- a/tests/mcp/playwright.config.ts
+++ b/tests/mcp/playwright.config.ts
@@ -21,6 +21,8 @@ import { defineConfig } from '@playwright/test';
import type { TestOptions } from './fixtures';
import type { ReporterDescription } from '@playwright/test';
+process.env.PWTEST_UNDER_TEST = '1';
+
dotenv.config({ path: path.resolve(__dirname, '../../.env'), quiet: true });
const rootTestDir = path.join(__dirname, '..');
diff --git a/utils/build/build.js b/utils/build/build.js
index a4b5ae0770..e7063494f4 100644
--- a/utils/build/build.js
+++ b/utils/build/build.js
@@ -278,7 +278,12 @@ class EsbuildStep extends Step {
constructor(options, watchPaths = []) {
// Starting esbuild steps in parallel showed longer overall time.
super({ concurrent: false });
- this._options = options;
+ options = {
+ sourcemap: withSourceMaps ? 'linked' : false,
+ platform: 'node',
+ format: 'cjs',
+ ...options,
+ };
this._watchPaths = watchPaths;
if (options.bundle) {
// For bundled outputs we always want a metafile so we can emit a
@@ -292,6 +297,7 @@ class EsbuildStep extends Step {
if (!options.logOverride['direct-eval'])
options.logOverride['direct-eval'] = 'silent';
}
+ this._options = options;
}
/** @override */
@@ -539,9 +545,6 @@ for (const pkg of workspace.packages()) {
steps.push(new EsbuildStep({
entryPoints: [path.join(pkg.path, 'src/**/*.ts')],
outdir: `${path.join(pkg.path, 'lib')}`,
- sourcemap: withSourceMaps ? 'linked' : false,
- platform: 'node',
- format: 'cjs',
plugins: [dynamicImportToRequirePlugin],
}));
}
@@ -565,9 +568,6 @@ steps.push(new EsbuildStep({
filePath('packages/playwright-core/src/tools/utils/socketConnection.ts'),
],
outdir: filePath('packages/playwright-core/lib'),
- sourcemap: withSourceMaps ? 'linked' : false,
- platform: 'node',
- format: 'cjs',
plugins: [dynamicImportToRequirePlugin],
}));
@@ -578,55 +578,17 @@ steps.push(new EsbuildStep({
bundle: true,
entryPoints: [filePath('packages/playwright-core/src/utilsBundle.ts')],
outfile: filePath('packages/playwright-core/lib/utilsBundle.js'),
- sourcemap: withSourceMaps ? 'linked' : false,
- platform: 'node',
- format: 'cjs',
external: ['fsevents', 'express', '@anthropic-ai/sdk'],
alias: {
'raw-body': filePath('utils/build/raw-body.ts'),
},
}, [filePath('packages/playwright-core/src/utilsBundle.ts'), filePath('utils/build/raw-body.ts')]));
-// Any import whose specifier ends with `/utilsBundle` (relative, any
-// depth — the dynamic-import-to-require plugin emits depth-based paths
-// like `./utilsBundle`, `../utilsBundle`, `../../utilsBundle` depending
-// on the source file) must be kept external and rewritten to the sibling
-// `./utilsBundle` at bundle output level, because the bundle file lives
-// at lib/coreBundle.js right next to lib/utilsBundle.js regardless of
-// where the original source lived.
-const externalizeUtilsBundlePlugin = {
- name: 'externalize-utilsBundle',
- setup(build) {
- build.onResolve({ filter: /utilsBundle/ }, args => {
- // Bare absolute specifier used by the dynamic-import-to-require
- // plugin for source files that live OUTSIDE playwright-core/src
- // (e.g., packages/utils/*.ts). These end up inlined into
- // coreBundle.js, so the runtime path resolves correctly against
- // lib/coreBundle.js's sibling lib/utilsBundle.js.
- if (args.path === 'playwright-core/lib/utilsBundle')
- return { path: './utilsBundle', external: true };
- if (!args.path.startsWith('.'))
- return;
- // Match `<depth>/utilsBundle` or `<depth>/utilsBundle.js` so
- // depth-based relative paths like ./utilsBundle, ../utilsBundle,
- // ../../utilsBundle all normalize to a single external at the
- // bundle output level. The `.js` suffix form is used by direct
- // require() call sites (e.g. tools/cli-client/program.ts).
- if (!/(^|\/)utilsBundle(\.js)?$/.test(args.path))
- return;
- return { path: './utilsBundle', external: true };
- });
- },
-};
-
// Build playwright-core as a single bundle.
steps.push(new EsbuildStep({
bundle: true,
entryPoints: [filePath('packages/playwright-core/src/coreBundle.ts')],
outfile: filePath('packages/playwright-core/lib/coreBundle.js'),
- sourcemap: withSourceMaps ? 'linked' : false,
- platform: 'node',
- format: 'cjs',
external: [
'../../api.json',
'./help.json',
@@ -637,7 +599,11 @@ steps.push(new EsbuildStep({
'chromium-bidi/*',
'mitt',
],
- plugins: [externalizeUtilsBundlePlugin, dynamicImportToRequirePlugin],
+ plugins: [{
+ name: 'externalize-utilsBundle',
+ setup: build => build.onResolve({ filter: /utilsBundle/ },
+ () => ({ path: './utilsBundle', external: true })),
+ }, dynamicImportToRequirePlugin],
}, [playwrightCoreSrc]));
function assertCoreBundleHasNoNodeModules() {
@@ -673,9 +639,6 @@ steps.push(new CustomCallbackStep(assertCoreBundleHasNoNodeModules));
bundle: true,
entryPoints: [filePath('packages/playwright/src/transform/esmLoader.ts')],
outfile: filePath('packages/playwright/lib/transform/esmLoader.js'),
- sourcemap: withSourceMaps ? 'linked' : false,
- platform: 'node',
- format: 'cjs',
external: [
'playwright-core',
'playwright-core/*',
@@ -696,9 +659,6 @@ steps.push(new EsbuildStep({
filePath('packages/playwright/src/mcp/**/*.ts'),
],
outdir: filePath('packages/playwright/lib'),
- sourcemap: withSourceMaps ? 'linked' : false,
- platform: 'node',
- format: 'cjs',
plugins: [dynamicImportToRequirePlugin],
}));
@@ -709,9 +669,6 @@ steps.push(new EsbuildStep({
bundle: true,
entryPoints: [filePath('packages/playwright/src/transform/babelBundle.ts')],
outfile: filePath('packages/playwright/lib/transform/babelBundle.js'),
- sourcemap: withSourceMaps ? 'linked' : false,
- platform: 'node',
- format: 'cjs',
external: [
'../package',
],
@@ -723,9 +680,6 @@ steps.push(new EsbuildStep({
bundle: true,
entryPoints: [filePath('packages/playwright/src/matchers/expect.ts')],
outfile: filePath('packages/playwright/lib/matchers/expect.js'),
- sourcemap: withSourceMaps ? 'linked' : false,
- platform: 'node',
- format: 'cjs',
external: [
'playwright-core',
'playwright-core/*',
@@ -741,9 +695,6 @@ steps.push(new EsbuildStep({
bundle: true,
entryPoints: [filePath('packages/playwright/src/common/index.ts')],
outfile: filePath('packages/playwright/lib/common/index.js'),
- sourcemap: withSourceMaps ? 'linked' : false,
- platform: 'node',
- format: 'cjs',
external: [
'playwright-core',
'playwright-core/*',
@@ -762,9 +713,6 @@ steps.push(new EsbuildStep({
bundle: true,
entryPoints: [filePath('packages/playwright/src/runner/index.ts')],
outfile: filePath('packages/playwright/lib/runner/index.js'),
- sourcemap: withSourceMaps ? 'linked' : false,
- platform: 'node',
- format: 'cjs',
external: [
'playwright-core',
'playwright-core/*',
@@ -786,9 +734,6 @@ steps.push(new EsbuildStep({
bundle: true,
entryPoints: [filePath('packages/playwright/src/isomorphic/index.ts')],
outfile: filePath('packages/playwright/lib/isomorphic.js'),
- sourcemap: withSourceMaps ? 'linked' : false,
- platform: 'node',
- format: 'cjs',
external: [
],
plugins: [dynamicImportToRequirePlugin],
@@ -801,9 +746,6 @@ steps.push(new EsbuildStep({
bundle: true,
entryPoints: [filePath('packages/playwright/src/loader/loaderProcessEntry.ts')],
outfile: filePath('packages/playwright/lib/loader/loaderProcessEntry.js'),
- sourcemap: withSourceMaps ? 'linked' : false,
- platform: 'node',
- format: 'cjs',
external: [
'playwright-core',
'playwright-core/*',
@@ -823,9 +765,6 @@ steps.push(new EsbuildStep({
bundle: true,
entryPoints: [filePath('packages/playwright/src/worker/workerProcessEntry.ts')],
outfile: filePath('packages/playwright/lib/worker/workerProcessEntry.js'),
- sourcemap: withSourceMaps ? 'linked' : false,
- platform: 'node',
- format: 'cjs',
external: [
'playwright-core',
'playwright-core/*',
@@ -846,9 +785,6 @@ steps.push(new EsbuildStep({
bundle: true,
entryPoints: [filePath('packages/playwright-core/src/server/electron/loader.ts')],
outfile: filePath('packages/playwright-core/lib/server/electron/loader.js'),
- sourcemap: withSourceMaps ? 'linked' : false,
- platform: 'node',
- format: 'cjs',
external: ['electron'],
}, [playwrightCoreSrc]));