From 7f4b67cd7a010eabce74774d4ce17f182a7c812e Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Wed, 21 Oct 2026 18:19:01 -0400 Subject: [PATCH] chore: Recover dev server after dependencies change (#14211) * chore: Restart dev server after yarn install and report missing dependencies * fix: Restart dev server from Yarn install state after postinstall --- build.js | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/build.js b/build.js index 1b7361441d..f282fe6f40 100755 --- a/build.js +++ b/build.js @@ -7,6 +7,7 @@ const { existsSync, mkdirSync, readdirSync, + readFileSync, rmSync, statSync, watch, @@ -112,6 +113,19 @@ function getDest(file) { return undefined; } +/** + * The dependencies in package.json that are not installed. + * @return {string[]} the names of the missing packages. + */ +function getMissingDependencies() { + const { dependencies = {} } = JSON.parse( + readFileSync("package.json", "utf8") + ); + return Object.keys(dependencies).filter( + (name) => !existsSync(path.join("node_modules", name, "package.json")) + ); +} + /** * Builds, then watches the source directories and updates the output of each * changed, added or removed file. A full build runs again after a failed one. @@ -129,6 +143,14 @@ async function watchSources(command) { let isBuildPending = false; const startServer = () => { + const missing = getMissingDependencies(); + if (missing.length) { + console.log( + `Missing dependencies: ${missing.join(", ")}. Run "yarn install", the server starts when it completes.` + ); + return; + } + // Run in a new process group so the forked service processes are stopped // together with the master. child = spawn(command[0], command.slice(1), { @@ -276,6 +298,15 @@ async function watchSources(command) { } }); + // Yarn writes the install state after the postinstall script, so a restart + // here picks up the dependencies of a newly checked out branch. + mkdirSync(".yarn", { recursive: true }); + watch(".yarn", (_event, filename) => { + if (filename === "install-state.gz") { + restartServer(); + } + }); + for (const signal of ["SIGINT", "SIGTERM"]) { process.once(signal, () => stopServer(() => process.exit(0))); }