Skip to content

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

chore(build): fix MaxListenersExceededWarning during build (#35905)

Max Schmitt64c553bb7182
diff --git a/utils/build/build.js b/utils/build/build.js
index 2d4e856e0c..a37acd86d1 100644
--- a/utils/build/build.js
+++ b/utils/build/build.js
@@ -57,6 +57,8 @@ const { build, context } = require('esbuild');
* @typedef {CommandOnChange|ScriptOnChange} OnChange
*/
+/** @type {(() => void)[]} */
+const disposables = [];
/** @type {Step[]} */
const steps = [];
/** @type {OnChange[]} */
@@ -130,7 +132,10 @@ class ProgramStep extends Step {
},
cwd: step.cwd,
});
- process.on('exit', () => child.kill());
+ disposables.push(() => {
+ if (child.exitCode === null)
+ child.kill();
+ });
return new Promise((resolve, reject) => {
child.on('close', (code, signal) => {
if (code || signal)
@@ -299,6 +304,7 @@ class EsbuildStep extends Step {
if (this._context)
return;
this._context = await context(this._options);
+ disposables.push(() => this._context?.dispose());
const watcher = chokidar.watch(this._options.entryPoints);
await new Promise(x => watcher.once('ready', x));
@@ -540,4 +546,20 @@ if (lintMode) {
}
}
+let cleanupCalled = false;
+function cleanup() {
+ if (cleanupCalled)
+ return;
+ cleanupCalled = true;
+ for (const disposable of disposables) {
+ try {
+ disposable();
+ } catch (e) {
+ console.error('Error during cleanup:', e);
+ }
+ }
+}
+process.on('exit', cleanup);
+process.on('SIGINT', cleanup);
+
watchMode ? runWatch() : runBuild();