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
…6) (#71941)

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
- Part 4: #71804
- Part 5: #71861

Closes PACK-3342
  • Loading branch information
bgw authored and kdy1 committed Oct 30, 2024
1 parent 0b73f79 commit 70db6f5
Show file tree
Hide file tree
Showing 19 changed files with 127 additions and 95 deletions.
16 changes: 9 additions & 7 deletions crates/next-api/src/middleware.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use next_core::{
util::{parse_config_from_source, MiddlewareMatcherKind},
};
use tracing::Instrument;
use turbo_tasks::{Completion, RcStr, Value, Vc};
use turbo_tasks::{Completion, RcStr, ResolvedVc, Value, Vc};
use turbo_tasks_fs::{self, File, FileContent, FileSystemPath};
use turbopack_core::{
asset::AssetContent,
Expand Down Expand Up @@ -36,8 +36,8 @@ pub struct MiddlewareEndpoint {
project: Vc<Project>,
asset_context: Vc<Box<dyn AssetContext>>,
source: Vc<Box<dyn Source>>,
app_dir: Option<Vc<FileSystemPath>>,
ecmascript_client_reference_transition_name: Option<Vc<RcStr>>,
app_dir: Option<ResolvedVc<FileSystemPath>>,
ecmascript_client_reference_transition_name: Option<ResolvedVc<RcStr>>,
}

#[turbo_tasks::value_impl]
Expand All @@ -47,8 +47,8 @@ impl MiddlewareEndpoint {
project: Vc<Project>,
asset_context: Vc<Box<dyn AssetContext>>,
source: Vc<Box<dyn Source>>,
app_dir: Option<Vc<FileSystemPath>>,
ecmascript_client_reference_transition_name: Option<Vc<RcStr>>,
app_dir: Option<ResolvedVc<FileSystemPath>>,
ecmascript_client_reference_transition_name: Option<ResolvedVc<RcStr>>,
) -> Vc<Self> {
Self {
project,
Expand Down Expand Up @@ -85,9 +85,11 @@ impl MiddlewareEndpoint {

let mut evaluatable_assets = get_server_runtime_entries(
Value::new(ServerContextType::Middleware {
app_dir: self.app_dir,
app_dir: self.app_dir.as_deref().copied(),
ecmascript_client_reference_transition_name: self
.ecmascript_client_reference_transition_name,
.ecmascript_client_reference_transition_name
.as_deref()
.copied(),
}),
self.project.next_mode(),
)
Expand Down
4 changes: 2 additions & 2 deletions crates/next-api/src/pages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ impl PagesProject {
Ok(())
}

if let Some(api) = api {
if let Some(api) = *api {
add_dir_to_routes(&mut routes, *api, |pathname, original_name, page| {
Route::PageApi {
endpoint: Vc::upcast(PageEndpoint::new(
Expand Down Expand Up @@ -174,7 +174,7 @@ impl PagesProject {
)),
};

if let Some(pages) = pages {
if let Some(pages) = *pages {
add_dir_to_routes(&mut routes, *pages, make_page_route).await?;
}

Expand Down
4 changes: 2 additions & 2 deletions crates/next-core/src/app_page_loader_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ impl AppPageLoaderTreeBuilder {
.await?;
self.write_modules_entry(AppDirModuleType::DefaultPage, *default)
.await?;
self.write_modules_entry(AppDirModuleType::GlobalError, *global_error)
self.write_modules_entry(AppDirModuleType::GlobalError, global_error.map(|err| *err))
.await?;

let modules_code = replace(&mut self.loader_tree_code, temp_loader_tree_code);
Expand Down Expand Up @@ -373,7 +373,7 @@ impl AppPageLoaderTreeBuilder {
if let Some(global_error) = modules.global_error {
let module = self
.base
.process_source(Vc::upcast(FileSource::new(global_error)));
.process_source(Vc::upcast(FileSource::new(*global_error)));
self.base.inner_assets.insert(GLOBAL_ERROR.into(), module);
};

Expand Down
16 changes: 9 additions & 7 deletions crates/next-core/src/app_structure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub struct AppDirModules {
#[serde(skip_serializing_if = "Option::is_none")]
pub error: Option<Vc<FileSystemPath>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub global_error: Option<Vc<FileSystemPath>>,
pub global_error: Option<ResolvedVc<FileSystemPath>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub loading: Option<Vc<FileSystemPath>>,
#[serde(skip_serializing_if = "Option::is_none")]
Expand All @@ -46,7 +46,7 @@ pub struct AppDirModules {
#[serde(skip_serializing_if = "Option::is_none")]
pub default: Option<Vc<FileSystemPath>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub route: Option<Vc<FileSystemPath>>,
pub route: Option<ResolvedVc<FileSystemPath>>,
#[serde(skip_serializing_if = "Metadata::is_empty", default)]
pub metadata: Metadata,
}
Expand Down Expand Up @@ -199,7 +199,7 @@ impl GlobalMetadata {
#[derive(Debug)]
pub struct DirectoryTree {
/// key is e.g. "dashboard", "(dashboard)", "@slot"
pub subdirectories: BTreeMap<RcStr, Vc<DirectoryTree>>,
pub subdirectories: BTreeMap<RcStr, ResolvedVc<DirectoryTree>>,
pub modules: AppDirModules,
}

Expand Down Expand Up @@ -302,12 +302,12 @@ async fn get_directory_tree_internal(
"page" => modules.page = Some(*file),
"layout" => modules.layout = Some(*file),
"error" => modules.error = Some(*file),
"global-error" => modules.global_error = Some(*file),
"global-error" => modules.global_error = Some(file),
"loading" => modules.loading = Some(*file),
"template" => modules.template = Some(*file),
"not-found" => modules.not_found = Some(*file),
"default" => modules.default = Some(*file),
"route" => modules.route = Some(*file),
"route" => modules.route = Some(file),
_ => {}
}
}
Expand Down Expand Up @@ -363,7 +363,9 @@ async fn get_directory_tree_internal(
DirectoryEntry::Directory(dir) => {
// appDir ignores paths starting with an underscore
if !basename.starts_with('_') {
let result = get_directory_tree(*dir, page_extensions);
let result = get_directory_tree(*dir, page_extensions)
.to_resolved()
.await?;
subdirectories.insert(basename.clone(), result);
}
}
Expand Down Expand Up @@ -1232,7 +1234,7 @@ async fn directory_tree_to_entrypoints_internal_untraced(
app_dir,
global_metadata,
subdir_name.clone(),
subdirectory,
*subdirectory,
child_app_page.clone(),
*root_layouts,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use turbo_tasks::{
debug::ValueDebugFormat,
graph::{AdjacencyMap, GraphTraversal, Visit, VisitControlFlow, VisitedNodes},
trace::TraceRawVcs,
FxIndexMap, FxIndexSet, RcStr, ReadRef, TryJoinIterExt, ValueToString, Vc,
FxIndexMap, FxIndexSet, RcStr, ReadRef, ResolvedVc, TryJoinIterExt, ValueToString, Vc,
};
use turbo_tasks_fs::FileSystemPath;
use turbopack::css::CssModuleAsset;
Expand Down Expand Up @@ -121,7 +121,7 @@ impl ClientReferenceGraphResult {

#[turbo_tasks::function]
pub async fn client_reference_graph(
entries: Vec<Vc<Box<dyn Module>>>,
entries: Vec<ResolvedVc<Box<dyn Module>>>,
visited_nodes: Vc<VisitedClientReferenceGraphNodes>,
) -> Result<Vc<ClientReferenceGraphResult>> {
async move {
Expand All @@ -143,11 +143,11 @@ pub async fn client_reference_graph(
.map(|module| async move {
Ok(VisitClientReferenceNode {
state: if let Some(server_component) =
Vc::try_resolve_downcast_type::<NextServerComponentModule>(module)
ResolvedVc::try_downcast_type::<NextServerComponentModule>(module)
.await?
{
VisitClientReferenceNodeState::InServerComponent {
server_component,
server_component: *server_component,
}
} else {
VisitClientReferenceNodeState::Entry {
Expand Down Expand Up @@ -190,10 +190,10 @@ pub async fn client_reference_graph(
}
}
VisitClientReferenceNodeType::ServerUtilEntry(server_util, _) => {
server_utils.push(*server_util);
server_utils.push(**server_util);
}
VisitClientReferenceNodeType::ServerComponentEntry(server_component, _) => {
server_component_entries.push(*server_component);
server_component_entries.push(**server_component);
}
}
}
Expand All @@ -219,7 +219,7 @@ pub struct ServerEntries {
}

#[turbo_tasks::function]
pub async fn find_server_entries(entry: Vc<Box<dyn Module>>) -> Result<Vc<ServerEntries>> {
pub async fn find_server_entries(entry: ResolvedVc<Box<dyn Module>>) -> Result<Vc<ServerEntries>> {
let graph = AdjacencyMap::new()
.skip_duplicates()
.visit(
Expand All @@ -244,10 +244,10 @@ pub async fn find_server_entries(entry: Vc<Box<dyn Module>>) -> Result<Vc<Server
for node in graph.reverse_topological() {
match &node.ty {
VisitClientReferenceNodeType::ServerUtilEntry(server_util, _) => {
server_utils.push(*server_util);
server_utils.push(**server_util);
}
VisitClientReferenceNodeType::ServerComponentEntry(server_component, _) => {
server_component_entries.push(*server_component);
server_component_entries.push(**server_component);
}
VisitClientReferenceNodeType::Internal(_, _)
| VisitClientReferenceNodeType::ClientReference(_, _) => {}
Expand Down Expand Up @@ -303,9 +303,9 @@ impl VisitClientReferenceNodeState {
)]
enum VisitClientReferenceNodeType {
ClientReference(ClientReference, ReadRef<RcStr>),
ServerComponentEntry(Vc<NextServerComponentModule>, ReadRef<RcStr>),
ServerUtilEntry(Vc<Box<dyn Module>>, ReadRef<RcStr>),
Internal(Vc<Box<dyn Module>>, ReadRef<RcStr>),
ServerComponentEntry(ResolvedVc<NextServerComponentModule>, ReadRef<RcStr>),
ServerUtilEntry(ResolvedVc<Box<dyn Module>>, ReadRef<RcStr>),
Internal(ResolvedVc<Box<dyn Module>>, ReadRef<RcStr>),
}

impl Visit<VisitClientReferenceNode> for VisitClientReference {
Expand Down Expand Up @@ -343,30 +343,32 @@ impl Visit<VisitClientReferenceNode> for VisitClientReference {
VisitClientReferenceNodeType::ClientReference(..) => return Ok(vec![]),
VisitClientReferenceNodeType::Internal(module, _) => module,
VisitClientReferenceNodeType::ServerUtilEntry(module, _) => module,
VisitClientReferenceNodeType::ServerComponentEntry(module, _) => Vc::upcast(module),
VisitClientReferenceNodeType::ServerComponentEntry(module, _) => {
ResolvedVc::upcast(module)
}
};

let referenced_modules = primary_referenced_modules(parent_module).await?;
let referenced_modules = primary_referenced_modules(*parent_module).await?;

let referenced_modules = referenced_modules.iter().map(|module| async move {
let module = module.resolve().await?;
let module = module.to_resolved().await?;
if let Some(client_reference_module) =
Vc::try_resolve_downcast_type::<EcmascriptClientReferenceModule>(module).await?
ResolvedVc::try_downcast_type::<EcmascriptClientReferenceModule>(module).await?
{
return Ok(VisitClientReferenceNode {
state: node.state,
ty: VisitClientReferenceNodeType::ClientReference(
ClientReference {
server_component: node.state.server_component(),
ty: ClientReferenceType::EcmascriptClientReference {
parent_module: Vc::try_resolve_downcast_type::<
parent_module: *ResolvedVc::try_downcast_type::<
EcmascriptClientReferenceProxyModule,
>(
parent_module
)
.await?
.unwrap(),
module: client_reference_module,
module: *client_reference_module,
},
},
client_reference_module.ident().to_string().await?,
Expand All @@ -375,15 +377,15 @@ impl Visit<VisitClientReferenceNode> for VisitClientReference {
}

if let Some(css_client_reference_asset) =
Vc::try_resolve_downcast_type::<CssModuleAsset>(module).await?
ResolvedVc::try_downcast_type::<CssModuleAsset>(module).await?
{
return Ok(VisitClientReferenceNode {
state: node.state,
ty: VisitClientReferenceNodeType::ClientReference(
ClientReference {
server_component: node.state.server_component(),
ty: ClientReferenceType::CssClientReference(
css_client_reference_asset,
*css_client_reference_asset,
),
},
css_client_reference_asset.ident().to_string().await?,
Expand All @@ -392,11 +394,11 @@ impl Visit<VisitClientReferenceNode> for VisitClientReference {
}

if let Some(server_component_asset) =
Vc::try_resolve_downcast_type::<NextServerComponentModule>(module).await?
ResolvedVc::try_downcast_type::<NextServerComponentModule>(module).await?
{
return Ok(VisitClientReferenceNode {
state: VisitClientReferenceNodeState::InServerComponent {
server_component: server_component_asset,
server_component: *server_component_asset,
},
ty: VisitClientReferenceNodeType::ServerComponentEntry(
server_component_asset,
Expand Down
21 changes: 12 additions & 9 deletions crates/next-core/src/next_dynamic/visit_dynamic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use anyhow::Result;
use tracing::Instrument;
use turbo_tasks::{
graph::{AdjacencyMap, GraphTraversal, Visit, VisitControlFlow},
RcStr, ReadRef, TryJoinIterExt, ValueToString, Vc,
RcStr, ReadRef, ResolvedVc, TryJoinIterExt, ValueToString, Vc,
};
use turbopack_core::{
module::{Module, Modules},
Expand All @@ -29,7 +29,10 @@ impl NextDynamicEntries {
.iter()
.copied()
.map(|m| async move {
Ok(VisitDynamicNode::Internal(m, m.ident().to_string().await?))
Ok(VisitDynamicNode::Internal(
m.to_resolved().await?,
m.ident().to_string().await?,
))
})
.try_join()
.await?,
Expand All @@ -50,7 +53,7 @@ impl NextDynamicEntries {
// traversal.
}
VisitDynamicNode::Dynamic(dynamic_asset, _) => {
next_dynamics.push(dynamic_asset);
next_dynamics.push(*dynamic_asset);
}
}
}
Expand All @@ -66,8 +69,8 @@ struct VisitDynamic;

#[derive(Clone, Eq, PartialEq, Hash)]
enum VisitDynamicNode {
Dynamic(Vc<NextDynamicEntryModule>, ReadRef<RcStr>),
Internal(Vc<Box<dyn Module>>, ReadRef<RcStr>),
Dynamic(ResolvedVc<NextDynamicEntryModule>, ReadRef<RcStr>),
Internal(ResolvedVc<Box<dyn Module>>, ReadRef<RcStr>),
}

impl Visit<VisitDynamicNode> for VisitDynamic {
Expand All @@ -85,16 +88,16 @@ impl Visit<VisitDynamicNode> for VisitDynamic {
let node = node.clone();
async move {
let module = match node {
VisitDynamicNode::Dynamic(dynamic_module, _) => Vc::upcast(dynamic_module),
VisitDynamicNode::Dynamic(dynamic_module, _) => ResolvedVc::upcast(dynamic_module),
VisitDynamicNode::Internal(module, _) => module,
};

let referenced_modules = primary_referenced_modules(module).await?;
let referenced_modules = primary_referenced_modules(*module).await?;

let referenced_modules = referenced_modules.iter().map(|module| async move {
let module = module.resolve().await?;
let module = module.to_resolved().await?;
if let Some(next_dynamic_module) =
Vc::try_resolve_downcast_type::<NextDynamicEntryModule>(module).await?
ResolvedVc::try_downcast_type::<NextDynamicEntryModule>(module).await?
{
return Ok(VisitDynamicNode::Dynamic(
next_dynamic_module,
Expand Down
8 changes: 4 additions & 4 deletions crates/next-core/src/next_image/source_asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ impl Asset for StructuredImageFileSource {
let blur_options = blur_options();
match self.blur_placeholder_mode {
BlurPlaceholderMode::NextImageUrl => {
let info = get_meta_data(self.image.ident(), content, None).await?;
let info = get_meta_data(self.image.ident(), *content, None).await?;
let width = info.width;
let height = info.height;
let blur_options = blur_options.await?;
Expand Down Expand Up @@ -86,7 +86,7 @@ impl Asset for StructuredImageFileSource {
)?;
}
BlurPlaceholderMode::DataUrl => {
let info = get_meta_data(self.image.ident(), content, Some(blur_options)).await?;
let info = get_meta_data(self.image.ident(), *content, Some(blur_options)).await?;
writeln!(
result,
"export default {{ src, width: {width}, height: {height}, blurDataURL: \
Expand All @@ -102,7 +102,7 @@ impl Asset for StructuredImageFileSource {
)?;
}
BlurPlaceholderMode::None => {
let info = get_meta_data(self.image.ident(), content, None).await?;
let info = get_meta_data(self.image.ident(), *content, None).await?;
writeln!(
result,
"export default {{ src, width: {width}, height: {height} }}",
Expand All @@ -111,6 +111,6 @@ impl Asset for StructuredImageFileSource {
)?;
}
};
Ok(AssetContent::File(FileContent::Content(result.build().into()).cell()).cell())
Ok(AssetContent::File(FileContent::Content(result.build().into()).resolved_cell()).cell())
}
}
Loading

0 comments on commit 70db6f5

Please sign in to comment.