Unverified Commit 88155cd4 authored by Will Binns-Smith's avatar Will Binns-Smith Committed by GitHub

Turbopack: aggregate server HMR into one subscription (#94948)

This replaces per-chunk Server HMR turbo tasks with a single firehose
subscription that diffs every HMR chunk. This significantly cuts the
number of tokio task churn on projects with many server chunks and
centralizes the diff/clear logic. It leads to a multi-second saving in
both cold and warm builds in a large app.

This PR also renames `clear()` to `reEvaluateAllModulesExpensive()` to
label directly that this is a costly operation and should only be done
in exceptional circumstances.
    
A following PR will bring this to client chunks.
    
Rust:
- New `aggregate_hmr` module: `AggregateHmrVersion` keyed by chunk path,
`merged_partial_update` builder, and `is_hmr_eligible_chunk` (excludes
`.map` files, which would force every diff to `Total`).
- `Project::all_hmr_version_state` / `all_hmr_update` aggregate over the
whole `hmr_root_path`. The seed transition emits an empty `Partial` so
the JS consumer doesn't treat it as a restart and wipe handlers the
triggering request just populated. Any chunk requiring `Total`/`Missing`
escalates the batch.
- `VersionedContentMap::hmr_chunks_in_path` lists eligible chunks with
their `VersionedContent`.
    
NAPI:
  - `projectAllHmrEvents(target)` returns a single subscription.
    
JS:
- `setupServerHmr` subscribes once via `allHmrEvents` instead of fanning
out over `hmrChunkNamesSubscribe`.
- `reEvaluateAllModulesExpensive()` evicts every chunk under
`server/chunks/` from `require.cache` directly rather than tracking
subscriptions. This is a bit fragile as it relies on the path prefix and
scanning require.cache.
parent 267d6b1a
use std::sync::Arc;
use anyhow::Result;
use rustc_hash::FxHashMap;
use turbo_rcstr::RcStr;
use turbo_tasks::{FxIndexMap, ReadRef, ResolvedVc, TraitRef, TryJoinIterExt, Vc};
use turbo_tasks_fs::FileSystemPath;
use turbo_tasks_hash::{Xxh3Hash64Hasher, encode_base64};
use turbopack_core::version::{PartialUpdate, Update, Version, VersionState, VersionedContent};
use crate::versioned_content_map::VersionedContentMap;
pub struct HmrChunkWithContent {
pub path: RcStr,
pub content: ResolvedVc<Box<dyn VersionedContent>>,
}
/// Per-chunk versions keyed by path
#[turbo_tasks::value(serialization = "skip", shared)]
pub struct AggregateHmrVersion {
#[turbo_tasks(trace_ignore)]
pub versions: FxIndexMap<RcStr, TraitRef<Box<dyn Version>>>,
}
#[turbo_tasks::value_impl]
impl Version for AggregateHmrVersion {
#[turbo_tasks::function]
async fn id(&self) -> Result<Vc<RcStr>> {
let entries = self
.versions
.iter()
.map(|(path, version)| {
let path = path.clone();
let version = TraitRef::cell(version.clone());
async move {
let id = version.id().owned().await?;
Ok::<_, anyhow::Error>((path, id))
}
})
.try_join()
.await?;
let mut hasher = Xxh3Hash64Hasher::new();
hasher.write_value(entries.len());
for (path, id) in entries {
hasher.write_value(path.as_str());
hasher.write_value(id.as_str());
}
Ok(Vc::cell(encode_base64(hasher.finish()).into()))
}
}
impl AggregateHmrVersion {
pub async fn from_map(
map: Vc<VersionedContentMap>,
root: &FileSystemPath,
) -> Result<Vc<Box<dyn Version>>> {
// An empty `versions` map behaves the same as `NotFoundVersion` would in
// `diff_chunks_against`, so no special case is needed here.
let chunks = map.hmr_chunks_in_path(root).await?;
Ok(Vc::upcast(Self::from_chunks(&chunks).await?))
}
pub async fn from_chunks(chunks: &[HmrChunkWithContent]) -> Result<Vc<Self>> {
let versions = chunks
.iter()
.map(|HmrChunkWithContent { path, content }| {
let path = path.clone();
let content = *content;
async move {
let version = content.version().into_trait_ref().await?;
Ok::<_, anyhow::Error>((path, version))
}
})
.try_join()
.await?
.into_iter()
.collect();
Ok(Self { versions }.cell())
}
}
pub fn merge_ecmascript_merged_update(
combined_entries: &mut FxHashMap<String, serde_json::Value>,
combined_chunks: &mut FxHashMap<String, serde_json::Value>,
instruction: &serde_json::Value,
) {
let Some(obj) = instruction.as_object() else {
return;
};
if let Some(entries) = obj.get("entries").and_then(|v| v.as_object()) {
for (k, v) in entries {
combined_entries.insert(k.clone(), v.clone());
}
}
if let Some(chunks) = obj.get("chunks").and_then(|v| v.as_object()) {
for (k, v) in chunks {
combined_chunks.insert(k.clone(), v.clone());
}
}
}
/// Builds an `Update::Partial` whose instruction is a combined
/// `EcmascriptMergedUpdate` covering `entries` and `chunks`. Empty maps are
/// omitted so an empty `entries`/`chunks` field never appears in the payload.
///
/// Passing empty maps produces an instruction with only `type:
/// "EcmascriptMergedUpdate"`, used to advance `VersionState` to `to` without
/// the JS consumer applying anything: it sees a `partial` event with nothing
/// to apply and short-circuits.
pub fn merged_partial_update(
to: TraitRef<Box<dyn Version>>,
entries: FxHashMap<String, serde_json::Value>,
chunks: FxHashMap<String, serde_json::Value>,
) -> Update {
let mut instruction = serde_json::Map::new();
instruction.insert(
"type".to_string(),
serde_json::Value::String("EcmascriptMergedUpdate".to_string()),
);
if !entries.is_empty() {
instruction.insert(
"entries".to_string(),
serde_json::Value::Object(entries.into_iter().collect()),
);
}
if !chunks.is_empty() {
instruction.insert(
"chunks".to_string(),
serde_json::Value::Object(chunks.into_iter().collect()),
);
}
Update::Partial(PartialUpdate {
to,
instruction: Arc::new(serde_json::Value::Object(instruction)),
})
}
/// Per-chunk [`Update`]s computed against an `AggregateHmrVersion` snapshot.
/// `has_new_chunks` is true when the current snapshot contains chunks absent
/// from `from` (e.g. a new endpoint was written); callers decide whether that
/// affects the batch shape.
pub struct DiffResult {
pub chunk_updates: Vec<(RcStr, ReadRef<Update>)>,
pub has_new_chunks: bool,
}
/// Diffs each chunk against the [`AggregateHmrVersion`] held by `from`.
///
/// If `from` holds some other kind of `Version`, there's nothing meaningful to
/// diff against, so this returns no updates and leaves it to the caller to
/// decide what to do.
pub async fn diff_chunks_against(
chunks: &[HmrChunkWithContent],
from: Vc<VersionState>,
) -> Result<DiffResult> {
if chunks.is_empty() {
return Ok(DiffResult {
chunk_updates: Vec::new(),
has_new_chunks: false,
});
}
let from_resolved = from.get().to_resolved().await?;
let Some(from_aggregate) = ResolvedVc::try_downcast_type::<AggregateHmrVersion>(from_resolved)
else {
return Ok(DiffResult {
chunk_updates: Vec::new(),
has_new_chunks: false,
});
};
let from_aggregate = from_aggregate.await?;
let mut has_new_chunks = false;
let chunk_updates = chunks
.iter()
.filter_map(|HmrChunkWithContent { path, content }| {
let Some(prev) = from_aggregate.versions.get(path).cloned() else {
has_new_chunks = true;
return None;
};
Some((path.clone(), *content, TraitRef::cell(prev)))
})
.map(|(path, content, prev)| async move {
let update = content.update(prev).await?;
Ok::<_, anyhow::Error>((path, update))
})
.try_join()
.await?;
Ok(DiffResult {
chunk_updates,
has_new_chunks,
})
}
......@@ -2,6 +2,7 @@
#![feature(arbitrary_self_types_pointers)]
#![feature(impl_trait_in_assoc_type)]
mod aggregate_hmr;
pub mod analyze;
mod app;
mod asset_hashes_manifest;
......
......@@ -84,7 +84,8 @@ use turbopack_core::{
reference_type::{CommonJsReferenceSubType, ReferenceType},
resolve::{FindContextFileResult, find_context_file},
version::{
NotFoundVersion, OptionVersionedContent, Update, Version, VersionState, VersionedContent,
NotFoundVersion, OptionVersionedContent, PartialUpdate, TotalUpdate, Update, Version,
VersionState, VersionedContent,
},
};
#[cfg(feature = "process_pool")]
......@@ -95,6 +96,10 @@ use turbopack_node::worker_threads_backend;
use turbopack_nodejs::NodeJsChunkingContext;
use crate::{
aggregate_hmr::{
AggregateHmrVersion, DiffResult, diff_chunks_against, merge_ecmascript_merged_update,
merged_partial_update,
},
app::{AppProject, OptionAppProject},
empty::EmptyEndpoint,
entrypoints::Entrypoints,
......@@ -2554,6 +2559,125 @@ impl Project {
}
}
/// Aggregate counterpart to [`Self::hmr_version_state`]: one [`VersionState`]
/// covering every HMR-eligible chunk under `target`'s root. See
/// [`Self::all_hmr_update`].
#[turbo_tasks::function(session_dependent)]
pub async fn all_hmr_version_state(
self: ResolvedVc<Self>,
target: HmrTarget,
) -> Result<Vc<VersionState>> {
if target == HmrTarget::Client {
bail!("all_hmr_version_state is not yet implemented for the client target");
}
#[tracing::instrument(
level = "info",
name = "get aggregate HMR version",
skip_all,
fields(target = %target),
)]
#[turbo_tasks::function(operation, root)]
async fn aggregate_hmr_version_operation(
this: ResolvedVc<Project>,
target: HmrTarget,
) -> Result<Vc<Box<dyn Version>>> {
let Some(map) = this.await?.versioned_content_map else {
bail!("must be in dev mode to hmr")
};
let root = this.hmr_root_path(target).owned().await?;
AggregateHmrVersion::from_map(*map, &root).await
}
let version_op = aggregate_hmr_version_operation(self, target);
// INVALIDATION: untracked initial read; the subscription drives invalidation.
let state = VersionState::new(
version_op
.read_trait_strongly_consistent()
.untracked()
.await?,
)
.await?;
Ok(state)
}
/// Aggregate counterpart to [`Self::hmr_update`]: a single `Update` whose
/// `EcmascriptMergedUpdate` is the union of per-chunk diffs under
/// `target`'s root.
///
/// All-or-nothing restart: any chunk needing `Total`/`Missing` escalates
/// the whole batch to `Total` (the runtime can't partially restart). New
/// chunks absent from `from` are skipped; the runtime require()s them on
/// demand.
#[turbo_tasks::function]
pub async fn all_hmr_update(
self: Vc<Self>,
target: HmrTarget,
from: Vc<VersionState>,
) -> Result<Vc<Update>> {
if target == HmrTarget::Client {
bail!("all_hmr_update is not yet implemented for the client target");
}
let Some(map) = self.await?.versioned_content_map else {
bail!("must be in dev mode to hmr")
};
let root = self.hmr_root_path(target).owned().await?;
let chunks_versioned_content = map.hmr_chunks_in_path(&root).await?;
// No chunks to diff yet (e.g. before any endpoints have been written).
if chunks_versioned_content.is_empty() {
return Ok(Update::None.cell());
}
// Build `to` up front so we can return it on every escape hatch below.
let to_aggregate = AggregateHmrVersion::from_chunks(&chunks_versioned_content).await?;
let to_ref = Vc::upcast::<Box<dyn Version>>(to_aggregate)
.into_trait_ref()
.await?;
let DiffResult {
chunk_updates,
has_new_chunks,
} = diff_chunks_against(&chunks_versioned_content, from).await?;
// Nothing to apply, but `from` still needs to advance to `to`. Reaching
// here means `from` held a version we couldn't diff against (it wasn't an
// `AggregateHmrVersion`), so `diff_chunks_against` gave up and returned
// nothing. An empty `Partial` moves the subscription's state forward so
// the *next* change produces a real diff; returning `Total` instead would
// force a needless full re-evaluation.
if chunk_updates.is_empty() && !has_new_chunks {
return Ok(
merged_partial_update(to_ref, FxHashMap::default(), FxHashMap::default()).cell(),
);
}
let mut combined_entries: FxHashMap<String, serde_json::Value> = FxHashMap::default();
let mut combined_chunks: FxHashMap<String, serde_json::Value> = FxHashMap::default();
for (_path, update) in chunk_updates {
match &*update {
Update::None => {}
Update::Missing | Update::Total(_) => {
return Ok(Update::Total(TotalUpdate { to: to_ref }).cell());
}
Update::Partial(PartialUpdate { instruction, .. }) => {
merge_ecmascript_merged_update(
&mut combined_entries,
&mut combined_chunks,
instruction,
);
}
}
}
if combined_entries.is_empty() && combined_chunks.is_empty() && !has_new_chunks {
return Ok(Update::None.cell());
}
Ok(merged_partial_update(to_ref, combined_entries, combined_chunks).cell())
}
/// Gets a list of all HMR chunk names that can be subscribed to for the
/// specified target. Used by the dev server to set up server-side HMR
/// subscriptions for all Node.js App Router entries (pages and route
......
......@@ -9,12 +9,14 @@ use turbo_tasks::{
};
use turbo_tasks_fs::{FileContent, FileSystemPath};
use turbopack_core::{
asset::Asset,
asset::{Asset, AssetContent},
output::{ExpandedOutputAssets, OptionOutputAsset, OutputAsset},
source_map::GenerateSourceMap,
version::OptionVersionedContent,
};
use crate::aggregate_hmr::HmrChunkWithContent;
#[derive(
Clone, TraceRawVcs, PartialEq, Eq, ValueDebugFormat, Debug, NonLocalValue, Encode, Decode,
)]
......@@ -66,6 +68,9 @@ unsafe impl OperationValue for PathToOutputOperation {}
type OutputOperationToComputeEntry =
FxHashMap<OperationVc<ExpandedOutputAssets>, OperationVc<OptionMapEntry>>;
// TODO: Ideally this structure is never persisted, so new sessions start from scratch and don't
// accumulate entries or force rebuilds of all chunks when a new session is only interested in some
// of them. If this happens, this should have #[turbo_tasks::value(evict = "never")].
#[turbo_tasks::value]
pub struct VersionedContentMap {
// TODO: turn into a bi-directional multimap, ExpandedOutputAssets ->
......@@ -84,6 +89,59 @@ impl VersionedContentMap {
}
.resolved_cell()
}
/// Lists every HMR-eligible chunk under `root` with its [`VersionedContent`],
/// sorted by path.
///
/// `map_path_to_op` is an `FxHashMap`, whose iteration order
/// depends on bucket layout rather than insertion order, so the same set of
/// paths can come out in a different order across calls.
///
/// Since this map contains entries that span server and client contexts, changes for one
/// context can shift the internals of the map, making iteration order different for the same
/// set of paths.
pub async fn hmr_chunks_in_path(
self: Vc<Self>,
root: &FileSystemPath,
) -> Result<Vec<HmrChunkWithContent>> {
let this = self.await?;
// `State::get` returns a lock guard, which can't be held across the
// awaits below, so snapshot the keys and release it.
let paths: Vec<FileSystemPath> = {
let map = &this.map_path_to_op.get().0;
map.keys().cloned().collect()
};
let mut chunks = paths
.into_iter()
.filter_map(|path| {
let rel = root.get_path_to(&path)?;
// Skip source maps: they aren't HMR-eligible chunks.
if rel.ends_with(".map") {
return None;
}
Some((RcStr::from(rel), path))
})
.map(|(name, path)| async move {
// Skip Redirect assets: they're symlinks with no file content,
// so versioning them would bail with "not a file".
let Some(asset) = *self.get_asset(path).await? else {
return Ok::<_, anyhow::Error>(None);
};
if !matches!(*asset.content().await?, AssetContent::File(_)) {
return Ok(None);
}
let content = asset.versioned_content().to_resolved().await?;
Ok(Some(HmrChunkWithContent {
path: name,
content,
}))
})
.try_flat_join()
.await?;
chunks.sort_by(|a, b| a.path.cmp(&b.path));
Ok(chunks)
}
}
#[turbo_tasks::value_impl]
......
......@@ -1845,6 +1845,134 @@ async fn hmr_update_with_issues_operation(
.cell())
}
/// Aggregate counterpart to [`project_hmr_update_operation`].
#[turbo_tasks::function(operation, root)]
fn project_all_hmr_update_operation(
project: ResolvedVc<Project>,
target: HmrTarget,
state: ResolvedVc<VersionState>,
) -> Vc<Update> {
project.all_hmr_update(target, *state)
}
/// Aggregate counterpart to [`hmr_update_with_issues_operation`].
#[tracing::instrument(
level = "info",
name = "aggregate hmr subscription",
skip_all,
fields(target = %target),
)]
#[turbo_tasks::function(operation, root)]
async fn all_hmr_update_with_issues_operation(
project: ResolvedVc<Project>,
state: ResolvedVc<VersionState>,
target: HmrTarget,
) -> Result<Vc<HmrUpdateWithIssues>> {
tracing::info!(target = %target, "aggregate hmr subscription");
let update_op = project_all_hmr_update_operation(project, target, state);
// See `hmr_update_with_issues_operation`: the JS consumer relies on this
// read *throwing* on build-graph failures; don't swallow errors.
let update = update_op
.read_strongly_consistent()
.final_read_hint()
.await?;
let filter = project.issue_filter().await?;
let issues = get_issues(update_op, &filter).await?;
let effects = Arc::new(take_effects(update_op).await?);
Ok(HmrUpdateWithIssues {
update,
issues,
effects,
}
.cell())
}
#[tracing::instrument(level = "info", name = "get all HMR events", skip(project, func), fields(target = %target))]
#[napi(ts_return_type = "{ __napiType: \"RootTask\" }")]
pub fn project_all_hmr_events(
#[napi(ts_arg_type = "{ __napiType: \"Project\" }")] project: External<ProjectInstance>,
target: String,
func: JsFunction,
) -> napi::Result<External<RootTask>> {
let hmr_target = target
.parse::<HmrTarget>()
.map_err(napi::Error::from_reason)?;
let container = project.container;
// Sentinel resource id for the aggregated stream (no real chunk path).
let identifier_path: RcStr = rcstr!("__next_all_hmr__");
subscribe(
project.turbopack_ctx.clone(),
func,
move || async move {
// HACK(bgw): Remove this unmark call
unmark_top_level_task_may_leak_eventually_consistent_state();
let project = container.project().to_resolved().await?;
let state = project
.all_hmr_version_state(hmr_target)
.to_resolved()
.await?;
let update_op = all_hmr_update_with_issues_operation(project, state, hmr_target);
// HACK(bgw): Remove this mark call
mark_top_level_task();
let read =
read_strongly_consistent_and_apply_effects(update_op, |v| &v.effects).await?;
// HACK(bgw): Remove this unmark call
unmark_top_level_task_may_leak_eventually_consistent_state();
let HmrUpdateWithIssues { update, issues, .. } = &*read;
match &**update {
Update::Missing | Update::None => {}
Update::Total(TotalUpdate { to }) => {
state.set(to.clone()).await?;
}
Update::Partial(PartialUpdate { to, .. }) => {
state.set(to.clone()).await?;
}
}
Ok((Some(update.clone()), issues.clone()))
},
move |ctx| {
let (update, issues) = ctx.value;
let napi_issues = issues
.iter()
.map(|issue| NapiIssue::from(&**issue))
.collect();
let update_issues = issues
.iter()
.map(|issue| Issue::from(&**issue))
.collect::<Vec<_>>();
let identifier = ResourceIdentifier {
path: identifier_path.clone(),
headers: None,
};
let update = match update.as_deref() {
None | Some(Update::Missing) | Some(Update::Total(_)) => {
ClientUpdateInstruction::restart(&identifier, &update_issues)
}
Some(Update::Partial(update)) => ClientUpdateInstruction::partial(
&identifier,
&update.instruction,
&update_issues,
),
Some(Update::None) => ClientUpdateInstruction::issues(&identifier, &update_issues),
};
Ok(vec![TurbopackResult {
result: ctx.env.to_js_value(&update)?,
issues: napi_issues,
}])
},
)
}
#[tracing::instrument(level = "info", name = "get HMR events", skip(project, func), fields(target = %target, chunk_name = %chunk_name))]
#[napi(ts_return_type = "{ __napiType: \"RootTask\" }")]
pub fn project_hmr_events(
......
......@@ -410,6 +410,11 @@ export declare function projectEntrypointsSubscribe(
project: { __napiType: 'Project' },
func: (...args: any[]) => any
): { __napiType: 'RootTask' }
export declare function projectAllHmrEvents(
project: { __napiType: 'Project' },
target: string,
func: (...args: any[]) => any
): { __napiType: 'RootTask' }
export declare function projectHmrEvents(
project: { __napiType: 'Project' },
chunkName: RcStr,
......@@ -478,7 +483,7 @@ export declare function projectGetSourceForAsset(
): Promise<string | null>
export declare function projectGetSourceMap(
project: { __napiType: 'Project' },
filePath: RcStr
sourceMapUrl: RcStr
): Promise<string | null>
export declare function projectGetSourceMapSync(
project: { __napiType: 'Project' },
......
......@@ -762,6 +762,16 @@ function bindingToApi(
})()
}
// Note: only the Server target is implemented in the native binding;
// add a Client overload once `all_hmr_update` supports it.
allHmrEvents(
target: HmrTarget.Server
): AsyncIterableIterator<TurbopackResult<NodeJsHmrUpdate>> {
return subscribe(true, async (callback) =>
binding.projectAllHmrEvents(this._nativeProject, target, callback)
)
}
hmrEvents(
chunkName: string,
target: HmrTarget.Client
......
......@@ -238,11 +238,15 @@ export interface NodeJsPartialHmrUpdate extends BaseUpdate {
type: 'partial'
instruction: {
type: 'EcmascriptMergedUpdate'
entries: Record<
entries?: Record<
string,
{ code: string; url: string; map?: string | undefined }
>
chunks?: Record<string, { type: 'partial' }>
chunks?: Record<
string,
| { type: 'added' | 'deleted'; modules?: string[] }
| { type: 'partial'; added?: string[]; deleted?: string[] }
>
}
}
......@@ -321,6 +325,12 @@ export interface Project {
TurbopackResult<RawEntrypoints | {}>
>
// Note: only the Server target is implemented in the native binding;
// add a Client overload once `all_hmr_update` supports it.
allHmrEvents(
target: import('./index').HmrTarget.Server
): AsyncIterableIterator<TurbopackResult<NodeJsHmrUpdate>>
hmrEvents(
identifier: string,
target: import('./index').HmrTarget.Client
......
......@@ -102,7 +102,7 @@ declare global {
var __next__clear_chunk_cache__: (() => void) | null | undefined
var __turbopack_clear_chunk_cache__: () => void | null | undefined
var __turbopack_server_hmr_apply__:
| ((update: NodeJsPartialHmrUpdate) => boolean)
| ((update: NodeJsPartialHmrUpdate) => void)
| undefined
}
......
......@@ -2,7 +2,7 @@ import type { Socket } from 'net'
import { mkdir, writeFile } from 'fs/promises'
import { realpathSync } from 'fs'
import * as inspector from 'inspector'
import { join, extname, relative, isAbsolute } from 'path'
import { join, extname, relative, isAbsolute, sep } from 'path'
import { fileURLToPath, pathToFileURL } from 'url'
import ws from 'next/dist/compiled/ws'
......@@ -152,117 +152,100 @@ const isTestMode = !!(
const sessionId = Math.floor(Number.MAX_SAFE_INTEGER * Math.random())
/** Output directory (relative to `distDir`) of server-HMR-managed chunks. */
const SERVER_HMR_CHUNKS_DIR = join('server', 'chunks')
declare const __next__clear_chunk_cache__: (() => void) | null | undefined
declare const __turbopack_server_hmr_apply__:
| ((update: NodeJsPartialHmrUpdate) => boolean)
| ((update: NodeJsPartialHmrUpdate) => void)
| undefined
type ServerHmrSubscriptions = Map<
string,
AsyncIterableIterator<TurbopackResult<NodeJsHmrUpdate>>
>
declare global {
/**
* Sync with `turbopack/crates/turbopack-ecmascript-runtime/js/src/nodejs/runtime/nodejs-globals.d.ts`.
*/
var __turbopack_server_hmr_handlers__: Map<string, unknown> | undefined
}
function setupServerHmr(
project: Project,
{
clear,
reEvaluateAllModulesExpensive,
}: {
clear: () => void | Promise<void>
reEvaluateAllModulesExpensive: () => void | Promise<void>
}
) {
const serverHmrSubscriptions: ServerHmrSubscriptions = new Map()
/**
* Subscribe to HMR updates for a server chunk.
* @param chunkPath - Server chunk output path (e.g., "server/chunks/ssr/..._.js")
*/
function subscribeToServerHmr(chunkPath: string) {
if (serverHmrSubscriptions.has(chunkPath)) {
return
}
const subscription = project.hmrEvents(chunkPath, HmrTarget.Server)
serverHmrSubscriptions.set(chunkPath, subscription)
// Start listening for changes in background
;(async () => {
// Skip initial state
await subscription.next()
for await (const result of subscription) {
const update = result as NodeJsHmrUpdate
async function runSubscription() {
const subscription = project.allHmrEvents(HmrTarget.Server)
// Subscribing immediately emits one event describing the current state.
// There's no previous state to diff it against, so it never carries anything
// to apply. Drop it; real updates start with the second event.
await subscription.next()
for await (const result of subscription) {
const update = result as NodeJsHmrUpdate
// A 'restart' from the wire protocol means the update can't be applied
// incrementally, so we must fully re-evaluate all chunks from disk. This
// clears the module cache and notifies browsers to refetch RSC.
const requiresFullReEvaluation = update.type === 'restart'
if (requiresFullReEvaluation) {
await reEvaluateAllModulesExpensive()
continue
}
// Fully re-evaluate all chunks from disk. Clears the module cache and
// notifies browsers to refetch RSC.
if (update.type === 'restart') {
await clear()
continue
}
if (update.type !== 'partial') {
continue
}
if (update.type !== 'partial') {
continue
}
// `EcmascriptMergedUpdate` is the only instruction the Node.js runtime
// knows how to apply; `ChunkListUpdate` is browser-only. Anything else is
// unknown to us, so ignore it rather than evicting the module cache.
const instruction = update.instruction
if (!instruction || instruction.type !== 'EcmascriptMergedUpdate') {
continue
}
const instruction = update.instruction
if (!instruction || instruction.type !== 'EcmascriptMergedUpdate') {
continue
}
// No handler registered yet (before first request, or right after
// reEvaluateAllModulesExpensive()) — nothing live to update, so skip
// until the next request.
const handlers = globalThis.__turbopack_server_hmr_handlers__
if (!handlers || handlers.size === 0) {
continue
}
if (typeof __turbopack_server_hmr_apply__ === 'function') {
const applied = __turbopack_server_hmr_apply__(update)
if (!applied) {
await clear()
}
if (typeof __turbopack_server_hmr_apply__ === 'function') {
try {
__turbopack_server_hmr_apply__(update)
} catch {
// A matching runtime tried the apply and threw. Evict require.cache
// so the next request loads fresh. (A no-match update is a no-op.)
await reEvaluateAllModulesExpensive()
}
} else {
await reEvaluateAllModulesExpensive()
}
})().catch(async (err) => {
console.error('[Server HMR] Subscription error:', err)
serverHmrSubscriptions.delete(chunkPath)
await clear()
})
}
}
// Listen to the Rust bindings update us on changing server HMR chunk paths
// Start listening for changes in background. Re-subscribe on error so
// server Fast Refresh continues working for the rest of the dev session.
// The delay keeps a persistently-failing subscription (which throws on the
// initial read) from hot-looping through reEvaluateAllModulesExpensive().
;(async () => {
try {
const serverHmrChunkPaths = project.hmrChunkNamesSubscribe(
HmrTarget.Server
)
// Process chunk paths (both initial and subsequent updates)
for await (const data of serverHmrChunkPaths) {
const currentChunkPaths = new Set<string>(
data.chunkNames.filter((path) => path.endsWith('.js'))
)
// Clean up subscriptions for removed chunk paths (like when pages are deleted)
const chunkPathsToRemove: string[] = []
for (const chunkPath of serverHmrSubscriptions.keys()) {
if (!currentChunkPaths.has(chunkPath)) {
chunkPathsToRemove.push(chunkPath)
}
}
for (const chunkPath of chunkPathsToRemove) {
const subscription = serverHmrSubscriptions.get(chunkPath)
subscription?.return?.()
serverHmrSubscriptions.delete(chunkPath)
}
// Subscribe to HMR events for new server chunks
for (const chunkPath of currentChunkPaths) {
if (!serverHmrSubscriptions.has(chunkPath)) {
subscribeToServerHmr(chunkPath)
}
}
for (;;) {
try {
await runSubscription()
return
} catch (err) {
console.error('[Server HMR] Subscription error, resubscribing:', err)
await reEvaluateAllModulesExpensive()
await new Promise((resolve) => setTimeout(resolve, 1000))
}
} catch (err) {
console.error('[Server HMR Setup] Error in chunk path subscription:', err)
}
})()
return serverHmrSubscriptions
}
function getSourceMapFromTurbopack(
......@@ -642,6 +625,7 @@ export async function createHotReloaderTurbopack(
entryType === 'app' &&
writtenEndpoint.type !== 'edge'
const serverChunksPrefix = SERVER_HMR_CHUNKS_DIR + sep
const filesToDelete: string[] = []
for (const file of serverPaths) {
clearModuleContext(file)
......@@ -654,7 +638,7 @@ export async function createHotReloaderTurbopack(
// contexts.
force ||
!usesServerHmr ||
!serverHmrSubscriptions?.has(relativePath)
!relativePath.startsWith(serverChunksPrefix)
) {
filesToDelete.push(file)
}
......@@ -717,8 +701,6 @@ export async function createHotReloaderTurbopack(
}
}
let serverHmrSubscriptions: ServerHmrSubscriptions | undefined
let hmrEventHappened = false
// A counter identifying the current version of the compiled output, included
// by `"use cache"` in dev cache keys so that cached entries revalidate after
......@@ -1719,7 +1701,7 @@ export async function createHotReloaderTurbopack(
subscribeToChanges = true,
}) {
// When there is no route definition this is an internal file not a route the user added.
// Middleware and instrumentation are handled in turbpack-utils.ts handleEntrypoints instead.
// Middleware and instrumentation are handled in turbopack-utils.ts handleEntrypoints instead.
if (!definition) {
if (inputPage === '/middleware') return
if (inputPage === '/src/middleware') return
......@@ -2025,11 +2007,13 @@ export async function createHotReloaderTurbopack(
})
if (serverFastRefresh) {
serverHmrSubscriptions = setupServerHmr(project, {
clear: async () => {
// Clear Node's require cache of all Turbopack-built modules
const chunkPaths = [...(serverHmrSubscriptions?.keys() ?? [])].map(
(chunkPath) => join(distDir, chunkPath)
setupServerHmr(project, {
reEvaluateAllModulesExpensive: async () => {
// Evict every server-HMR-managed chunk from `require.cache`.
// Trailing `sep` so e.g. `server/chunks-other/...` doesn't match.
const serverChunksDir = join(distDir, SERVER_HMR_CHUNKS_DIR) + sep
const chunkPaths = Object.keys(require.cache).filter((p) =>
p.startsWith(serverChunksDir)
)
deleteCache(chunkPaths)
......@@ -2041,7 +2025,7 @@ export async function createHotReloaderTurbopack(
// Reset the server HMR handler registry. All server runtime chunks are
// cleared from require.cache above; when they're next required they'll
// re-register into this Map and reinstall the routing dispatcher.
;(globalThis as any).__turbopack_server_hmr_handlers__ = new Map()
globalThis.__turbopack_server_hmr_handlers__ = new Map()
// Clear all edge contexts
await clearAllModuleContexts()
......
......@@ -123,9 +123,8 @@ const STATS_RELATIVE_PATH = '.next/warm-restart-task-stats.json'
const missed = await readMissedTaskNames()
expect(missed).toMatchInlineSnapshot(`
[
"<dyn turbopack_core::version::VersionedContent>::update",
"<turbopack_browser::ecmascript::list::content::EcmascriptDevChunkListContent as dyn turbopack_core::version::VersionedContent>::update",
"<turbopack_nodejs::ecmascript::node::content::EcmascriptBuildNodeChunkContent as dyn turbopack_core::version::VersionedContent>::update",
"<turbopack_core::version::FileHashVersion as dyn turbopack_core::version::Version>::id",
"next_api::project::Project::hmr_update",
"next_api::project::Project::hmr_version_state",
"next_napi_bindings::next_api::project::hmr_update_with_issues_operation",
......
......@@ -18,19 +18,14 @@ function ensureHmrClientInitialized() {
initializeServerHmr(moduleFactories, devModuleCache)
}
function __turbopack_server_hmr_apply__(update: NodeJsHmrPayload): boolean {
try {
ensureHmrClientInitialized()
// emitMessage returns false if any listener failed to apply the update
return emitMessage({
type: 'turbopack-message',
data: update,
})
} catch (err) {
console.error('[Server HMR] Failed to apply update:', err)
return false
}
function __turbopack_server_hmr_apply__(update: NodeJsHmrPayload): void {
ensureHmrClientInitialized()
// Throws if the update can't be applied in-process; the consumer catches it
// and falls back to evicting require.cache.
emitMessage({
type: 'turbopack-message',
data: update,
})
}
// Turbopack produces one server runtime per chunking context (e.g.
......@@ -41,7 +36,7 @@ function __turbopack_server_hmr_apply__(update: NodeJsHmrPayload): boolean {
// stale entries when a runtime file is re-evaluated after require.cache eviction.
type HmrHandlerEntry = {
handler: (update: NodeJsHmrPayload) => boolean
handler: (update: NodeJsHmrPayload) => void
/** Output directory relative to RUNTIME_ROOT, e.g. "server/chunks/ssr". */
chunkPrefix: string
}
......@@ -49,13 +44,17 @@ type HmrHandlerEntry = {
const handlers: Map<string, HmrHandlerEntry> =
globalThis.__turbopack_server_hmr_handlers__ ?? new Map()
const chunkPrefix = path.relative(RUNTIME_ROOT, path.dirname(__filename))
// Normalize to forward slashes so it matches the virtual chunk paths in
// `update.instruction.chunks`, which always use `/` regardless of OS.
const chunkPrefix = path
.relative(RUNTIME_ROOT, path.dirname(__filename))
.replaceAll(path.sep, '/')
if (handlers.size === 0) {
// First registration in this generation: install the routing dispatcher.
globalThis.__turbopack_server_hmr_apply__ = (
update: NodeJsHmrPayload
): boolean => {
): void => {
const registry: Map<string, HmrHandlerEntry> =
globalThis.__turbopack_server_hmr_handlers__ ?? new Map()
const updateChunkPaths = Object.keys(update.instruction?.chunks ?? {})
......@@ -76,16 +75,12 @@ if (handlers.size === 0) {
}
}
let applied = false
// No matching runtime loaded (e.g. editing a route not required yet this
// session): nothing live to patch, so this is a no-op. A handler that
// throws propagates to the consumer, which evicts require.cache.
for (const { handler } of toCall) {
try {
if (handler(update)) applied = true
} catch (err) {
console.error('[Server HMR] Handler error:', err)
}
handler(update)
}
return applied
}
}
......
......@@ -55,22 +55,14 @@ function initializeServerHmr(
* Note: This is only called via __turbopack_server_hmr_apply__ which ensures
* the handler is initialized first via ensureHmrClientInitialized().
*/
function emitMessage(msg: { type: string; data: any }): boolean {
function emitMessage(msg: { type: string; data: any }): void {
if (serverHmrUpdateHandler == null) {
console.warn(
'[Server HMR] No update handler registered to receive message:',
msg
throw new Error(
'[Server HMR] No update handler registered to receive message'
)
return false
}
try {
serverHmrUpdateHandler(msg.data)
return true
} catch (err) {
console.error('[Server HMR] Listener error:', err)
return false
}
serverHmrUpdateHandler(msg.data)
}
/**
......
......@@ -19,7 +19,7 @@ declare global {
// in this .d.ts file, so the handler parameter must use any here.
var __turbopack_server_hmr_handlers__:
| Map<string, { handler: (update: any) => boolean; chunkPrefix: string }>
| Map<string, { handler: (update: any) => void; chunkPrefix: string }>
| undefined
}
......
......@@ -1659,16 +1659,9 @@ function initializeServerHmr(moduleFactories, devModuleCache) {
* the handler is initialized first via ensureHmrClientInitialized().
*/ function emitMessage(msg) {
if (serverHmrUpdateHandler == null) {
console.warn('[Server HMR] No update handler registered to receive message:', msg);
return false;
}
try {
serverHmrUpdateHandler(msg.data);
return true;
} catch (err) {
console.error('[Server HMR] Listener error:', err);
return false;
throw new Error('[Server HMR] No update handler registered to receive message');
}
serverHmrUpdateHandler(msg.data);
}
/**
* Handles server message updates and applies them to the Node.js runtime.
......@@ -1722,20 +1715,18 @@ function ensureHmrClientInitialized() {
initializeServerHmr(moduleFactories, devModuleCache);
}
function __turbopack_server_hmr_apply__(update) {
try {
ensureHmrClientInitialized();
// emitMessage returns false if any listener failed to apply the update
return emitMessage({
type: 'turbopack-message',
data: update
});
} catch (err) {
console.error('[Server HMR] Failed to apply update:', err);
return false;
}
ensureHmrClientInitialized();
// Throws if the update can't be applied in-process; the consumer catches it
// and falls back to evicting require.cache.
emitMessage({
type: 'turbopack-message',
data: update
});
}
const handlers = globalThis.__turbopack_server_hmr_handlers__ ?? new Map();
const chunkPrefix = path.relative(RUNTIME_ROOT, path.dirname(__filename));
// Normalize to forward slashes so it matches the virtual chunk paths in
// `update.instruction.chunks`, which always use `/` regardless of OS.
const chunkPrefix = path.relative(RUNTIME_ROOT, path.dirname(__filename)).replaceAll(path.sep, '/');
if (handlers.size === 0) {
// First registration in this generation: install the routing dispatcher.
globalThis.__turbopack_server_hmr_apply__ = (update)=>{
......@@ -1756,15 +1747,12 @@ if (handlers.size === 0) {
}
}
}
let applied = false;
// No matching runtime loaded (e.g. editing a route not required yet this
// session): nothing live to patch, so this is a no-op. A handler that
// throws propagates to the consumer, which evicts require.cache.
for (const { handler } of toCall){
try {
if (handler(update)) applied = true;
} catch (err) {
console.error('[Server HMR] Handler error:', err);
}
handler(update);
}
return applied;
};
}
globalThis.__turbopack_server_hmr_handlers__ = handlers;
......
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