Skip to content

Commit bee0454

Browse files
authored
chore: do not resource source maps under test (#40199)
1 parent b6c0a18 commit bee0454

4 files changed

Lines changed: 30 additions & 80 deletions

File tree

packages/playwright/src/transform/compilationCache.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@ import fs from 'fs';
1818
import os from 'os';
1919
import path from 'path';
2020

21-
import { calculateSha1 } from '@utils/crypto';
2221
import sourceMapSupport from 'source-map-support';
22+
import { calculateSha1 } from '@utils/crypto';
23+
import { isUnderTest } from '@utils/debug';
24+
2325
import { isWorkerProcess } from '../globals';
2426
import { packageRoot } from '../package';
2527

@@ -67,13 +69,17 @@ const fileDependencies = new Map<string, Set<string>>();
6769
// Dependencies resolved by the external bundler.
6870
const externalDependencies = new Map<string, Set<string>>();
6971

72+
const devSourceInfix = path.sep + 'playwright' + path.sep + 'packages' + path.sep;
73+
7074
export function installSourceMapSupport() {
7175
Error.stackTraceLimit = 200;
7276

7377
sourceMapSupport.install({
7478
environment: 'node',
7579
handleUncaughtExceptions: false,
7680
retrieveSourceMap(source) {
81+
if (!process.env.PWDEBUGIMPL && isUnderTest() && source.includes(devSourceInfix))
82+
return { map: identitySourceMap(source), url: source };
7783
if (!sourceMaps.has(source))
7884
return null;
7985
const sourceMapPath = sourceMaps.get(source)!;
@@ -89,6 +95,15 @@ export function installSourceMapSupport() {
8995
});
9096
}
9197

98+
function identitySourceMap(source: string) {
99+
const lineCount = fs.readFileSync(source, 'utf8').split('\n').length;
100+
return {
101+
version: 3,
102+
sources: [source],
103+
mappings: lineCount ? 'AAAA' + ';AACA'.repeat(lineCount - 1) : '',
104+
};
105+
}
106+
92107
function _innerAddToCompilationCacheAndSerialize(filename: string, entry: MemoryCache) {
93108
sourceMaps.set(entry.moduleUrl || filename, entry.sourceMapPath);
94109
memoryCache.set(filename, entry);

packages/playwright/src/util.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ import type { TestCase } from './common/test';
3636

3737
const PLAYWRIGHT_TEST_PATH = path.join(__dirname, '..');
3838
const PLAYWRIGHT_CORE_PATH = path.dirname(require.resolve('playwright-core/package.json'));
39-
const PLAYWRIGHT_SRC_INFIX = ['', 'playwright', 'packages', ''].join(path.sep);
4039

4140
export function filterStackTrace(e: Error): { message: string, stack: string, cause?: ReturnType<typeof filterStackTrace> } {
4241
const name = e.name ? e.name + ': ' : '';
@@ -59,8 +58,6 @@ export function filterStackFile(file: string) {
5958
return false;
6059
if (file.startsWith(PLAYWRIGHT_CORE_PATH))
6160
return false;
62-
if (file.includes(PLAYWRIGHT_SRC_INFIX))
63-
return false;
6461
return true;
6562
}
6663

tests/mcp/playwright.config.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import { defineConfig } from '@playwright/test';
2121
import type { TestOptions } from './fixtures';
2222
import type { ReporterDescription } from '@playwright/test';
2323

24+
process.env.PWTEST_UNDER_TEST = '1';
25+
2426
dotenv.config({ path: path.resolve(__dirname, '../../.env'), quiet: true });
2527

2628
const rootTestDir = path.join(__dirname, '..');

utils/build/build.js

Lines changed: 12 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,12 @@ class EsbuildStep extends Step {
278278
constructor(options, watchPaths = []) {
279279
// Starting esbuild steps in parallel showed longer overall time.
280280
super({ concurrent: false });
281-
this._options = options;
281+
options = {
282+
sourcemap: withSourceMaps ? 'linked' : false,
283+
platform: 'node',
284+
format: 'cjs',
285+
...options,
286+
};
282287
this._watchPaths = watchPaths;
283288
if (options.bundle) {
284289
// For bundled outputs we always want a metafile so we can emit a
@@ -292,6 +297,7 @@ class EsbuildStep extends Step {
292297
if (!options.logOverride['direct-eval'])
293298
options.logOverride['direct-eval'] = 'silent';
294299
}
300+
this._options = options;
295301
}
296302

297303
/** @override */
@@ -539,9 +545,6 @@ for (const pkg of workspace.packages()) {
539545
steps.push(new EsbuildStep({
540546
entryPoints: [path.join(pkg.path, 'src/**/*.ts')],
541547
outdir: `${path.join(pkg.path, 'lib')}`,
542-
sourcemap: withSourceMaps ? 'linked' : false,
543-
platform: 'node',
544-
format: 'cjs',
545548
plugins: [dynamicImportToRequirePlugin],
546549
}));
547550
}
@@ -565,9 +568,6 @@ steps.push(new EsbuildStep({
565568
filePath('packages/playwright-core/src/tools/utils/socketConnection.ts'),
566569
],
567570
outdir: filePath('packages/playwright-core/lib'),
568-
sourcemap: withSourceMaps ? 'linked' : false,
569-
platform: 'node',
570-
format: 'cjs',
571571
plugins: [dynamicImportToRequirePlugin],
572572
}));
573573

@@ -578,55 +578,17 @@ steps.push(new EsbuildStep({
578578
bundle: true,
579579
entryPoints: [filePath('packages/playwright-core/src/utilsBundle.ts')],
580580
outfile: filePath('packages/playwright-core/lib/utilsBundle.js'),
581-
sourcemap: withSourceMaps ? 'linked' : false,
582-
platform: 'node',
583-
format: 'cjs',
584581
external: ['fsevents', 'express', '@anthropic-ai/sdk'],
585582
alias: {
586583
'raw-body': filePath('utils/build/raw-body.ts'),
587584
},
588585
}, [filePath('packages/playwright-core/src/utilsBundle.ts'), filePath('utils/build/raw-body.ts')]));
589586

590-
// Any import whose specifier ends with `/utilsBundle` (relative, any
591-
// depth — the dynamic-import-to-require plugin emits depth-based paths
592-
// like `./utilsBundle`, `../utilsBundle`, `../../utilsBundle` depending
593-
// on the source file) must be kept external and rewritten to the sibling
594-
// `./utilsBundle` at bundle output level, because the bundle file lives
595-
// at lib/coreBundle.js right next to lib/utilsBundle.js regardless of
596-
// where the original source lived.
597-
const externalizeUtilsBundlePlugin = {
598-
name: 'externalize-utilsBundle',
599-
setup(build) {
600-
build.onResolve({ filter: /utilsBundle/ }, args => {
601-
// Bare absolute specifier used by the dynamic-import-to-require
602-
// plugin for source files that live OUTSIDE playwright-core/src
603-
// (e.g., packages/utils/*.ts). These end up inlined into
604-
// coreBundle.js, so the runtime path resolves correctly against
605-
// lib/coreBundle.js's sibling lib/utilsBundle.js.
606-
if (args.path === 'playwright-core/lib/utilsBundle')
607-
return { path: './utilsBundle', external: true };
608-
if (!args.path.startsWith('.'))
609-
return;
610-
// Match `<depth>/utilsBundle` or `<depth>/utilsBundle.js` so
611-
// depth-based relative paths like ./utilsBundle, ../utilsBundle,
612-
// ../../utilsBundle all normalize to a single external at the
613-
// bundle output level. The `.js` suffix form is used by direct
614-
// require() call sites (e.g. tools/cli-client/program.ts).
615-
if (!/(^|\/)utilsBundle(\.js)?$/.test(args.path))
616-
return;
617-
return { path: './utilsBundle', external: true };
618-
});
619-
},
620-
};
621-
622587
// Build playwright-core as a single bundle.
623588
steps.push(new EsbuildStep({
624589
bundle: true,
625590
entryPoints: [filePath('packages/playwright-core/src/coreBundle.ts')],
626591
outfile: filePath('packages/playwright-core/lib/coreBundle.js'),
627-
sourcemap: withSourceMaps ? 'linked' : false,
628-
platform: 'node',
629-
format: 'cjs',
630592
external: [
631593
'../../api.json',
632594
'./help.json',
@@ -637,7 +599,11 @@ steps.push(new EsbuildStep({
637599
'chromium-bidi/*',
638600
'mitt',
639601
],
640-
plugins: [externalizeUtilsBundlePlugin, dynamicImportToRequirePlugin],
602+
plugins: [{
603+
name: 'externalize-utilsBundle',
604+
setup: build => build.onResolve({ filter: /utilsBundle/ },
605+
() => ({ path: './utilsBundle', external: true })),
606+
}, dynamicImportToRequirePlugin],
641607
}, [playwrightCoreSrc]));
642608

643609
function assertCoreBundleHasNoNodeModules() {
@@ -673,9 +639,6 @@ steps.push(new CustomCallbackStep(assertCoreBundleHasNoNodeModules));
673639
bundle: true,
674640
entryPoints: [filePath('packages/playwright/src/transform/esmLoader.ts')],
675641
outfile: filePath('packages/playwright/lib/transform/esmLoader.js'),
676-
sourcemap: withSourceMaps ? 'linked' : false,
677-
platform: 'node',
678-
format: 'cjs',
679642
external: [
680643
'playwright-core',
681644
'playwright-core/*',
@@ -696,9 +659,6 @@ steps.push(new EsbuildStep({
696659
filePath('packages/playwright/src/mcp/**/*.ts'),
697660
],
698661
outdir: filePath('packages/playwright/lib'),
699-
sourcemap: withSourceMaps ? 'linked' : false,
700-
platform: 'node',
701-
format: 'cjs',
702662
plugins: [dynamicImportToRequirePlugin],
703663
}));
704664

@@ -709,9 +669,6 @@ steps.push(new EsbuildStep({
709669
bundle: true,
710670
entryPoints: [filePath('packages/playwright/src/transform/babelBundle.ts')],
711671
outfile: filePath('packages/playwright/lib/transform/babelBundle.js'),
712-
sourcemap: withSourceMaps ? 'linked' : false,
713-
platform: 'node',
714-
format: 'cjs',
715672
external: [
716673
'../package',
717674
],
@@ -723,9 +680,6 @@ steps.push(new EsbuildStep({
723680
bundle: true,
724681
entryPoints: [filePath('packages/playwright/src/matchers/expect.ts')],
725682
outfile: filePath('packages/playwright/lib/matchers/expect.js'),
726-
sourcemap: withSourceMaps ? 'linked' : false,
727-
platform: 'node',
728-
format: 'cjs',
729683
external: [
730684
'playwright-core',
731685
'playwright-core/*',
@@ -741,9 +695,6 @@ steps.push(new EsbuildStep({
741695
bundle: true,
742696
entryPoints: [filePath('packages/playwright/src/common/index.ts')],
743697
outfile: filePath('packages/playwright/lib/common/index.js'),
744-
sourcemap: withSourceMaps ? 'linked' : false,
745-
platform: 'node',
746-
format: 'cjs',
747698
external: [
748699
'playwright-core',
749700
'playwright-core/*',
@@ -762,9 +713,6 @@ steps.push(new EsbuildStep({
762713
bundle: true,
763714
entryPoints: [filePath('packages/playwright/src/runner/index.ts')],
764715
outfile: filePath('packages/playwright/lib/runner/index.js'),
765-
sourcemap: withSourceMaps ? 'linked' : false,
766-
platform: 'node',
767-
format: 'cjs',
768716
external: [
769717
'playwright-core',
770718
'playwright-core/*',
@@ -786,9 +734,6 @@ steps.push(new EsbuildStep({
786734
bundle: true,
787735
entryPoints: [filePath('packages/playwright/src/isomorphic/index.ts')],
788736
outfile: filePath('packages/playwright/lib/isomorphic.js'),
789-
sourcemap: withSourceMaps ? 'linked' : false,
790-
platform: 'node',
791-
format: 'cjs',
792737
external: [
793738
],
794739
plugins: [dynamicImportToRequirePlugin],
@@ -801,9 +746,6 @@ steps.push(new EsbuildStep({
801746
bundle: true,
802747
entryPoints: [filePath('packages/playwright/src/loader/loaderProcessEntry.ts')],
803748
outfile: filePath('packages/playwright/lib/loader/loaderProcessEntry.js'),
804-
sourcemap: withSourceMaps ? 'linked' : false,
805-
platform: 'node',
806-
format: 'cjs',
807749
external: [
808750
'playwright-core',
809751
'playwright-core/*',
@@ -823,9 +765,6 @@ steps.push(new EsbuildStep({
823765
bundle: true,
824766
entryPoints: [filePath('packages/playwright/src/worker/workerProcessEntry.ts')],
825767
outfile: filePath('packages/playwright/lib/worker/workerProcessEntry.js'),
826-
sourcemap: withSourceMaps ? 'linked' : false,
827-
platform: 'node',
828-
format: 'cjs',
829768
external: [
830769
'playwright-core',
831770
'playwright-core/*',
@@ -846,9 +785,6 @@ steps.push(new EsbuildStep({
846785
bundle: true,
847786
entryPoints: [filePath('packages/playwright-core/src/server/electron/loader.ts')],
848787
outfile: filePath('packages/playwright-core/lib/server/electron/loader.js'),
849-
sourcemap: withSourceMaps ? 'linked' : false,
850-
platform: 'node',
851-
format: 'cjs',
852788
external: ['electron'],
853789
}, [playwrightCoreSrc]));
854790

0 commit comments

Comments
 (0)