Commit ecf1e419 authored by Tobias Koppers's avatar Tobias Koppers Committed by Tobias Koppers

fix: use marker file for .next deletion detection

The polling approach checked existsSync on the distDir, but:
1. In dev mode, config.distDir is .next/dev, not .next
2. Bundlers (webpack/turbopack) recreate the directory structure almost
   immediately after deletion, making the check always pass

Write a marker file (server-marker) inside the distDir at server start.
Poll for its existence instead - when .next is deleted, the marker file
disappears and is never recreated by the bundler, reliably detecting
the deletion. Also reduce poll interval from 2s to 500ms for faster
detection.
parent 8a93f700
...@@ -7,7 +7,7 @@ import type { NextUrlWithParsedQuery, RequestMeta } from '../request-meta' ...@@ -7,7 +7,7 @@ import type { NextUrlWithParsedQuery, RequestMeta } from '../request-meta'
import '../node-environment' import '../node-environment'
import '../require-hook' import '../require-hook'
import { existsSync } from 'fs' import { existsSync, mkdirSync, writeFileSync } from 'fs'
import url from 'url' import url from 'url'
import path from 'path' import path from 'path'
import loadConfig, { type ConfiguredExperimentalFeature } from '../config' import loadConfig, { type ConfiguredExperimentalFeature } from '../config'
...@@ -140,9 +140,14 @@ export async function initialize(opts: { ...@@ -140,9 +140,14 @@ export async function initialize(opts: {
let originalFetch = globalThis.fetch let originalFetch = globalThis.fetch
const distDir = path.join(opts.dir, config.distDir) // distDirRoot is the user-visible directory (e.g. .next), while
const distDirLabel = // config.distDir may be .next/dev in development mode.
const distDirRoot =
(config as NextConfigComplete).distDirRoot || config.distDir (config as NextConfigComplete).distDirRoot || config.distDir
// Marker file used to detect distDir deletion. Bundlers may recreate
// the directory structure immediately, so we check a marker file that
// only gets written once at server start.
let distDirMarker = ''
if (opts.dev) { if (opts.dev) {
const { Telemetry } = const { Telemetry } =
...@@ -230,15 +235,21 @@ export async function initialize(opts: { ...@@ -230,15 +235,21 @@ export async function initialize(opts: {
// We use polling because fs.watch is unreliable for detecting directory // We use polling because fs.watch is unreliable for detecting directory
// deletion across platforms (on Linux the inotify watch is destroyed with // deletion across platforms (on Linux the inotify watch is destroyed with
// the directory; on macOS kqueue may not fire at all). // the directory; on macOS kqueue may not fire at all).
// We check a marker file rather than the directory itself, because
// bundlers may recreate the directory structure almost immediately
// after deletion.
distDirMarker = path.join(opts.dir, config.distDir, 'server-marker')
mkdirSync(path.dirname(distDirMarker), { recursive: true })
writeFileSync(distDirMarker, '')
const distDirPollInterval = setInterval(() => { const distDirPollInterval = setInterval(() => {
if (!existsSync(distDir)) { if (!existsSync(distDirMarker)) {
clearInterval(distDirPollInterval) clearInterval(distDirPollInterval)
Log.warn( Log.warn(
`The ${distDirLabel} directory was removed while the dev server was running. Restarting...` `The ${distDirRoot} directory was removed while the dev server was running. Restarting...`
) )
process.exit(RESTART_EXIT_CODE) process.exit(RESTART_EXIT_CODE)
} }
}, 2000) }, 500)
distDirPollInterval.unref() distDirPollInterval.unref()
} }
...@@ -722,9 +733,9 @@ export async function initialize(opts: { ...@@ -722,9 +733,9 @@ export async function initialize(opts: {
await handleRequest(0) await handleRequest(0)
} catch (err) { } catch (err) {
// If the distDir was deleted, restart instead of showing a 500 error. // If the distDir was deleted, restart instead of showing a 500 error.
if (opts.dev && !existsSync(distDir)) { if (distDirMarker && !existsSync(distDirMarker)) {
Log.warn( Log.warn(
`The ${distDirLabel} directory was removed while the dev server was running. Restarting...` `The ${distDirRoot} directory was removed while the dev server was running. Restarting...`
) )
return process.exit(RESTART_EXIT_CODE) return process.exit(RESTART_EXIT_CODE)
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment