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
…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
  • Loading branch information
bgw authored Oct 27, 2024
1 parent 0f43e4e commit ea7319c
Show file tree
Hide file tree
Showing 22 changed files with 209 additions and 160 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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?;
Expand Down
24 changes: 14 additions & 10 deletions turbopack/crates/node-file-trace/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -565,11 +565,11 @@ async fn main_operation(

#[turbo_tasks::function]
async fn create_module_asset(
root: Vc<FileSystemPath>,
root: ResolvedVc<FileSystemPath>,
process_cwd: Option<RcStr>,
module_options: TransientInstance<ModuleOptionsContext>,
resolve_options: TransientInstance<ResolveOptionsContext>,
) -> Vc<ModuleAssetContext> {
) -> Result<Vc<ModuleAssetContext>> {
let env = Environment::new(Value::new(ExecutionEnvironment::NodeJsLambda(
NodeJsEnvironment {
cwd: Vc::cell(process_cwd),
Expand All @@ -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);
Expand All @@ -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() {
Expand Down
4 changes: 2 additions & 2 deletions turbopack/crates/turbopack-browser/src/chunking_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ impl ChunkingContext for BrowserChunkingContext {

let mut assets: Vec<Vc<Box<dyn OutputAsset>>> = chunks
.iter()
.map(|chunk| self.generate_chunk(*chunk))
.map(|chunk| self.generate_chunk(**chunk))
.collect();

if this.enable_hot_module_replacement {
Expand Down Expand Up @@ -455,7 +455,7 @@ impl ChunkingContext for BrowserChunkingContext {

let mut assets: Vec<Vc<Box<dyn OutputAsset>>> = chunks
.iter()
.map(|chunk| self.generate_chunk(*chunk))
.map(|chunk| self.generate_chunk(**chunk))
.collect();

let other_assets = Vc::cell(assets.clone());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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?;
Expand All @@ -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);
}
Expand Down
14 changes: 9 additions & 5 deletions turbopack/crates/turbopack-browser/src/ecmascript/update.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -11,9 +11,9 @@ pub(super) enum EcmascriptChunkUpdate {
}

pub(super) struct EcmascriptChunkPartialUpdate {
pub added: FxIndexMap<ReadRef<ModuleId>, (u64, Vc<Code>)>,
pub added: FxIndexMap<ReadRef<ModuleId>, (u64, ResolvedVc<Code>)>,
pub deleted: FxIndexMap<ReadRef<ModuleId>, u64>,
pub modified: FxIndexMap<ReadRef<ModuleId>, Vc<Code>>,
pub modified: FxIndexMap<ReadRef<ModuleId>, ResolvedVc<Code>>,
}

pub(super) async fn update_ecmascript_chunk(
Expand All @@ -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);
Expand All @@ -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?),
);
}
}

Expand Down
2 changes: 1 addition & 1 deletion turbopack/crates/turbopack-cli-utils/src/runtime_entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ impl RuntimeEntry {
}

#[turbo_tasks::value(transparent)]
pub struct RuntimeEntries(Vec<Vc<RuntimeEntry>>);
pub struct RuntimeEntries(Vec<ResolvedVc<RuntimeEntry>>);

#[turbo_tasks::value_impl]
impl RuntimeEntries {
Expand Down
2 changes: 1 addition & 1 deletion turbopack/crates/turbopack-cli/src/build/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ impl TurbopackBuildBuilder {
self.entry_requests
.iter()
.cloned()
.map(EntryRequest::cell)
.map(EntryRequest::resolved_cell)
.collect(),
)
.cell(),
Expand Down
4 changes: 2 additions & 2 deletions turbopack/crates/turbopack-cli/src/dev/web_entry_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
)
};

Expand All @@ -82,7 +82,7 @@ pub async fn get_client_runtime_entries(
.to_resolved()
.await?,
))
.cell(),
.resolved_cell(),
);

Ok(Vc::cell(runtime_entries))
Expand Down
4 changes: 2 additions & 2 deletions turbopack/crates/turbopack-cli/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Vc<EntryRequest>>);
pub struct EntryRequests(pub Vec<ResolvedVc<EntryRequest>>);

#[turbo_tasks::value(shared)]
#[derive(Clone)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ pub struct AvailableChunkItemInfo {
pub struct OptionAvailableChunkItemInfo(Option<AvailableChunkItemInfo>);

#[turbo_tasks::value(transparent)]
pub struct AvailableChunkItemInfoMap(FxIndexMap<Vc<Box<dyn ChunkItem>>, AvailableChunkItemInfo>);
pub struct AvailableChunkItemInfoMap(
FxIndexMap<ResolvedVc<Box<dyn ChunkItem>>, AvailableChunkItemInfo>,
);

/// Allows to gather information about which assets are already available.
/// Adding more roots will form a linked list like structure to allow caching
Expand All @@ -41,23 +43,23 @@ impl AvailableChunkItems {

#[turbo_tasks::function]
pub async fn with_chunk_items(
self: Vc<Self>,
self: ResolvedVc<Self>,
chunk_items: Vc<AvailableChunkItemInfoMap>,
) -> Result<Vc<Self>> {
let chunk_items = chunk_items
.await?
.into_iter()
.map(|(&chunk_item, &info)| async move {
Ok(self
.get(chunk_item)
.get(*chunk_item)
.await?
.is_none()
.then_some((chunk_item, info)))
})
.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())
Expand Down Expand Up @@ -87,13 +89,13 @@ impl AvailableChunkItems {
#[turbo_tasks::function]
pub async fn get(
&self,
chunk_item: Vc<Box<dyn ChunkItem>>,
chunk_item: ResolvedVc<Box<dyn ChunkItem>>,
) -> Result<Vc<OptionAvailableChunkItemInfo>> {
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))
}
Expand Down
27 changes: 19 additions & 8 deletions turbopack/crates/turbopack-core/src/chunk/chunk_group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -12,7 +14,7 @@ use super::{
use crate::{module::Module, output::OutputAssets, reference::ModuleReference};

pub struct MakeChunkGroupResult {
pub chunks: Vec<Vc<Box<dyn Chunk>>>,
pub chunks: Vec<ResolvedVc<Box<dyn Chunk>>>,
pub availability_info: AvailabilityInfo,
}

Expand Down Expand Up @@ -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?
Expand All @@ -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::<Vec<_>>();
let has_async_loaders = !async_loaders.is_empty();
Expand Down Expand Up @@ -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,
})
}
Expand Down
Loading

0 comments on commit ea7319c

Please sign in to comment.