Fix watcher rebuild on page add and delete

This commit is contained in:
2026-10-13 15:18:41 +01:00
parent 7bab610b38
commit 4238d9a4ea
7 changed files with 153 additions and 27 deletions
+55 -13
View File
@@ -5,7 +5,8 @@ import grabDirNames from "../../utils/grab-dir-names";
import fullRebuild from "./full-rebuild";
import { AppData } from "../../data/app-data";
import checkExcludedPatterns from "../../utils/check-excluded-patterns";
import allPagesESBuildContextBundler from "../bundler/all-pages-esbuild-context-bundler";
import allPagesESBuildContextBundler, { isHydrationEntryPage, } from "../bundler/all-pages-esbuild-context-bundler";
import grabAllPages from "../../utils/grab-all-pages";
import serverPostBuildFn from "./server-post-build-fn";
import { log } from "../../utils/log";
import { logBuildErrorsOnce } from "../../utils/build-error-log";
@@ -23,13 +24,28 @@ export function restartWatcher() {
return;
if (Date.now() - last_watcher_restart < 1000)
return;
// Files created between close() and the new watcher's initial scan
// completing emit no `add` (ignoreInitial treats them as pre-existing).
// Snapshot the page set so the new watcher can detect them on `ready`.
const pages_snapshot = grabPagesSnapshot();
try {
global.BUNEXT_PAGES_SRC_WATCHER.close();
}
catch (error) { }
global.BUNEXT_PAGES_SRC_WATCHER = undefined;
last_watcher_restart = Date.now();
watcherEsbuildCTX();
watcherEsbuildCTX({ pages_snapshot });
}
function grabPagesSnapshot() {
try {
return grabAllPages()
.map((p) => p.local_path)
.sort()
.join("\n");
}
catch (error) {
return undefined;
}
}
/**
* True when the watcher was last (re)created long enough ago that the
@@ -47,12 +63,21 @@ const REBUILD_DEBOUNCE_MS = 200;
let pending_structural = false;
let pending_content_file = undefined;
let debounce_timer = undefined;
// Guards against overlapping runners: BUNEXT_RECOMPILING alone is not enough
// since the artifact tracker clears it mid-run.
let runner_active = false;
function scheduleRebuild() {
if (debounce_timer) {
clearTimeout(debounce_timer);
}
debounce_timer = setTimeout(async () => {
debounce_timer = undefined;
// Another rebuild (fullRebuild, or a runner already looping) owns the
// bundler. Retry later rather than dropping the pending events.
if (runner_active || global.BUNEXT_RECOMPILING) {
scheduleRebuild();
return;
}
try {
await runRebuilds();
}
@@ -62,10 +87,6 @@ function scheduleRebuild() {
logBuildErrorsOnce([{ text: `Watcher rebuild failed: ${msg}` }], (line) => log.error(line));
}
}
finally {
global.BUNEXT_RECOMPILING = false;
global.BUNEXT_IS_SERVER_COMPONENT = false;
}
}, REBUILD_DEBOUNCE_MS);
}
/**
@@ -76,9 +97,7 @@ function scheduleRebuild() {
* tracker then queues the single-flighted SSR rebuild and pushes HMR updates.
*/
async function runRebuilds() {
if (global.BUNEXT_RECOMPILING) {
return;
}
runner_active = true;
global.BUNEXT_RECOMPILING = true;
global.BUNEXT_IS_SERVER_COMPONENT = false;
try {
@@ -124,6 +143,7 @@ async function runRebuilds() {
}
}
finally {
runner_active = false;
global.BUNEXT_RECOMPILING = false;
global.BUNEXT_IS_SERVER_COMPONENT = false;
}
@@ -174,13 +194,23 @@ function buildIgnoredPatterns() {
}
return ignored;
}
export default async function watcherEsbuildCTX() {
export default async function watcherEsbuildCTX(params) {
const pages_src_watcher = chokidar.watch(ROOT_DIR, {
ignored: buildIgnoredPatterns(),
persistent: true,
ignoreInitial: true,
});
watcherHandlers(pages_src_watcher, (event, filePath) => handleEvent(event, filePath));
const pages_snapshot = params?.pages_snapshot;
if (pages_snapshot !== undefined) {
pages_src_watcher.on("ready", () => {
const current = grabPagesSnapshot();
if (current !== undefined && current !== pages_snapshot) {
pending_structural = true;
scheduleRebuild();
}
});
}
global.BUNEXT_PAGES_SRC_WATCHER = pages_src_watcher;
}
function handleEvent(event, filePath) {
@@ -220,10 +250,22 @@ function handleEvent(event, filePath) {
return;
}
if (event === "change") {
if (filename.match(TARGET_FILES_MATCH)) {
pending_content_file = full_file_path;
scheduleRebuild();
if (!filename.match(TARGET_FILES_MATCH)) {
return;
}
// A page that isn't a hydration entry yet (e.g. created empty,
// then given its default export) needs the entry set recomputed;
// an incremental rebuild of the existing context would miss it.
if (filename.match(/^src\/pages\//) &&
!filename.match(/^src\/pages\/api\/|\.server\.tsx?$/) &&
!checkExcludedPatterns({ path: filename }) &&
!isHydrationEntryPage(full_file_path)) {
pending_structural = true;
}
else {
pending_content_file = full_file_path;
}
scheduleRebuild();
return;
}
// add / unlink / addDir / unlinkDir