From ea7319c13c541c4e8a0cc55badc9621647b02e19 Mon Sep 17 00:00:00 2001 From: Benjamin Woodruff Date: Sun, 27 Oct 2024 13:15:20 -0700 Subject: [PATCH] codemod(turbopack): Rewrite Vc fields in structs as ResolvedVc (part 4) (#71804) Generated using a version of https://github.com/vercel/turbopack-resolved-vc-codemod . This uses Anthropic Claude Sonnet 3.5 for more complicated errors we don't have hardcoded logic for. - Part 1: #70927 - Part 2: #71172 - Part 3: #71665 Closes PACK-3336 --- .../ecmascript_client_reference_transition.rs | 2 +- turbopack/crates/node-file-trace/src/lib.rs | 24 +-- .../turbopack-browser/src/chunking_context.rs | 4 +- .../src/ecmascript/merged/update.rs | 4 +- .../src/ecmascript/update.rs | 14 +- .../turbopack-cli-utils/src/runtime_entry.rs | 2 +- .../crates/turbopack-cli/src/build/mod.rs | 2 +- .../turbopack-cli/src/dev/web_entry_source.rs | 4 +- turbopack/crates/turbopack-cli/src/util.rs | 4 +- .../src/chunk/available_chunk_items.rs | 14 +- .../turbopack-core/src/chunk/chunk_group.rs | 27 +++- .../crates/turbopack-core/src/chunk/mod.rs | 40 ++--- .../turbopack-core/src/chunk/optimize.rs | 6 +- .../src/introspect/output_asset.rs | 14 +- .../turbopack-core/src/reference_type.rs | 4 +- .../crates/turbopack-core/src/resolve/mod.rs | 143 ++++++++++-------- .../turbopack-core/src/resolve/options.rs | 37 +++-- .../turbopack-core/src/resolve/pattern.rs | 14 +- .../src/references/async_module.rs | 2 +- .../src/references/esm/base.rs | 2 +- .../turbopack-nodejs/src/chunking_context.rs | 4 +- turbopack/crates/turbopack/src/lib.rs | 2 +- 22 files changed, 209 insertions(+), 160 deletions(-) diff --git a/crates/next-core/src/next_client_reference/ecmascript_client_reference/ecmascript_client_reference_transition.rs b/crates/next-core/src/next_client_reference/ecmascript_client_reference/ecmascript_client_reference_transition.rs index 045d2c7035b69..8c74b601d2f8a 100644 --- a/crates/next-core/src/next_client_reference/ecmascript_client_reference/ecmascript_client_reference_transition.rs +++ b/crates/next-core/src/next_client_reference/ecmascript_client_reference/ecmascript_client_reference_transition.rs @@ -61,7 +61,7 @@ impl Transition for NextEcmascriptClientReferenceTransition { let this = self.await?; let ident = match part { - Some(part) => source.ident().with_part(part), + Some(part) => source.ident().with_part(*part), None => source.ident(), }; let ident_ref = ident.await?; diff --git a/turbopack/crates/node-file-trace/src/lib.rs b/turbopack/crates/node-file-trace/src/lib.rs index 197248c429954..993cbf5e681c6 100644 --- a/turbopack/crates/node-file-trace/src/lib.rs +++ b/turbopack/crates/node-file-trace/src/lib.rs @@ -23,8 +23,8 @@ use serde::Deserialize; use serde::Serialize; use tokio::sync::mpsc::channel; use turbo_tasks::{ - backend::Backend, util::FormatDuration, RcStr, ReadConsistency, TaskId, TransientInstance, - TransientValue, TurboTasks, UpdateInfo, Value, Vc, + backend::Backend, util::FormatDuration, RcStr, ReadConsistency, ResolvedVc, TaskId, + TransientInstance, TransientValue, TurboTasks, UpdateInfo, Value, Vc, }; use turbo_tasks_fs::{ glob::Glob, DirectoryEntry, DiskFileSystem, FileSystem, FileSystemPath, ReadGlobResult, @@ -565,11 +565,11 @@ async fn main_operation( #[turbo_tasks::function] async fn create_module_asset( - root: Vc, + root: ResolvedVc, process_cwd: Option, module_options: TransientInstance, resolve_options: TransientInstance, -) -> Vc { +) -> Result> { let env = Environment::new(Value::new(ExecutionEnvironment::NodeJsLambda( NodeJsEnvironment { cwd: Vc::cell(process_cwd), @@ -581,13 +581,17 @@ async fn create_module_asset( let glob_mappings = vec![ ( root, - Glob::new("**/*/next/dist/server/next.js".into()), - ImportMapping::Ignore.into(), + Glob::new("**/*/next/dist/server/next.js".into()) + .to_resolved() + .await?, + ImportMapping::Ignore.resolved_cell(), ), ( root, - Glob::new("**/*/next/dist/bin/next".into()), - ImportMapping::Ignore.into(), + Glob::new("**/*/next/dist/bin/next".into()) + .to_resolved() + .await?, + ImportMapping::Ignore.resolved_cell(), ), ]; let mut resolve_options = ResolveOptionsContext::clone(&*resolve_options); @@ -603,13 +607,13 @@ async fn create_module_asset( ); } - ModuleAssetContext::new( + Ok(ModuleAssetContext::new( Default::default(), compile_time_info, ModuleOptionsContext::clone(&*module_options).cell(), resolve_options.cell(), Vc::cell("node_file_trace".into()), - ) + )) } fn register() { diff --git a/turbopack/crates/turbopack-browser/src/chunking_context.rs b/turbopack/crates/turbopack-browser/src/chunking_context.rs index d1bab2c0355e1..4f677f4248fb6 100644 --- a/turbopack/crates/turbopack-browser/src/chunking_context.rs +++ b/turbopack/crates/turbopack-browser/src/chunking_context.rs @@ -386,7 +386,7 @@ impl ChunkingContext for BrowserChunkingContext { let mut assets: Vec>> = chunks .iter() - .map(|chunk| self.generate_chunk(*chunk)) + .map(|chunk| self.generate_chunk(**chunk)) .collect(); if this.enable_hot_module_replacement { @@ -455,7 +455,7 @@ impl ChunkingContext for BrowserChunkingContext { let mut assets: Vec>> = chunks .iter() - .map(|chunk| self.generate_chunk(*chunk)) + .map(|chunk| self.generate_chunk(**chunk)) .collect(); let other_assets = Vc::cell(assets.clone()); diff --git a/turbopack/crates/turbopack-browser/src/ecmascript/merged/update.rs b/turbopack/crates/turbopack-browser/src/ecmascript/merged/update.rs index 79809d5395d6a..48ea045b72879 100644 --- a/turbopack/crates/turbopack-browser/src/ecmascript/merged/update.rs +++ b/turbopack/crates/turbopack-browser/src/ecmascript/merged/update.rs @@ -215,7 +215,7 @@ pub(super) async fn update_ecmascript_merged_chunk( if merged_module_map.get(&module_id) != Some(module_hash) { let entry = EcmascriptModuleEntry::from_code( &module_id, - module_code, + *module_code, chunk_path, ) .await?; @@ -227,7 +227,7 @@ pub(super) async fn update_ecmascript_merged_chunk( for (module_id, module_code) in chunk_partial.modified { let entry = - EcmascriptModuleEntry::from_code(&module_id, module_code, chunk_path) + EcmascriptModuleEntry::from_code(&module_id, *module_code, chunk_path) .await?; merged_update.entries.insert(module_id, entry); } diff --git a/turbopack/crates/turbopack-browser/src/ecmascript/update.rs b/turbopack/crates/turbopack-browser/src/ecmascript/update.rs index 5c54f8aeb4b22..fb92948275f64 100644 --- a/turbopack/crates/turbopack-browser/src/ecmascript/update.rs +++ b/turbopack/crates/turbopack-browser/src/ecmascript/update.rs @@ -1,5 +1,5 @@ use anyhow::Result; -use turbo_tasks::{FxIndexMap, ReadRef, Vc}; +use turbo_tasks::{FxIndexMap, ReadRef, ResolvedVc, Vc}; use turbopack_core::{chunk::ModuleId, code_builder::Code}; use super::{content::EcmascriptDevChunkContent, version::EcmascriptDevChunkVersion}; @@ -11,9 +11,9 @@ pub(super) enum EcmascriptChunkUpdate { } pub(super) struct EcmascriptChunkPartialUpdate { - pub added: FxIndexMap, (u64, Vc)>, + pub added: FxIndexMap, (u64, ResolvedVc)>, pub deleted: FxIndexMap, u64>, - pub modified: FxIndexMap, Vc>, + pub modified: FxIndexMap, ResolvedVc>, } pub(super) async fn update_ecmascript_chunk( @@ -40,7 +40,8 @@ pub(super) async fn update_ecmascript_chunk( for (id, from_hash) in &from.entries_hashes { if let Some(entry) = entries.get(id) { if *entry.hash.await? != *from_hash { - modified.insert(id.clone(), entry.code); + let resolved_code = entry.code.to_resolved().await?; + modified.insert(id.clone(), resolved_code); } } else { deleted.insert(id.clone(), *from_hash); @@ -50,7 +51,10 @@ pub(super) async fn update_ecmascript_chunk( // Remaining entries are added for (id, entry) in entries.iter() { if !from.entries_hashes.contains_key(id) { - added.insert(id.clone(), (*entry.hash.await?, entry.code)); + added.insert( + id.clone(), + (*entry.hash.await?, entry.code.to_resolved().await?), + ); } } diff --git a/turbopack/crates/turbopack-cli-utils/src/runtime_entry.rs b/turbopack/crates/turbopack-cli-utils/src/runtime_entry.rs index 6bee158c7413f..8f38df55b861a 100644 --- a/turbopack/crates/turbopack-cli-utils/src/runtime_entry.rs +++ b/turbopack/crates/turbopack-cli-utils/src/runtime_entry.rs @@ -62,7 +62,7 @@ impl RuntimeEntry { } #[turbo_tasks::value(transparent)] -pub struct RuntimeEntries(Vec>); +pub struct RuntimeEntries(Vec>); #[turbo_tasks::value_impl] impl RuntimeEntries { diff --git a/turbopack/crates/turbopack-cli/src/build/mod.rs b/turbopack/crates/turbopack-cli/src/build/mod.rs index 635fa6f6472fd..bc794d3f1bdd6 100644 --- a/turbopack/crates/turbopack-cli/src/build/mod.rs +++ b/turbopack/crates/turbopack-cli/src/build/mod.rs @@ -118,7 +118,7 @@ impl TurbopackBuildBuilder { self.entry_requests .iter() .cloned() - .map(EntryRequest::cell) + .map(EntryRequest::resolved_cell) .collect(), ) .cell(), diff --git a/turbopack/crates/turbopack-cli/src/dev/web_entry_source.rs b/turbopack/crates/turbopack-cli/src/dev/web_entry_source.rs index 5412fecaa604a..e336c8abe54d4 100644 --- a/turbopack/crates/turbopack-cli/src/dev/web_entry_source.rs +++ b/turbopack/crates/turbopack-cli/src/dev/web_entry_source.rs @@ -72,7 +72,7 @@ pub async fn get_client_runtime_entries( request.to_resolved().await?, project_path.join("_".into()).to_resolved().await?, ) - .cell(), + .resolved_cell(), ) }; @@ -82,7 +82,7 @@ pub async fn get_client_runtime_entries( .to_resolved() .await?, )) - .cell(), + .resolved_cell(), ); Ok(Vc::cell(runtime_entries)) diff --git a/turbopack/crates/turbopack-cli/src/util.rs b/turbopack/crates/turbopack-cli/src/util.rs index 69ebdf0da9d4f..6e5c6a449a131 100644 --- a/turbopack/crates/turbopack-cli/src/util.rs +++ b/turbopack/crates/turbopack-cli/src/util.rs @@ -2,11 +2,11 @@ use std::{env::current_dir, path::PathBuf}; use anyhow::{Context, Result}; use dunce::canonicalize; -use turbo_tasks::{RcStr, Vc}; +use turbo_tasks::{RcStr, ResolvedVc, Vc}; use turbo_tasks_fs::{DiskFileSystem, FileSystem}; #[turbo_tasks::value(transparent)] -pub struct EntryRequests(pub Vec>); +pub struct EntryRequests(pub Vec>); #[turbo_tasks::value(shared)] #[derive(Clone)] diff --git a/turbopack/crates/turbopack-core/src/chunk/available_chunk_items.rs b/turbopack/crates/turbopack-core/src/chunk/available_chunk_items.rs index b05c9d31f1de0..279e172a928e5 100644 --- a/turbopack/crates/turbopack-core/src/chunk/available_chunk_items.rs +++ b/turbopack/crates/turbopack-core/src/chunk/available_chunk_items.rs @@ -17,7 +17,9 @@ pub struct AvailableChunkItemInfo { pub struct OptionAvailableChunkItemInfo(Option); #[turbo_tasks::value(transparent)] -pub struct AvailableChunkItemInfoMap(FxIndexMap>, AvailableChunkItemInfo>); +pub struct AvailableChunkItemInfoMap( + FxIndexMap>, AvailableChunkItemInfo>, +); /// Allows to gather information about which assets are already available. /// Adding more roots will form a linked list like structure to allow caching @@ -41,7 +43,7 @@ impl AvailableChunkItems { #[turbo_tasks::function] pub async fn with_chunk_items( - self: Vc, + self: ResolvedVc, chunk_items: Vc, ) -> Result> { let chunk_items = chunk_items @@ -49,7 +51,7 @@ impl AvailableChunkItems { .into_iter() .map(|(&chunk_item, &info)| async move { Ok(self - .get(chunk_item) + .get(*chunk_item) .await? .is_none() .then_some((chunk_item, info))) @@ -57,7 +59,7 @@ impl AvailableChunkItems { .try_flat_join() .await?; Ok(AvailableChunkItems { - parent: Some(self.to_resolved().await?), + parent: Some(self), chunk_items: Vc::cell(chunk_items.into_iter().collect()), } .cell()) @@ -87,13 +89,13 @@ impl AvailableChunkItems { #[turbo_tasks::function] pub async fn get( &self, - chunk_item: Vc>, + chunk_item: ResolvedVc>, ) -> Result> { if let Some(&info) = self.chunk_items.await?.get(&chunk_item) { return Ok(Vc::cell(Some(info))); }; if let Some(parent) = self.parent { - return Ok(parent.get(chunk_item)); + return Ok(parent.get(*chunk_item)); } Ok(Vc::cell(None)) } diff --git a/turbopack/crates/turbopack-core/src/chunk/chunk_group.rs b/turbopack/crates/turbopack-core/src/chunk/chunk_group.rs index 2cebdd266cf39..b6e8c3035e37b 100644 --- a/turbopack/crates/turbopack-core/src/chunk/chunk_group.rs +++ b/turbopack/crates/turbopack-core/src/chunk/chunk_group.rs @@ -2,7 +2,9 @@ use std::collections::HashSet; use anyhow::Result; use auto_hash_map::AutoSet; -use turbo_tasks::{FxIndexMap, FxIndexSet, TryFlatJoinIterExt, TryJoinIterExt, Value, Vc}; +use turbo_tasks::{ + FxIndexMap, FxIndexSet, ResolvedVc, TryFlatJoinIterExt, TryJoinIterExt, Value, Vc, +}; use super::{ availability_info::AvailabilityInfo, available_chunk_items::AvailableChunkItemInfo, @@ -12,7 +14,7 @@ use super::{ use crate::{module::Module, output::OutputAssets, reference::ModuleReference}; pub struct MakeChunkGroupResult { - pub chunks: Vec>>, + pub chunks: Vec>>, pub availability_info: AvailabilityInfo, } @@ -104,14 +106,17 @@ pub async fn make_chunk_group( let availability_info = { let map = chunk_items .iter() - .map(|(&chunk_item, async_info)| { - ( - chunk_item, + .map(|(&chunk_item, async_info)| async move { + Ok(( + chunk_item.to_resolved().await?, AvailableChunkItemInfo { is_async: async_info.is_some(), }, - ) + )) }) + .try_join() + .await? + .into_iter() .collect(); let map = Vc::cell(map); availability_info.with_chunk_items(map).await? @@ -121,7 +126,7 @@ pub async fn make_chunk_group( let async_loaders = async_modules .into_iter() .map(|module| { - chunking_context.async_loader_chunk_item(module, Value::new(availability_info)) + chunking_context.async_loader_chunk_item(*module, Value::new(availability_info)) }) .collect::>(); let has_async_loaders = !async_loaders.is_empty(); @@ -164,8 +169,14 @@ pub async fn make_chunk_group( chunks.extend(async_loader_chunks.iter().copied()); } + let resolved_chunks = chunks + .into_iter() + .map(|chunk| chunk.to_resolved()) + .try_join() + .await?; + Ok(MakeChunkGroupResult { - chunks, + chunks: resolved_chunks, availability_info, }) } diff --git a/turbopack/crates/turbopack-core/src/chunk/mod.rs b/turbopack/crates/turbopack-core/src/chunk/mod.rs index 1daf8422e0e06..ebb8e19394cdd 100644 --- a/turbopack/crates/turbopack-core/src/chunk/mod.rs +++ b/turbopack/crates/turbopack-core/src/chunk/mod.rs @@ -190,7 +190,7 @@ type AsyncInfo = FxIndexMap>, Vec>>> pub struct ChunkContentResult { pub chunk_items: FxIndexSet>>, - pub async_modules: FxIndexSet>>, + pub async_modules: FxIndexSet>>, pub external_module_references: FxIndexSet>>, /// A map from local module to all children from which the async module /// status is inherited @@ -245,7 +245,7 @@ enum ChunkContentGraphNode { /// will be inherited. InheritAsyncInfo { item: Vc>, - references: Vec<(Vc>, InheritAsyncEdge)>, + references: Vec<(ResolvedVc>, InheritAsyncEdge)>, }, } @@ -413,7 +413,10 @@ async fn graph_node_to_referenced_nodes( ident: module.ident().to_string().await?, }, }), - Some((chunk_item, InheritAsyncEdge::LocalModule)), + Some(( + chunk_item.to_resolved().await?, + InheritAsyncEdge::LocalModule, + )), )) } ChunkingType::Passthrough => { @@ -590,10 +593,7 @@ async fn chunk_content_internal_parallel( Ok(Some(ChunkGraphEdge { key: Some(entry.to_resolved().await?), node: ChunkContentGraphNode::ChunkItem { - item: chunkable_module - .as_chunk_item(chunking_context) - .resolve() - .await?, + item: chunkable_module.as_chunk_item(chunking_context), ident: chunkable_module.ident().to_string().await?, }, })) @@ -626,10 +626,10 @@ async fn chunk_content_internal_parallel( match graph_node { ChunkContentGraphNode::PassthroughChunkItem { .. } => {} ChunkContentGraphNode::ChunkItem { item, .. } => { - chunk_items.insert(item); + chunk_items.insert(*item.to_resolved().await?); } ChunkContentGraphNode::AsyncModule { module } => { - let module = module.resolve().await?; + let module = module.to_resolved().await?; async_modules.insert(module); } ChunkContentGraphNode::ExternalModuleReference(reference) => { @@ -640,12 +640,12 @@ async fn chunk_content_internal_parallel( for &(reference, ty) in &references { match ty { InheritAsyncEdge::LocalModule => local_back_edges_inherit_async - .entry(reference) + .entry(*reference) .or_insert_with(Vec::new) .push(item), InheritAsyncEdge::AvailableAsyncModule => { available_async_modules_back_edges_inherit_async - .entry(reference) + .entry(*reference) .or_insert_with(Vec::new) .push(item) } @@ -654,7 +654,7 @@ async fn chunk_content_internal_parallel( forward_edges_inherit_async .entry(item) .or_insert_with(Vec::new) - .extend(references.into_iter().map(|(r, _)| r)); + .extend(references.into_iter().map(|(r, _)| *r)); } } } @@ -733,17 +733,23 @@ pub struct ChunkItems(pub Vec>>); #[turbo_tasks::value] pub struct AsyncModuleInfo { - pub referenced_async_modules: AutoSet>>, + pub referenced_async_modules: AutoSet>>, } #[turbo_tasks::value_impl] impl AsyncModuleInfo { #[turbo_tasks::function] - pub fn new(referenced_async_modules: Vec>>) -> Vc { - Self { - referenced_async_modules: referenced_async_modules.into_iter().collect(), + pub async fn new(referenced_async_modules: Vec>>) -> Result> { + let resolved_modules = referenced_async_modules + .into_iter() + .map(|m| m.to_resolved()) + .try_join() + .await?; + + Ok(Self { + referenced_async_modules: resolved_modules.into_iter().collect(), } - .cell() + .cell()) } } diff --git a/turbopack/crates/turbopack-core/src/chunk/optimize.rs b/turbopack/crates/turbopack-core/src/chunk/optimize.rs index bea7a542ce426..4da9b7a8cac53 100644 --- a/turbopack/crates/turbopack-core/src/chunk/optimize.rs +++ b/turbopack/crates/turbopack-core/src/chunk/optimize.rs @@ -4,17 +4,17 @@ //! their size and eliminating duplicates between them. use anyhow::Result; -use turbo_tasks::{TryJoinIterExt, Vc}; +use turbo_tasks::{ResolvedVc, TryJoinIterExt, Vc}; use turbo_tasks_fs::{FileSystemPath, FileSystemPathOption}; use crate::chunk::containment_tree::{ContainmentTree, ContainmentTreeKey}; #[derive(Debug, Clone, Eq, PartialEq, Hash)] -struct FileSystemPathKey(Vc); +struct FileSystemPathKey(ResolvedVc); impl FileSystemPathKey { async fn new(path: Vc) -> Result { - Ok(Self(path.resolve().await?)) + Ok(Self(path.to_resolved().await?)) } } diff --git a/turbopack/crates/turbopack-core/src/introspect/output_asset.rs b/turbopack/crates/turbopack-core/src/introspect/output_asset.rs index 946cdd30a8dd4..f681e72689aa9 100644 --- a/turbopack/crates/turbopack-core/src/introspect/output_asset.rs +++ b/turbopack/crates/turbopack-core/src/introspect/output_asset.rs @@ -1,5 +1,5 @@ use anyhow::Result; -use turbo_tasks::{RcStr, ValueToString, Vc}; +use turbo_tasks::{RcStr, ResolvedVc, ValueToString, Vc}; use super::{ utils::{children_from_output_assets, content_to_details}, @@ -8,15 +8,19 @@ use super::{ use crate::{asset::Asset, output::OutputAsset}; #[turbo_tasks::value] -pub struct IntrospectableOutputAsset(Vc>); +pub struct IntrospectableOutputAsset(ResolvedVc>); #[turbo_tasks::value_impl] impl IntrospectableOutputAsset { #[turbo_tasks::function] - pub async fn new(asset: Vc>) -> Result>> { - Ok(Vc::try_resolve_sidecast::>(asset) + pub async fn new( + asset: ResolvedVc>, + ) -> Result>> { + Ok(*ResolvedVc::try_sidecast::>(asset) .await? - .unwrap_or_else(|| Vc::upcast(IntrospectableOutputAsset(asset).cell()))) + .unwrap_or_else(|| { + ResolvedVc::upcast(IntrospectableOutputAsset(asset).resolved_cell()) + })) } } diff --git a/turbopack/crates/turbopack-core/src/reference_type.rs b/turbopack/crates/turbopack-core/src/reference_type.rs index 735e51bf79620..76fd2c332a354 100644 --- a/turbopack/crates/turbopack-core/src/reference_type.rs +++ b/turbopack/crates/turbopack-core/src/reference_type.rs @@ -1,7 +1,7 @@ use std::fmt::Display; use anyhow::Result; -use turbo_tasks::{FxIndexMap, RcStr, Vc}; +use turbo_tasks::{FxIndexMap, RcStr, ResolvedVc, Vc}; use crate::{module::Module, resolve::ModulePart}; @@ -42,7 +42,7 @@ pub enum ImportWithType { #[turbo_tasks::value(serialization = "auto_for_input")] #[derive(Debug, Default, Clone, Hash)] pub enum EcmaScriptModulesReferenceSubType { - ImportPart(Vc), + ImportPart(ResolvedVc), Import, ImportWithType(ImportWithType), DynamicImport, diff --git a/turbopack/crates/turbopack-core/src/resolve/mod.rs b/turbopack/crates/turbopack-core/src/resolve/mod.rs index 48cd30d40cc0b..2b8b044001569 100644 --- a/turbopack/crates/turbopack-core/src/resolve/mod.rs +++ b/turbopack/crates/turbopack-core/src/resolve/mod.rs @@ -1070,7 +1070,7 @@ async fn exports_field(package_json_path: Vc) -> Result, + ResolvedVc, ), None, } @@ -1094,7 +1094,7 @@ async fn imports_field(lookup_path: Vc) -> Result Ok(ImportsFieldResult::Some(imports, **package_json_path).cell()), + Ok(imports) => Ok(ImportsFieldResult::Some(imports, *package_json_path).cell()), Err(err) => { PackageJsonIssue { path: **package_json_path, @@ -1136,6 +1136,8 @@ pub async fn find_context_file( if refs.is_empty() { // Tailcall Ok(find_context_file( + // Hot codepath optimization: resolve all arguments to avoid an automatically-created + // intermediate task lookup_path.parent().resolve().await?, names, )) @@ -1151,7 +1153,7 @@ pub async fn find_context_file( FindContextFileResult::NotFound(refs) } } - .into()) + .cell()) } } @@ -1584,16 +1586,16 @@ async fn handle_after_resolve_plugins( #[turbo_tasks::function] async fn resolve_internal( - lookup_path: Vc, - request: Vc, + lookup_path: ResolvedVc, + request: ResolvedVc, options: Vc, ) -> Result> { resolve_internal_inline(lookup_path, request, options).await } async fn resolve_internal_inline( - lookup_path: Vc, - request: Vc, + lookup_path: ResolvedVc, + request: ResolvedVc, options: Vc, ) -> Result> { let span = { @@ -1614,17 +1616,17 @@ async fn resolve_internal_inline( if let Some(import_map) = &options_value.import_map { let request_parts = match &*request_value { Request::Alternatives { requests } => requests.as_slice(), - _ => &[request], + _ => &[*request], }; for request in request_parts { - let result = import_map.await?.lookup(lookup_path, *request).await?; + let result = import_map.await?.lookup(*lookup_path, *request).await?; if !matches!(result, ImportMapResult::NoEntry) { has_alias = true; let resolved_result = resolve_import_map_result( &result, lookup_path, lookup_path, - *request, + request.to_resolved().await?, options, request.query(), ) @@ -1649,7 +1651,10 @@ async fn resolve_internal_inline( Request::Alternatives { requests } => { let results = requests .iter() - .map(|req| Box::pin(resolve_internal_inline(lookup_path, *req, options))) + .map(|req| async { + resolve_internal_inline(lookup_path, req.to_resolved().await?, options) + .await + }) .try_join() .await?; @@ -1663,7 +1668,7 @@ async fn resolve_internal_inline( } => { let mut results = Vec::new(); let matches = read_matches( - lookup_path, + *lookup_path, "".into(), *force_in_lookup_dir, Pattern::new(path.clone()).resolve().await?, @@ -1764,8 +1769,8 @@ async fn resolve_internal_inline( ResolvingIssue { severity: error_severity(options).await?, request_type: "server relative import: not implemented yet".to_string(), - request, - file_path: lookup_path, + request: *request, + file_path: *lookup_path, resolve_options: options, error_message: Some( "server relative imports are not implemented yet. Please try an \ @@ -1779,8 +1784,8 @@ async fn resolve_internal_inline( } Box::pin(resolve_internal_inline( - lookup_path.root().resolve().await?, - relative.resolve().await?, + lookup_path.root().to_resolved().await?, + relative.to_resolved().await?, options, )) .await? @@ -1794,8 +1799,8 @@ async fn resolve_internal_inline( ResolvingIssue { severity: error_severity(options).await?, request_type: "windows import: not implemented yet".to_string(), - request, - file_path: lookup_path, + request: *request, + file_path: *lookup_path, resolve_options: options, error_message: Some("windows imports are not implemented yet".to_string()), source: None, @@ -1848,8 +1853,8 @@ async fn resolve_internal_inline( ResolvingIssue { severity: error_severity(options).await?, request_type: format!("unknown import: `{}`", path), - request, - file_path: lookup_path, + request: *request, + file_path: *lookup_path, resolve_options: options, error_message: None, source: None, @@ -1864,7 +1869,7 @@ async fn resolve_internal_inline( // Apply fallback import mappings if provided if let Some(import_map) = &options_value.fallback_import_map { if *result.is_unresolvable().await? { - let result = import_map.await?.lookup(lookup_path, request).await?; + let result = import_map.await?.lookup(*lookup_path, *request).await?; let resolved_result = resolve_import_map_result( &result, lookup_path, @@ -1890,7 +1895,7 @@ async fn resolve_internal_inline( #[turbo_tasks::function] async fn resolve_into_folder( - package_path: Vc, + package_path: ResolvedVc, options: Vc, ) -> Result> { let package_json_path = package_path.join("package.json".into()); @@ -1908,7 +1913,9 @@ async fn resolve_into_folder( { continue; } - let request = Request::parse(Value::new(normalized_request.into())); + let request = Request::parse(Value::new(normalized_request.into())) + .to_resolved() + .await?; // main field will always resolve not fully specified let options = if options_value.fully_specified { @@ -1954,7 +1961,7 @@ async fn resolve_into_folder( let request = Request::parse(Value::new(pattern)); Ok( - resolve_internal_inline(package_path, request.resolve().await?, options) + resolve_internal_inline(package_path, request.to_resolved().await?, options) .await? .with_request(".".into()), ) @@ -1962,8 +1969,8 @@ async fn resolve_into_folder( #[tracing::instrument(level = Level::TRACE, skip_all)] async fn resolve_relative_request( - lookup_path: Vc, - request: Vc, + lookup_path: ResolvedVc, + request: ResolvedVc, options: Vc, options_value: &ResolveOptions, path_pattern: &Pattern, @@ -1974,7 +1981,7 @@ async fn resolve_relative_request( // Check alias field for aliases first let lookup_path_ref = &*lookup_path.await?; if let Some(result) = apply_in_package( - lookup_path, + *lookup_path, options, options_value, |package_path| { @@ -2061,7 +2068,7 @@ async fn resolve_relative_request( let mut results = Vec::new(); let matches = read_matches( - lookup_path, + *lookup_path, "".into(), force_in_lookup_dir, Pattern::new(new_path).resolve().await?, @@ -2304,8 +2311,8 @@ async fn find_self_reference( #[tracing::instrument(level = Level::TRACE, skip_all)] async fn resolve_module_request( - lookup_path: Vc, - request: Vc, + lookup_path: ResolvedVc, + request: ResolvedVc, options: Vc, options_value: &ResolveOptions, module: &str, @@ -2315,7 +2322,7 @@ async fn resolve_module_request( ) -> Result> { // Check alias field for module aliases first if let Some(result) = apply_in_package( - lookup_path, + *lookup_path, options, options_value, |_| { @@ -2334,7 +2341,7 @@ async fn resolve_module_request( // module. This should match only using the exports field and no other // fields/fallbacks. if let FindSelfReferencePackageResult::Found { name, package_path } = - &*find_self_reference(lookup_path).await? + &*find_self_reference(*lookup_path).await? { if name == module { let result = resolve_into_package( @@ -2351,7 +2358,7 @@ async fn resolve_module_request( } let result = find_package( - lookup_path, + *lookup_path, module.into(), resolve_modules_options(options).resolve().await?, ) @@ -2412,13 +2419,11 @@ async fn resolve_module_request( RcStr::from("/").into(), path.clone(), ]); - let relative = Request::relative(Value::new(pattern), query, fragment, true); - let relative_result = Box::pin(resolve_internal_inline( - lookup_path, - relative.resolve().await?, - options, - )) - .await?; + let relative = Request::relative(Value::new(pattern), query, fragment, true) + .to_resolved() + .await?; + let relative_result = + Box::pin(resolve_internal_inline(lookup_path, relative, options)).await?; let relative_result = relative_result .with_replaced_request_key(module_prefix, Value::new(RequestKey::new(module.into()))); @@ -2431,7 +2436,7 @@ async fn resolve_module_request( #[turbo_tasks::function] async fn resolve_into_package( path: Value, - package_path: Vc, + package_path: ResolvedVc, query: Vc, fragment: Vc, options: Vc, @@ -2492,7 +2497,7 @@ async fn resolve_into_package( // apply main field(s) or fallback to index.js if there's no subpath if is_root_match { results.push(resolve_into_folder( - package_path, + *package_path, options.with_fully_specified(false), )); } @@ -2501,9 +2506,10 @@ async fn resolve_into_package( let mut new_pat = path.clone(); new_pat.push_front(RcStr::from(".").into()); - let relative = Request::relative(Value::new(new_pat), query, fragment, true); - results - .push(resolve_internal_inline(package_path, relative.resolve().await?, options).await?); + let relative = Request::relative(Value::new(new_pat), query, fragment, true) + .to_resolved() + .await?; + results.push(resolve_internal_inline(package_path, relative, options).await?); } Ok(merge_results(results)) @@ -2512,9 +2518,9 @@ async fn resolve_into_package( #[tracing::instrument(level = Level::TRACE, skip_all)] async fn resolve_import_map_result( result: &ImportMapResult, - lookup_path: Vc, - original_lookup_path: Vc, - original_request: Vc, + lookup_path: ResolvedVc, + original_lookup_path: ResolvedVc, + original_request: ResolvedVc, options: Vc, query: Vc, ) -> Result>> { @@ -2522,14 +2528,15 @@ async fn resolve_import_map_result( ImportMapResult::Result(result) => Some(*result), ImportMapResult::Alias(request, alias_lookup_path) => { let request = *request; - let lookup_path = alias_lookup_path.unwrap_or(lookup_path); + let lookup_path = match alias_lookup_path { + Some(path) => *path, + None => lookup_path, + }; // We must avoid cycles during resolving - if request.resolve().await? == original_request - && lookup_path.resolve().await? == original_lookup_path - { + if request == original_request && lookup_path == original_lookup_path { None } else { - let result = resolve_internal(lookup_path, request, options); + let result = resolve_internal(*lookup_path, *request, options); Some(result.with_replaced_request_key_pattern( request.request_pattern(), original_request.request_pattern(), @@ -2562,8 +2569,8 @@ async fn resolve_import_map_result( async fn resolved( request_key: RequestKey, fs_path: Vc, - original_context: Vc, - original_request: Vc, + original_context: ResolvedVc, + original_request: ResolvedVc, options_value: &ResolveOptions, options: Vc, query: Vc, @@ -2588,12 +2595,12 @@ async fn resolved( if let Some(resolved_map) = options_value.resolved_map { let result = resolved_map - .lookup(**path, original_context, original_request) + .lookup(**path, *original_context, *original_request) .await?; let resolved_result = resolve_import_map_result( &result, - path.parent(), + path.parent().to_resolved().await?, original_context, original_request, options, @@ -2620,7 +2627,7 @@ async fn resolved( } async fn handle_exports_imports_field( - package_path: Vc, + package_path: ResolvedVc, package_json_path: Vc, options: Vc, exports_imports_field: &AliasMap, @@ -2657,7 +2664,9 @@ async fn handle_exports_imports_field( let request = Request::parse(Value::new(Pattern::Concatenation(vec![ Pattern::Constant("./".into()), result_path, - ]))); + ]))) + .to_resolved() + .await?; let resolve_result = Box::pin(resolve_internal_inline(package_path, request, options)).await?; @@ -2683,8 +2692,8 @@ async fn handle_exports_imports_field( /// static strings or conditions like `import` or `require` to handle ESM/CJS /// with differently compiled files. async fn resolve_package_internal_with_imports_field( - file_path: Vc, - request: Vc, + file_path: ResolvedVc, + request: ResolvedVc, resolve_options: Vc, pattern: &Pattern, conditions: &BTreeMap, @@ -2697,9 +2706,9 @@ async fn resolve_package_internal_with_imports_field( if specifier == "#" || specifier.starts_with("#/") || specifier.ends_with('/') { ResolvingIssue { severity: error_severity(resolve_options).await?, - file_path, + file_path: *file_path, request_type: format!("package imports request: `{specifier}`"), - request, + request: *request, resolve_options, error_message: None, source: None, @@ -2709,14 +2718,14 @@ async fn resolve_package_internal_with_imports_field( return Ok(ResolveResult::unresolvable().into()); } - let imports_result = imports_field(file_path).await?; + let imports_result = imports_field(*file_path).await?; let (imports, package_json_path) = match &*imports_result { - ImportsFieldResult::Some(i, p) => (i, p), - ImportsFieldResult::None => return Ok(ResolveResult::unresolvable().into()), + ImportsFieldResult::Some(i, p) => (i, *p), + ImportsFieldResult::None => return Ok(ResolveResult::unresolvable().cell()), }; handle_exports_imports_field( - package_json_path.parent(), + package_json_path.parent().to_resolved().await?, *package_json_path, resolve_options, imports, diff --git a/turbopack/crates/turbopack-core/src/resolve/options.rs b/turbopack/crates/turbopack-core/src/resolve/options.rs index 762c70b900d1e..9297d235e3f51 100644 --- a/turbopack/crates/turbopack-core/src/resolve/options.rs +++ b/turbopack/crates/turbopack-core/src/resolve/options.rs @@ -117,7 +117,7 @@ pub enum ReplacedImportMapping { Ignore, Empty, Alternatives(Vec>), - Dynamic(Vc>), + Dynamic(ResolvedVc>), } impl ImportMapping { @@ -162,7 +162,9 @@ impl AliasTemplate for Vc { .try_join() .await?, ), - ImportMapping::Dynamic(replacement) => ReplacedImportMapping::Dynamic(*replacement), + ImportMapping::Dynamic(replacement) => { + ReplacedImportMapping::Dynamic(replacement.to_resolved().await?) + } } .cell()) }) @@ -300,14 +302,18 @@ impl ImportMap { #[turbo_tasks::value(shared)] #[derive(Clone, Default)] pub struct ResolvedMap { - pub by_glob: Vec<(Vc, Vc, Vc)>, + pub by_glob: Vec<( + ResolvedVc, + ResolvedVc, + ResolvedVc, + )>, } #[turbo_tasks::value(shared)] #[derive(Clone, Debug)] pub enum ImportMapResult { Result(Vc), - Alias(Vc, Option>), + Alias(ResolvedVc, Option>), Alternatives(Vec), NoEntry, } @@ -320,25 +326,28 @@ async fn import_mapping_to_result( Ok(match &*mapping.await? { ReplacedImportMapping::Direct(result) => ImportMapResult::Result(*result), ReplacedImportMapping::External(name, ty) => ImportMapResult::Result( - ResolveResult::primary(if let Some(name) = name { + *ResolveResult::primary(if let Some(name) = name { ResolveResultItem::External(name.clone(), *ty) } else if let Some(request) = request.await?.request() { ResolveResultItem::External(request, *ty) } else { bail!("Cannot resolve external reference without request") }) - .cell(), + .resolved_cell(), + ), + ReplacedImportMapping::Ignore => ImportMapResult::Result( + *ResolveResult::primary(ResolveResultItem::Ignore).resolved_cell(), + ), + ReplacedImportMapping::Empty => ImportMapResult::Result( + *ResolveResult::primary(ResolveResultItem::Empty).resolved_cell(), ), - ReplacedImportMapping::Ignore => { - ImportMapResult::Result(ResolveResult::primary(ResolveResultItem::Ignore).into()) - } - ReplacedImportMapping::Empty => { - ImportMapResult::Result(ResolveResult::primary(ResolveResultItem::Empty).into()) - } ReplacedImportMapping::PrimaryAlternative(name, context) => { let request = Request::parse(Value::new(name.clone())); - - ImportMapResult::Alias(request, *context) + let context_resolved = match context { + Some(c) => Some((*c).to_resolved().await?), + None => None, + }; + ImportMapResult::Alias(request.to_resolved().await?, context_resolved) } ReplacedImportMapping::Alternatives(list) => ImportMapResult::Alternatives( list.iter() diff --git a/turbopack/crates/turbopack-core/src/resolve/pattern.rs b/turbopack/crates/turbopack-core/src/resolve/pattern.rs index 14297dcff4971..1f5b34013dac7 100644 --- a/turbopack/crates/turbopack-core/src/resolve/pattern.rs +++ b/turbopack/crates/turbopack-core/src/resolve/pattern.rs @@ -9,7 +9,7 @@ use lazy_static::lazy_static; use regex::Regex; use serde::{Deserialize, Serialize}; use tracing::Instrument; -use turbo_tasks::{trace::TraceRawVcs, RcStr, Value, ValueToString, Vc}; +use turbo_tasks::{trace::TraceRawVcs, RcStr, ResolvedVc, Value, ValueToString, Vc}; use turbo_tasks_fs::{ util::normalize_path, DirectoryContent, DirectoryEntry, FileSystemEntryType, FileSystemPath, LinkContent, LinkType, @@ -1322,7 +1322,7 @@ pub struct PatternMatches(Vec); /// symlinks when they are interested in that. #[turbo_tasks::function] pub async fn read_matches( - lookup_dir: Vc, + lookup_dir: ResolvedVc, prefix: RcStr, force_in_lookup_dir: bool, pattern: Vc, @@ -1468,17 +1468,17 @@ pub async fn read_matches( if let Some(pos) = pat.match_position(&prefix) { results.push(( pos, - PatternMatch::Directory(prefix.clone().into(), lookup_dir), + PatternMatch::Directory(prefix.clone().into(), *lookup_dir), )); } prefix.pop(); } if prefix.is_empty() { if let Some(pos) = pat.match_position("./") { - results.push((pos, PatternMatch::Directory("./".into(), lookup_dir))); + results.push((pos, PatternMatch::Directory("./".into(), *lookup_dir))); } if let Some(pos) = pat.could_match_position("./") { - nested.push((pos, read_matches(lookup_dir, "./".into(), false, pattern))); + nested.push((pos, read_matches(*lookup_dir, "./".into(), false, pattern))); } } else { prefix.push('/'); @@ -1486,7 +1486,7 @@ pub async fn read_matches( if let Some(pos) = pat.could_match_position(&prefix) { nested.push(( pos, - read_matches(lookup_dir, prefix.to_string().into(), false, pattern), + read_matches(*lookup_dir, prefix.to_string().into(), false, pattern), )); } prefix.pop(); @@ -1495,7 +1495,7 @@ pub async fn read_matches( if let Some(pos) = pat.could_match_position(&prefix) { nested.push(( pos, - read_matches(lookup_dir, prefix.to_string().into(), false, pattern), + read_matches(*lookup_dir, prefix.to_string().into(), false, pattern), )); } prefix.pop(); diff --git a/turbopack/crates/turbopack-ecmascript/src/references/async_module.rs b/turbopack/crates/turbopack-ecmascript/src/references/async_module.rs index 1be520f9e505d..67520294e2a6a 100644 --- a/turbopack/crates/turbopack-ecmascript/src/references/async_module.rs +++ b/turbopack/crates/turbopack-ecmascript/src/references/async_module.rs @@ -123,7 +123,7 @@ impl AsyncModule { ReferencedAsset::Some(placeable) => { let chunk_item = placeable .as_chunk_item(Vc::upcast(chunking_context)) - .resolve() + .to_resolved() .await?; if async_module_info .referenced_async_modules diff --git a/turbopack/crates/turbopack-ecmascript/src/references/esm/base.rs b/turbopack/crates/turbopack-ecmascript/src/references/esm/base.rs index b41a16c8b904b..190e745d0115f 100644 --- a/turbopack/crates/turbopack-ecmascript/src/references/esm/base.rs +++ b/turbopack/crates/turbopack-ecmascript/src/references/esm/base.rs @@ -153,7 +153,7 @@ impl ModuleReference for EsmAssetReference { let ty = if matches!(self.annotations.module_type(), Some("json")) { EcmaScriptModulesReferenceSubType::ImportWithType(ImportWithType::Json) } else if let Some(part) = &self.export_name { - EcmaScriptModulesReferenceSubType::ImportPart(**part) + EcmaScriptModulesReferenceSubType::ImportPart(*part) } else { EcmaScriptModulesReferenceSubType::Import }; diff --git a/turbopack/crates/turbopack-nodejs/src/chunking_context.rs b/turbopack/crates/turbopack-nodejs/src/chunking_context.rs index 719856fc7e721..3bad35b5c4134 100644 --- a/turbopack/crates/turbopack-nodejs/src/chunking_context.rs +++ b/turbopack/crates/turbopack-nodejs/src/chunking_context.rs @@ -282,7 +282,7 @@ impl ChunkingContext for NodeJsChunkingContext { let mut assets: Vec>> = chunks .iter() - .map(|chunk| self.generate_chunk(*chunk)) + .map(|chunk| self.generate_chunk(**chunk)) .collect(); // Resolve assets @@ -333,7 +333,7 @@ impl ChunkingContext for NodeJsChunkingContext { let other_chunks: Vec<_> = extra_chunks .iter() .copied() - .chain(chunks.iter().map(|chunk| self.generate_chunk(*chunk))) + .chain(chunks.iter().map(|chunk| self.generate_chunk(**chunk))) .collect(); let Some(module) = Vc::try_resolve_downcast(module).await? else { diff --git a/turbopack/crates/turbopack/src/lib.rs b/turbopack/crates/turbopack/src/lib.rs index 3dc82e7e63aa1..61d90bdee2541 100644 --- a/turbopack/crates/turbopack/src/lib.rs +++ b/turbopack/crates/turbopack/src/lib.rs @@ -484,7 +484,7 @@ async fn process_default_internal( let reference_type = reference_type.into_value(); let part: Option> = match &reference_type { ReferenceType::EcmaScriptModules(EcmaScriptModulesReferenceSubType::ImportPart(part)) => { - Some(*part) + Some(**part) } _ => None, };