Unverified Commit 53ff410e authored by Tobias Koppers's avatar Tobias Koppers Committed by GitHub

Revert "Revert "Initial MCP implementation (#81770)" (#82217)"

This reverts commit d2f9c0f9.
parent f618d0b7
...@@ -4,21 +4,25 @@ use anyhow::Result; ...@@ -4,21 +4,25 @@ use anyhow::Result;
use futures_util::TryFutureExt; use futures_util::TryFutureExt;
use napi::{JsFunction, bindgen_prelude::External}; use napi::{JsFunction, bindgen_prelude::External};
use next_api::{ use next_api::{
module_graph_snapshot::{ModuleGraphSnapshot, get_module_graph_snapshot},
operation::OptionEndpoint, operation::OptionEndpoint,
paths::ServerPath, paths::ServerPath,
route::{ route::{
EndpointOutputPaths, endpoint_client_changed_operation, endpoint_server_changed_operation, Endpoint, EndpointOutputPaths, endpoint_client_changed_operation,
endpoint_write_to_disk_operation, endpoint_server_changed_operation, endpoint_write_to_disk_operation,
}, },
}; };
use tracing::Instrument; use tracing::Instrument;
use turbo_tasks::{Completion, Effects, OperationVc, ReadRef, Vc}; use turbo_tasks::{
use turbopack_core::{diagnostics::PlainDiagnostic, issue::PlainIssue}; Completion, Effects, OperationVc, ReadRef, TryFlatJoinIterExt, TryJoinIterExt, Vc,
};
use turbopack_core::{diagnostics::PlainDiagnostic, error::PrettyPrintError, issue::PlainIssue};
use super::utils::{ use super::utils::{
DetachedVc, NapiDiagnostic, NapiIssue, RootTask, TurbopackResult, DetachedVc, NapiDiagnostic, NapiIssue, RootTask, TurbopackResult,
strongly_consistent_catch_collectables, subscribe, strongly_consistent_catch_collectables, subscribe,
}; };
use crate::next_api::module_graph::NapiModuleGraphSnapshot;
#[napi(object)] #[napi(object)]
#[derive(Default)] #[derive(Default)]
...@@ -81,6 +85,11 @@ impl From<Option<EndpointOutputPaths>> for NapiWrittenEndpoint { ...@@ -81,6 +85,11 @@ impl From<Option<EndpointOutputPaths>> for NapiWrittenEndpoint {
} }
} }
#[napi(object)]
pub struct NapiModuleGraphSnapshots {
pub module_graphs: Vec<NapiModuleGraphSnapshot>,
}
// NOTE(alexkirsz) We go through an extra layer of indirection here because of // NOTE(alexkirsz) We go through an extra layer of indirection here because of
// two factors: // two factors:
// 1. rustc currently has a bug where using a dyn trait as a type argument to // 1. rustc currently has a bug where using a dyn trait as a type argument to
...@@ -155,6 +164,105 @@ pub async fn endpoint_write_to_disk( ...@@ -155,6 +164,105 @@ pub async fn endpoint_write_to_disk(
}) })
} }
#[turbo_tasks::value(serialization = "none")]
struct ModuleGraphsWithIssues {
module_graphs: Option<ReadRef<ModuleGraphSnapshots>>,
issues: Arc<Vec<ReadRef<PlainIssue>>>,
diagnostics: Arc<Vec<ReadRef<PlainDiagnostic>>>,
effects: Arc<Effects>,
}
#[turbo_tasks::function(operation)]
async fn get_module_graphs_with_issues_operation(
endpoint_op: OperationVc<OptionEndpoint>,
) -> Result<Vc<ModuleGraphsWithIssues>> {
let module_graphs_op = get_module_graphs_operation(endpoint_op);
let (module_graphs, issues, diagnostics, effects) =
strongly_consistent_catch_collectables(module_graphs_op).await?;
Ok(ModuleGraphsWithIssues {
module_graphs,
issues,
diagnostics,
effects,
}
.cell())
}
#[turbo_tasks::value(transparent)]
struct ModuleGraphSnapshots(Vec<ReadRef<ModuleGraphSnapshot>>);
#[turbo_tasks::function(operation)]
async fn get_module_graphs_operation(
endpoint_op: OperationVc<OptionEndpoint>,
) -> Result<Vc<ModuleGraphSnapshots>> {
let Some(endpoint) = *endpoint_op.connect().await? else {
return Ok(Vc::cell(vec![]));
};
let graphs = endpoint.module_graphs().await?;
let entries = endpoint.entries().await?;
let entry_modules = entries.iter().flat_map(|e| e.entries()).collect::<Vec<_>>();
let snapshots = graphs
.iter()
.map(async |&graph| {
let module_graph = graph.await?;
let entry_modules = entry_modules
.iter()
.map(async |&m| Ok(module_graph.has_entry(m).await?.then_some(m)))
.try_flat_join()
.await?;
Ok((*graph, entry_modules))
})
.try_join()
.await?
.into_iter()
.map(|(graph, entry_modules)| (graph, Vc::cell(entry_modules)))
.collect::<Vec<_>>()
.into_iter()
.map(async |(graph, entry_modules)| {
get_module_graph_snapshot(graph, Some(entry_modules)).await
})
.try_join()
.await?;
Ok(Vc::cell(snapshots))
}
#[napi]
pub async fn endpoint_module_graphs(
#[napi(ts_arg_type = "{ __napiType: \"Endpoint\" }")] endpoint: External<ExternalEndpoint>,
) -> napi::Result<TurbopackResult<NapiModuleGraphSnapshots>> {
let endpoint_op: OperationVc<OptionEndpoint> = ***endpoint;
let (module_graphs, issues, diagnostics) = endpoint
.turbopack_ctx()
.turbo_tasks()
.run_once(async move {
let module_graphs_op = get_module_graphs_with_issues_operation(endpoint_op);
let ModuleGraphsWithIssues {
module_graphs,
issues,
diagnostics,
effects: _,
} = &*module_graphs_op.connect().await?;
Ok((module_graphs.clone(), issues.clone(), diagnostics.clone()))
})
.await
.map_err(|e| napi::Error::from_reason(PrettyPrintError(&e).to_string()))?;
Ok(TurbopackResult {
result: NapiModuleGraphSnapshots {
module_graphs: module_graphs
.into_iter()
.flat_map(|m| m.into_iter())
.map(|m| NapiModuleGraphSnapshot::from(&**m))
.collect(),
},
issues: issues.iter().map(|i| NapiIssue::from(&**i)).collect(),
diagnostics: diagnostics
.iter()
.map(|d| NapiDiagnostic::from(d))
.collect(),
})
}
#[napi(ts_return_type = "{ __napiType: \"RootTask\" }")] #[napi(ts_return_type = "{ __napiType: \"RootTask\" }")]
pub fn endpoint_server_changed_subscribe( pub fn endpoint_server_changed_subscribe(
#[napi(ts_arg_type = "{ __napiType: \"Endpoint\" }")] endpoint: External<ExternalEndpoint>, #[napi(ts_arg_type = "{ __napiType: \"Endpoint\" }")] endpoint: External<ExternalEndpoint>,
......
pub mod endpoint; pub mod endpoint;
pub mod module_graph;
pub mod project; pub mod project;
pub mod turbopack_ctx; pub mod turbopack_ctx;
pub mod utils; pub mod utils;
use next_api::module_graph_snapshot::{ModuleGraphSnapshot, ModuleInfo, ModuleReference};
use turbo_rcstr::RcStr;
use turbopack_core::chunk::ChunkingType;
#[napi(object)]
pub struct NapiModuleReference {
/// The index of the referenced/referencing module in the modules list.
pub index: u32,
/// The export used in the module reference.
pub export: String,
/// The type of chunking for the module reference.
pub chunking_type: String,
}
impl From<&ModuleReference> for NapiModuleReference {
fn from(reference: &ModuleReference) -> Self {
Self {
index: reference.index as u32,
export: reference.export.to_string(),
chunking_type: match &reference.chunking_type {
ChunkingType::Parallel { hoisted: true, .. } => "hoisted".to_string(),
ChunkingType::Parallel { hoisted: false, .. } => "sync".to_string(),
ChunkingType::Async => "async".to_string(),
ChunkingType::Isolated {
merge_tag: None, ..
} => "isolated".to_string(),
ChunkingType::Isolated {
merge_tag: Some(name),
..
} => format!("isolated {name}"),
ChunkingType::Shared {
merge_tag: None, ..
} => "shared".to_string(),
ChunkingType::Shared {
merge_tag: Some(name),
..
} => format!("shared {name}"),
ChunkingType::Traced => "traced".to_string(),
},
}
}
}
#[napi(object)]
pub struct NapiModuleInfo {
pub ident: RcStr,
pub path: RcStr,
pub depth: u32,
pub size: u32,
pub retained_size: u32,
pub references: Vec<NapiModuleReference>,
pub incoming_references: Vec<NapiModuleReference>,
}
impl From<&ModuleInfo> for NapiModuleInfo {
fn from(info: &ModuleInfo) -> Self {
Self {
ident: info.ident.clone(),
path: info.path.clone(),
depth: info.depth,
size: info.size,
retained_size: info.retained_size,
references: info
.references
.iter()
.map(NapiModuleReference::from)
.collect(),
incoming_references: info
.incoming_references
.iter()
.map(NapiModuleReference::from)
.collect(),
}
}
}
#[napi(object)]
#[derive(Default)]
pub struct NapiModuleGraphSnapshot {
pub modules: Vec<NapiModuleInfo>,
pub entries: Vec<u32>,
}
impl From<&ModuleGraphSnapshot> for NapiModuleGraphSnapshot {
fn from(snapshot: &ModuleGraphSnapshot) -> Self {
Self {
modules: snapshot.modules.iter().map(NapiModuleInfo::from).collect(),
entries: snapshot
.entries
.iter()
.map(|&i| {
// If you have more that 4294967295 entries, you probably have other problems...
i.try_into().unwrap()
})
.collect(),
}
}
}
...@@ -9,6 +9,7 @@ use napi::{ ...@@ -9,6 +9,7 @@ use napi::{
}; };
use next_api::{ use next_api::{
entrypoints::Entrypoints, entrypoints::Entrypoints,
module_graph_snapshot::{ModuleGraphSnapshot, get_module_graph_snapshot},
operation::{ operation::{
EntrypointsOperation, InstrumentationOperation, MiddlewareOperation, OptionEndpoint, EntrypointsOperation, InstrumentationOperation, MiddlewareOperation, OptionEndpoint,
RouteOperation, RouteOperation,
...@@ -63,13 +64,14 @@ use url::Url; ...@@ -63,13 +64,14 @@ use url::Url;
use crate::{ use crate::{
next_api::{ next_api::{
endpoint::ExternalEndpoint, endpoint::ExternalEndpoint,
module_graph::NapiModuleGraphSnapshot,
turbopack_ctx::{ turbopack_ctx::{
NapiNextTurbopackCallbacks, NapiNextTurbopackCallbacksJsObject, NextTurboTasks, NapiNextTurbopackCallbacks, NapiNextTurbopackCallbacksJsObject, NextTurboTasks,
NextTurbopackContext, create_turbo_tasks, NextTurbopackContext, create_turbo_tasks,
}, },
utils::{ utils::{
DetachedVc, NapiDiagnostic, NapiIssue, RootTask, TurbopackResult, get_diagnostics, DetachedVc, NapiDiagnostic, NapiIssue, RootTask, TurbopackResult, get_diagnostics,
get_issues, subscribe, get_issues, strongly_consistent_catch_collectables, subscribe,
}, },
}, },
register, register,
...@@ -987,6 +989,40 @@ async fn output_assets_operation( ...@@ -987,6 +989,40 @@ async fn output_assets_operation(
Ok(Vc::cell(output_assets.into_iter().collect())) Ok(Vc::cell(output_assets.into_iter().collect()))
} }
#[napi]
pub async fn project_entrypoints(
#[napi(ts_arg_type = "{ __napiType: \"Project\" }")] project: External<ProjectInstance>,
) -> napi::Result<TurbopackResult<NapiEntrypoints>> {
let container = project.container;
let (entrypoints, issues, diags) = project
.turbopack_ctx
.turbo_tasks()
.run_once(async move {
let entrypoints_with_issues_op = get_entrypoints_with_issues_operation(container);
// Read and compile the files
let EntrypointsWithIssues {
entrypoints,
issues,
diagnostics,
effects: _,
} = &*entrypoints_with_issues_op
.read_strongly_consistent()
.await?;
Ok((entrypoints.clone(), issues.clone(), diagnostics.clone()))
})
.await
.map_err(|e| napi::Error::from_reason(PrettyPrintError(&e).to_string()))?;
Ok(TurbopackResult {
result: NapiEntrypoints::from_entrypoints_op(&entrypoints, &project.turbopack_ctx)?,
issues: issues.iter().map(|i| NapiIssue::from(&**i)).collect(),
diagnostics: diags.iter().map(|d| NapiDiagnostic::from(d)).collect(),
})
}
#[napi(ts_return_type = "{ __napiType: \"RootTask\" }")] #[napi(ts_return_type = "{ __napiType: \"RootTask\" }")]
pub fn project_entrypoints_subscribe( pub fn project_entrypoints_subscribe(
#[napi(ts_arg_type = "{ __napiType: \"Project\" }")] project: External<ProjectInstance>, #[napi(ts_arg_type = "{ __napiType: \"Project\" }")] project: External<ProjectInstance>,
...@@ -1650,3 +1686,70 @@ pub fn project_get_source_map_sync( ...@@ -1650,3 +1686,70 @@ pub fn project_get_source_map_sync(
tokio::runtime::Handle::current().block_on(project_get_source_map(project, file_path)) tokio::runtime::Handle::current().block_on(project_get_source_map(project, file_path))
}) })
} }
#[napi]
pub async fn project_module_graph(
#[napi(ts_arg_type = "{ __napiType: \"Project\" }")] project: External<ProjectInstance>,
) -> napi::Result<TurbopackResult<NapiModuleGraphSnapshot>> {
let container = project.container;
let (module_graph, issues, diagnostics) = project
.turbopack_ctx
.turbo_tasks()
.run_once(async move {
let module_graph_op = get_module_graph_with_issues_operation(container);
let ModuleGraphWithIssues {
module_graph,
issues,
diagnostics,
effects: _,
} = &*module_graph_op.connect().await?;
Ok((module_graph.clone(), issues.clone(), diagnostics.clone()))
})
.await
.map_err(|e| napi::Error::from_reason(PrettyPrintError(&e).to_string()))?;
Ok(TurbopackResult {
result: module_graph.map_or_else(NapiModuleGraphSnapshot::default, |m| {
NapiModuleGraphSnapshot::from(&*m)
}),
issues: issues.iter().map(|i| NapiIssue::from(&**i)).collect(),
diagnostics: diagnostics
.iter()
.map(|d| NapiDiagnostic::from(d))
.collect(),
})
}
#[turbo_tasks::value(serialization = "none")]
struct ModuleGraphWithIssues {
module_graph: Option<ReadRef<ModuleGraphSnapshot>>,
issues: Arc<Vec<ReadRef<PlainIssue>>>,
diagnostics: Arc<Vec<ReadRef<PlainDiagnostic>>>,
effects: Arc<Effects>,
}
#[turbo_tasks::function(operation)]
async fn get_module_graph_with_issues_operation(
project: ResolvedVc<ProjectContainer>,
) -> Result<Vc<ModuleGraphWithIssues>> {
let module_graph_op = get_module_graph_operation(project);
let (module_graph, issues, diagnostics, effects) =
strongly_consistent_catch_collectables(module_graph_op).await?;
Ok(ModuleGraphWithIssues {
module_graph,
issues,
diagnostics,
effects,
}
.cell())
}
#[turbo_tasks::function(operation)]
async fn get_module_graph_operation(
project: ResolvedVc<ProjectContainer>,
) -> Result<Vc<ModuleGraphSnapshot>> {
let project = project.project();
let graph = project.whole_app_module_graphs().await?.full;
let snapshot = get_module_graph_snapshot(*graph, None).resolve().await?;
Ok(snapshot)
}
...@@ -83,8 +83,10 @@ use crate::{ ...@@ -83,8 +83,10 @@ use crate::{
all_paths_in_root, all_server_paths, get_asset_paths_from_root, get_js_paths_from_root, all_paths_in_root, all_server_paths, get_asset_paths_from_root, get_js_paths_from_root,
get_wasm_paths_from_root, paths_to_bindings, wasm_paths_to_bindings, get_wasm_paths_from_root, paths_to_bindings, wasm_paths_to_bindings,
}, },
project::{ModuleGraphs, Project}, project::{BaseAndFullModuleGraph, Project},
route::{AppPageRoute, Endpoint, EndpointOutput, EndpointOutputPaths, Route, Routes}, route::{
AppPageRoute, Endpoint, EndpointOutput, EndpointOutputPaths, ModuleGraphs, Route, Routes,
},
server_actions::{build_server_actions_loader, create_server_actions_manifest}, server_actions::{build_server_actions_loader, create_server_actions_manifest},
webpack_stats::generate_webpack_stats, webpack_stats::generate_webpack_stats,
}; };
...@@ -871,7 +873,7 @@ impl AppProject { ...@@ -871,7 +873,7 @@ impl AppProject {
rsc_entry: ResolvedVc<Box<dyn Module>>, rsc_entry: ResolvedVc<Box<dyn Module>>,
client_shared_entries: Vc<EvaluatableAssets>, client_shared_entries: Vc<EvaluatableAssets>,
has_layout_segments: bool, has_layout_segments: bool,
) -> Result<Vc<ModuleGraphs>> { ) -> Result<Vc<BaseAndFullModuleGraph>> {
if *self.project.per_page_module_graph().await? { if *self.project.per_page_module_graph().await? {
let should_trace = self.project.next_mode().await?.is_production(); let should_trace = self.project.next_mode().await?.is_production();
let client_shared_entries = client_shared_entries let client_shared_entries = client_shared_entries
...@@ -968,7 +970,7 @@ impl AppProject { ...@@ -968,7 +970,7 @@ impl AppProject {
graphs.push(additional_module_graph); graphs.push(additional_module_graph);
let full = ModuleGraph::from_graphs(graphs); let full = ModuleGraph::from_graphs(graphs);
Ok(ModuleGraphs { Ok(BaseAndFullModuleGraph {
base: base.to_resolved().await?, base: base.to_resolved().await?,
full: full.to_resolved().await?, full: full.to_resolved().await?,
} }
...@@ -2086,6 +2088,22 @@ impl Endpoint for AppEndpoint { ...@@ -2086,6 +2088,22 @@ impl Endpoint for AppEndpoint {
server_actions_loader, server_actions_loader,
])])) ])]))
} }
#[turbo_tasks::function]
async fn module_graphs(self: Vc<Self>) -> Result<Vc<ModuleGraphs>> {
let this = self.await?;
let app_entry = self.app_endpoint_entry().await?;
let module_graphs = this
.app_project
.app_module_graphs(
self,
*app_entry.rsc_entry,
this.app_project.client_runtime_entries(),
matches!(this.ty, AppEndpointType::Page { .. }),
)
.await?;
Ok(Vc::cell(vec![module_graphs.full]))
}
} }
#[turbo_tasks::value] #[turbo_tasks::value]
......
...@@ -2,7 +2,7 @@ use anyhow::{Result, bail}; ...@@ -2,7 +2,7 @@ use anyhow::{Result, bail};
use turbo_tasks::{Completion, Vc}; use turbo_tasks::{Completion, Vc};
use turbopack_core::module_graph::GraphEntries; use turbopack_core::module_graph::GraphEntries;
use crate::route::{Endpoint, EndpointOutput}; use crate::route::{Endpoint, EndpointOutput, ModuleGraphs};
#[turbo_tasks::value] #[turbo_tasks::value]
pub struct EmptyEndpoint; pub struct EmptyEndpoint;
...@@ -36,4 +36,9 @@ impl Endpoint for EmptyEndpoint { ...@@ -36,4 +36,9 @@ impl Endpoint for EmptyEndpoint {
fn entries(self: Vc<Self>) -> Vc<GraphEntries> { fn entries(self: Vc<Self>) -> Vc<GraphEntries> {
GraphEntries::empty() GraphEntries::empty()
} }
#[turbo_tasks::function]
fn module_graphs(self: Vc<Self>) -> Vc<ModuleGraphs> {
Vc::cell(vec![])
}
} }
...@@ -33,7 +33,7 @@ use crate::{ ...@@ -33,7 +33,7 @@ use crate::{
all_server_paths, get_js_paths_from_root, get_wasm_paths_from_root, wasm_paths_to_bindings, all_server_paths, get_js_paths_from_root, get_wasm_paths_from_root, wasm_paths_to_bindings,
}, },
project::Project, project::Project,
route::{Endpoint, EndpointOutput, EndpointOutputPaths}, route::{Endpoint, EndpointOutput, EndpointOutputPaths, ModuleGraphs},
}; };
#[turbo_tasks::value] #[turbo_tasks::value]
...@@ -70,7 +70,7 @@ impl InstrumentationEndpoint { ...@@ -70,7 +70,7 @@ impl InstrumentationEndpoint {
} }
#[turbo_tasks::function] #[turbo_tasks::function]
async fn core_modules(&self) -> Result<Vc<InstrumentationCoreModules>> { async fn entry_module(&self) -> Result<Vc<Box<dyn Module>>> {
let userland_module = self let userland_module = self
.asset_context .asset_context
.process( .process(
...@@ -81,6 +81,10 @@ impl InstrumentationEndpoint { ...@@ -81,6 +81,10 @@ impl InstrumentationEndpoint {
.to_resolved() .to_resolved()
.await?; .await?;
if !self.is_edge {
return Ok(Vc::upcast(*userland_module));
}
let edge_entry_module = wrap_edge_entry( let edge_entry_module = wrap_edge_entry(
*self.asset_context, *self.asset_context,
self.project.project_path().owned().await?, self.project.project_path().owned().await?,
...@@ -90,17 +94,13 @@ impl InstrumentationEndpoint { ...@@ -90,17 +94,13 @@ impl InstrumentationEndpoint {
.to_resolved() .to_resolved()
.await?; .await?;
Ok(InstrumentationCoreModules { Ok(Vc::upcast(*edge_entry_module))
userland_module,
edge_entry_module,
}
.cell())
} }
#[turbo_tasks::function] #[turbo_tasks::function]
async fn edge_files(self: Vc<Self>) -> Result<Vc<OutputAssets>> { async fn edge_files(self: Vc<Self>) -> Result<Vc<OutputAssets>> {
let this = self.await?; let this = self.await?;
let module = self.core_modules().await?.edge_entry_module; let module = self.entry_module().to_resolved().await?;
let module_graph = this.project.module_graph(*module); let module_graph = this.project.module_graph(*module);
...@@ -137,7 +137,7 @@ impl InstrumentationEndpoint { ...@@ -137,7 +137,7 @@ impl InstrumentationEndpoint {
let chunking_context = this.project.server_chunking_context(false); let chunking_context = this.project.server_chunking_context(false);
let userland_module = self.core_modules().await?.userland_module; let userland_module = self.entry_module().to_resolved().await?;
let module_graph = this.project.module_graph(*userland_module); let module_graph = this.project.module_graph(*userland_module);
let Some(module) = ResolvedVc::try_downcast(userland_module) else { let Some(module) = ResolvedVc::try_downcast(userland_module) else {
...@@ -227,12 +227,6 @@ impl InstrumentationEndpoint { ...@@ -227,12 +227,6 @@ impl InstrumentationEndpoint {
} }
} }
#[turbo_tasks::value]
struct InstrumentationCoreModules {
pub userland_module: ResolvedVc<Box<dyn Module>>,
pub edge_entry_module: ResolvedVc<Box<dyn Module>>,
}
#[turbo_tasks::value_impl] #[turbo_tasks::value_impl]
impl Endpoint for InstrumentationEndpoint { impl Endpoint for InstrumentationEndpoint {
#[turbo_tasks::function] #[turbo_tasks::function]
...@@ -276,13 +270,15 @@ impl Endpoint for InstrumentationEndpoint { ...@@ -276,13 +270,15 @@ impl Endpoint for InstrumentationEndpoint {
#[turbo_tasks::function] #[turbo_tasks::function]
async fn entries(self: Vc<Self>) -> Result<Vc<GraphEntries>> { async fn entries(self: Vc<Self>) -> Result<Vc<GraphEntries>> {
let core_modules = self.core_modules().await?; let entry_module = self.entry_module().to_resolved().await?;
Ok(Vc::cell(vec![ChunkGroupEntry::Entry( Ok(Vc::cell(vec![ChunkGroupEntry::Entry(vec![entry_module])]))
if self.await?.is_edge { }
vec![core_modules.edge_entry_module]
} else { #[turbo_tasks::function]
vec![core_modules.userland_module] async fn module_graphs(self: Vc<Self>) -> Result<Vc<ModuleGraphs>> {
}, let this = self.await?;
)])) let module = self.entry_module();
let module_graph = this.project.module_graph(module).to_resolved().await?;
Ok(Vc::cell(vec![module_graph]))
} }
} }
...@@ -13,6 +13,7 @@ mod instrumentation; ...@@ -13,6 +13,7 @@ mod instrumentation;
mod loadable_manifest; mod loadable_manifest;
mod middleware; mod middleware;
mod module_graph; mod module_graph;
pub mod module_graph_snapshot;
mod nft_json; mod nft_json;
pub mod operation; pub mod operation;
mod pages; mod pages;
......
...@@ -38,7 +38,7 @@ use crate::{ ...@@ -38,7 +38,7 @@ use crate::{
get_wasm_paths_from_root, paths_to_bindings, wasm_paths_to_bindings, get_wasm_paths_from_root, paths_to_bindings, wasm_paths_to_bindings,
}, },
project::Project, project::Project,
route::{Endpoint, EndpointOutput, EndpointOutputPaths}, route::{Endpoint, EndpointOutput, EndpointOutputPaths, ModuleGraphs},
}; };
#[turbo_tasks::value] #[turbo_tasks::value]
...@@ -408,4 +408,15 @@ impl Endpoint for MiddlewareEndpoint { ...@@ -408,4 +408,15 @@ impl Endpoint for MiddlewareEndpoint {
self.entry_module().to_resolved().await?, self.entry_module().to_resolved().await?,
])])) ])]))
} }
#[turbo_tasks::function]
async fn module_graphs(self: Vc<Self>) -> Result<Vc<ModuleGraphs>> {
let this = self.await?;
let module_graph = this
.project
.module_graph(self.entry_module())
.to_resolved()
.await?;
Ok(Vc::cell(vec![module_graph]))
}
} }
use std::{cell::RefCell, cmp::Reverse, collections::hash_map::Entry, mem::take};
use anyhow::Result;
use either::Either;
use rustc_hash::{FxHashMap, FxHashSet};
use serde::{Deserialize, Serialize};
use turbo_rcstr::RcStr;
use turbo_tasks::{
NonLocalValue, ResolvedVc, TryJoinIterExt, ValueToString, Vc, trace::TraceRawVcs,
};
use turbopack_core::{
asset::Asset,
chunk::ChunkingType,
module::{Module, Modules},
module_graph::{GraphTraversalAction, ModuleGraph},
resolve::ExportUsage,
};
#[derive(PartialEq, Eq, Serialize, Deserialize, TraceRawVcs, NonLocalValue, Debug)]
pub struct ModuleReference {
pub index: usize,
pub chunking_type: ChunkingType,
pub export: ExportUsage,
}
#[derive(PartialEq, Eq, Serialize, Deserialize, TraceRawVcs, NonLocalValue, Debug)]
pub struct ModuleInfo {
pub ident: RcStr,
pub path: RcStr,
pub depth: u32,
pub size: u32,
// TODO this should be per layer
pub retained_size: u32,
pub references: Vec<ModuleReference>,
pub incoming_references: Vec<ModuleReference>,
}
#[turbo_tasks::value]
pub struct ModuleGraphSnapshot {
pub modules: Vec<ModuleInfo>,
pub entries: Vec<usize>,
}
#[turbo_tasks::function]
pub async fn get_module_graph_snapshot(
module_graph: Vc<ModuleGraph>,
entry_modules: Option<Vc<Modules>>,
) -> Result<Vc<ModuleGraphSnapshot>> {
let module_graph = module_graph.await?;
struct RawModuleInfo {
module: ResolvedVc<Box<dyn Module>>,
depth: u32,
retained_modules: RefCell<FxHashSet<u32>>,
references: Vec<ModuleReference>,
incoming_references: Vec<ModuleReference>,
}
let mut entries = Vec::new();
let mut modules = Vec::new();
let mut module_to_index = FxHashMap::default();
fn get_or_create_module(
modules: &mut Vec<RawModuleInfo>,
module_to_index: &mut FxHashMap<ResolvedVc<Box<dyn Module>>, usize>,
module: ResolvedVc<Box<dyn Module>>,
) -> usize {
match module_to_index.entry(module) {
Entry::Occupied(entry) => *entry.get(),
Entry::Vacant(entry) => {
let index = modules.len();
modules.push(RawModuleInfo {
module,
depth: u32::MAX,
references: Vec::new(),
incoming_references: Vec::new(),
retained_modules: Default::default(),
});
entry.insert(index);
index
}
}
}
let entry_modules = if let Some(entry_modules) = entry_modules {
Either::Left(entry_modules.await?)
} else {
Either::Right(module_graph.entries().await?)
};
module_graph
.traverse_edges_from_entries_bfs(entry_modules.iter().copied(), |parent_info, node| {
let module = node.module;
let module_index = get_or_create_module(&mut modules, &mut module_to_index, module);
if let Some((parent_module, ty)) = parent_info {
let parent_index =
get_or_create_module(&mut modules, &mut module_to_index, parent_module.module);
let parent_module = &mut modules[parent_index];
let parent_depth = parent_module.depth;
debug_assert!(parent_depth < u32::MAX);
parent_module.references.push(ModuleReference {
index: module_index,
chunking_type: ty.chunking_type.clone(),
export: ty.export.clone(),
});
let module = &mut modules[module_index];
module.depth = module.depth.min(parent_depth + 1);
module.incoming_references.push(ModuleReference {
index: parent_index,
chunking_type: ty.chunking_type.clone(),
export: ty.export.clone(),
});
} else {
entries.push(module_index);
let module = &mut modules[module_index];
module.depth = 0;
}
Ok(GraphTraversalAction::Continue)
})
.await?;
let mut modules_by_depth = FxHashMap::default();
for (index, info) in modules.iter().enumerate() {
modules_by_depth
.entry(info.depth)
.or_insert_with(Vec::new)
.push(index);
}
let mut modules_by_depth = modules_by_depth.into_iter().collect::<Vec<_>>();
modules_by_depth.sort_by_key(|(depth, _)| Reverse(*depth));
for (depth, module_indicies) in modules_by_depth {
for module_index in module_indicies {
let module = &modules[module_index];
for ref_info in &module.incoming_references {
let ref_module = &modules[ref_info.index];
if ref_module.depth < depth {
let mut retained_modules = ref_module.retained_modules.borrow_mut();
retained_modules.insert(module_index as u32);
for retained in module.retained_modules.borrow().iter() {
retained_modules.insert(*retained);
}
}
}
}
}
let mut final_modules = modules
.iter_mut()
.map(async |info| {
Ok(ModuleInfo {
ident: info.module.ident().to_string().owned().await?,
path: info.module.ident().path().to_string().owned().await?,
depth: info.depth,
size: info
.module
.content()
.len()
.owned()
.await
// TODO all modules should report some content and should not crash
.unwrap_or_default()
.unwrap_or_default()
.try_into()
.unwrap_or(u32::MAX),
retained_size: 0,
references: take(&mut info.references),
incoming_references: take(&mut info.incoming_references),
})
})
.try_join()
.await?;
for (index, info) in modules.into_iter().enumerate() {
let retained_size = info
.retained_modules
.into_inner()
.iter()
.map(|&retained_index| {
let retained_info = &final_modules[retained_index as usize];
retained_info.size
})
.reduce(|a, b| a.saturating_add(b))
.unwrap_or_default();
final_modules[index].retained_size = retained_size + final_modules[index].size;
}
Ok(ModuleGraphSnapshot {
modules: final_modules,
entries,
}
.cell())
}
...@@ -77,7 +77,7 @@ use crate::{ ...@@ -77,7 +77,7 @@ use crate::{
get_wasm_paths_from_root, paths_to_bindings, wasm_paths_to_bindings, get_wasm_paths_from_root, paths_to_bindings, wasm_paths_to_bindings,
}, },
project::Project, project::Project,
route::{Endpoint, EndpointOutput, EndpointOutputPaths, Route, Routes}, route::{Endpoint, EndpointOutput, EndpointOutputPaths, ModuleGraphs, Route, Routes},
webpack_stats::generate_webpack_stats, webpack_stats::generate_webpack_stats,
}; };
...@@ -1729,6 +1729,13 @@ impl Endpoint for PageEndpoint { ...@@ -1729,6 +1729,13 @@ impl Endpoint for PageEndpoint {
Ok(Vc::cell(modules)) Ok(Vc::cell(modules))
} }
#[turbo_tasks::function]
async fn module_graphs(self: Vc<Self>) -> Result<Vc<ModuleGraphs>> {
let client_module_graph = self.client_module_graph().to_resolved().await?;
let ssr_module_graph = self.ssr_module_graph().to_resolved().await?;
Ok(Vc::cell(vec![client_module_graph, ssr_module_graph]))
}
} }
#[turbo_tasks::value] #[turbo_tasks::value]
......
...@@ -971,7 +971,9 @@ impl Project { ...@@ -971,7 +971,9 @@ impl Project {
} }
#[turbo_tasks::function] #[turbo_tasks::function]
pub async fn whole_app_module_graphs(self: ResolvedVc<Self>) -> Result<Vc<ModuleGraphs>> { pub async fn whole_app_module_graphs(
self: ResolvedVc<Self>,
) -> Result<Vc<BaseAndFullModuleGraph>> {
async move { async move {
let module_graphs_op = whole_app_module_graph_operation(self); let module_graphs_op = whole_app_module_graph_operation(self);
let module_graphs_vc = module_graphs_op.resolve_strongly_consistent().await?; let module_graphs_vc = module_graphs_op.resolve_strongly_consistent().await?;
...@@ -1815,7 +1817,7 @@ impl Project { ...@@ -1815,7 +1817,7 @@ impl Project {
#[turbo_tasks::function(operation)] #[turbo_tasks::function(operation)]
async fn whole_app_module_graph_operation( async fn whole_app_module_graph_operation(
project: ResolvedVc<Project>, project: ResolvedVc<Project>,
) -> Result<Vc<ModuleGraphs>> { ) -> Result<Vc<BaseAndFullModuleGraph>> {
mark_root(); mark_root();
let should_trace = project.next_mode().await?.is_production(); let should_trace = project.next_mode().await?.is_production();
...@@ -1833,7 +1835,7 @@ async fn whole_app_module_graph_operation( ...@@ -1833,7 +1835,7 @@ async fn whole_app_module_graph_operation(
); );
let full = ModuleGraph::from_graphs(vec![base_single_module_graph, additional_module_graph]); let full = ModuleGraph::from_graphs(vec![base_single_module_graph, additional_module_graph]);
Ok(ModuleGraphs { Ok(BaseAndFullModuleGraph {
base: base.to_resolved().await?, base: base.to_resolved().await?,
full: full.to_resolved().await?, full: full.to_resolved().await?,
} }
...@@ -1841,7 +1843,7 @@ async fn whole_app_module_graph_operation( ...@@ -1841,7 +1843,7 @@ async fn whole_app_module_graph_operation(
} }
#[turbo_tasks::value(shared)] #[turbo_tasks::value(shared)]
pub struct ModuleGraphs { pub struct BaseAndFullModuleGraph {
pub base: ResolvedVc<ModuleGraph>, pub base: ResolvedVc<ModuleGraph>,
pub full: ResolvedVc<ModuleGraph>, pub full: ResolvedVc<ModuleGraph>,
} }
......
...@@ -47,6 +47,9 @@ pub enum Route { ...@@ -47,6 +47,9 @@ pub enum Route {
Conflict, Conflict,
} }
#[turbo_tasks::value(transparent)]
pub struct ModuleGraphs(Vec<ResolvedVc<ModuleGraph>>);
#[turbo_tasks::value_trait] #[turbo_tasks::value_trait]
pub trait Endpoint { pub trait Endpoint {
#[turbo_tasks::function] #[turbo_tasks::function]
...@@ -65,6 +68,8 @@ pub trait Endpoint { ...@@ -65,6 +68,8 @@ pub trait Endpoint {
fn additional_entries(self: Vc<Self>, _graph: Vc<ModuleGraph>) -> Vc<GraphEntries> { fn additional_entries(self: Vc<Self>, _graph: Vc<ModuleGraph>) -> Vc<GraphEntries> {
GraphEntries::empty() GraphEntries::empty()
} }
#[turbo_tasks::function]
fn module_graphs(self: Vc<Self>) -> Vc<ModuleGraphs>;
} }
#[turbo_tasks::value(transparent)] #[turbo_tasks::value(transparent)]
......
...@@ -109,6 +109,7 @@ ...@@ -109,6 +109,7 @@
"styled-jsx": "5.1.6" "styled-jsx": "5.1.6"
}, },
"peerDependencies": { "peerDependencies": {
"@modelcontextprotocol/sdk": "^1.15.1",
"@opentelemetry/api": "^1.1.0", "@opentelemetry/api": "^1.1.0",
"@playwright/test": "^1.51.1", "@playwright/test": "^1.51.1",
"babel-plugin-react-compiler": "*", "babel-plugin-react-compiler": "*",
...@@ -123,6 +124,9 @@ ...@@ -123,6 +124,9 @@
"sass": { "sass": {
"optional": true "optional": true
}, },
"@modelcontextprotocol/sdk": {
"optional": true
},
"@opentelemetry/api": { "@opentelemetry/api": {
"optional": true "optional": true
}, },
...@@ -164,6 +168,7 @@ ...@@ -164,6 +168,7 @@
"@hapi/accept": "5.0.2", "@hapi/accept": "5.0.2",
"@jest/transform": "29.5.0", "@jest/transform": "29.5.0",
"@jest/types": "29.5.0", "@jest/types": "29.5.0",
"@modelcontextprotocol/sdk": "1.15.1",
"@mswjs/interceptors": "0.23.0", "@mswjs/interceptors": "0.23.0",
"@napi-rs/triples": "1.2.0", "@napi-rs/triples": "1.2.0",
"@next/font": "15.4.2-canary.23", "@next/font": "15.4.2-canary.23",
......
...@@ -64,9 +64,15 @@ export interface NapiWrittenEndpoint { ...@@ -64,9 +64,15 @@ export interface NapiWrittenEndpoint {
serverPaths: Array<NapiServerPath> serverPaths: Array<NapiServerPath>
config: NapiEndpointConfig config: NapiEndpointConfig
} }
export interface NapiModuleGraphSnapshots {
moduleGraphs: Array<NapiModuleGraphSnapshot>
}
export declare function endpointWriteToDisk(endpoint: { export declare function endpointWriteToDisk(endpoint: {
__napiType: 'Endpoint' __napiType: 'Endpoint'
}): Promise<TurbopackResult> }): Promise<TurbopackResult>
export declare function endpointModuleGraphs(endpoint: {
__napiType: 'Endpoint'
}): Promise<TurbopackResult>
export declare function endpointServerChangedSubscribe( export declare function endpointServerChangedSubscribe(
endpoint: { __napiType: 'Endpoint' }, endpoint: { __napiType: 'Endpoint' },
issues: boolean, issues: boolean,
...@@ -76,6 +82,27 @@ export declare function endpointClientChangedSubscribe( ...@@ -76,6 +82,27 @@ export declare function endpointClientChangedSubscribe(
endpoint: { __napiType: 'Endpoint' }, endpoint: { __napiType: 'Endpoint' },
func: (...args: any[]) => any func: (...args: any[]) => any
): { __napiType: 'RootTask' } ): { __napiType: 'RootTask' }
export interface NapiModuleReference {
/** The index of the referenced/referencing module in the modules list. */
index: number
/** The export used in the module reference. */
export: string
/** The type of chunking for the module reference. */
chunkingType: string
}
export interface NapiModuleInfo {
ident: RcStr
path: RcStr
depth: number
size: number
retainedSize: number
references: Array<NapiModuleReference>
incomingReferences: Array<NapiModuleReference>
}
export interface NapiModuleGraphSnapshot {
modules: Array<NapiModuleInfo>
entries: Array<number>
}
export interface NapiEnvVar { export interface NapiEnvVar {
name: RcStr name: RcStr
value: RcStr value: RcStr
...@@ -286,6 +313,9 @@ export declare function projectWriteAllEntrypointsToDisk( ...@@ -286,6 +313,9 @@ export declare function projectWriteAllEntrypointsToDisk(
project: { __napiType: 'Project' }, project: { __napiType: 'Project' },
appDirOnly: boolean appDirOnly: boolean
): Promise<TurbopackResult> ): Promise<TurbopackResult>
export declare function projectEntrypoints(project: {
__napiType: 'Project'
}): Promise<TurbopackResult>
export declare function projectEntrypointsSubscribe( export declare function projectEntrypointsSubscribe(
project: { __napiType: 'Project' }, project: { __napiType: 'Project' },
func: (...args: any[]) => any func: (...args: any[]) => any
...@@ -362,6 +392,9 @@ export declare function projectGetSourceMapSync( ...@@ -362,6 +392,9 @@ export declare function projectGetSourceMapSync(
project: { __napiType: 'Project' }, project: { __napiType: 'Project' },
filePath: RcStr filePath: RcStr
): string | null ): string | null
export declare function projectModuleGraph(project: {
__napiType: 'Project'
}): Promise<TurbopackResult>
/** /**
* A version of [`NapiNextTurbopackCallbacks`] that can accepted as an argument to a napi function. * A version of [`NapiNextTurbopackCallbacks`] that can accepted as an argument to a napi function.
* *
......
...@@ -19,6 +19,8 @@ import { isDeepStrictEqual } from 'util' ...@@ -19,6 +19,8 @@ import { isDeepStrictEqual } from 'util'
import { type DefineEnvOptions, getDefineEnv } from '../define-env' import { type DefineEnvOptions, getDefineEnv } from '../define-env'
import { getReactCompilerLoader } from '../get-babel-loader-config' import { getReactCompilerLoader } from '../get-babel-loader-config'
import type { import type {
NapiModuleGraphSnapshot,
NapiModuleGraphSnapshots,
NapiPartialProjectOptions, NapiPartialProjectOptions,
NapiProjectOptions, NapiProjectOptions,
NapiSourceDiagnostic, NapiSourceDiagnostic,
...@@ -669,6 +671,14 @@ function bindingToApi( ...@@ -669,6 +671,14 @@ function bindingToApi(
return napiEntrypointsToRawEntrypoints(napiEndpoints) return napiEntrypointsToRawEntrypoints(napiEndpoints)
} }
async getEntrypoints() {
const napiEndpoints = (await binding.projectEntrypoints(
this._nativeProject
)) as TurbopackResult<NapiEntrypoints>
return napiEntrypointsToRawEntrypoints(napiEndpoints)
}
entrypointsSubscribe() { entrypointsSubscribe() {
const subscription = subscribe<TurbopackResult<NapiEntrypoints>>( const subscription = subscribe<TurbopackResult<NapiEntrypoints>>(
false, false,
...@@ -742,6 +752,12 @@ function bindingToApi( ...@@ -742,6 +752,12 @@ function bindingToApi(
) )
} }
moduleGraph(): Promise<TurbopackResult<NapiModuleGraphSnapshot>> {
return binding.projectModuleGraph(this._nativeProject) as Promise<
TurbopackResult<NapiModuleGraphSnapshot>
>
}
invalidatePersistentCache(): Promise<void> { invalidatePersistentCache(): Promise<void> {
return binding.projectInvalidatePersistentCache(this._nativeProject) return binding.projectInvalidatePersistentCache(this._nativeProject)
} }
...@@ -793,6 +809,12 @@ function bindingToApi( ...@@ -793,6 +809,12 @@ function bindingToApi(
await serverSubscription.next() await serverSubscription.next()
return serverSubscription return serverSubscription
} }
async moduleGraphs(): Promise<TurbopackResult<NapiModuleGraphSnapshots>> {
return binding.endpointModuleGraphs(this._nativeEndpoint) as Promise<
TurbopackResult<NapiModuleGraphSnapshots>
>
}
} }
/** /**
......
...@@ -5,6 +5,8 @@ import type { ...@@ -5,6 +5,8 @@ import type {
RefCell, RefCell,
NapiTurboEngineOptions, NapiTurboEngineOptions,
NapiSourceDiagnostic, NapiSourceDiagnostic,
NapiModuleGraphSnapshots,
NapiModuleGraphSnapshot,
} from './generated-native' } from './generated-native'
export type { NapiTurboEngineOptions as TurboEngineOptions } export type { NapiTurboEngineOptions as TurboEngineOptions }
...@@ -216,6 +218,7 @@ export interface Project { ...@@ -216,6 +218,7 @@ export interface Project {
appDirOnly: boolean appDirOnly: boolean
): Promise<TurbopackResult<RawEntrypoints>> ): Promise<TurbopackResult<RawEntrypoints>>
getEntrypoints(): Promise<TurbopackResult<RawEntrypoints>>
entrypointsSubscribe(): AsyncIterableIterator<TurbopackResult<RawEntrypoints>> entrypointsSubscribe(): AsyncIterableIterator<TurbopackResult<RawEntrypoints>>
hmrEvents(identifier: string): AsyncIterableIterator<TurbopackResult<Update>> hmrEvents(identifier: string): AsyncIterableIterator<TurbopackResult<Update>>
...@@ -244,6 +247,8 @@ export interface Project { ...@@ -244,6 +247,8 @@ export interface Project {
invalidatePersistentCache(): Promise<void> invalidatePersistentCache(): Promise<void>
moduleGraph(): Promise<TurbopackResult<NapiModuleGraphSnapshot>>
shutdown(): Promise<void> shutdown(): Promise<void>
onExit(): Promise<void> onExit(): Promise<void>
...@@ -295,6 +300,11 @@ export interface Endpoint { ...@@ -295,6 +300,11 @@ export interface Endpoint {
serverChanged( serverChanged(
includeIssues: boolean includeIssues: boolean
): Promise<AsyncIterableIterator<TurbopackResult>> ): Promise<AsyncIterableIterator<TurbopackResult>>
/**
* Gets a snapshot of the module graphs for the endpoint.
*/
moduleGraphs(): Promise<TurbopackResult<NapiModuleGraphSnapshots>>
} }
interface EndpointConfig { interface EndpointConfig {
......
This diff is collapsed.
...@@ -85,6 +85,8 @@ import { getDefineEnv } from '../../../build/define-env' ...@@ -85,6 +85,8 @@ import { getDefineEnv } from '../../../build/define-env'
import { TurbopackInternalError } from '../../../shared/lib/turbopack/internal-error' import { TurbopackInternalError } from '../../../shared/lib/turbopack/internal-error'
import { normalizePath } from '../../../lib/normalize-path' import { normalizePath } from '../../../lib/normalize-path'
import { JSON_CONTENT_TYPE_HEADER } from '../../../lib/constants' import { JSON_CONTENT_TYPE_HEADER } from '../../../lib/constants'
import { parseBody } from '../../api-utils/node/parse-body'
import { timingSafeEqual } from 'crypto'
export type SetupOpts = { export type SetupOpts = {
renderServer: LazyRenderServerInstance renderServer: LazyRenderServerInstance
...@@ -969,9 +971,114 @@ async function startWatcher( ...@@ -969,9 +971,114 @@ async function startWatcher(
const devTurbopackMiddlewareManifestPath = `/_next/${CLIENT_STATIC_FILES_PATH}/development/${TURBOPACK_CLIENT_MIDDLEWARE_MANIFEST}` const devTurbopackMiddlewareManifestPath = `/_next/${CLIENT_STATIC_FILES_PATH}/development/${TURBOPACK_CLIENT_MIDDLEWARE_MANIFEST}`
opts.fsChecker.devVirtualFsItems.add(devTurbopackMiddlewareManifestPath) opts.fsChecker.devVirtualFsItems.add(devTurbopackMiddlewareManifestPath)
const mcpPath = `/_next/mcp`
opts.fsChecker.devVirtualFsItems.add(mcpPath)
let mcpSecret = process.env.NEXT_EXPERIMENTAL_MCP_SECRET
? Buffer.from(process.env.NEXT_EXPERIMENTAL_MCP_SECRET)
: undefined
if (mcpSecret) {
try {
// eslint-disable-next-line import/no-extraneous-dependencies
require('@modelcontextprotocol/sdk/package.json')
} catch (error) {
Log.error(
'To use the MCP server, please install the `@modelcontextprotocol/sdk` package.'
)
mcpSecret = undefined
}
}
let createMcpServer: typeof import('./mcp').createMcpServer | undefined
let StreamableHTTPServerTransport:
| typeof import('./mcp').StreamableHTTPServerTransport
| undefined
if (mcpSecret) {
;({ createMcpServer, StreamableHTTPServerTransport } =
require('./mcp') as typeof import('./mcp'))
Log.info(
`Experimental MCP server is available at: /_next/mcp?${mcpSecret.toString()}`
)
}
async function requestHandler(req: IncomingMessage, res: ServerResponse) { async function requestHandler(req: IncomingMessage, res: ServerResponse) {
const parsedUrl = url.parse(req.url || '/') const parsedUrl = url.parse(req.url || '/')
if (parsedUrl.pathname?.includes(mcpPath)) {
function sendMcpInternalError(message: string) {
res.statusCode = 500
res.setHeader('Content-Type', 'application/json; charset=utf-8')
res.end(
JSON.stringify({
jsonrpc: '2.0',
error: {
// "Internal error" see https://www.jsonrpc.org/specification
code: -32603,
message,
},
id: null,
})
)
return { finished: true }
}
if (!mcpSecret) {
Log.error('Next.js MCP server is not enabled')
Log.info(
'To enable it, set the NEXT_EXPERIMENTAL_MCP_SECRET environment variable to a secret value. This will make the MCP server available at /_next/mcp?{NEXT_EXPERIMENTAL_MCP_SECRET}'
)
return sendMcpInternalError(
'Missing NEXT_EXPERIMENTAL_MCP_SECRET environment variable'
)
}
if (!createMcpServer || !StreamableHTTPServerTransport) {
return sendMcpInternalError(
'Model Context Protocol (MCP) server is not available'
)
}
if (!parsedUrl.query) {
Log.error('No MCP secret provided in request query')
Log.info(
`Experimental MCP server is available at: /_next/mcp?${mcpSecret.toString()}`
)
return sendMcpInternalError('No MCP secret provided in request query')
}
let mcpSecretQuery = Buffer.from(parsedUrl.query)
if (
mcpSecretQuery.length !== mcpSecret.length ||
!timingSafeEqual(mcpSecretQuery, mcpSecret)
) {
Log.error('Invalid MCP secret provided in request query')
Log.info(
`Experimental MCP server is available at: /_next/mcp?${mcpSecret.toString()}`
)
return sendMcpInternalError(
'Invalid MCP secret provided in request query'
)
}
const server = createMcpServer(hotReloader)
if (server) {
try {
const transport = new StreamableHTTPServerTransport({
sessionIdGenerator: undefined,
})
res.on('close', () => {
transport.close()
server.close()
})
await server.connect(transport)
const parsedBody = await parseBody(req, 1024 * 1024 * 1024)
await transport.handleRequest(req, res, parsedBody)
} catch (error) {
Log.error('Error handling MCP request:', error)
if (!res.headersSent) {
return sendMcpInternalError('Internal server error')
}
}
return { finished: true }
}
}
if (parsedUrl.pathname?.includes(clientPagesManifestPath)) { if (parsedUrl.pathname?.includes(clientPagesManifestPath)) {
res.statusCode = 200 res.statusCode = 200
res.setHeader('Content-Type', JSON_CONTENT_TYPE_HEADER) res.setHeader('Content-Type', JSON_CONTENT_TYPE_HEADER)
......
...@@ -185,36 +185,7 @@ export function formatIssue(issue: Issue) { ...@@ -185,36 +185,7 @@ export function formatIssue(issue: Issue) {
if (importTraces?.length) { if (importTraces?.length) {
// This is the same logic as in turbopack/crates/turbopack-cli-utils/src/issue.rs // This is the same logic as in turbopack/crates/turbopack-cli-utils/src/issue.rs
// We end up with multiple traces when the file with the error is reachable from multiple message += formatImportTraces(importTraces)
// different entry points (e.g. ssr, client)
message += `Import trace${importTraces.length > 1 ? 's' : ''}:\n`
const everyTraceHasADistinctRootLayer =
new Set(importTraces.map(leafLayerName).filter((l) => l != null)).size ===
importTraces.length
for (let i = 0; i < importTraces.length; i++) {
const trace = importTraces[i]
const layer = leafLayerName(trace)
let traceIndent = ' '
// If this is true, layer must be present
if (everyTraceHasADistinctRootLayer) {
message += ` ${layer}:\n`
} else {
if (importTraces.length > 1) {
// Otherwise use simple 1 based indices to disambiguate
message += ` #${i + 1}`
if (layer) {
message += ` [${layer}]`
}
message += ':\n'
} else if (layer) {
message += ` [${layer}]:\n`
} else {
// If there is a single trace and no layer name just don't indent it.
traceIndent = ' '
}
}
message += formatIssueTrace(trace, traceIndent, !identicalLayers(trace))
}
} }
if (documentationLink) { if (documentationLink) {
message += documentationLink + '\n\n' message += documentationLink + '\n\n'
...@@ -222,6 +193,40 @@ export function formatIssue(issue: Issue) { ...@@ -222,6 +193,40 @@ export function formatIssue(issue: Issue) {
return message return message
} }
export function formatImportTraces(importTraces: PlainTraceItem[][]) {
// We end up with multiple traces when the file with the error is reachable from multiple
// different entry points (e.g. ssr, client)
let message = `Import trace${importTraces.length > 1 ? 's' : ''}:\n`
const everyTraceHasADistinctRootLayer =
new Set(importTraces.map(leafLayerName).filter((l) => l != null)).size ===
importTraces.length
for (let i = 0; i < importTraces.length; i++) {
const trace = importTraces[i]
const layer = leafLayerName(trace)
let traceIndent = ' '
// If this is true, layer must be present
if (everyTraceHasADistinctRootLayer) {
message += ` ${layer}:\n`
} else {
if (importTraces.length > 1) {
// Otherwise use simple 1 based indices to disambiguate
message += ` #${i + 1}`
if (layer) {
message += ` [${layer}]`
}
message += ':\n'
} else if (layer) {
message += ` [${layer}]:\n`
} else {
// If there is a single trace and no layer name just don't indent it.
traceIndent = ' '
}
}
message += formatIssueTrace(trace, traceIndent, !identicalLayers(trace))
}
return message
}
/** Returns the first present layer name in the trace */ /** Returns the first present layer name in the trace */
function leafLayerName(items: PlainTraceItem[]): string | undefined { function leafLayerName(items: PlainTraceItem[]): string | undefined {
for (const item of items) { for (const item of items) {
......
This diff is collapsed.
...@@ -1221,6 +1221,23 @@ impl ModuleGraph { ...@@ -1221,6 +1221,23 @@ impl ModuleGraph {
Ok(idx) Ok(idx)
} }
pub async fn entries(&self) -> Result<Vec<ResolvedVc<Box<dyn Module>>>> {
Ok(self
.get_graphs()
.await?
.iter()
.flat_map(|g| g.entries.iter())
.flat_map(|e| e.entries())
.collect())
}
pub async fn has_entry(&self, entry: ResolvedVc<Box<dyn Module>>) -> Result<bool> {
let graphs = self.get_graphs().await?;
Ok(graphs
.iter()
.any(|graph| graph.modules.contains_key(&entry)))
}
/// Traverses all reachable edges exactly once and calls the visitor with the edge source and /// Traverses all reachable edges exactly once and calls the visitor with the edge source and
/// target. /// target.
/// ///
......
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