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
…3) (vercel#71665)

Generated using a version of https://github.com/vercel/turbopack-resolved-vc-codemod using Anthropic Claude Sonnet 3.5 (`claude-3-5-sonnet-20241022`) for more complicated errors we don't have hardcoded logic for.

- Part 1: vercel#70927
- Part 2: vercel#71172

I kept running the script until I ran out of tokens (the tier 1 rate limit is 1M input tokens). I'll keep improving how I'm calling the LLM (there's still a lot of room for improvement from my side), and try running it again tomorrow to fix more things.

This fixes 13 struct field types. There are still 271 struct fields remaining that need to be codemodded.
  • Loading branch information
bgw authored and stipsan committed Nov 6, 2024
1 parent a88bd00 commit b7e3b95
Show file tree
Hide file tree
Showing 18 changed files with 61 additions and 54 deletions.
4 changes: 2 additions & 2 deletions crates/next-api/src/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -989,7 +989,7 @@ impl Project {
let FindContextFileResult::Found(fs_path, _) = *middleware.await? else {
return Ok(Vc::upcast(EmptyEndpoint::new()));
};
let source = Vc::upcast(FileSource::new(fs_path));
let source = Vc::upcast(FileSource::new(*fs_path));
let app_dir = *find_app_dir(self.project_path()).await?;
let ecmascript_client_reference_transition_name = (*self.app_project().await?)
.as_ref()
Expand Down Expand Up @@ -1123,7 +1123,7 @@ impl Project {
let FindContextFileResult::Found(fs_path, _) = *instrumentation.await? else {
return Ok(Vc::upcast(EmptyEndpoint::new()));
};
let source = Vc::upcast(FileSource::new(fs_path));
let source = Vc::upcast(FileSource::new(*fs_path));
let app_dir = *find_app_dir(self.project_path()).await?;
let ecmascript_client_reference_transition_name = (*self.app_project().await?)
.as_ref()
Expand Down
5 changes: 3 additions & 2 deletions crates/next-api/src/versioned_content_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,8 @@ impl VersionedContentMap {
};

if let Some(generate_source_map) =
Vc::try_resolve_sidecast::<Box<dyn GenerateSourceMap>>(*asset).await?
Vc::try_resolve_sidecast::<Box<dyn GenerateSourceMap>>(*asset.to_resolved().await?)
.await?
{
Ok(if let Some(section) = section {
generate_source_map.by_section(section)
Expand Down Expand Up @@ -200,7 +201,7 @@ impl VersionedContentMap {
side_effects.await?;

if let Some(asset) = path_to_asset.get(&path) {
return Ok(Vc::cell(Some(*asset)));
return Ok(Vc::cell(Some(asset.to_resolved().await?)));
} else {
let path = path.to_string().await?;
bail!(
Expand Down
2 changes: 1 addition & 1 deletion crates/next-core/src/transform_options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ async fn get_typescript_options(
FindContextFileResult::Found(path, _) => Some(
read_tsconfigs(
path.read(),
Vc::upcast(FileSource::new(path)),
Vc::upcast(FileSource::new(*path)),
node_cjs_resolve_options(path.root()),
)
.await
Expand Down
6 changes: 3 additions & 3 deletions turbopack/crates/turbo-tasks-testing/tests/all_in_one.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ async fn all_in_one() {
let a: Vc<MyTransparentValue> = Vc::cell(4242);
assert_eq!(*a.await?, 4242);

let b = MyEnumValue::cell(MyEnumValue::More(MyEnumValue::Yeah(42).into()));
let b = MyEnumValue::cell(MyEnumValue::More(MyEnumValue::Yeah(42).resolved_cell()));
assert_eq!(*b.to_string().await?, "42");

let c = MyStructValue {
Expand Down Expand Up @@ -71,7 +71,7 @@ struct MyTransparentValue(u32);
enum MyEnumValue {
Yeah(u32),
Nah,
More(Vc<MyEnumValue>),
More(ResolvedVc<MyEnumValue>),
}

#[turbo_tasks::value_impl]
Expand All @@ -80,7 +80,7 @@ impl MyEnumValue {
pub async fn get_last(self: Vc<Self>) -> Result<Vc<Self>> {
let mut current = self;
while let MyEnumValue::More(more) = &*current.await? {
current = *more;
current = **more;
}
Ok(current)
}
Expand Down
12 changes: 6 additions & 6 deletions turbopack/crates/turbo-tasks-testing/tests/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ async fn enum_none_debug() {
#[tokio::test]
async fn enum_transparent_debug() {
run(&REGISTRATION, || async {
let a: Vc<Enum> = Enum::Transparent(Transparent(42).cell()).cell();
let a: Vc<Enum> = Enum::Transparent(Transparent(42).resolved_cell()).cell();
assert_eq!(
format!("{:?}", a.dbg().await?),
r#"Enum :: Transparent(
Expand All @@ -63,7 +63,7 @@ async fn enum_transparent_debug() {
#[tokio::test]
async fn enum_inner_vc_debug() {
run(&REGISTRATION, || async {
let a: Vc<Enum> = Enum::Enum(Enum::None.cell()).cell();
let a: Vc<Enum> = Enum::Enum(Enum::None.resolved_cell()).cell();
assert_eq!(
format!("{:?}", a.dbg().await?),
r#"Enum :: Enum(
Expand Down Expand Up @@ -118,7 +118,7 @@ async fn struct_vec_debug() {
);

let b: Vc<StructWithVec> = StructWithVec {
vec: vec![Transparent(42).cell()],
vec: vec![Transparent(42).resolved_cell()],
}
.cell();
assert_eq!(
Expand Down Expand Up @@ -163,8 +163,8 @@ struct Transparent(u32);
#[turbo_tasks::value(shared)]
enum Enum {
None,
Transparent(Vc<Transparent>),
Enum(Vc<Enum>),
Transparent(ResolvedVc<Transparent>),
Enum(ResolvedVc<Enum>),
}

#[turbo_tasks::value(shared)]
Expand All @@ -182,7 +182,7 @@ struct StructWithOption {

#[turbo_tasks::value(shared)]
struct StructWithVec {
vec: Vec<Vc<Transparent>>,
vec: Vec<ResolvedVc<Transparent>>,
}

#[turbo_tasks::value(shared, eq = "manual")]
Expand Down
8 changes: 4 additions & 4 deletions turbopack/crates/turbopack-browser/src/ecmascript/chunk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@ impl ValueToString for EcmascriptDevChunk {
#[turbo_tasks::value_impl]
impl OutputChunk for EcmascriptDevChunk {
#[turbo_tasks::function]
fn runtime_info(&self) -> Vc<OutputChunkRuntimeInfo> {
OutputChunkRuntimeInfo {
included_ids: Some(self.chunk.entry_ids()),
async fn runtime_info(&self) -> Result<Vc<OutputChunkRuntimeInfo>> {
Ok(OutputChunkRuntimeInfo {
included_ids: Some(self.chunk.entry_ids().to_resolved().await?),
..Default::default()
}
.cell()
.cell())
}
}

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 @@ -14,7 +14,7 @@ use turbopack_resolve::ecmascript::cjs_resolve;
pub enum RuntimeEntry {
Request(ResolvedVc<Request>, ResolvedVc<FileSystemPath>),
Evaluatable(ResolvedVc<Box<dyn EvaluatableAsset>>),
Source(Vc<Box<dyn Source>>),
Source(ResolvedVc<Box<dyn Source>>),
}

#[turbo_tasks::value_impl]
Expand Down
8 changes: 5 additions & 3 deletions turbopack/crates/turbopack-cli/src/dev/web_entry_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,11 @@ pub async fn get_client_runtime_entries(
};

runtime_entries.push(
RuntimeEntry::Source(Vc::upcast(FileSource::new(embed_file_path(
"entry/bootstrap.ts".into(),
))))
RuntimeEntry::Source(ResolvedVc::upcast(
FileSource::new(embed_file_path("entry/bootstrap.ts".into()))
.to_resolved()
.await?,
))
.cell(),
);

Expand Down
14 changes: 7 additions & 7 deletions turbopack/crates/turbopack-core/src/chunk/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,12 @@ pub trait Chunk: Asset {
#[turbo_tasks::value(shared)]
#[derive(Default)]
pub struct OutputChunkRuntimeInfo {
pub included_ids: Option<Vc<ModuleIds>>,
pub included_ids: Option<ResolvedVc<ModuleIds>>,
pub excluded_ids: Option<ResolvedVc<ModuleIds>>,
/// List of paths of chunks containing individual modules that are part of
/// this chunk. This is useful for selectively loading modules from a chunk
/// without loading the whole chunk.
pub module_chunks: Option<Vc<OutputAssets>>,
pub module_chunks: Option<ResolvedVc<OutputAssets>>,
pub placeholder_for_future_extensions: (),
}

Expand Down Expand Up @@ -251,8 +251,8 @@ enum ChunkContentGraphNode {

#[derive(Debug, Clone, Copy, TaskInput, PartialEq, Eq, Hash, Serialize, Deserialize)]
enum ChunkGraphNodeToReferences {
PassthroughChunkItem(Vc<Box<dyn ChunkItem>>),
ChunkItem(Vc<Box<dyn ChunkItem>>),
PassthroughChunkItem(ResolvedVc<Box<dyn ChunkItem>>),
ChunkItem(ResolvedVc<Box<dyn ChunkItem>>),
}

#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq, TraceRawVcs)]
Expand Down Expand Up @@ -483,7 +483,7 @@ async fn graph_node_to_referenced_nodes(
graph_nodes.push(ChunkGraphEdge {
key: None,
node: ChunkContentGraphNode::InheritAsyncInfo {
item: parent,
item: *parent,
references: inherit_async_references,
},
})
Expand Down Expand Up @@ -540,10 +540,10 @@ impl Visit<ChunkContentGraphNode, ()> for ChunkContentVisit {
async move {
let node = match node {
ChunkContentGraphNode::PassthroughChunkItem { item } => {
ChunkGraphNodeToReferences::PassthroughChunkItem(item)
ChunkGraphNodeToReferences::PassthroughChunkItem(item.to_resolved().await?)
}
ChunkContentGraphNode::ChunkItem { item, .. } => {
ChunkGraphNodeToReferences::ChunkItem(item)
ChunkGraphNodeToReferences::ChunkItem(item.to_resolved().await?)
}
_ => {
return Ok(None.into_iter().flatten());
Expand Down
8 changes: 4 additions & 4 deletions turbopack/crates/turbopack-core/src/compile_time_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ pub struct CompileTimeDefines(pub FxIndexMap<Vec<DefineableNameSegment>, Compile
#[turbo_tasks::value(transparent)]
#[derive(Debug, Clone)]
pub struct CompileTimeDefinesIndividual(
pub FxIndexMap<Vec<DefineableNameSegment>, Vc<CompileTimeDefineValue>>,
pub FxIndexMap<Vec<DefineableNameSegment>, ResolvedVc<CompileTimeDefineValue>>,
);

impl IntoIterator for CompileTimeDefines {
Expand All @@ -192,7 +192,7 @@ impl CompileTimeDefines {
Vc::cell(
self.0
.iter()
.map(|(key, value)| (key.clone(), value.clone().cell()))
.map(|(key, value)| (key.clone(), value.clone().resolved_cell()))
.collect(),
)
}
Expand Down Expand Up @@ -241,7 +241,7 @@ pub struct FreeVarReferences(pub FxIndexMap<Vec<DefineableNameSegment>, FreeVarR
#[turbo_tasks::value(transparent)]
#[derive(Debug, Clone)]
pub struct FreeVarReferencesIndividual(
pub FxIndexMap<Vec<DefineableNameSegment>, Vc<FreeVarReference>>,
pub FxIndexMap<Vec<DefineableNameSegment>, ResolvedVc<FreeVarReference>>,
);

#[turbo_tasks::value_impl]
Expand All @@ -256,7 +256,7 @@ impl FreeVarReferences {
Vc::cell(
self.0
.iter()
.map(|(key, value)| (key.clone(), value.clone().cell()))
.map(|(key, value)| (key.clone(), value.clone().resolved_cell()))
.collect(),
)
}
Expand Down
4 changes: 2 additions & 2 deletions turbopack/crates/turbopack-core/src/output.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use anyhow::Result;
use turbo_tasks::{FxIndexSet, Vc};
use turbo_tasks::{FxIndexSet, ResolvedVc, Vc};

use crate::{asset::Asset, ident::AssetIdent};

#[turbo_tasks::value(transparent)]
pub struct OptionOutputAsset(Option<Vc<Box<dyn OutputAsset>>>);
pub struct OptionOutputAsset(Option<ResolvedVc<Box<dyn OutputAsset>>>);

/// An asset that should be outputted, e. g. written to disk or served from a
/// server.
Expand Down
24 changes: 14 additions & 10 deletions turbopack/crates/turbopack-core/src/resolve/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1084,7 +1084,7 @@ async fn imports_field(lookup_path: Vc<FileSystemPath>) -> Result<Vc<ImportsFiel
return Ok(ImportsFieldResult::None.cell());
};

let read = read_package_json(*package_json_path).await?;
let read = read_package_json(**package_json_path).await?;
let package_json = match &*read {
Some(json) => json,
None => return Ok(ImportsFieldResult::None.cell()),
Expand All @@ -1094,10 +1094,10 @@ async fn imports_field(lookup_path: Vc<FileSystemPath>) -> Result<Vc<ImportsFiel
return Ok(ImportsFieldResult::None.cell());
};
match imports.try_into() {
Ok(imports) => 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,
path: **package_json_path,
error_message: err.to_string().into(),
}
.cell()
Expand All @@ -1114,7 +1114,7 @@ pub fn package_json() -> Vc<Vec<RcStr>> {

#[turbo_tasks::value(shared)]
pub enum FindContextFileResult {
Found(Vc<FileSystemPath>, Vec<Vc<Box<dyn Source>>>),
Found(ResolvedVc<FileSystemPath>, Vec<Vc<Box<dyn Source>>>),
NotFound(Vec<Vc<Box<dyn Source>>>),
}

Expand All @@ -1127,7 +1127,7 @@ pub async fn find_context_file(
for name in &*names.await? {
let fs_path = lookup_path.join(name.clone());
if let Some(fs_path) = exists(fs_path, &mut refs).await? {
return Ok(FindContextFileResult::Found(fs_path, refs).into());
return Ok(FindContextFileResult::Found(fs_path.to_resolved().await?, refs).into());
}
}
if lookup_path.await?.is_root() {
Expand Down Expand Up @@ -1167,14 +1167,18 @@ pub async fn find_context_file_or_package_key(
if let Some(package_json_path) = exists(package_json_path, &mut refs).await? {
if let Some(json) = &*read_package_json(package_json_path).await? {
if json.get(&**package_key).is_some() {
return Ok(FindContextFileResult::Found(package_json_path, refs).into());
return Ok(FindContextFileResult::Found(
package_json_path.to_resolved().await?,
refs,
)
.into());
}
}
}
for name in &*names.await? {
let fs_path = lookup_path.join(name.clone());
if let Some(fs_path) = exists(fs_path, &mut refs).await? {
return Ok(FindContextFileResult::Found(fs_path, refs).into());
return Ok(FindContextFileResult::Found(fs_path.to_resolved().await?, refs).into());
}
}
if lookup_path.await?.is_root() {
Expand Down Expand Up @@ -2188,7 +2192,7 @@ async fn apply_in_package(
continue;
};

let read = read_package_json(*package_json_path).await?;
let read = read_package_json(**package_json_path).await?;
let Some(package_json) = &*read else {
continue;
};
Expand Down Expand Up @@ -2248,7 +2252,7 @@ async fn apply_in_package(

ResolvingIssue {
severity: error_severity(options).await?,
file_path: *package_json_path,
file_path: **package_json_path,
request_type: format!("alias field ({field})"),
request: Request::parse(Value::new(Pattern::Constant(request))),
resolve_options: options,
Expand Down Expand Up @@ -2282,7 +2286,7 @@ async fn find_self_reference(
) -> Result<Vc<FindSelfReferencePackageResult>> {
let package_json_context = find_context_file(lookup_path, package_json()).await?;
if let FindContextFileResult::Found(package_json_path, _refs) = &*package_json_context {
let read = read_package_json(*package_json_path).await?;
let read = read_package_json(**package_json_path).await?;
if let Some(json) = &*read {
if json.get("exports").is_some() {
if let Some(name) = json["name"].as_str() {
Expand Down
4 changes: 2 additions & 2 deletions turbopack/crates/turbopack-css/src/chunk/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,8 @@ impl OutputChunk for CssChunk {
.map(|item| Vc::upcast(SingleItemCssChunk::new(self.chunking_context, *item)))
.collect();
Ok(OutputChunkRuntimeInfo {
included_ids: Some(Vc::cell(included_ids)),
module_chunks: Some(Vc::cell(module_chunks)),
included_ids: Some(ResolvedVc::cell(included_ids)),
module_chunks: Some(ResolvedVc::cell(module_chunks)),
..Default::default()
}
.cell())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ pub async fn is_marked_as_side_effect_free(
let find_package_json = find_context_file(path.parent(), package_json()).await?;

if let FindContextFileResult::Found(package_json, _) = *find_package_json {
match *side_effects_from_package_json(package_json).await? {
match *side_effects_from_package_json(*package_json).await? {
SideEffectsValue::None => {}
SideEffectsValue::Constant(side_effects) => return Ok(Vc::cell(!side_effects)),
SideEffectsValue::Glob(glob) => {
Expand Down
4 changes: 2 additions & 2 deletions turbopack/crates/turbopack-ecmascript/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -504,14 +504,14 @@ impl EcmascriptModuleAsset {
Some("commonjs") => SpecifiedModuleType::CommonJs,
_ => SpecifiedModuleType::Automatic,
},
package_json,
*package_json,
));
}
}

Ok(ModuleTypeResult::new_with_package_json(
SpecifiedModuleType::Automatic,
package_json,
*package_json,
))
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ pub(crate) async fn analyse_ecmascript_module_internal(
if analyze_types {
match &*find_context_file(path.parent(), tsconfig()).await? {
FindContextFileResult::Found(tsconfig, _) => {
analysis.add_reference(TsConfigReference::new(origin, *tsconfig));
analysis.add_reference(TsConfigReference::new(origin, **tsconfig));
}
FindContextFileResult::NotFound(_) => {}
};
Expand Down
Loading

0 comments on commit b7e3b95

Please sign in to comment.