Unverified Commit 87ee4fe4 authored by Niklas Mischkulnig's avatar Niklas Mischkulnig Committed by GitHub

Turbopack: refactor NFT to add Endpoint.traced_files (#94224)

Refactor the module-graph bit of NFT so that we can provide
`Endpoint.traced_files()` which is needed for the bundle analyzer

There should be no logic change whatsoever
parent 52008001
...@@ -4826,11 +4826,13 @@ dependencies = [ ...@@ -4826,11 +4826,13 @@ dependencies = [
"swc_core", "swc_core",
"swc_sourcemap", "swc_sourcemap",
"thiserror 1.0.69", "thiserror 1.0.69",
"tokio",
"tracing", "tracing",
"turbo-bincode", "turbo-bincode",
"turbo-esregex", "turbo-esregex",
"turbo-rcstr", "turbo-rcstr",
"turbo-tasks", "turbo-tasks",
"turbo-tasks-backend",
"turbo-tasks-bytes", "turbo-tasks-bytes",
"turbo-tasks-env", "turbo-tasks-env",
"turbo-tasks-fetch", "turbo-tasks-fetch",
......
...@@ -78,6 +78,7 @@ use crate::{ ...@@ -78,6 +78,7 @@ use crate::{
font::FontManifest, font::FontManifest,
loadable_manifest::create_react_loadable_manifest, loadable_manifest::create_react_loadable_manifest,
module_graph::{ClientReferencesGraphs, NextDynamicGraphs, ServerActionsGraphs}, module_graph::{ClientReferencesGraphs, NextDynamicGraphs, ServerActionsGraphs},
nft::{EndpointTraceResult, trace_endpoint},
nft_json::NftJsonAsset, nft_json::NftJsonAsset,
paths::{ paths::{
all_asset_paths, all_paths_in_root, get_asset_paths_from_root, get_js_paths_from_root, all_asset_paths, all_paths_in_root, get_asset_paths_from_root, get_js_paths_from_root,
...@@ -1789,8 +1790,7 @@ impl AppEndpoint { ...@@ -1789,8 +1790,7 @@ impl AppEndpoint {
.chain(loadable_manifest_output.iter().flat_map(|m| &**m).copied()) .chain(loadable_manifest_output.iter().flat_map(|m| &**m).copied())
.map(|m| *m) .map(|m| *m)
.collect(), .collect(),
*module_graphs.full, self.trace_result(),
vec![*rsc_entry],
) )
.to_resolved() .to_resolved()
.await?, .await?,
...@@ -1978,6 +1978,33 @@ impl AppEndpoint { ...@@ -1978,6 +1978,33 @@ impl AppEndpoint {
} }
}) })
} }
#[turbo_tasks::function]
async fn trace_result(self: Vc<Self>) -> Result<Vc<EndpointTraceResult>> {
let this = self.await?;
let app_entry = self.app_endpoint_entry().await?;
let rsc_entry = app_entry.rsc_entry;
let is_app_page = matches!(this.ty, AppEndpointType::Page { .. });
let module_graphs = this
.app_project
.app_module_graphs(
self,
*rsc_entry,
// We only need the client runtime entries for pages not for Route Handlers
is_app_page.then(|| this.app_project.client_runtime_entries()),
)
.await?;
Ok(trace_endpoint(
this.app_project.project(),
Some(app_function_name(&app_entry.original_name).into()),
*module_graphs.full,
vec![*rsc_entry],
))
}
} }
async fn create_app_paths_manifest( async fn create_app_paths_manifest(
...@@ -2200,6 +2227,11 @@ impl Endpoint for AppEndpoint { ...@@ -2200,6 +2227,11 @@ impl Endpoint for AppEndpoint {
async fn project(self: Vc<Self>) -> Result<Vc<Project>> { async fn project(self: Vc<Self>) -> Result<Vc<Project>> {
Ok(self.await?.app_project.project()) Ok(self.await?.app_project.project())
} }
#[turbo_tasks::function]
fn traced_files(self: Vc<Self>) -> Vc<FileSystemPathVec> {
self.trace_result().all_files()
}
} }
#[turbo_tasks::value] #[turbo_tasks::value]
......
use anyhow::{Result, bail}; use anyhow::{Result, bail};
use next_core::app_structure::FileSystemPathVec;
use turbo_tasks::{Completion, ResolvedVc, Vc}; use turbo_tasks::{Completion, ResolvedVc, Vc};
use turbopack_core::module_graph::GraphEntries; use turbopack_core::module_graph::GraphEntries;
...@@ -51,4 +52,9 @@ impl Endpoint for EmptyEndpoint { ...@@ -51,4 +52,9 @@ impl Endpoint for EmptyEndpoint {
fn project(&self) -> Vc<Project> { fn project(&self) -> Vc<Project> {
*self.project *self.project
} }
#[turbo_tasks::function]
fn traced_files(self: Vc<Self>) -> Vc<FileSystemPathVec> {
Vc::cell(vec![])
}
} }
use anyhow::Result; use anyhow::Result;
use next_core::{ use next_core::{
app_structure::FileSystemPathVec,
next_edge::entry::wrap_edge_entry, next_edge::entry::wrap_edge_entry,
next_manifests::{InstrumentationDefinition, MiddlewaresManifestV2}, next_manifests::{InstrumentationDefinition, MiddlewaresManifestV2},
}; };
...@@ -26,6 +27,7 @@ use turbopack_core::{ ...@@ -26,6 +27,7 @@ use turbopack_core::{
}; };
use crate::{ use crate::{
nft::{EndpointTraceResult, trace_endpoint},
nft_json::NftJsonAsset, nft_json::NftJsonAsset,
paths::{ paths::{
all_asset_paths, get_js_paths_from_root, get_wasm_paths_from_root, wasm_paths_to_bindings, all_asset_paths, get_js_paths_from_root, get_wasm_paths_from_root, wasm_paths_to_bindings,
...@@ -185,23 +187,27 @@ impl InstrumentationEndpoint { ...@@ -185,23 +187,27 @@ impl InstrumentationEndpoint {
let chunk = self.node_chunk().to_resolved().await?; let chunk = self.node_chunk().to_resolved().await?;
let mut output_assets = vec![chunk]; let mut output_assets = vec![chunk];
if this.project.next_mode().await?.is_production() { if this.project.next_mode().await?.is_production() {
let userland_module = self.entry_module();
output_assets.push(ResolvedVc::upcast( output_assets.push(ResolvedVc::upcast(
NftJsonAsset::new( NftJsonAsset::new(*this.project, None, *chunk, vec![], self.trace_result())
*this.project, .to_resolved()
None, .await?,
*chunk,
vec![],
this.project.module_graph(userland_module),
vec![userland_module],
)
.to_resolved()
.await?,
)); ));
} }
Ok(Vc::cell(output_assets)) Ok(Vc::cell(output_assets))
} }
} }
#[turbo_tasks::function]
async fn trace_result(self: Vc<Self>) -> Result<Vc<EndpointTraceResult>> {
let this = self.await?;
let userland_module = self.entry_module();
Ok(trace_endpoint(
*this.project,
None,
this.project.module_graph(userland_module),
vec![userland_module],
))
}
} }
#[turbo_tasks::value_impl] #[turbo_tasks::value_impl]
...@@ -265,4 +271,9 @@ impl Endpoint for InstrumentationEndpoint { ...@@ -265,4 +271,9 @@ impl Endpoint for InstrumentationEndpoint {
fn project(&self) -> Vc<Project> { fn project(&self) -> Vc<Project> {
*self.project *self.project
} }
#[turbo_tasks::function]
fn traced_files(self: Vc<Self>) -> Vc<FileSystemPathVec> {
self.trace_result().all_files()
}
} }
...@@ -15,6 +15,7 @@ mod loadable_manifest; ...@@ -15,6 +15,7 @@ mod loadable_manifest;
mod middleware; mod middleware;
mod module_graph; mod module_graph;
pub mod next_server_nft; pub mod next_server_nft;
mod nft;
mod nft_json; mod nft_json;
pub mod operation; pub mod operation;
mod pages; mod pages;
......
...@@ -2,6 +2,7 @@ use std::future::IntoFuture; ...@@ -2,6 +2,7 @@ use std::future::IntoFuture;
use anyhow::{Context, Result}; use anyhow::{Context, Result};
use next_core::{ use next_core::{
app_structure::FileSystemPathVec,
middleware::get_middleware_module, middleware::get_middleware_module,
next_edge::entry::wrap_edge_entry, next_edge::entry::wrap_edge_entry,
next_manifests::{EdgeFunctionDefinition, MiddlewaresManifestV2, ProxyMatcher, Regions}, next_manifests::{EdgeFunctionDefinition, MiddlewaresManifestV2, ProxyMatcher, Regions},
...@@ -28,6 +29,7 @@ use turbopack_core::{ ...@@ -28,6 +29,7 @@ use turbopack_core::{
}; };
use crate::{ use crate::{
nft::{EndpointTraceResult, trace_endpoint},
nft_json::NftJsonAsset, nft_json::NftJsonAsset,
paths::{ paths::{
all_asset_paths, all_paths_in_root, get_asset_paths_from_root, get_js_paths_from_root, all_asset_paths, all_paths_in_root, get_asset_paths_from_root, get_js_paths_from_root,
...@@ -227,18 +229,10 @@ impl MiddlewareEndpoint { ...@@ -227,18 +229,10 @@ impl MiddlewareEndpoint {
let chunk = self.node_chunk().to_resolved().await?; let chunk = self.node_chunk().to_resolved().await?;
let mut output_assets = vec![chunk]; let mut output_assets = vec![chunk];
if this.project.next_mode().await?.is_production() { if this.project.next_mode().await?.is_production() {
let userland_module = self.entry_module();
output_assets.push(ResolvedVc::upcast( output_assets.push(ResolvedVc::upcast(
NftJsonAsset::new( NftJsonAsset::new(*this.project, None, *chunk, vec![], self.trace_result())
*this.project, .to_resolved()
None, .await?,
*chunk,
vec![],
this.project.module_graph(userland_module),
vec![userland_module],
)
.to_resolved()
.await?,
)); ));
} }
let middleware_manifest_v2 = MiddlewaresManifestV2 { let middleware_manifest_v2 = MiddlewaresManifestV2 {
...@@ -344,6 +338,18 @@ impl MiddlewareEndpoint { ...@@ -344,6 +338,18 @@ impl MiddlewareEndpoint {
) )
.module() .module()
} }
#[turbo_tasks::function]
async fn trace_result(self: Vc<Self>) -> Result<Vc<EndpointTraceResult>> {
let this = self.await?;
let userland_module = self.entry_module();
Ok(trace_endpoint(
*this.project,
None,
this.project.module_graph(userland_module),
vec![userland_module],
))
}
} }
#[turbo_tasks::value_impl] #[turbo_tasks::value_impl]
...@@ -420,4 +426,9 @@ impl Endpoint for MiddlewareEndpoint { ...@@ -420,4 +426,9 @@ impl Endpoint for MiddlewareEndpoint {
fn project(&self) -> Vc<Project> { fn project(&self) -> Vc<Project> {
*self.project *self.project
} }
#[turbo_tasks::function]
fn traced_files(self: Vc<Self>) -> Vc<FileSystemPathVec> {
self.trace_result().all_files()
}
} }
...@@ -4,8 +4,7 @@ use anyhow::{Context, Result, bail}; ...@@ -4,8 +4,7 @@ use anyhow::{Context, Result, bail};
use bincode::{Decode, Encode}; use bincode::{Decode, Encode};
use either::Either; use either::Either;
use next_core::{get_next_package, next_server::get_tracing_compile_time_info}; use next_core::{get_next_package, next_server::get_tracing_compile_time_info};
use serde_json::{Value, json}; use serde_json::json;
use turbo_rcstr::RcStr;
use turbo_tasks::{ use turbo_tasks::{
NonLocalValue, ResolvedVc, TaskInput, TryFlatJoinIterExt, TryJoinIterExt, Vc, NonLocalValue, ResolvedVc, TaskInput, TryFlatJoinIterExt, TryJoinIterExt, Vc,
trace::TraceRawVcs, trace::TraceRawVcs,
...@@ -27,10 +26,7 @@ use turbopack_core::{ ...@@ -27,10 +26,7 @@ use turbopack_core::{
}; };
use turbopack_resolve::ecmascript::cjs_resolve; use turbopack_resolve::ecmascript::cjs_resolve;
use crate::{ use crate::{nft::traced_modules_for_entries, project::Project};
nft_json::{relativize_glob, traced_modules_for_entries},
project::Project,
};
#[derive( #[derive(
PartialEq, Eq, TraceRawVcs, NonLocalValue, Debug, Clone, Hash, TaskInput, Encode, Decode, PartialEq, Eq, TraceRawVcs, NonLocalValue, Debug, Clone, Hash, TaskInput, Encode, Decode,
...@@ -304,30 +300,19 @@ impl ServerNftJsonAsset { ...@@ -304,30 +300,19 @@ impl ServerNftJsonAsset {
let output_file_tracing_excludes = self let output_file_tracing_excludes = self
.project .project
.next_config() .next_config()
.output_file_tracing_excludes() .output_file_tracing_excludes(project_path)
.await?; .await?;
let mut additional_ignores = BTreeSet::new(); let mut additional_ignores = BTreeSet::new();
if let Some(output_file_tracing_excludes) = output_file_tracing_excludes
.as_ref() for (route_glob, exclude_patterns) in output_file_tracing_excludes.iter() {
.and_then(Value::as_object) // Check if the route matches the glob pattern
{ if route_glob.await?.matches("next-server") {
for (glob_pattern, exclude_patterns) in output_file_tracing_excludes { for (glob, root) in exclude_patterns {
// Check if the route matches the glob pattern additional_ignores.insert(if root.path.is_empty() {
let glob = Glob::new(RcStr::from(glob_pattern.clone()), Default::default()).await?; glob.to_string()
if glob.matches("next-server") } else {
&& let Some(patterns) = exclude_patterns.as_array() format!("{root}/{glob}")
{ });
for pattern in patterns {
if let Some(pattern_str) = pattern.as_str() {
let (glob, root) = relativize_glob(pattern_str, project_path.clone())?;
let glob = if root.path.is_empty() {
glob.to_string()
} else {
format!("{root}/{glob}")
};
additional_ignores.insert(glob);
}
}
} }
} }
} }
......
This diff is collapsed.
This diff is collapsed.
...@@ -2,7 +2,9 @@ use anyhow::{Context, Result, bail}; ...@@ -2,7 +2,9 @@ use anyhow::{Context, Result, bail};
use bincode::{Decode, Encode}; use bincode::{Decode, Encode};
use futures::future::BoxFuture; use futures::future::BoxFuture;
use next_core::{ use next_core::{
PageLoaderAsset, create_page_loader_entry_module, get_asset_path_from_pathname, PageLoaderAsset,
app_structure::FileSystemPathVec,
create_page_loader_entry_module, get_asset_path_from_pathname,
get_edge_resolve_options_context, get_edge_resolve_options_context,
hmr_entry::HmrEntryModule, hmr_entry::HmrEntryModule,
mode::NextMode, mode::NextMode,
...@@ -73,6 +75,7 @@ use crate::{ ...@@ -73,6 +75,7 @@ use crate::{
font::FontManifest, font::FontManifest,
loadable_manifest::create_react_loadable_manifest, loadable_manifest::create_react_loadable_manifest,
module_graph::{NextDynamicGraphs, validate_pages_css_imports}, module_graph::{NextDynamicGraphs, validate_pages_css_imports},
nft::{EndpointTraceResult, trace_endpoint},
nft_json::NftJsonAsset, nft_json::NftJsonAsset,
paths::{ paths::{
all_asset_paths, all_paths_in_root, get_asset_paths_from_root, get_js_paths_from_root, all_asset_paths, all_paths_in_root, get_asset_paths_from_root, get_js_paths_from_root,
...@@ -1089,8 +1092,7 @@ impl PageEndpoint { ...@@ -1089,8 +1092,7 @@ impl PageEndpoint {
Some(pages_function_name(&this.original_name).into()), Some(pages_function_name(&this.original_name).into()),
*ssr_entry_chunk, *ssr_entry_chunk,
additional_assets, additional_assets,
ssr_module_graph, self.trace_result(),
vec![*ssr_module],
) )
.to_resolved() .to_resolved()
.await?, .await?,
...@@ -1557,6 +1559,19 @@ impl PageEndpoint { ...@@ -1557,6 +1559,19 @@ impl PageEndpoint {
.await?, .await?,
))) )))
} }
#[turbo_tasks::function]
async fn trace_result(self: Vc<Self>) -> Result<Vc<EndpointTraceResult>> {
let this = self.await?;
let ssr_module_graph = self.ssr_module_graph();
let InternalSsrChunkModule { ssr_module, .. } = *self.internal_ssr_chunk_module().await?;
Ok(trace_endpoint(
this.pages_project.project(),
Some(pages_function_name(&this.original_name).into()),
ssr_module_graph,
vec![*ssr_module],
))
}
} }
#[turbo_tasks::value] #[turbo_tasks::value]
...@@ -1734,6 +1749,11 @@ impl Endpoint for PageEndpoint { ...@@ -1734,6 +1749,11 @@ impl Endpoint for PageEndpoint {
async fn project(self: Vc<Self>) -> Result<Vc<Project>> { async fn project(self: Vc<Self>) -> Result<Vc<Project>> {
Ok(self.await?.pages_project.project()) Ok(self.await?.pages_project.project())
} }
#[turbo_tasks::function]
fn traced_files(self: Vc<Self>) -> Vc<FileSystemPathVec> {
self.trace_result().all_files()
}
} }
#[turbo_tasks::value] #[turbo_tasks::value]
......
...@@ -2,6 +2,7 @@ use std::fmt::Display; ...@@ -2,6 +2,7 @@ use std::fmt::Display;
use anyhow::Result; use anyhow::Result;
use bincode::{Decode, Encode}; use bincode::{Decode, Encode};
use next_core::app_structure::FileSystemPathVec;
use turbo_rcstr::RcStr; use turbo_rcstr::RcStr;
use turbo_tasks::{ use turbo_tasks::{
Completion, FxIndexMap, FxIndexSet, NonLocalValue, OperationVc, ResolvedVc, TryFlatJoinIterExt, Completion, FxIndexMap, FxIndexSet, NonLocalValue, OperationVc, ResolvedVc, TryFlatJoinIterExt,
...@@ -67,6 +68,12 @@ pub trait Endpoint { ...@@ -67,6 +68,12 @@ pub trait Endpoint {
/// The project this endpoint belongs to. /// The project this endpoint belongs to.
#[turbo_tasks::function] #[turbo_tasks::function]
fn project(self: Vc<Self>) -> Vc<Project>; fn project(self: Vc<Self>) -> Vc<Project>;
/// The traced files included by this endpoint. This is only used for analysis purposes.
/// Usually, `output()` includes the NFT file and everything else is handled outside of
/// Turbopack.
#[turbo_tasks::function]
fn traced_files(self: Vc<Self>) -> Vc<FileSystemPathVec>;
} }
#[derive( #[derive(
...@@ -154,6 +161,15 @@ impl EndpointGroup { ...@@ -154,6 +161,15 @@ impl EndpointGroup {
.collect(), .collect(),
) )
} }
pub fn traced_files(&self) -> Vc<FileSystemPathVec> {
traced_files_of_endpoints(
self.primary
.iter()
.map(|endpoint| *endpoint.endpoint)
.collect(),
)
}
} }
#[turbo_tasks::function] #[turbo_tasks::function]
...@@ -182,6 +198,17 @@ async fn module_graphs_of_endpoints( ...@@ -182,6 +198,17 @@ async fn module_graphs_of_endpoints(
Ok(Vc::cell(module_graphs)) Ok(Vc::cell(module_graphs))
} }
#[turbo_tasks::function]
async fn traced_files_of_endpoints(
endpoints: Vec<Vc<Box<dyn Endpoint>>>,
) -> Result<Vc<FileSystemPathVec>> {
let mut modules: FxIndexSet<_> = FxIndexSet::default();
for endpoint in endpoints {
modules.extend(endpoint.traced_files().await?.iter().cloned());
}
Ok(Vc::cell(modules.into_iter().collect()))
}
#[turbo_tasks::value(transparent)] #[turbo_tasks::value(transparent)]
pub struct EndpointGroups(Vec<(EndpointGroupKey, EndpointGroup)>); pub struct EndpointGroups(Vec<(EndpointGroupKey, EndpointGroup)>);
......
...@@ -87,6 +87,10 @@ turbopack-resolve = { workspace = true } ...@@ -87,6 +87,10 @@ turbopack-resolve = { workspace = true }
turbopack-static = { workspace = true } turbopack-static = { workspace = true }
turbopack-trace-utils = { workspace = true } turbopack-trace-utils = { workspace = true }
[dev-dependencies]
tokio = { workspace = true }
turbo-tasks-backend = { workspace = true }
[features] [features]
default = ["process_pool"] default = ["process_pool"]
process_pool = ["turbopack-node/process_pool"] process_pool = ["turbopack-node/process_pool"]
......
...@@ -8,8 +8,8 @@ use serde_json::Value as JsonValue; ...@@ -8,8 +8,8 @@ use serde_json::Value as JsonValue;
use turbo_esregex::EsRegex; use turbo_esregex::EsRegex;
use turbo_rcstr::{RcStr, rcstr}; use turbo_rcstr::{RcStr, rcstr};
use turbo_tasks::{ use turbo_tasks::{
FxIndexMap, NonLocalValue, OperationValue, ResolvedVc, TaskInput, Vc, debug::ValueDebugFormat, FxIndexMap, NonLocalValue, OperationValue, ResolvedVc, TaskInput, TryJoinIterExt, Vc,
trace::TraceRawVcs, debug::ValueDebugFormat, trace::TraceRawVcs,
}; };
use turbo_tasks_env::EnvMap; use turbo_tasks_env::EnvMap;
use turbo_tasks_fetch::FetchClientConfig; use turbo_tasks_fetch::FetchClientConfig;
...@@ -43,6 +43,7 @@ use crate::{ ...@@ -43,6 +43,7 @@ use crate::{
next_shared::{ next_shared::{
transforms::ModularizeImportPackageConfig, webpack_rules::WebpackLoaderBuiltinCondition, transforms::ModularizeImportPackageConfig, webpack_rules::WebpackLoaderBuiltinCondition,
}, },
util::relativize_glob,
}; };
#[turbo_tasks::value(transparent)] #[turbo_tasks::value(transparent)]
...@@ -1694,6 +1695,53 @@ impl Issue for InvalidLoaderRuleConditionIssue { ...@@ -1694,6 +1695,53 @@ impl Issue for InvalidLoaderRuleConditionIssue {
} }
} }
#[turbo_tasks::value(transparent)]
pub struct OutputFileTracingIncludesExcludes(
#[bincode(with = "turbo_bincode::indexmap")]
FxIndexMap<ResolvedVc<Glob>, Vec<(RcStr, FileSystemPath)>>,
);
impl OutputFileTracingIncludesExcludes {
pub async fn parse(
project_path: FileSystemPath,
value: &Option<serde_json::Value>,
) -> Result<OutputFileTracingIncludesExcludes> {
if let Some(value) = value
&& let Some(map) = value.as_object()
{
Ok(OutputFileTracingIncludesExcludes(
map.iter()
.map(async |(route_pattern, file_patterns)| {
let route_pattern = Glob::new(
RcStr::from(route_pattern.clone()),
GlobOptions { contains: true },
)
.to_resolved()
.await?;
let file_patterns = file_patterns
.as_array()
.iter()
.flat_map(|pattern| pattern.iter())
.filter_map(|pattern| pattern.as_str())
.map(async |pattern_str| {
let (glob, root) = relativize_glob(pattern_str, &project_path)?;
Ok((RcStr::from(glob), root))
})
.try_join()
.await?;
Ok((route_pattern, file_patterns))
})
.try_join()
.await?
.into_iter()
.collect(),
))
} else {
Ok(OutputFileTracingIncludesExcludes(FxIndexMap::default()))
}
}
}
#[turbo_tasks::value_impl] #[turbo_tasks::value_impl]
impl NextConfig { impl NextConfig {
#[turbo_tasks::function] #[turbo_tasks::function]
...@@ -2480,13 +2528,29 @@ impl NextConfig { ...@@ -2480,13 +2528,29 @@ impl NextConfig {
} }
#[turbo_tasks::function] #[turbo_tasks::function]
pub fn output_file_tracing_includes(&self) -> Vc<OptionJsonValue> { pub async fn output_file_tracing_includes(
Vc::cell(self.output_file_tracing_includes.clone()) &self,
project_path: FileSystemPath,
) -> Result<Vc<OutputFileTracingIncludesExcludes>> {
Ok(OutputFileTracingIncludesExcludes::parse(
project_path,
&self.output_file_tracing_includes,
)
.await?
.cell())
} }
#[turbo_tasks::function] #[turbo_tasks::function]
pub fn output_file_tracing_excludes(&self) -> Vc<OptionJsonValue> { pub async fn output_file_tracing_excludes(
Vc::cell(self.output_file_tracing_excludes.clone()) &self,
project_path: FileSystemPath,
) -> Result<Vc<OutputFileTracingIncludesExcludes>> {
Ok(OutputFileTracingIncludesExcludes::parse(
project_path,
&self.output_file_tracing_excludes,
)
.await?
.cell())
} }
#[turbo_tasks::function] #[turbo_tasks::function]
......
use std::{fmt::Display, str::FromStr}; use std::{borrow::Cow, fmt::Display, str::FromStr};
use anyhow::{Result, bail}; use anyhow::{Result, bail};
use bincode::{Decode, Encode}; use bincode::{Decode, Encode};
...@@ -557,3 +557,208 @@ pub fn worker_forwarded_globals() -> Vec<RcStr> { ...@@ -557,3 +557,208 @@ pub fn worker_forwarded_globals() -> Vec<RcStr> {
rcstr!("NEXT_CLIENT_ASSET_SUFFIX"), rcstr!("NEXT_CLIENT_ASSET_SUFFIX"),
] ]
} }
/// The globs defined in the next.config.mjs are relative to the project root.
/// The glob walker in turbopack is somewhat naive so we handle relative path directives first so
/// traversal doesn't need to consider them and can just traverse 'down' the tree.
/// The main alternative is to merge glob evaluation with directory traversal which is what the npm
/// `glob` package does, but this would be a substantial rewrite.
pub fn relativize_glob<'a>(
glob: &'a str,
relative_to: &FileSystemPath,
) -> Result<(&'a str, FileSystemPath)> {
let mut relative_to = Cow::Borrowed(relative_to);
let mut processed_glob = glob;
loop {
if let Some(stripped) = processed_glob.strip_prefix("../") {
if relative_to.path.is_empty() {
bail!(
"glob '{glob}' is invalid, it has a prefix that navigates out of the project \
root"
);
}
relative_to = Cow::Owned(relative_to.parent());
processed_glob = stripped;
} else if let Some(stripped) = processed_glob.strip_prefix("./") {
processed_glob = stripped;
} else {
break;
}
}
Ok((processed_glob, relative_to.into_owned()))
}
#[cfg(test)]
mod tests {
use turbo_tasks::ResolvedVc;
use turbo_tasks_backend::{BackendOptions, TurboTasksBackend, noop_backing_storage};
use turbo_tasks_fs::{FileSystemPath, NullFileSystem};
use super::*;
fn create_test_fs_path(path: &str) -> FileSystemPath {
FileSystemPath {
fs: ResolvedVc::upcast(NullFileSystem {}.resolved_cell()),
path: path.into(),
}
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_relativize_glob_normal_patterns() {
let tt = turbo_tasks::TurboTasks::new(TurboTasksBackend::new(
BackendOptions::default(),
noop_backing_storage(),
));
tt.run_once(async {
// Test normal glob patterns without relative prefixes
let base_path = create_test_fs_path("project/src");
let (glob, path) = relativize_glob("*.js", &base_path).unwrap();
assert_eq!(glob, "*.js");
assert_eq!(path.path.as_str(), "project/src");
let (glob, path) = relativize_glob("components/**/*.tsx", &base_path).unwrap();
assert_eq!(glob, "components/**/*.tsx");
assert_eq!(path.path.as_str(), "project/src");
let (glob, path) = relativize_glob("lib/utils.ts", &base_path).unwrap();
assert_eq!(glob, "lib/utils.ts");
assert_eq!(path.path.as_str(), "project/src");
Ok(())
})
.await
.unwrap();
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_relativize_glob_current_directory_prefix() {
let tt = turbo_tasks::TurboTasks::new(TurboTasksBackend::new(
BackendOptions::default(),
noop_backing_storage(),
));
tt.run_once(async {
let base_path = create_test_fs_path("project/src");
// Single ./ prefix
let (glob, path) = relativize_glob("./components/*.tsx", &base_path).unwrap();
assert_eq!(glob, "components/*.tsx");
assert_eq!(path.path.as_str(), "project/src");
// Multiple ./ prefixes
let (glob, path) = relativize_glob("././utils.js", &base_path).unwrap();
assert_eq!(glob, "utils.js");
assert_eq!(path.path.as_str(), "project/src");
// ./ with complex glob
let (glob, path) = relativize_glob("./lib/**/*.{js,ts}", &base_path).unwrap();
assert_eq!(glob, "lib/**/*.{js,ts}");
assert_eq!(path.path.as_str(), "project/src");
Ok(())
})
.await
.unwrap();
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_relativize_glob_parent_directory_navigation() {
let tt = turbo_tasks::TurboTasks::new(TurboTasksBackend::new(
BackendOptions::default(),
noop_backing_storage(),
));
tt.run_once(async {
let base_path = create_test_fs_path("project/src/components");
// Single ../ prefix
let (glob, path) = relativize_glob("../utils/*.js", &base_path).unwrap();
assert_eq!(glob, "utils/*.js");
assert_eq!(path.path.as_str(), "project/src");
// Multiple ../ prefixes
let (glob, path) = relativize_glob("../../lib/*.ts", &base_path).unwrap();
assert_eq!(glob, "lib/*.ts");
assert_eq!(path.path.as_str(), "project");
// Complex navigation with glob
let (glob, path) = relativize_glob("../../../external/**/*.json", &base_path).unwrap();
assert_eq!(glob, "external/**/*.json");
assert_eq!(path.path.as_str(), "");
Ok(())
})
.await
.unwrap();
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_relativize_glob_mixed_prefixes() {
let tt = turbo_tasks::TurboTasks::new(TurboTasksBackend::new(
BackendOptions::default(),
noop_backing_storage(),
));
tt.run_once(async {
let base_path = create_test_fs_path("project/src/components");
// ../ followed by ./
let (glob, path) = relativize_glob(".././utils/*.js", &base_path).unwrap();
assert_eq!(glob, "utils/*.js");
assert_eq!(path.path.as_str(), "project/src");
// ./ followed by ../
let (glob, path) = relativize_glob("./../lib/*.ts", &base_path).unwrap();
assert_eq!(glob, "lib/*.ts");
assert_eq!(path.path.as_str(), "project/src");
// Multiple mixed prefixes
let (glob, path) = relativize_glob("././../.././external/*.json", &base_path).unwrap();
assert_eq!(glob, "external/*.json");
assert_eq!(path.path.as_str(), "project");
Ok(())
})
.await
.unwrap();
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_relativize_glob_error_navigation_out_of_root() {
let tt = turbo_tasks::TurboTasks::new(TurboTasksBackend::new(
BackendOptions::default(),
noop_backing_storage(),
));
tt.run_once(async {
// Test navigating out of project root with empty path
let empty_path = create_test_fs_path("");
let result = relativize_glob("../outside.js", &empty_path);
assert!(result.is_err());
assert!(
result
.unwrap_err()
.to_string()
.contains("navigates out of the project root")
);
// Test navigating too far up from a shallow path
let shallow_path = create_test_fs_path("project");
let result = relativize_glob("../../outside.js", &shallow_path);
assert!(result.is_err());
assert!(
result
.unwrap_err()
.to_string()
.contains("navigates out of the project root")
);
// Test multiple ../ that would go out of root
let base_path = create_test_fs_path("a/b");
let result = relativize_glob("../../../outside.js", &base_path);
assert!(result.is_err());
assert!(
result
.unwrap_err()
.to_string()
.contains("navigates out of the project root")
);
Ok(())
})
.await
.unwrap();
}
}
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