Skip to content

Commit

Permalink
codemod(turbopack): Rewrite Vc fields in structs as ResolvedVc (part 5)
Browse files Browse the repository at this point in the history
  • Loading branch information
bgw committed Oct 25, 2024
1 parent 22a6e01 commit c406ab7
Show file tree
Hide file tree
Showing 18 changed files with 147 additions and 115 deletions.
6 changes: 4 additions & 2 deletions turbopack/crates/node-file-trace/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -576,8 +576,10 @@ async fn create_module_asset(
..Default::default()
}
.into(),
)));
let compile_time_info = CompileTimeInfo::builder(env).cell();
)))
.to_resolved()
.await?;
let compile_time_info = CompileTimeInfo::builder(*env).cell();
let glob_mappings = vec![
(
root.to_resolved().await?,
Expand Down
4 changes: 2 additions & 2 deletions turbopack/crates/turbopack-core/src/ident.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub struct AssetIdent {
/// The fragment of the asset (e.g. `#foo`)
pub fragment: Option<ResolvedVc<RcStr>>,
/// The assets that are nested in this asset
pub assets: Vec<(Vc<RcStr>, Vc<AssetIdent>)>,
pub assets: Vec<(ResolvedVc<RcStr>, ResolvedVc<AssetIdent>)>,
/// The modifiers of this asset (e.g. `client chunks`)
pub modifiers: Vec<Vc<RcStr>>,
/// The part of the asset that is a (ECMAScript) module
Expand All @@ -31,7 +31,7 @@ impl AssetIdent {
self.modifiers.push(modifier);
}

pub fn add_asset(&mut self, key: Vc<RcStr>, asset: Vc<AssetIdent>) {
pub fn add_asset(&mut self, key: ResolvedVc<RcStr>, asset: ResolvedVc<AssetIdent>) {
self.assets.push((key, asset));
}

Expand Down
20 changes: 9 additions & 11 deletions turbopack/crates/turbopack-css/src/chunk/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ impl OutputAsset for CssChunk {

let CssChunkContent { chunk_items, .. } = &*self.content.await?;
let mut common_path = if let Some(chunk_item) = chunk_items.first() {
let path = chunk_item.asset_ident().path().resolve().await?;
let path = chunk_item.asset_ident().path().to_resolved().await?;
Some((path, path.await?))
} else {
None
Expand All @@ -248,7 +248,7 @@ impl OutputAsset for CssChunk {
if let Some((common_path_vc, common_path_ref)) = common_path.as_mut() {
let path = chunk_item.asset_ident().path().await?;
while !path.is_inside_or_equal_ref(common_path_ref) {
let parent = common_path_vc.parent().resolve().await?;
let parent = common_path_vc.parent().to_resolved().await?;
if parent == *common_path_vc {
common_path = None;
break;
Expand All @@ -257,21 +257,19 @@ impl OutputAsset for CssChunk {
*common_path_ref = (*common_path_vc).await?;
}
}
assets.push((chunk_item_key, chunk_item.content_ident()));
}

// Make sure the idents are resolved
for (_, ident) in assets.iter_mut() {
*ident = ident.resolve().await?;
assets.push((
chunk_item_key.to_resolved().await?,
chunk_item.content_ident().to_resolved().await?,
));
}

let ident = AssetIdent {
path: if let Some((common_path, _)) = common_path {
common_path
*common_path
} else {
ServerFileSystem::new().root()
*ServerFileSystem::new().root().to_resolved().await?
},
query: Vc::<RcStr>::default(),
query: *Vc::<RcStr>::default().to_resolved().await?,
fragment: None,
assets,
modifiers: Vec::new(),
Expand Down
4 changes: 2 additions & 2 deletions turbopack/crates/turbopack-dev-server/src/source/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use super::{
#[turbo_tasks::value(serialization = "none")]
pub enum ResolveSourceRequestResult {
NotFound,
Static(Vc<StaticContent>, ResolvedVc<HeaderList>),
Static(ResolvedVc<StaticContent>, ResolvedVc<HeaderList>),
HttpProxy(Vc<ProxyResult>),
}

Expand Down Expand Up @@ -107,7 +107,7 @@ pub async fn resolve_source_request(
}
ContentSourceContent::Static(static_content) => {
return Ok(ResolveSourceRequestResult::Static(
**static_content,
static_content.to_resolved().await?,
HeaderList::new(response_header_overwrites)
.to_resolved()
.await?,
Expand Down
48 changes: 26 additions & 22 deletions turbopack/crates/turbopack-dev-server/src/source/route_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ use std::{fmt::Write, mem::replace};
use anyhow::Result;
use serde::{Deserialize, Serialize};
use turbo_tasks::{
fxindexmap, trace::TraceRawVcs, FxIndexMap, RcStr, TaskInput, TryJoinIterExt, ValueToString, Vc,
fxindexmap, trace::TraceRawVcs, FxIndexMap, RcStr, ResolvedVc, TaskInput, TryJoinIterExt,
ValueToString, Vc,
};

use super::{GetContentSourceContent, GetContentSourceContents};
Expand Down Expand Up @@ -101,7 +102,7 @@ pub struct RouteTree {
base: Vec<BaseSegment>,
sources: Vec<Vc<Box<dyn GetContentSourceContent>>>,
static_segments: FxIndexMap<RcStr, Vc<RouteTree>>,
dynamic_segments: Vec<Vc<RouteTree>>,
dynamic_segments: Vec<ResolvedVc<RouteTree>>,
catch_all_sources: Vec<Vc<Box<dyn GetContentSourceContent>>>,
fallback_sources: Vec<Vc<Box<dyn GetContentSourceContent>>>,
not_found_sources: Vec<Vc<Box<dyn GetContentSourceContent>>>,
Expand Down Expand Up @@ -333,13 +334,13 @@ impl RouteTree {
match selector_segment {
BaseSegment::Static(value) => Ok(RouteTree {
base,
static_segments: fxindexmap! { value => inner.cell() },
static_segments: fxindexmap! { value => *inner.resolved_cell() },
..Default::default()
}
.cell()),
BaseSegment::Dynamic => Ok(RouteTree {
base,
dynamic_segments: vec![inner.cell()],
dynamic_segments: vec![inner.resolved_cell()],
..Default::default()
}
.cell()),
Expand All @@ -366,24 +367,27 @@ impl RouteTree {
fallback_sources,
not_found_sources,
} = &mut this;
sources
.iter_mut()
.for_each(|s| *s = mapper.map_get_content(*s));
catch_all_sources
.iter_mut()
.for_each(|s| *s = mapper.map_get_content(*s));
fallback_sources
.iter_mut()
.for_each(|s| *s = mapper.map_get_content(*s));
not_found_sources
.iter_mut()
.for_each(|s| *s = mapper.map_get_content(*s));
static_segments
.values_mut()
.for_each(|r| *r = r.map_routes(mapper));
dynamic_segments
.iter_mut()
.for_each(|r| *r = r.map_routes(mapper));

for s in sources.iter_mut() {
*s = mapper.map_get_content(*s);
}
for s in catch_all_sources.iter_mut() {
*s = mapper.map_get_content(*s);
}
for s in fallback_sources.iter_mut() {
*s = mapper.map_get_content(*s);
}
for s in not_found_sources.iter_mut() {
*s = mapper.map_get_content(*s);
}

for r in static_segments.values_mut() {
*r = *r.map_routes(mapper).to_resolved().await?;
}
for r in dynamic_segments.iter_mut() {
*r = r.map_routes(mapper).to_resolved().await?;
}

Ok(this.cell())
}
}
Expand Down
20 changes: 10 additions & 10 deletions turbopack/crates/turbopack-ecmascript/src/chunk/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ impl Chunk for EcmascriptChunk {

let EcmascriptChunkContent { chunk_items, .. } = &*self.content.await?;
let mut common_path = if let Some((chunk_item, _)) = chunk_items.first() {
let path = chunk_item.asset_ident().path().resolve().await?;
let path = chunk_item.asset_ident().path().to_resolved().await?;
Some((path, path.await?))
} else {
None
Expand All @@ -90,7 +90,7 @@ impl Chunk for EcmascriptChunk {
if let Some((common_path_vc, common_path_ref)) = common_path.as_mut() {
let path = chunk_item.asset_ident().path().await?;
while !path.is_inside_or_equal_ref(common_path_ref) {
let parent = common_path_vc.parent().resolve().await?;
let parent = common_path_vc.parent().to_resolved().await?;
if parent == *common_path_vc {
common_path = None;
break;
Expand All @@ -99,21 +99,21 @@ impl Chunk for EcmascriptChunk {
*common_path_ref = (*common_path_vc).await?;
}
}
assets.push((chunk_item_key, chunk_item.content_ident()));
assets.push((
chunk_item_key.to_resolved().await?,
chunk_item.content_ident().to_resolved().await?,
));
}

// Make sure the idents are resolved
for (_, ident) in assets.iter_mut() {
*ident = ident.resolve().await?;
}
// The previous resolve loop is no longer needed since we're already using ResolvedVc

let ident = AssetIdent {
path: if let Some((common_path, _)) = common_path {
common_path
*common_path
} else {
ServerFileSystem::new().root()
*ServerFileSystem::new().root().to_resolved().await?
},
query: Vc::<RcStr>::default(),
query: *Vc::<RcStr>::default().to_resolved().await?,
fragment: None,
assets,
modifiers: Vec::new(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use anyhow::Result;
use turbo_tasks::{FxIndexSet, RcStr, TryJoinIterExt, Value, ValueToString, Vc};
use turbo_tasks::{FxIndexSet, RcStr, ResolvedVc, TryJoinIterExt, Value, ValueToString, Vc};
use turbo_tasks_fs::{File, FileSystemPath};
use turbopack_core::{
asset::{Asset, AssetContent},
Expand Down Expand Up @@ -39,7 +39,7 @@ pub struct ChunkGroupFilesAsset {
pub module: Vc<Box<dyn ChunkableModule>>,
pub client_root: Vc<FileSystemPath>,
pub chunking_context: Vc<Box<dyn ChunkingContext>>,
pub runtime_entries: Option<Vc<EvaluatableAssets>>,
pub runtime_entries: Option<ResolvedVc<EvaluatableAssets>>,
}

#[turbo_tasks::function]
Expand Down Expand Up @@ -126,20 +126,22 @@ impl ChunkGroupFilesChunkItem {
async fn chunks(&self) -> Result<Vc<OutputAssets>> {
let inner = self.inner.await?;
let chunks = if let Some(ecma) =
Vc::try_resolve_sidecast::<Box<dyn EvaluatableAsset>>(inner.module).await?
ResolvedVc::try_sidecast::<Box<dyn EvaluatableAsset>>(inner.module.to_resolved().await?)
.await?
{
let empty_assets = EvaluatableAssets::empty().to_resolved().await?;
inner.chunking_context.evaluated_chunk_group_assets(
inner.module.ident(),
inner

Check failure on line 135 in turbopack/crates/turbopack-ecmascript/src/chunk_group_files_asset.rs

View workflow job for this annotation

GitHub Actions / rust check / build

unnecessary closure used to substitute value for `Option::None`
.runtime_entries
.unwrap_or_else(EvaluatableAssets::empty)
.with_entry(ecma),
.unwrap_or_else(|| empty_assets)
.with_entry(*ecma),
Value::new(AvailabilityInfo::Root),
)
} else {
inner
.chunking_context
.root_chunk_group_assets(Vc::upcast(inner.module))
.root_chunk_group_assets(*ResolvedVc::upcast(inner.module.to_resolved().await?))
};
Ok(chunks)
}
Expand Down
4 changes: 2 additions & 2 deletions turbopack/crates/turbopack-ecmascript/src/code_gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use swc_core::ecma::{
ast::Stmt,
visit::{AstParentKind, VisitMut},
};
use turbo_tasks::{debug::ValueDebugFormat, trace::TraceRawVcs, RcStr, Vc};
use turbo_tasks::{debug::ValueDebugFormat, trace::TraceRawVcs, RcStr, ResolvedVc, Vc};
use turbopack_core::chunk::{AsyncModuleInfo, ChunkingContext};

/// impl of code generation inferred from a ModuleReference.
Expand Down Expand Up @@ -108,7 +108,7 @@ pub trait CodeGenerateableWithAsyncModuleInfo {
#[derive(Clone, Copy, PartialEq, Eq, Serialize, Deserialize, TraceRawVcs, ValueDebugFormat)]
pub enum CodeGen {
CodeGenerateable(Vc<Box<dyn CodeGenerateable>>),
CodeGenerateableWithAsyncModuleInfo(Vc<Box<dyn CodeGenerateableWithAsyncModuleInfo>>),
CodeGenerateableWithAsyncModuleInfo(ResolvedVc<Box<dyn CodeGenerateableWithAsyncModuleInfo>>),
}

#[turbo_tasks::value(transparent)]
Expand Down
5 changes: 4 additions & 1 deletion turbopack/crates/turbopack-ecmascript/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,10 @@ impl Module for EcmascriptModuleAsset {
if let Some(inner_assets) = self.inner_assets {
let mut ident = self.source.ident().await?.clone_value();
for (name, asset) in inner_assets.await?.iter() {
ident.add_asset(Vc::cell(name.to_string().into()), asset.ident());
ident.add_asset(
ResolvedVc::cell(name.to_string().into()),
asset.ident().to_resolved().await?,
);
}
ident.add_modifier(modifier());
ident.layer = Some(self.asset_context.layer().to_resolved().await?);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ use swc_core::{
},
quote, quote_expr,
};
use turbo_tasks::{trace::TraceRawVcs, FxIndexMap, RcStr, TryFlatJoinIterExt, ValueToString, Vc};
use turbo_tasks::{
trace::TraceRawVcs, FxIndexMap, RcStr, ResolvedVc, TryFlatJoinIterExt, ValueToString, Vc,
};
use turbo_tasks_fs::glob::Glob;
use turbopack_core::{
chunk::ChunkingContext,
Expand Down Expand Up @@ -425,7 +427,7 @@ pub struct EsmExports {
pub struct ExpandedExports {
pub exports: BTreeMap<RcStr, EsmExport>,
/// Modules we couldn't analyse all exports of.
pub dynamic_exports: Vec<Vc<Box<dyn EcmascriptChunkPlaceable>>>,
pub dynamic_exports: Vec<ResolvedVc<Box<dyn EcmascriptChunkPlaceable>>>,
}

#[turbo_tasks::value_impl]
Expand Down Expand Up @@ -456,7 +458,7 @@ impl EsmExports {
}

if export_info.has_dynamic_exports {
dynamic_exports.push(**asset);
dynamic_exports.push((**asset).to_resolved().await?);
}
}

Expand Down
12 changes: 6 additions & 6 deletions turbopack/crates/turbopack-ecmascript/src/references/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,14 +240,14 @@ impl AnalyzeEcmascriptModuleResultBuilder {

/// Adds a codegen to the analysis result.
#[allow(dead_code)]
pub fn add_code_gen_with_availability_info<C>(&mut self, code_gen: Vc<C>)
pub fn add_code_gen_with_availability_info<C>(&mut self, code_gen: ResolvedVc<C>)
where
C: Upcast<Box<dyn CodeGenerateableWithAsyncModuleInfo>>,
{
self.code_gens
.push(CodeGen::CodeGenerateableWithAsyncModuleInfo(Vc::upcast(
code_gen,
)));
.push(CodeGen::CodeGenerateableWithAsyncModuleInfo(
ResolvedVc::upcast(code_gen),
));
}

pub fn add_binding(&mut self, binding: EsmBinding) {
Expand Down Expand Up @@ -316,10 +316,10 @@ impl AnalyzeEcmascriptModuleResultBuilder {
for c in self.code_gens.iter_mut() {
match c {
CodeGen::CodeGenerateable(c) => {
*c = c.resolve().await?;
*c = *c.to_resolved().await?;
}
CodeGen::CodeGenerateableWithAsyncModuleInfo(c) => {
*c = c.resolve().await?;
*c = c.to_resolved().await?;
}
}
}
Expand Down
Loading

0 comments on commit c406ab7

Please sign in to comment.