Skip to content

Commit

Permalink
codemod(turbopack): Rewrite Vc fields in structs as ResolvedVc (part …
Browse files Browse the repository at this point in the history
…5) (vercel#71861)

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: vercel#70927
- Part 2: vercel#71172
- Part 3: vercel#71665
- Part 4: vercel#71804

Closes PACK-3339
  • Loading branch information
bgw authored and stipsan committed Nov 6, 2024
1 parent 50f6682 commit 864b577
Show file tree
Hide file tree
Showing 20 changed files with 133 additions and 108 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,
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
18 changes: 8 additions & 10 deletions turbopack/crates/turbopack-css/src/chunk/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,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 @@ -265,7 +265,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 @@ -274,19 +274,17 @@ 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(),
fragment: None,
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,
HeaderList::new(response_header_overwrites)
.to_resolved()
.await?,
Expand Down
45 changes: 24 additions & 21 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 @@ -339,7 +340,7 @@ impl RouteTree {
.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,26 @@ 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);
}
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 @@ -132,6 +132,8 @@ impl ChunkGroupFilesChunkItem {
inner.module.ident(),
inner
.runtime_entries
.as_deref()
.copied()
.unwrap_or_else(EvaluatableAssets::empty)
.with_entry(ecma),
Value::new(AvailabilityInfo::Root),
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);
}
}

Expand Down
10 changes: 5 additions & 5 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 @@ -319,7 +319,7 @@ impl AnalyzeEcmascriptModuleResultBuilder {
*c = c.resolve().await?;
}
CodeGen::CodeGenerateableWithAsyncModuleInfo(c) => {
*c = c.resolve().await?;
*c = c.to_resolved().await?;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use swc_core::{
},
quote, quote_expr,
};
use turbo_tasks::{primitives::Regex, FxIndexMap, RcStr, Value, ValueToString, Vc};
use turbo_tasks::{primitives::Regex, FxIndexMap, RcStr, ResolvedVc, Value, ValueToString, Vc};
use turbo_tasks_fs::{DirectoryContent, DirectoryEntry, FileSystemPath};
use turbopack_core::{
asset::{Asset, AssetContent},
Expand Down Expand Up @@ -46,8 +46,8 @@ use crate::{
#[turbo_tasks::value]
#[derive(Debug)]
pub(crate) enum DirListEntry {
File(Vc<FileSystemPath>),
Dir(Vc<DirList>),
File(ResolvedVc<FileSystemPath>),
Dir(ResolvedVc<DirList>),
}

#[turbo_tasks::value(transparent)]
Expand Down Expand Up @@ -83,17 +83,19 @@ impl DirList {
DirectoryEntry::File(path) => {
if let Some(relative_path) = root_val.get_relative_path_to(&*path.await?) {
if regex.is_match(&relative_path) {
list.insert(relative_path, DirListEntry::File(**path));
list.insert(relative_path, DirListEntry::File(*path));
}
}
}
DirectoryEntry::Directory(path) if recursive => {
if let Some(relative_path) = root_val.get_relative_path_to(&*path.await?) {
list.insert(
relative_path,
DirListEntry::Dir(DirList::read_internal(
root, **path, recursive, filter,
)),
DirListEntry::Dir(
DirList::read_internal(root, **path, recursive, filter)
.to_resolved()
.await?,
),
);
}
}
Expand All @@ -119,7 +121,7 @@ impl DirList {
for (k, entry) in &*dir {
match entry {
DirListEntry::File(path) => {
list.insert(k.clone(), *path);
list.insert(k.clone(), **path);
}
DirListEntry::Dir(d) => {
queue.push_back(d.await?);
Expand Down
Loading

0 comments on commit 864b577

Please sign in to comment.