Webpack Build Conflict Prevention
Date: November 26, 2025
Category: Frontend / Build Tools
Status: Active
Problem
Section titled “Problem”Running yarn build while the webpack dev server (yarn wpd) is active causes conflicts because both processes try to write to the same output directory (public/javascripts/webpack/). This results in:
- Corrupted assets
- Build failures
- Cryptic error messages
- Wasted time debugging
Solution
Section titled “Solution”A pre-build check script that detects if the dev server is running and aborts with a helpful message.
How It Works
Section titled “How It Works”Detection Methods
Section titled “Detection Methods”The script (script/check_wpd_running.sh) checks two ways, both scoped to
the current working directory so a dev server running in a sibling worktree
does not block builds in this checkout:
- Lock File - Does
${PWD}/.wpd.lockexist? (set bywpd_start.sh) - Port Check - Is a process listening on port 8081 and is its
cwdequal to this directory? Resolved vialsof -d cwd(or/proc/<pid>/cwdon Linux), with both paths normalised throughpwd -P.
The earlier global curl http://localhost:8081/... manifest probe was
removed: it could not be made directory-aware, so it produced false
positives in worktrees.
Lock File Management
Section titled “Lock File Management”The dev server start script (script/wpd_start.sh):
- Creates
.wpd.lockon start - Removes it on exit (Ctrl+C, kill, etc.)
- Uses bash
trapto ensure cleanup
User Experience
Section titled “User Experience”When Dev Server is Running
Section titled “When Dev Server is Running”$ yarn build
╔══════════════════════════════════════════════════════════════════╗║ ⚠️ WEBPACK DEV SERVER IS RUNNING! ║╠══════════════════════════════════════════════════════════════════╣║ Cannot run 'yarn build' while dev server is active. ║║ Both processes write to the same output directory. ║╠══════════════════════════════════════════════════════════════════╣║ Options: ║║ 1. Stop the dev server first (Ctrl+C in that terminal) ║║ 2. Or use 'yarn wpd:rebuild:inplace' to trigger a rebuild ║║ within the running dev server ║╚══════════════════════════════════════════════════════════════════╝When Dev Server is Not Running
Section titled “When Dev Server is Not Running”$ yarn build# Proceeds normally with webpack build| File | Purpose |
|---|---|
script/check_wpd_running.sh | Pre-build detection script |
script/wpd_start.sh | Dev server start with lock file |
package.json | Updated build scripts |
.gitignore | Added .wpd.lock |
Scripts Updated
Section titled “Scripts Updated”{ "scripts": { "build": "./script/check_wpd_running.sh && yarn install && ...", "wpd": "./script/wpd_start.sh", "wp:rebuild": "./script/check_wpd_running.sh && ..." }}Protected Commands
Section titled “Protected Commands”| Command | Protection |
|---|---|
yarn build | ✅ Checks before build |
yarn build:p | ✅ Uses build, so protected |
yarn wp:rebuild | ✅ Checks before rebuild |
yarn wpd | Creates lock file |
yarn wpd:rebuild:inplace | Safe alternative for rebuilding |
Alternatives When Dev Server is Running
Section titled “Alternatives When Dev Server is Running”Instead of stopping the dev server, you can:
1. Trigger In-Place Rebuild
Section titled “1. Trigger In-Place Rebuild”yarn wpd:rebuild:inplaceThis touches webpack.config.js to trigger a rebuild within the running dev server.
2. Stop Dev Server First
Section titled “2. Stop Dev Server First”# In the dev server terminalCtrl+C
# Then buildyarn buildTechnical Details
Section titled “Technical Details”check_wpd_running.sh
Section titled “check_wpd_running.sh”#!/usr/bin/env bashset -euo pipefail
WPD_PORT="${WPD_PORT:-8081}"PWD_REAL="$(pwd -P)"WPD_LOCKFILE="${PWD_REAL}/.wpd.lock"
# Check lockfile (already PWD-scoped) or that the listener on the port has# its cwd in *this* checkout. A dev server in a sibling worktree won't match.check_lockfile() { [ -f "${WPD_LOCKFILE}" ]}
check_port_in_pwd() { local pids; pids="$(lsof -ti ":${WPD_PORT}" -sTCP:LISTEN 2>/dev/null || true)" [ -z "$pids" ] && return 1 for pid in $pids; do cwd="$(lsof -a -p "${pid}" -d cwd -Fn 2>/dev/null | awk '/^n/ {sub(/^n/,""); print; exit}')" cwd="$( cd "$cwd" 2>/dev/null && pwd -P )" [ "$cwd" = "$PWD_REAL" ] && return 0 done return 1}
# Exit 1 if running here, 0 if notwpd_start.sh
Section titled “wpd_start.sh”#!/usr/bin/env bashset -euo pipefail
WPD_LOCKFILE="${PWD}/.wpd.lock"
# Cleanup on exitcleanup() { rm -f "${WPD_LOCKFILE}"}trap cleanup EXIT INT TERM
# Create lock fileecho $$ > "${WPD_LOCKFILE}"
# Start webpack dev serverwebpack serve --profile --host 0.0.0.0Troubleshooting
Section titled “Troubleshooting”Lock file exists but dev server not running
Section titled “Lock file exists but dev server not running”rm .wpd.lockThis can happen if the dev server crashed without cleanup.
Check is too slow
Section titled “Check is too slow”The HTTP check has a 1-second timeout. If your network is slow, increase it in the script or rely on port/lockfile checks.
See Also
Section titled “See Also”- ASSET_BUILD_WORKFLOW.md - General asset build process
- YARN_SETUP.md - Yarn configuration