|
3 | 3 | /** |
4 | 4 | * External dependencies |
5 | 5 | */ |
6 | | -import { spawn } from 'child_process'; |
| 6 | +import { execSync, spawn } from 'child_process'; |
7 | 7 | import { fileURLToPath } from 'url'; |
8 | 8 | import path from 'path'; |
9 | 9 | import fs from 'fs'; |
@@ -259,4 +259,60 @@ async function dev() { |
259 | 259 | } |
260 | 260 | } |
261 | 261 |
|
| 262 | +/** |
| 263 | + * Warn if a webpack process is watching this checkout. A stale webpack |
| 264 | + * dev server (e.g. left over from a previous setup) can clobber the |
| 265 | + * non-minified index.js files in build/scripts/ with webpack chunk |
| 266 | + * format, breaking the editor when SCRIPT_DEBUG is enabled. |
| 267 | + * |
| 268 | + * Best-effort and intentionally non-fatal: webpack processes that |
| 269 | + * happen to be running for an unrelated project shouldn't block |
| 270 | + * `npm run dev`. Match against this checkout's path (with trailing /) |
| 271 | + * to avoid false positives on sibling directories. |
| 272 | + */ |
| 273 | +function checkForConflictingProcesses() { |
| 274 | + try { |
| 275 | + const ps = execSync( 'ps aux', { encoding: 'utf-8' } ); |
| 276 | + const repoPathPrefix = ROOT_DIR + '/'; |
| 277 | + const webpackLines = ps |
| 278 | + .split( '\n' ) |
| 279 | + .filter( |
| 280 | + ( line ) => |
| 281 | + /webpack/.test( line ) && |
| 282 | + ! /grep/.test( line ) && |
| 283 | + ! line.includes( String( process.pid ) ) && |
| 284 | + line.includes( repoPathPrefix ) |
| 285 | + ); |
| 286 | + |
| 287 | + if ( webpackLines.length === 0 ) { |
| 288 | + return; |
| 289 | + } |
| 290 | + |
| 291 | + const pids = webpackLines |
| 292 | + .map( ( line ) => line.trim().split( /\s+/ )[ 1 ] ) |
| 293 | + .filter( Boolean ); |
| 294 | + |
| 295 | + console.warn( |
| 296 | + `\n⚠️ Found ${ pids.length } webpack process(es) that look like they may be watching this checkout:\n` |
| 297 | + ); |
| 298 | + for ( const line of webpackLines ) { |
| 299 | + console.warn( ` ${ line.trim() }` ); |
| 300 | + } |
| 301 | + console.warn( |
| 302 | + `\nA stale webpack dev server can overwrite esbuild output in build/scripts/` |
| 303 | + ); |
| 304 | + console.warn( |
| 305 | + `with webpack chunk format, breaking the editor when SCRIPT_DEBUG is enabled.` |
| 306 | + ); |
| 307 | + console.warn( |
| 308 | + `If things look wrong, kill them with: kill -9 ${ pids.join( |
| 309 | + ' ' |
| 310 | + ) }\n` |
| 311 | + ); |
| 312 | + } catch { |
| 313 | + // If ps fails, just continue — this is a best-effort check. |
| 314 | + } |
| 315 | +} |
| 316 | + |
| 317 | +checkForConflictingProcesses(); |
262 | 318 | dev(); |
0 commit comments