Unverified Commit 2dda444c authored by Niklas Mischkulnig's avatar Niklas Mischkulnig Committed by GitHub

Turbopack: don't read binding_usage in dev (#86722)

I'm debating whether this should be `Option<BindingUsage>` instead, so that we can detect if you built the graph without the binding_usage but then still try to enable `remove_unused_exports` (which would now silently noop and never tree shaking anything).
parent 64e537cc
......@@ -858,7 +858,10 @@ impl AppProject {
) -> Result<Vc<BaseAndFullModuleGraph>> {
if *self.project.per_page_module_graph().await? {
let next_mode = self.project.next_mode();
let should_trace = next_mode.await?.is_production();
let next_mode_ref = next_mode.await?;
let should_trace = next_mode_ref.is_production();
let should_read_binding_usage = next_mode_ref.is_production();
let client_shared_entries = client_shared_entries
.await?
.into_iter()
......@@ -874,7 +877,8 @@ impl AppProject {
let ServerEntries {
server_utils,
server_component_entries,
} = &*find_server_entries(*rsc_entry, should_trace).await?;
} = &*find_server_entries(*rsc_entry, should_trace, should_read_binding_usage)
.await?;
let graph = SingleModuleGraph::new_with_entries_visited_intern(
vec![
......@@ -891,6 +895,7 @@ impl AppProject {
],
VisitedModules::empty(),
should_trace,
should_read_binding_usage,
);
graphs.push(graph);
let mut visited_modules = VisitedModules::from_graph(graph);
......@@ -908,6 +913,7 @@ impl AppProject {
vec![ChunkGroupEntry::Entry(vec![ResolvedVc::upcast(*module)])],
visited_modules,
should_trace,
should_read_binding_usage,
);
graphs.push(graph);
let is_layout = module.server_path().await?.file_stem() == Some("layout");
......@@ -929,6 +935,7 @@ impl AppProject {
vec![ChunkGroupEntry::Entry(client_shared_entries)],
VisitedModules::empty(),
should_trace,
should_read_binding_usage,
);
graphs.push(graph);
VisitedModules::from_graph(graph)
......@@ -938,6 +945,7 @@ impl AppProject {
vec![rsc_entry_chunk_group],
visited_modules,
should_trace,
should_read_binding_usage,
);
graphs.push(graph);
visited_modules = visited_modules.concatenate(graph);
......@@ -948,6 +956,7 @@ impl AppProject {
additional_entries.owned().await?,
visited_modules,
should_trace,
should_read_binding_usage,
);
graphs.push(additional_module_graph);
......@@ -1282,12 +1291,15 @@ impl AppEndpoint {
.get_next_dynamic_imports_for_endpoint(*rsc_entry)
.await?;
let is_production = project.next_mode().await?.is_production();
let client_references =
ClientReferencesGraphs::new(*module_graphs.base, per_page_module_graph)
.get_client_references_for_endpoint(
*rsc_entry,
matches!(this.ty, AppEndpointType::Page { .. }),
project.next_mode().await?.is_production(),
is_production,
is_production,
)
.to_resolved()
.await?;
......
......@@ -460,6 +460,7 @@ impl ClientReferencesGraphs {
entry: Vc<Box<dyn Module>>,
has_layout_segments: bool,
include_traced: bool,
include_binding_usage: bool,
) -> Result<Vc<ClientReferenceGraphResult>> {
let span = tracing::info_span!("collect all client references for endpoint");
async move {
......@@ -477,7 +478,9 @@ impl ClientReferencesGraphs {
// messes up the order of the server_component_entries.
let server_entries = async {
if has_layout_segments {
let server_entries = find_server_entries(entry, include_traced).await?;
let server_entries =
find_server_entries(entry, include_traced, include_binding_usage)
.await?;
Ok(Some(server_entries))
} else {
Ok(None)
......
......@@ -728,7 +728,10 @@ impl PageEndpoint {
if *project.per_page_module_graph().await? {
let next_mode = project.next_mode();
let should_trace = next_mode.await?.is_production();
let next_mode_ref = next_mode.await?;
let should_trace = next_mode_ref.is_production();
let should_read_binding_usage = next_mode_ref.is_production();
let ssr_chunk_module = self.internal_ssr_chunk_module().await?;
// Implements layout segment optimization to compute a graph "chain" for document, app,
// page
......@@ -745,6 +748,7 @@ impl PageEndpoint {
vec![ChunkGroupEntry::Shared(module)],
visited_modules,
should_trace,
should_read_binding_usage,
);
graphs.push(graph);
visited_modules = visited_modules.concatenate(graph);
......@@ -754,6 +758,7 @@ impl PageEndpoint {
vec![ChunkGroupEntry::Entry(vec![ssr_chunk_module.ssr_module])],
visited_modules,
should_trace,
should_read_binding_usage,
);
graphs.push(graph);
......
......@@ -1065,7 +1065,8 @@ impl Project {
entry: ResolvedVc<Box<dyn Module>>,
) -> Result<Vc<ModuleGraph>> {
Ok(if *self.per_page_module_graph().await? {
ModuleGraph::from_entry_module(*entry, self.next_mode().await?.is_production())
let is_production = self.next_mode().await?.is_production();
ModuleGraph::from_entry_module(*entry, is_production, is_production)
} else {
*self.whole_app_module_graphs().await?.full
})
......@@ -1077,6 +1078,7 @@ impl Project {
evaluatable_assets: Vc<EvaluatableAssets>,
) -> Result<Vc<ModuleGraph>> {
Ok(if *self.per_page_module_graph().await? {
let is_production = self.next_mode().await?.is_production();
let entries = evaluatable_assets
.await?
.iter()
......@@ -1085,7 +1087,8 @@ impl Project {
.collect();
ModuleGraph::from_modules(
Vc::cell(vec![ChunkGroupEntry::Entry(entries)]),
self.next_mode().await?.is_production(),
is_production,
is_production,
)
} else {
*self.whole_app_module_graphs().await?.full
......@@ -2000,9 +2003,14 @@ async fn whole_app_module_graph_operation(
mark_root();
let next_mode = project.next_mode();
let should_trace = next_mode.await?.is_production();
let base_single_module_graph =
SingleModuleGraph::new_with_entries(project.get_all_entries(), should_trace);
let next_mode_ref = next_mode.await?;
let should_trace = next_mode_ref.is_production();
let should_read_binding_usage = next_mode_ref.is_production();
let base_single_module_graph = SingleModuleGraph::new_with_entries(
project.get_all_entries(),
should_trace,
should_read_binding_usage,
);
let base_visited_modules = VisitedModules::from_graph(base_single_module_graph);
let base = ModuleGraph::from_single_graph(base_single_module_graph);
......@@ -2030,6 +2038,7 @@ async fn whole_app_module_graph_operation(
additional_entries,
base_visited_modules,
should_trace,
should_read_binding_usage,
);
let full_with_unused_references =
......
......@@ -81,6 +81,7 @@ pub struct ServerEntries {
pub async fn find_server_entries(
entry: ResolvedVc<Box<dyn Module>>,
include_traced: bool,
include_binding_usage: bool,
) -> Result<Vc<ServerEntries>> {
async move {
let emit_spans = tracing::enabled!(Level::INFO);
......@@ -97,8 +98,9 @@ pub async fn find_server_entries(
},
)],
FindServerEntries {
include_traced,
emit_spans,
include_traced,
include_binding_usage,
},
)
.await
......@@ -130,9 +132,11 @@ pub async fn find_server_entries(
}
struct FindServerEntries {
emit_spans: bool,
/// Whether to walk ChunkingType::Traced references
include_traced: bool,
emit_spans: bool,
/// Whether to read the binding usage information from modules
include_binding_usage: bool,
}
#[derive(
......@@ -173,6 +177,7 @@ impl Visit<FindServerEntriesNode> for FindServerEntries {
fn edges(&mut self, node: &FindServerEntriesNode) -> Self::EdgesFuture {
let include_traced = self.include_traced;
let include_binding_usage = self.include_binding_usage;
let parent_module = match node {
// This should never occur since we always skip visiting these
// nodes' edges.
......@@ -185,10 +190,15 @@ impl Visit<FindServerEntriesNode> for FindServerEntries {
};
let emit_spans = self.emit_spans;
async move {
// Pass include_traced to reuse the same cached `primary_chunkable_referenced_modules`
// task result, but the traced references will be filtered out again afterwards.
let referenced_modules =
primary_chunkable_referenced_modules(parent_module, include_traced).await?;
// Pass include_traced and include_binding_usage to reuse the same cached
// `primary_chunkable_referenced_modules` task result, but the traced references will be
// filtered out again afterwards.
let referenced_modules = primary_chunkable_referenced_modules(
parent_module,
include_traced,
include_binding_usage,
)
.await?;
let referenced_modules = referenced_modules
.iter()
......
......@@ -756,7 +756,7 @@ async fn get_mock_stylesheet(
.module();
let entries = get_evaluate_entries(mocked_response_asset, asset_context, None);
let module_graph = ModuleGraph::from_modules(entries.graph_entries(), false);
let module_graph = ModuleGraph::from_modules(entries.graph_entries(), false, false);
let root = mock_fs.root().owned().await?;
let val = evaluate(
......
......@@ -308,6 +308,7 @@ async fn build_internal(
let mut module_graph = ModuleGraph::from_modules(
Vc::cell(vec![ChunkGroupEntry::Entry(entries.clone())]),
false,
true,
);
let module_id_strategy = ResolvedVc::upcast(
get_global_module_id_strategy(module_graph)
......
......@@ -161,10 +161,13 @@ pub async fn create_web_entry_source(
.map(|&entry| ResolvedVc::upcast(entry)),
)
.collect::<Vec<ResolvedVc<Box<dyn Module>>>>();
let module_graph =
ModuleGraph::from_modules(Vc::cell(vec![ChunkGroupEntry::Entry(all_modules)]), false)
.to_resolved()
.await?;
let module_graph = ModuleGraph::from_modules(
Vc::cell(vec![ChunkGroupEntry::Entry(all_modules)]),
false,
false,
)
.to_resolved()
.await?;
let entries: Vec<_> = entries
.into_iter()
......
......@@ -240,6 +240,7 @@ impl SingleModuleGraph {
entries: &GraphEntriesT,
visited_modules: &FxIndexMap<ResolvedVc<Box<dyn Module>>, GraphNodeIndex>,
include_traced: bool,
include_binding_usage: bool,
) -> Result<Vc<Self>> {
let emit_spans = tracing::enabled!(Level::INFO);
let root_edges = entries
......@@ -263,6 +264,7 @@ impl SingleModuleGraph {
visited_modules,
emit_spans,
include_traced,
include_binding_usage,
},
)
.await
......@@ -783,16 +785,26 @@ impl ModuleGraph {
pub fn from_entry_module(
module: ResolvedVc<Box<dyn Module>>,
include_traced: bool,
include_binding_usage: bool,
) -> Vc<Self> {
Self::from_single_graph(SingleModuleGraph::new_with_entries(
Vc::cell(vec![ChunkGroupEntry::Entry(vec![module])]),
include_traced,
include_binding_usage,
))
}
#[turbo_tasks::function]
pub fn from_modules(modules: Vc<GraphEntries>, include_traced: bool) -> Vc<Self> {
Self::from_single_graph(SingleModuleGraph::new_with_entries(modules, include_traced))
pub fn from_modules(
modules: Vc<GraphEntries>,
include_traced: bool,
include_binding_usage: bool,
) -> Vc<Self> {
Self::from_single_graph(SingleModuleGraph::new_with_entries(
modules,
include_traced,
include_binding_usage,
))
}
#[turbo_tasks::function]
......@@ -1534,8 +1546,15 @@ impl SingleModuleGraph {
pub async fn new_with_entries(
entries: Vc<GraphEntries>,
include_traced: bool,
include_binding_usage: bool,
) -> Result<Vc<Self>> {
SingleModuleGraph::new_inner(&*entries.await?, &Default::default(), include_traced).await
SingleModuleGraph::new_inner(
&*entries.await?,
&Default::default(),
include_traced,
include_binding_usage,
)
.await
}
#[turbo_tasks::function]
......@@ -1543,11 +1562,13 @@ impl SingleModuleGraph {
entries: Vc<GraphEntries>,
visited_modules: Vc<VisitedModules>,
include_traced: bool,
include_binding_usage: bool,
) -> Result<Vc<Self>> {
SingleModuleGraph::new_inner(
&*entries.await?,
&visited_modules.await?.modules,
include_traced,
include_binding_usage,
)
.await
}
......@@ -1558,9 +1579,15 @@ impl SingleModuleGraph {
entries: GraphEntriesT,
visited_modules: Vc<VisitedModules>,
include_traced: bool,
include_binding_usage: bool,
) -> Result<Vc<Self>> {
SingleModuleGraph::new_inner(&entries, &visited_modules.await?.modules, include_traced)
.await
SingleModuleGraph::new_inner(
&entries,
&visited_modules.await?.modules,
include_traced,
include_binding_usage,
)
.await
}
}
......@@ -1689,6 +1716,9 @@ struct SingleModuleGraphBuilder<'a> {
/// Whether to walk ChunkingType::Traced references
include_traced: bool,
/// Whether to read ChunkableModuleReference::binding_usage()
include_binding_usage: bool,
}
impl
Visit<(
......@@ -1753,10 +1783,15 @@ impl
let visited_modules = self.visited_modules;
let emit_spans = self.emit_spans;
let include_traced = self.include_traced;
let include_binding_usage = self.include_binding_usage;
async move {
Ok(match (module, chunkable_ref_target) {
(Some(module), None) => {
let refs_cell = primary_chunkable_referenced_modules(*module, include_traced);
let refs_cell = primary_chunkable_referenced_modules(
*module,
include_traced,
include_binding_usage,
);
let refs = match refs_cell.await {
Ok(refs) => refs,
Err(e) => {
......@@ -2107,6 +2142,7 @@ pub mod tests {
let parent_graph = SingleModuleGraph::new_with_entries(
GraphEntries::cell(GraphEntries(vec![ChunkGroupEntry::Entry(vec![b_module])])),
false,
false,
);
let module_graph = ModuleGraph::from_graphs(vec![
......@@ -2115,6 +2151,7 @@ pub mod tests {
GraphEntries::cell(GraphEntries(vec![ChunkGroupEntry::Entry(vec![a_module])])),
VisitedModules::from_graph(parent_graph),
false,
false,
),
])
.await?;
......@@ -2340,6 +2377,7 @@ pub mod tests {
entry_modules.clone(),
)])),
false,
false,
);
// Create a simple name mapping to make analyzing the visitors easier.
......
......@@ -314,6 +314,7 @@ pub struct ModulesWithRefData(Vec<(ResolvedVc<Box<dyn ModuleReference>>, Resolve
pub async fn primary_chunkable_referenced_modules(
module: ResolvedVc<Box<dyn Module>>,
include_traced: bool,
include_binding_usage: bool,
) -> Result<Vc<ModulesWithRefData>> {
let modules = module
.references()
......@@ -333,7 +334,11 @@ pub async fn primary_chunkable_referenced_modules(
.await?
.primary_modules_ref()
.await?;
let binding_usage = reference.binding_usage().owned().await?;
let binding_usage = if include_binding_usage {
reference.binding_usage().owned().await?
} else {
BindingUsage::default()
};
return Ok(Some((
ResolvedVc::upcast(reference),
......
......@@ -499,7 +499,7 @@ impl PostCssTransformedAsset {
.to_resolved()
.await?;
let module_graph = ModuleGraph::from_modules(entries.graph_entries(), false)
let module_graph = ModuleGraph::from_modules(entries.graph_entries(), false, false)
.to_resolved()
.await?;
......
......@@ -245,7 +245,7 @@ impl WebpackLoadersProcessedAsset {
.to_resolved()
.await?;
let module_graph = ModuleGraph::from_modules(entries.graph_entries(), false)
let module_graph = ModuleGraph::from_modules(entries.graph_entries(), false, false)
.to_resolved()
.await?;
......
......@@ -476,7 +476,7 @@ async fn run_test_operation(prepared_test: ResolvedVc<PreparedTest>) -> Result<V
let entries = get_evaluate_entries(jest_entry_asset, asset_context, None);
let mut module_graph = ModuleGraph::from_modules(entries.graph_entries(), false);
let mut module_graph = ModuleGraph::from_modules(entries.graph_entries(), false, true);
let binding_usage = if options.remove_unused_imports || options.remove_unused_exports {
Some(
......
......@@ -432,6 +432,7 @@ async fn run_test_operation(resource: RcStr) -> Result<Vc<FileSystemPath>> {
let mut module_graph = ModuleGraph::from_modules(
Vc::cell(vec![ChunkGroupEntry::Entry(entry_modules.clone())]),
false,
true,
);
let binding_usage = if options.remove_unused_imports || options.remove_unused_exports {
......
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