Unverified Commit 8d3606c1 authored by Will Binns-Smith's avatar Will Binns-Smith Committed by GitHub

Bundle Analyzer Agent Skill: Expose more route details in analyze data (#99172)

## Summary

Export typed App, Pages and API endpoint roots and nested client
references from analyzer route provenance into JSON Lines
`route.entries`. Keep server/shared roles separate from verified client
bootstrap identities: a client reference is not another endpoint or
evidence of an observed initial request. Preserve UI binary artifacts
internally while agents consume semantic records.

## Verification

- This layer: root TypeScript and converter unit 5/5; targeted Rust
analyzer tests 3/3. Completed stack: analyzer e2e 7/7 (App, Pages, API),
full build 18/18. New-head CI pending.

<!-- NEXT_JS_LLM -->

<!-- fleet d7bdbc2b-b0bc-4591-99eb-dccb0fa49c73 -->

<!-- fleet ea768377-d71c-4a15-90b2-f4d50d4f6a1c -->

---------
Co-authored-by: 's avatarvercel-fleet-prod[bot] <318278635+vercel-fleet-prod[bot]@users.noreply.github.com>
Co-authored-by: 's avatarWill Binns-Smith <755844+wbinnssmith@users.noreply.github.com>
parent 1119ce6b
use std::{borrow::Cow, io::Write}; use std::{borrow::Cow, io::Write};
use anyhow::Result; use anyhow::Result;
use bincode::{Decode, Encode};
use byteorder::{BE, WriteBytesExt}; use byteorder::{BE, WriteBytesExt};
use either::Either; use either::Either;
use next_core::app_structure::FileSystemPathVec; use next_core::app_structure::FileSystemPathVec;
...@@ -8,7 +9,8 @@ use rustc_hash::{FxHashMap, FxHashSet}; ...@@ -8,7 +9,8 @@ use rustc_hash::{FxHashMap, FxHashSet};
use serde::Serialize; use serde::Serialize;
use turbo_rcstr::RcStr; use turbo_rcstr::RcStr;
use turbo_tasks::{ use turbo_tasks::{
FxIndexSet, JoinIterExt, ResolvedVc, TryFlatJoinIterExt, ValueToString, ValueToStringRef, Vc, FxIndexSet, JoinIterExt, NonLocalValue, ResolvedVc, TryFlatJoinIterExt, ValueToString,
ValueToStringRef, Vc,
}; };
use turbo_tasks_fs::{ use turbo_tasks_fs::{
File, FileContent, FileSystemPath, File, FileContent, FileSystemPath,
...@@ -103,6 +105,40 @@ pub struct AnalyzeOutputFile { ...@@ -103,6 +105,40 @@ pub struct AnalyzeOutputFile {
pub filename: RcStr, pub filename: RcStr,
} }
/// Exact endpoint graph root. Client roles and references are build-time
/// provenance; neither establishes that a browser requested a chunk.
#[turbo_tasks::value(shared)]
#[derive(Clone, Debug, Serialize)]
pub struct AnalyzeRouteEntry {
pub route_entry_id: RcStr,
pub module_ident: RcStr,
pub module_path: RcStr,
pub role: RcStr,
#[serde(skip_serializing_if = "Option::is_none")]
pub entry_kind: Option<RcStr>,
#[serde(skip_serializing_if = "Vec::is_empty")]
pub client_references: Vec<AnalyzeClientReferenceEntry>,
}
#[derive(Clone, Debug, PartialEq, Eq, NonLocalValue, Encode, Decode, Serialize)]
pub struct AnalyzeClientReferenceEntry {
pub module_ident: RcStr,
pub module_path: RcStr,
pub reference_kind: RcStr,
}
#[turbo_tasks::value(transparent)]
pub struct AnalyzeRouteEntries(Vec<AnalyzeRouteEntry>);
pub fn analyze_route_entry_id(
route: &str,
role: &str,
sub_name: &str,
module_ident: &str,
) -> RcStr {
format!("{route}|{role}|{sub_name}|{module_ident}").into()
}
#[derive(Serialize)] #[derive(Serialize)]
struct EdgesDataReference { struct EdgesDataReference {
pub offset: u32, pub offset: u32,
...@@ -116,6 +152,8 @@ struct AnalyzeDataHeader { ...@@ -116,6 +152,8 @@ struct AnalyzeDataHeader {
pub sources: Vec<AnalyzeSource>, pub sources: Vec<AnalyzeSource>,
pub chunk_parts: Vec<AnalyzeChunkPart>, pub chunk_parts: Vec<AnalyzeChunkPart>,
pub output_files: Vec<AnalyzeOutputFile>, pub output_files: Vec<AnalyzeOutputFile>,
/// Exact endpoint roots; nested client references do not become roots.
pub route_entries: Vec<AnalyzeRouteEntry>,
/// Edges from chunks to chunk parts /// Edges from chunks to chunk parts
pub output_file_chunk_parts: EdgesDataReference, pub output_file_chunk_parts: EdgesDataReference,
/// Edges from sources to chunk parts /// Edges from sources to chunk parts
...@@ -171,6 +209,7 @@ struct AnalyzeDataBuilder { ...@@ -171,6 +209,7 @@ struct AnalyzeDataBuilder {
source_index_map: FxHashMap<RcStr, u32>, source_index_map: FxHashMap<RcStr, u32>,
chunk_parts: Vec<AnalyzeChunkPart>, chunk_parts: Vec<AnalyzeChunkPart>,
output_files: Vec<AnalyzeOutputFileBuilder>, output_files: Vec<AnalyzeOutputFileBuilder>,
route_entries: Vec<AnalyzeRouteEntry>,
} }
struct ModulesDataBuilder { struct ModulesDataBuilder {
...@@ -196,12 +235,13 @@ impl EdgesDataSectionBuilder { ...@@ -196,12 +235,13 @@ impl EdgesDataSectionBuilder {
} }
impl AnalyzeDataBuilder { impl AnalyzeDataBuilder {
fn new() -> Self { fn new(route_entries: Vec<AnalyzeRouteEntry>) -> Self {
Self { Self {
sources: vec![], sources: vec![],
source_index_map: FxHashMap::default(), source_index_map: FxHashMap::default(),
chunk_parts: vec![], chunk_parts: vec![],
output_files: vec![], output_files: vec![],
route_entries,
} }
} }
...@@ -284,6 +324,7 @@ impl AnalyzeDataBuilder { ...@@ -284,6 +324,7 @@ impl AnalyzeDataBuilder {
.into_iter() .into_iter()
.map(|of| of.output_file) .map(|of| of.output_file)
.collect(), .collect(),
route_entries: self.route_entries,
output_file_chunk_parts: binary_section.add_edges(&output_file_chunk_parts), output_file_chunk_parts: binary_section.add_edges(&output_file_chunk_parts),
source_chunk_parts: binary_section.add_edges(&source_chunk_parts), source_chunk_parts: binary_section.add_edges(&source_chunk_parts),
source_children: binary_section.add_edges(&source_children), source_children: binary_section.add_edges(&source_children),
...@@ -442,6 +483,7 @@ impl AnalyzedRoute { ...@@ -442,6 +483,7 @@ impl AnalyzedRoute {
pub async fn analyze_output_assets( pub async fn analyze_output_assets(
output_assets: Vc<OutputAssets>, output_assets: Vc<OutputAssets>,
traced_files: Vc<FileSystemPathVec>, traced_files: Vc<FileSystemPathVec>,
route_entries: Vc<AnalyzeRouteEntries>,
) -> Result<Vc<AnalyzedRoute>> { ) -> Result<Vc<AnalyzedRoute>> {
let output_assets = all_assets_from_entries(output_assets); let output_assets = all_assets_from_entries(output_assets);
let mut summary = RouteBundleSummary { let mut summary = RouteBundleSummary {
...@@ -452,8 +494,9 @@ pub async fn analyze_output_assets( ...@@ -452,8 +494,9 @@ pub async fn analyze_output_assets(
compressed_size: 0, compressed_size: 0,
}, },
}; };
let route_entries = route_entries.await?.iter().cloned().collect();
let mut builder = AnalyzeDataBuilder::new(); let mut builder = AnalyzeDataBuilder::new(route_entries);
let prefix = format!("{SOURCE_URL_PROTOCOL}///"); let prefix = format!("{SOURCE_URL_PROTOCOL}///");
...@@ -697,6 +740,7 @@ pub struct AnalyzeDataOutputAsset { ...@@ -697,6 +740,7 @@ pub struct AnalyzeDataOutputAsset {
pub path: FileSystemPath, pub path: FileSystemPath,
pub output_assets: ResolvedVc<OutputAssets>, pub output_assets: ResolvedVc<OutputAssets>,
pub traced_files: ResolvedVc<FileSystemPathVec>, pub traced_files: ResolvedVc<FileSystemPathVec>,
pub route_entries: ResolvedVc<AnalyzeRouteEntries>,
} }
#[turbo_tasks::value_impl] #[turbo_tasks::value_impl]
...@@ -706,11 +750,13 @@ impl AnalyzeDataOutputAsset { ...@@ -706,11 +750,13 @@ impl AnalyzeDataOutputAsset {
path: FileSystemPath, path: FileSystemPath,
output_assets: ResolvedVc<OutputAssets>, output_assets: ResolvedVc<OutputAssets>,
traced_files: ResolvedVc<FileSystemPathVec>, traced_files: ResolvedVc<FileSystemPathVec>,
route_entries: ResolvedVc<AnalyzeRouteEntries>,
) -> Result<Vc<Self>> { ) -> Result<Vc<Self>> {
Ok(Self { Ok(Self {
path, path,
output_assets, output_assets,
traced_files, traced_files,
route_entries,
} }
.cell()) .cell())
} }
...@@ -721,7 +767,8 @@ impl Asset for AnalyzeDataOutputAsset { ...@@ -721,7 +767,8 @@ impl Asset for AnalyzeDataOutputAsset {
#[turbo_tasks::function] #[turbo_tasks::function]
fn content(&self) -> Vc<AssetContent> { fn content(&self) -> Vc<AssetContent> {
let file_content = let file_content =
analyze_output_assets(*self.output_assets, *self.traced_files).file_content(); analyze_output_assets(*self.output_assets, *self.traced_files, *self.route_entries)
.file_content();
AssetContent::file(file_content) AssetContent::file(file_content)
} }
} }
......
...@@ -17,7 +17,7 @@ use next_core::{ ...@@ -17,7 +17,7 @@ use next_core::{
get_client_runtime_entries, get_client_runtime_entries,
}, },
next_client_reference::{ next_client_reference::{
ClientReferenceGraphResult, NextCssClientReferenceTransition, ClientReferenceGraphResult, ClientReferenceType, NextCssClientReferenceTransition,
NextEcmascriptClientReferenceTransition, ServerEntries, find_server_entries, NextEcmascriptClientReferenceTransition, ServerEntries, find_server_entries,
}, },
next_config::NextConfig, next_config::NextConfig,
...@@ -84,7 +84,8 @@ use crate::{ ...@@ -84,7 +84,8 @@ use crate::{
}, },
project::{BaseAndFullModuleGraph, Project}, project::{BaseAndFullModuleGraph, Project},
route::{ route::{
AppPageRoute, Endpoint, EndpointOutput, EndpointOutputPaths, ModuleGraphs, Route, Routes, AnalyzeClientEntries, AnalyzeClientReference, 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},
service_worker::service_worker_output_assets, service_worker::service_worker_output_assets,
...@@ -2294,6 +2295,59 @@ impl Endpoint for AppEndpoint { ...@@ -2294,6 +2295,59 @@ impl Endpoint for AppEndpoint {
.client_changed(self.output().client_assets())) .client_changed(self.output().client_assets()))
} }
#[turbo_tasks::function]
async fn analyze_client_entries(self: Vc<Self>) -> Result<Vc<AnalyzeClientEntries>> {
let this = self.await?;
let app_entry = self.app_endpoint_entry().await?;
if !matches!(this.ty, AppEndpointType::Page { .. }) {
return Ok(AnalyzeClientEntries {
server_modules: vec![app_entry.rsc_entry],
..Default::default()
}
.cell());
}
let project = this.app_project.project();
let module_graphs = project.whole_app_module_graphs().await?;
let references = ClientReferencesGraphs::new(*module_graphs.base, false)
.get_client_references_for_endpoint(
*app_entry.rsc_entry,
true,
*project.should_write_nft_manifests().await?,
project.next_mode().await?.is_production(),
)
.await?;
let references = references
.client_references
.iter()
.map(async |reference| {
let (module, kind) = match reference.ty {
ClientReferenceType::EcmascriptClientReference(reference) => (
ResolvedVc::upcast(reference.await?.client_module),
rcstr!("ecmascript"),
),
ClientReferenceType::CssClientReference(reference) => {
(ResolvedVc::upcast(reference), rcstr!("css"))
}
};
Ok(AnalyzeClientReference { module, kind })
})
.try_join()
.await?;
Ok(AnalyzeClientEntries {
server_modules: vec![app_entry.rsc_entry],
bootstrap_modules: this
.app_project
.client_runtime_entries()
.await?
.iter()
.map(|module| ResolvedVc::upcast(*module))
.collect(),
references,
}
.cell())
}
#[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 this = self.await?; let this = self.await?;
......
...@@ -79,7 +79,10 @@ use crate::{ ...@@ -79,7 +79,10 @@ 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, ModuleGraphs, Route, Routes}, route::{
AnalyzeClientEntries, Endpoint, EndpointOutput, EndpointOutputPaths, ModuleGraphs, Route,
Routes,
},
service_worker::service_worker_output_assets, service_worker::service_worker_output_assets,
sri_manifest::get_sri_manifest_asset, sri_manifest::get_sri_manifest_asset,
}; };
...@@ -1758,6 +1761,26 @@ impl Endpoint for PageEndpoint { ...@@ -1758,6 +1761,26 @@ impl Endpoint for PageEndpoint {
.client_changed(self.output().client_assets())) .client_changed(self.output().client_assets()))
} }
#[turbo_tasks::function]
async fn analyze_client_entries(self: Vc<Self>) -> Result<Vc<AnalyzeClientEntries>> {
let is_html = self.await?.ty == PageEndpointType::Html;
let server_modules = vec![self.internal_ssr_chunk_module().await?.ssr_module];
Ok(AnalyzeClientEntries {
server_modules,
bootstrap_modules: if is_html {
self.client_evaluatable_assets()
.await?
.iter()
.map(|module| ResolvedVc::upcast(*module))
.collect()
} else {
vec![]
},
references: vec![],
}
.cell())
}
#[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 this = self.await?; let this = self.await?;
......
...@@ -10,6 +10,7 @@ use turbo_tasks::{ ...@@ -10,6 +10,7 @@ use turbo_tasks::{
TryJoinIterExt, Vc, debug::ValueDebugFormat, TryJoinIterExt, Vc, debug::ValueDebugFormat,
}; };
use turbopack_core::{ use turbopack_core::{
module::Module,
module_graph::{GraphEntries, ModuleGraph}, module_graph::{GraphEntries, ModuleGraph},
output::OutputAssets, output::OutputAssets,
}; };
...@@ -45,6 +46,26 @@ pub enum Route { ...@@ -45,6 +46,26 @@ pub enum Route {
#[turbo_tasks::value(transparent)] #[turbo_tasks::value(transparent)]
pub struct ModuleGraphs(Vec<ResolvedVc<ModuleGraph>>); pub struct ModuleGraphs(Vec<ResolvedVc<ModuleGraph>>);
/// Client-side modules associated with an endpoint.
#[turbo_tasks::value(shared)]
#[derive(Clone, Debug, Default)]
pub struct AnalyzeClientEntries {
/// Server entry modules established by this endpoint's build graph.
pub server_modules: Vec<ResolvedVc<Box<dyn Module>>>,
/// Client entry modules used to bootstrap this endpoint, not observed requests.
pub bootstrap_modules: Vec<ResolvedVc<Box<dyn Module>>>,
/// Client references discovered from the endpoint's server graph.
pub references: Vec<AnalyzeClientReference>,
}
#[derive(Clone, Debug, PartialEq, Eq, NonLocalValue, Encode, Decode)]
pub struct AnalyzeClientReference {
/// The referenced client module, with its bundler identity and variants intact.
pub module: ResolvedVc<Box<dyn Module>>,
/// Producer reference category (for example ECMAScript or CSS), not a load trigger.
pub kind: RcStr,
}
#[turbo_tasks::value_trait] #[turbo_tasks::value_trait]
pub trait Endpoint { pub trait Endpoint {
#[turbo_tasks::function] #[turbo_tasks::function]
...@@ -57,6 +78,11 @@ pub trait Endpoint { ...@@ -57,6 +78,11 @@ pub trait Endpoint {
/// The entry modules for the modules graph. /// The entry modules for the modules graph.
#[turbo_tasks::function] #[turbo_tasks::function]
fn entries(self: Vc<Self>) -> Vc<GraphEntries>; fn entries(self: Vc<Self>) -> Vc<GraphEntries>;
/// Build-time client bootstrap and client-reference modules for analysis.
#[turbo_tasks::function]
fn analyze_client_entries(self: Vc<Self>) -> Vc<AnalyzeClientEntries> {
AnalyzeClientEntries::default().cell()
}
/// Additional entry modules for the module graph. /// Additional entry modules for the module graph.
/// This may read the module graph and return additional modules. /// This may read the module graph and return additional modules.
#[turbo_tasks::function] #[turbo_tasks::function]
......
use std::sync::Arc; use std::sync::Arc;
use anyhow::Result; use anyhow::{Result, ensure};
use next_api::{ use next_api::{
analyze::{ analyze::{
AnalyzeDataOutputAsset, ModulesDataOutputAsset, RouteBundleSummary, analyze_output_assets, AnalyzeClientReferenceEntry, AnalyzeDataOutputAsset, AnalyzeRouteEntries,
combine_output_assets, combine_traced_files, AnalyzeRouteEntry, ModulesDataOutputAsset, RouteBundleSummary, analyze_output_assets,
analyze_route_entry_id, combine_output_assets, combine_traced_files,
}, },
project::ProjectContainer, project::ProjectContainer,
route::EndpointGroupKey, route::{Endpoint, EndpointGroup, EndpointGroupKey},
}; };
use serde::Serialize; use serde::Serialize;
use turbo_tasks::{Effects, ReadRef, ResolvedVc, TryJoinIterExt, Vc}; use turbo_rcstr::rcstr;
use turbo_tasks::{
Effects, FxIndexSet, ReadRef, ResolvedVc, TryJoinIterExt, ValueToString, ValueToStringRef, Vc,
};
use turbo_tasks_fs::{File, FileContent, FileSystemPath}; use turbo_tasks_fs::{File, FileContent, FileSystemPath};
use turbopack_core::{ use turbopack_core::{
asset::AssetContent, asset::AssetContent,
issue::PlainIssue, issue::PlainIssue,
module::Module,
output::{OutputAsset, OutputAssets}, output::{OutputAsset, OutputAssets},
virtual_output::VirtualOutputAsset, virtual_output::VirtualOutputAsset,
}; };
...@@ -64,6 +69,89 @@ async fn write_analyze_data_with_issues_operation_inner( ...@@ -64,6 +69,89 @@ async fn write_analyze_data_with_issues_operation_inner(
Ok(()) Ok(())
} }
/// Routes are rooted by server code, with client references nested under those roots.
async fn route_entries(
key: &EndpointGroupKey,
endpoint_group: &EndpointGroup,
role: &str,
) -> Result<Vec<AnalyzeRouteEntry>> {
let mut result = vec![];
for endpoint in &endpoint_group.primary {
let entries = endpoint.endpoint.entries().await?;
let client = endpoint.endpoint.analyze_client_entries().await?;
let bootstrap = client
.bootstrap_modules
.iter()
.map(|module| module.ident().to_string().owned())
.try_join()
.await?
.into_iter()
.collect::<FxIndexSet<_>>();
let server = client
.server_modules
.iter()
.map(|module| module.ident().to_string().owned())
.try_join()
.await?
.into_iter()
.collect::<FxIndexSet<_>>();
let owner = if let Some(module) = client.server_modules.first() {
Some(module.ident().to_string().owned().await?)
} else {
None
};
let mut seen_references = FxIndexSet::default();
let mut references = vec![];
for reference in &client.references {
let module_ident = reference.module.ident().to_string().owned().await?;
if !seen_references.insert((module_ident.clone(), reference.kind.clone())) {
continue;
}
references.push(AnalyzeClientReferenceEntry {
module_path: reference.module.ident().await?.path.to_string_ref().await?,
module_ident,
reference_kind: reference.kind.clone(),
});
}
ensure!(
references.is_empty() || owner.is_some(),
"client references without an endpoint root"
);
let mut seen = FxIndexSet::default();
let mut attached = false;
for module in entries.all_modules() {
let module_ident = module.ident().to_string().owned().await?;
if !seen.insert(module_ident.clone()) {
continue;
}
let module_path = module.ident().await?.path.to_string_ref().await?;
let is_owner = owner.as_ref() == Some(&module_ident);
attached |= is_owner;
let sub_name = endpoint.sub_name.as_deref().unwrap_or("");
result.push(AnalyzeRouteEntry {
route_entry_id: analyze_route_entry_id(key.as_str(), role, sub_name, &module_ident),
entry_kind: if bootstrap.contains(&module_ident) {
Some(rcstr!("client_bootstrap"))
} else if server.contains(&module_ident) {
Some(rcstr!("server"))
} else {
None
},
client_references: if is_owner { references.clone() } else { vec![] },
module_ident,
module_path,
role: role.into(),
});
}
ensure!(
references.is_empty() || attached,
"client references cannot be attached to an endpoint root"
);
}
result.sort_by(|a, b| a.route_entry_id.cmp(&b.route_entry_id));
Ok(result)
}
#[turbo_tasks::function(operation)] #[turbo_tasks::function(operation)]
async fn get_analyze_data_operation( async fn get_analyze_data_operation(
container: ResolvedVc<ProjectContainer>, container: ResolvedVc<ProjectContainer>,
...@@ -98,6 +186,15 @@ async fn get_analyze_data_operation( ...@@ -98,6 +186,15 @@ async fn get_analyze_data_operation(
let has_combined = !combined_output_assets.is_empty(); let has_combined = !combined_output_assets.is_empty();
let combined_assets_vc = Vc::cell(combined_output_assets); let combined_assets_vc = Vc::cell(combined_output_assets);
let combined_traced_vc = Vc::cell(combined_traced_files); let combined_traced_vc = Vc::cell(combined_traced_files);
let mut shared_route_entries = vec![];
for (key, endpoint_group) in endpoint_groups.iter() {
if matches!(
key,
EndpointGroupKey::PagesApp | EndpointGroupKey::PagesDocument
) {
shared_route_entries.extend(route_entries(key, endpoint_group, "shared").await?);
}
}
let analyze_data_and_summaries = endpoint_groups let analyze_data_and_summaries = endpoint_groups
.iter() .iter()
...@@ -124,12 +221,36 @@ async fn get_analyze_data_operation( ...@@ -124,12 +221,36 @@ async fn get_analyze_data_operation(
} else { } else {
endpoint_group.traced_files() endpoint_group.traced_files()
}; };
let mut entries = route_entries(key, endpoint_group, "route").await?;
let has_client_bootstrap = entries
.iter()
.any(|entry| entry.entry_kind.as_deref() == Some("client_bootstrap"));
if has_combined
&& !matches!(
key,
EndpointGroupKey::PagesApp | EndpointGroupKey::PagesDocument
)
{
entries.extend(shared_route_entries.iter().cloned().map(|mut entry| {
// Shared Pages output appears in API route artifacts as well.
// That does not make it an API route's browser bootstrap.
if !has_client_bootstrap {
entry.entry_kind = None;
entry.client_references.clear();
}
entry
}));
entries.sort_by(|a, b| a.route_entry_id.cmp(&b.route_entry_id));
entries.dedup_by(|a, b| a.route_entry_id == b.route_entry_id);
}
let route_entries: Vc<AnalyzeRouteEntries> = Vc::cell(entries);
let analyze_data = AnalyzeDataOutputAsset::new( let analyze_data = AnalyzeDataOutputAsset::new(
analyze_output_root analyze_output_root
.join(&key.to_string())? .join(&key.to_string())?
.join("analyze.data")?, .join("analyze.data")?,
output_assets, output_assets,
traced_files, traced_files,
route_entries,
) )
.to_resolved() .to_resolved()
.await?; .await?;
...@@ -137,7 +258,7 @@ async fn get_analyze_data_operation( ...@@ -137,7 +258,7 @@ async fn get_analyze_data_operation(
let summary = if let EndpointGroupKey::Route(route) = key { let summary = if let EndpointGroupKey::Route(route) = key {
Some(RouteSummary { Some(RouteSummary {
route: route.to_string(), route: route.to_string(),
bundles: analyze_output_assets(output_assets, traced_files) bundles: analyze_output_assets(output_assets, traced_files, route_entries)
.await? .await?
.summary, .summary,
}) })
......
...@@ -90,14 +90,84 @@ ...@@ -90,14 +90,84 @@
"additionalProperties": true, "additionalProperties": true,
"description": "Whole-app module graph, including when --route filters route-local records." "description": "Whole-app module graph, including when --route filters route-local records."
}, },
"clientReference": {
"type": "object",
"required": ["module_ident", "module_path", "reference_kind"],
"properties": {
"module_ident": {
"$ref": "#/$defs/identity",
"description": "Exact nested client module ident; join to the module graph's ident, not module_path."
},
"module_path": {
"type": "string",
"description": "Client reference path, not a unique module key."
},
"reference_kind": {
"enum": ["ecmascript", "css"],
"description": "Client reference category; not an endpoint root or observed request."
}
},
"additionalProperties": true
},
"entry": {
"type": "object",
"required": ["route_entry_id", "module_ident", "module_path", "role"],
"properties": {
"route_entry_id": {
"type": "string",
"description": "Snapshot-scoped endpoint root ID, distinguishing endpoint variants on one route."
},
"module_ident": {
"$ref": "#/$defs/identity",
"description": "Exact root module ident; variants can share a module_path."
},
"module_path": {
"type": "string",
"description": "Source path associated with this endpoint root; not a unique module identity."
},
"role": {
"enum": ["route", "shared"],
"description": "Endpoint build role, not a browser-load classification."
},
"entry_kind": {
"enum": ["server", "client_bootstrap"],
"description": "Server or client bootstrap when known; omitted for unknown roles."
},
"client_references": {
"type": "array",
"items": { "$ref": "#/$defs/clientReference" },
"description": "Nested client references, not additional roots or proof of an initial request. Join by module_ident."
}
},
"additionalProperties": true
},
"route": { "route": {
"type": "object", "type": "object",
"required": ["type", "route"], "required": ["type", "route", "entries", "coverage"],
"properties": { "properties": {
"type": { "const": "route", "description": "Defines a route record." }, "type": { "const": "route", "description": "Defines a route record." },
"route": { "route": {
"type": "string", "type": "string",
"description": "The unique normalized route key from routes.json." "description": "The unique normalized route key from routes.json."
},
"entries": {
"description": "Endpoint roots; shared Pages roots on an API artifact do not establish browser work for that API. Null means unavailable evidence.",
"oneOf": [
{ "type": "null" },
{ "type": "array", "items": { "$ref": "#/$defs/entry" } }
]
},
"coverage": {
"type": "object",
"required": ["entries"],
"properties": {
"entries": {
"enum": ["exact", "unknown"],
"description": "The producer supplied typed endpoint roots for this route when exact. This is not a statement about browser requests or timing."
}
},
"additionalProperties": true,
"description": "Completeness of separate build-time route evidence; unknown is not empty."
} }
}, },
"additionalProperties": true, "additionalProperties": true,
......
...@@ -24,6 +24,18 @@ type RouteHeader = { ...@@ -24,6 +24,18 @@ type RouteHeader = {
sources: Source[] sources: Source[]
chunk_parts: Part[] chunk_parts: Part[]
output_files: Array<{ filename: string }> output_files: Array<{ filename: string }>
route_entries?: Array<{
route_entry_id: string
module_ident: string
module_path: string
role: 'route' | 'shared'
entry_kind?: 'server' | 'client_bootstrap'
client_references?: Array<{
module_ident: string
module_path: string
reference_kind: 'ecmascript' | 'css'
}>
}>
} }
type Data<H> = { header: H; binary: Buffer } type Data<H> = { header: H; binary: Buffer }
...@@ -199,7 +211,7 @@ function parseRoutes(file: string) { ...@@ -199,7 +211,7 @@ function parseRoutes(file: string) {
for (const output of outputs) for (const output of outputs)
if (typeof output.filename !== 'string') if (typeof output.filename !== 'string')
throw new Error('Invalid output filename') throw new Error('Invalid output filename')
return { paths, header } return { paths, header, entries: header.route_entries ?? null }
} }
function writeRecord(record: object): void { function writeRecord(record: object): void {
...@@ -262,11 +274,13 @@ export function dumpAnalyzeGraph( ...@@ -262,11 +274,13 @@ export function dumpAnalyzeGraph(
// Retain only the current route's data. A later validation error may leave // Retain only the current route's data. A later validation error may leave
// partial output; callers must check the exit status before using it. // partial output; callers must check the exit status before using it.
for (const route of selected) { for (const route of selected) {
const { paths, header } = parseRoutes(routeFile(directory, route)) const { paths, header, entries } = parseRoutes(routeFile(directory, route))
const prefix = { route } const prefix = { route }
writeRecord({ writeRecord({
type: 'route', type: 'route',
...prefix, ...prefix,
entries,
coverage: { entries: entries ? 'exact' : 'unknown' },
}) })
for (let i = 0; i < header.output_files.length; i++) { for (let i = 0; i < header.output_files.length; i++) {
writeRecord({ writeRecord({
......
export function GET() {
return Response.json({ ok: true })
}
'use client'
import { useState } from 'react'
export default function ClientEntry() {
const [count, setCount] = useState(0)
return <button onClick={() => setCount(count + 1)}>Count: {count}</button>
}
.analyze-page {
color: #135;
}
import ClientEntry from './client-entry'
import './page.css'
export default function Page() { export default function Page() {
return <div>Hello World</div> return (
<div className="analyze-page">
Hello World
<ClientEntry />
</div>
)
} }
...@@ -10,6 +10,24 @@ import path from 'node:path' ...@@ -10,6 +10,24 @@ import path from 'node:path'
import type { ChildProcess } from 'node:child_process' import type { ChildProcess } from 'node:child_process'
import { existsSync, readFileSync, writeFileSync } from 'node:fs' import { existsSync, readFileSync, writeFileSync } from 'node:fs'
type RouteEntry = {
route_entry_id: string
module_ident: string
role: 'route' | 'shared'
entry_kind?: 'server' | 'client_bootstrap'
client_references?: Array<{
module_ident: string
module_path: string
reference_kind: 'ecmascript' | 'css'
}>
}
function readAnalyzeHeader<T>(filename: string): T {
const buffer = readFileSync(filename)
const jsonLength = buffer.readUInt32BE(0)
return JSON.parse(buffer.subarray(4, 4 + jsonLength).toString('utf8')) as T
}
// TODO(deploy-test-completion): Re-enable this suite in deploy mode. // TODO(deploy-test-completion): Re-enable this suite in deploy mode.
// It likely inspects local build artifacts that deploy tests do not expose. // It likely inspects local build artifacts that deploy tests do not expose.
// @force-gate !deploy // @force-gate !deploy
...@@ -321,7 +339,9 @@ describe('next analyze', () => { ...@@ -321,7 +339,9 @@ describe('next analyze', () => {
const routes = JSON.parse( const routes = JSON.parse(
readFileSync(path.join(defaultOutputPath, 'data/routes.json'), 'utf8') readFileSync(path.join(defaultOutputPath, 'data/routes.json'), 'utf8')
) )
expect(routes).toEqual(['/', '/_not-found']) expect([...routes].sort()).toEqual(
['/', '/_not-found', '/api/ping', '/legacy'].sort()
)
const routeSummaries = JSON.parse( const routeSummaries = JSON.parse(
readFileSync( readFileSync(
path.join(defaultOutputPath, 'data/route-summaries.json'), path.join(defaultOutputPath, 'data/route-summaries.json'),
...@@ -401,6 +421,75 @@ describe('next analyze', () => { ...@@ -401,6 +421,75 @@ describe('next analyze', () => {
(snapshot: { name: string }) => snapshot.name (snapshot: { name: string }) => snapshot.name
) )
expect(new Set(generatedNames).size).toBe(generatedNames.length) expect(new Set(generatedNames).size).toBe(generatedNames.length)
const dataDir = path.join(defaultOutputPath, 'data')
const { modules } = readAnalyzeHeader<{
modules: Array<{ ident: string }>
}>(path.join(dataDir, 'modules.data'))
const moduleIdents = new Set(modules.map((module) => module.ident))
const { route_entries: appEntries } = readAnalyzeHeader<{
route_entries: RouteEntry[]
}>(path.join(dataDir, 'analyze.data'))
const { route_entries: pagesEntries } = readAnalyzeHeader<{
route_entries: RouteEntry[]
}>(path.join(dataDir, 'legacy', 'analyze.data'))
const { route_entries: apiEntries } = readAnalyzeHeader<{
route_entries: RouteEntry[]
}>(path.join(dataDir, 'api', 'ping', 'analyze.data'))
expect(appEntries.some((entry) => entry.entry_kind === 'server')).toBe(
true
)
const routeRoots = appEntries.filter(
(entry) => entry.role === 'route' && entry.entry_kind === 'server'
)
expect(routeRoots).toHaveLength(2)
expect(
new Set(routeRoots.map((entry) => entry.route_entry_id)).size
).toBe(2)
expect(
appEntries.some((entry) => entry.entry_kind === 'client_bootstrap')
).toBe(true)
const clientReferences = appEntries.flatMap(
(entry) => entry.client_references ?? []
)
expect(
clientReferences.some((reference) =>
reference.module_path.includes('client-entry')
)
).toBe(true)
expect(
clientReferences.some(
(reference) => reference.reference_kind === 'css'
)
).toBe(true)
const rootIdents = new Set(
appEntries.map((entry) => entry.module_ident)
)
for (const reference of clientReferences) {
expect(rootIdents.has(reference.module_ident)).toBe(false)
expect(moduleIdents.has(reference.module_ident)).toBe(true)
expect(['ecmascript', 'css']).toContain(reference.reference_kind)
}
expect(
pagesEntries.some((entry) => entry.entry_kind === 'client_bootstrap')
).toBe(true)
expect(pagesEntries.some((entry) => entry.role === 'shared')).toBe(true)
expect(apiEntries.some((entry) => entry.entry_kind === 'server')).toBe(
true
)
expect(
apiEntries.some((entry) => entry.entry_kind === 'client_bootstrap')
).toBe(false)
expect(
apiEntries.every((entry) => !entry.client_references?.length)
).toBe(true)
for (const entry of [...appEntries, ...pagesEntries, ...apiEntries]) {
expect(moduleIdents.has(entry.module_ident)).toBe(true)
expect(entry).not.toHaveProperty('runtime')
expect(entry).not.toHaveProperty('initial')
expect(entry).not.toHaveProperty('load_scope')
}
}) })
}) })
}) })
......
export default function LegacyPage() {
return <p>Legacy route</p>
}
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