Skip to content

Commit

Permalink
codemod(turbopack): Rewrite more Vc fields in structs as ResolvedVc (#…
Browse files Browse the repository at this point in the history
…71172)

**This is a** *(mostly)* **machine-generated PR.**

This uses a much updated version of the ast-grep based codemod script from #70927, which can now match a bunch more patterns.

Codemod scripts: https://github.com/vercel/turbopack-resolved-vc-codemod/tree/90518e64cfaf9328546ff4688692f16f54d4fc3e

I did a small bit of cleanup after running the script with:

```bash
sg -U --pattern '$OBJ.resolve().await?.to_resolved().await?' -r '$OBJ.to_resolved().await?'
```

Notable additions since last time are:
- Runs the compiler/fixer in a loop a few times, as fixing an issue often allows compilation to get further, creating new errors.
- Can apply compiler-suggested edits (usually these are manual derefs needed with `*`)
- Knows how to rewrite `$OBJ.cell()` to `$OBJ.resolved_cell()`
- Knows how to rewrite `Vc::cell($ARG)` to `ResolvedVc::cell($ARG)`
- Knows how to rewrite `$TYPE::cell($ARG)` to `$TYPE::resolved_cell($ARG)`.
- Knows how to add `.to_resolved().await?` to expressions.
  • Loading branch information
bgw authored Oct 22, 2024
1 parent cfd816d commit 9bd38dd
Show file tree
Hide file tree
Showing 56 changed files with 364 additions and 294 deletions.
14 changes: 7 additions & 7 deletions crates/next-api/src/dynamic_imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use tracing::{Instrument, Level};
use turbo_tasks::{
graph::{GraphTraversal, NonDeterministic, VisitControlFlow, VisitedNodes},
trace::TraceRawVcs,
FxIndexMap, RcStr, ReadRef, TryJoinIterExt, Value, ValueToString, Vc,
FxIndexMap, RcStr, ReadRef, ResolvedVc, TryJoinIterExt, Value, ValueToString, Vc,
};
use turbopack_core::{
chunk::{
Expand Down Expand Up @@ -162,7 +162,7 @@ pub(crate) async fn collect_next_dynamic_imports(
.iter()
.map(|module| async move {
Ok(NextDynamicVisitEntry::Module(
module.resolve().await?,
module.to_resolved().await?,
module.ident().to_string().await?,
))
})
Expand Down Expand Up @@ -209,8 +209,8 @@ pub(crate) async fn collect_next_dynamic_imports(

#[derive(Debug, PartialEq, Eq, Hash, Clone, TraceRawVcs, Serialize, Deserialize)]
enum NextDynamicVisitEntry {
Module(Vc<Box<dyn Module>>, ReadRef<RcStr>),
DynamicImportsMap(Vc<DynamicImportsMap>),
Module(ResolvedVc<Box<dyn Module>>, ReadRef<RcStr>),
DynamicImportsMap(ResolvedVc<DynamicImportsMap>),
}

#[turbo_tasks::value(transparent)]
Expand All @@ -227,7 +227,7 @@ async fn get_next_dynamic_edges(
.iter()
.map(|&referenced_module| async move {
Ok(NextDynamicVisitEntry::Module(
referenced_module,
referenced_module.to_resolved().await?,
referenced_module.ident().to_string().await?,
))
})
Expand All @@ -236,7 +236,7 @@ async fn get_next_dynamic_edges(
if let Some(dynamic_imports_map) = *dynamic_imports_map.await? {
edges.reserve_exact(1);
edges.push(NextDynamicVisitEntry::DynamicImportsMap(
dynamic_imports_map,
dynamic_imports_map.to_resolved().await?,
));
}
Ok(Vc::cell(edges))
Expand Down Expand Up @@ -264,7 +264,7 @@ impl turbo_tasks::graph::Visit<NextDynamicVisitEntry> for NextDynamicVisit {
};
let client_asset_context = self.client_asset_context;
async move {
Ok(get_next_dynamic_edges(client_asset_context, module)
Ok(get_next_dynamic_edges(client_asset_context, *module)
.await?
.into_iter()
.cloned())
Expand Down
8 changes: 4 additions & 4 deletions crates/next-core/src/next_app/include_modules_module.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use anyhow::Result;
use turbo_tasks::{RcStr, TryJoinIterExt, ValueToString, Vc};
use turbo_tasks::{RcStr, ResolvedVc, TryJoinIterExt, ValueToString, Vc};
use turbo_tasks_fs::glob::Glob;
use turbopack_core::{
asset::{Asset, AssetContent},
Expand All @@ -19,13 +19,13 @@ use turbopack_ecmascript::chunk::{
#[turbo_tasks::value]
pub struct IncludeModulesModule {
ident: Vc<AssetIdent>,
modules: Vec<Vc<Box<dyn Module>>>,
modules: Vec<ResolvedVc<Box<dyn Module>>>,
}

#[turbo_tasks::value_impl]
impl IncludeModulesModule {
#[turbo_tasks::function]
pub fn new(ident: Vc<AssetIdent>, modules: Vec<Vc<Box<dyn Module>>>) -> Vc<Self> {
pub fn new(ident: Vc<AssetIdent>, modules: Vec<ResolvedVc<Box<dyn Module>>>) -> Vc<Self> {
Self { ident, modules }.cell()
}
}
Expand All @@ -50,7 +50,7 @@ impl Module for IncludeModulesModule {
.iter()
.map(|&module| async move {
Ok(Vc::upcast(
IncludedModuleReference::new(module).resolve().await?,
IncludedModuleReference::new(*module).resolve().await?,
))
})
.try_join()
Expand Down
20 changes: 12 additions & 8 deletions crates/next-core/src/next_client/context.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::iter::once;

use anyhow::Result;
use turbo_tasks::{FxIndexMap, RcStr, Value, Vc};
use turbo_tasks::{FxIndexMap, RcStr, ResolvedVc, Value, Vc};
use turbo_tasks_env::EnvMap;
use turbo_tasks_fs::{FileSystem, FileSystemPath};
use turbopack::{
Expand Down Expand Up @@ -193,16 +193,16 @@ fn internal_assets_conditions() -> ContextCondition {
#[turbo_tasks::function]
pub async fn get_client_module_options_context(
project_path: Vc<FileSystemPath>,
execution_context: Vc<ExecutionContext>,
env: Vc<Environment>,
execution_context: ResolvedVc<ExecutionContext>,
env: ResolvedVc<Environment>,
ty: Value<ClientContextType>,
mode: Vc<NextMode>,
next_config: Vc<NextConfig>,
) -> Result<Vc<ModuleOptionsContext>> {
let next_mode = mode.await?;

let resolve_options_context =
get_client_resolve_options_context(project_path, ty, mode, next_config, execution_context);
get_client_resolve_options_context(project_path, ty, mode, next_config, *execution_context);

let tsconfig = get_typescript_transform_options(project_path);
let decorators_options = get_decorators_transform_options(project_path);
Expand Down Expand Up @@ -312,7 +312,7 @@ pub async fn get_client_module_options_context(
ecmascript: EcmascriptOptionsContext {
enable_jsx: Some(jsx_runtime_options),
enable_typescript_transform: Some(tsconfig),
enable_decorators: Some(decorators_options),
enable_decorators: Some(decorators_options.to_resolved().await?),
..module_options_context.ecmascript.clone()
},
enable_webpack_loaders,
Expand Down Expand Up @@ -408,8 +408,10 @@ pub async fn get_client_runtime_entries(
// because the bootstrap contains JSX which requires Refresh's global
// functions to be available.
if let Some(request) = enable_react_refresh {
runtime_entries
.push(RuntimeEntry::Request(request, project_root.join("_".into())).cell())
runtime_entries.push(
RuntimeEntry::Request(request.to_resolved().await?, project_root.join("_".into()))
.cell(),
)
};
}

Expand All @@ -418,7 +420,9 @@ pub async fn get_client_runtime_entries(
RuntimeEntry::Request(
Request::parse(Value::new(Pattern::Constant(
"next/dist/client/app-next-turbopack.js".into(),
))),
)))
.to_resolved()
.await?,
project_root.join("_".into()),
)
.cell(),
Expand Down
8 changes: 4 additions & 4 deletions crates/next-core/src/next_client/runtime_entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ use turbopack_ecmascript::resolve::cjs_resolve;

#[turbo_tasks::value(shared)]
pub enum RuntimeEntry {
Request(Vc<Request>, Vc<FileSystemPath>),
Evaluatable(Vc<Box<dyn EvaluatableAsset>>),
Request(ResolvedVc<Request>, Vc<FileSystemPath>),
Evaluatable(ResolvedVc<Box<dyn EvaluatableAsset>>),
Source(ResolvedVc<Box<dyn Source>>),
}

Expand All @@ -25,7 +25,7 @@ impl RuntimeEntry {
asset_context: Vc<Box<dyn AssetContext>>,
) -> Result<Vc<EvaluatableAssets>> {
let (request, path) = match *self.await? {
RuntimeEntry::Evaluatable(e) => return Ok(EvaluatableAssets::one(e)),
RuntimeEntry::Evaluatable(e) => return Ok(EvaluatableAssets::one(*e)),
RuntimeEntry::Source(source) => {
return Ok(EvaluatableAssets::one(source.to_evaluatable(asset_context)));
}
Expand All @@ -34,7 +34,7 @@ impl RuntimeEntry {

let modules = cjs_resolve(
Vc::upcast(PlainResolveOrigin::new(asset_context, path)),
request,
*request,
None,
false,
)
Expand Down
8 changes: 4 additions & 4 deletions crates/next-core/src/next_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::collections::HashSet;
use anyhow::{bail, Context, Result};
use serde::{Deserialize, Deserializer, Serialize};
use serde_json::Value as JsonValue;
use turbo_tasks::{trace::TraceRawVcs, FxIndexMap, RcStr, TaskInput, Vc};
use turbo_tasks::{trace::TraceRawVcs, FxIndexMap, RcStr, ResolvedVc, TaskInput, Vc};
use turbo_tasks_env::EnvMap;
use turbo_tasks_fs::FileSystemPath;
use turbopack::module_options::{
Expand Down Expand Up @@ -481,7 +481,7 @@ pub enum ReactCompilerOptionsOrBoolean {
}

#[turbo_tasks::value(transparent)]
pub struct OptionalReactCompilerOptions(Option<Vc<ReactCompilerOptions>>);
pub struct OptionalReactCompilerOptions(Option<ResolvedVc<ReactCompilerOptions>>);

#[turbo_tasks::value(eq = "manual")]
#[derive(Clone, Debug, Default, PartialEq)]
Expand Down Expand Up @@ -1080,11 +1080,11 @@ impl NextConfig {
compilation_mode: None,
panic_threshold: None,
}
.cell(),
.resolved_cell(),
))
}
Some(ReactCompilerOptionsOrBoolean::Option(options)) => OptionalReactCompilerOptions(
Some(ReactCompilerOptions { ..options.clone() }.cell()),
Some(ReactCompilerOptions { ..options.clone() }.resolved_cell()),
),
_ => OptionalReactCompilerOptions(None),
};
Expand Down
4 changes: 2 additions & 2 deletions crates/next-core/src/next_edge/context.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use anyhow::Result;
use turbo_tasks::{FxIndexMap, RcStr, Value, Vc};
use turbo_tasks::{FxIndexMap, RcStr, ResolvedVc, Value, Vc};
use turbo_tasks_env::EnvMap;
use turbo_tasks_fs::FileSystemPath;
use turbopack::resolve_options_context::ResolveOptionsContext;
Expand Down Expand Up @@ -60,7 +60,7 @@ async fn next_edge_defines(define_env: Vc<EnvMap>) -> Result<Vc<CompileTimeDefin
/// See [here](https://github.com/vercel/next.js/blob/160bb99b06e9c049f88e25806fd995f07f4cc7e1/packages/next/src/build/webpack-config.ts#L1715-L1718) how webpack configures it.
#[turbo_tasks::function]
async fn next_edge_free_vars(
project_path: Vc<FileSystemPath>,
project_path: ResolvedVc<FileSystemPath>,
define_env: Vc<EnvMap>,
) -> Result<Vc<FreeVarReferences>> {
Ok(free_var_references!(
Expand Down
18 changes: 9 additions & 9 deletions crates/next-core/src/next_server/context.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::iter::once;

use anyhow::{bail, Result};
use turbo_tasks::{FxIndexMap, RcStr, Value, Vc};
use turbo_tasks::{FxIndexMap, RcStr, ResolvedVc, Value, Vc};
use turbo_tasks_env::{EnvMap, ProcessEnv};
use turbo_tasks_fs::{FileSystem, FileSystemPath};
use turbopack::{
Expand Down Expand Up @@ -179,7 +179,7 @@ pub async fn get_server_resolve_options_context(
let server_external_packages_plugin = ExternalCjsModulesResolvePlugin::new(
project_path,
project_path.root(),
ExternalPredicate::Only(Vc::cell(external_packages)).cell(),
ExternalPredicate::Only(ResolvedVc::cell(external_packages)).cell(),
*next_config.import_externals().await?,
);

Expand All @@ -202,7 +202,7 @@ pub async fn get_server_resolve_options_context(
ExternalCjsModulesResolvePlugin::new(
project_path,
project_path.root(),
ExternalPredicate::AllExcept(Vc::cell(transpiled_packages)).cell(),
ExternalPredicate::AllExcept(ResolvedVc::cell(transpiled_packages)).cell(),
*next_config.import_externals().await?,
)
};
Expand Down Expand Up @@ -375,7 +375,7 @@ fn internal_assets_conditions() -> ContextCondition {
#[turbo_tasks::function]
pub async fn get_server_module_options_context(
project_path: Vc<FileSystemPath>,
execution_context: Vc<ExecutionContext>,
execution_context: ResolvedVc<ExecutionContext>,
ty: Value<ServerContextType>,
mode: Vc<NextMode>,
next_config: Vc<NextConfig>,
Expand Down Expand Up @@ -569,7 +569,7 @@ pub async fn get_server_module_options_context(
ecmascript: EcmascriptOptionsContext {
enable_jsx: Some(jsx_runtime_options),
enable_typescript_transform: Some(tsconfig),
enable_decorators: Some(decorators_options),
enable_decorators: Some(decorators_options.to_resolved().await?),
..module_options_context.ecmascript
},
enable_webpack_loaders,
Expand Down Expand Up @@ -632,7 +632,7 @@ pub async fn get_server_module_options_context(
ecmascript: EcmascriptOptionsContext {
enable_jsx: Some(jsx_runtime_options),
enable_typescript_transform: Some(tsconfig),
enable_decorators: Some(decorators_options),
enable_decorators: Some(decorators_options.to_resolved().await?),
..module_options_context.ecmascript
},
enable_webpack_loaders,
Expand Down Expand Up @@ -706,7 +706,7 @@ pub async fn get_server_module_options_context(
ecmascript: EcmascriptOptionsContext {
enable_jsx: Some(rsc_jsx_runtime_options),
enable_typescript_transform: Some(tsconfig),
enable_decorators: Some(decorators_options),
enable_decorators: Some(decorators_options.to_resolved().await?),
..module_options_context.ecmascript
},
enable_webpack_loaders,
Expand Down Expand Up @@ -779,7 +779,7 @@ pub async fn get_server_module_options_context(
ecmascript: EcmascriptOptionsContext {
enable_jsx: Some(rsc_jsx_runtime_options),
enable_typescript_transform: Some(tsconfig),
enable_decorators: Some(decorators_options),
enable_decorators: Some(decorators_options.to_resolved().await?),
..module_options_context.ecmascript
},
enable_webpack_loaders,
Expand Down Expand Up @@ -869,7 +869,7 @@ pub async fn get_server_module_options_context(
ecmascript: EcmascriptOptionsContext {
enable_jsx: Some(jsx_runtime_options),
enable_typescript_transform: Some(tsconfig),
enable_decorators: Some(decorators_options),
enable_decorators: Some(decorators_options.to_resolved().await?),
..module_options_context.ecmascript
},
enable_webpack_loaders,
Expand Down
10 changes: 5 additions & 5 deletions crates/next-core/src/next_server/resolve.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use anyhow::Result;
use serde::{Deserialize, Serialize};
use turbo_tasks::{trace::TraceRawVcs, RcStr, Value, Vc};
use turbo_tasks::{trace::TraceRawVcs, RcStr, ResolvedVc, Value, Vc};
use turbo_tasks_fs::{self, glob::Glob, FileJsonContent, FileSystemPath};
use turbopack_core::{
issue::{Issue, IssueExt, IssueSeverity, IssueStage, OptionStyledString, StyledString},
Expand All @@ -24,10 +24,10 @@ use turbopack_core::{
pub enum ExternalPredicate {
/// Mark all modules as external if they're not listed in the list.
/// Applies only to imports outside of node_modules.
AllExcept(Vc<Vec<RcStr>>),
AllExcept(ResolvedVc<Vec<RcStr>>),
/// Only mark modules listed as external, whether inside node_modules or
/// not.
Only(Vc<Vec<RcStr>>),
Only(ResolvedVc<Vec<RcStr>>),
}

/// Mark modules as external, so they're resolved at runtime instead of bundled.
Expand Down Expand Up @@ -111,7 +111,7 @@ impl AfterResolvePlugin for ExternalCjsModulesResolvePlugin {
return Ok(ResolveResultOption::none());
}

let exception_glob = packages_glob(*exceptions).await?;
let exception_glob = packages_glob(**exceptions).await?;

if let Some(PackagesGlobs {
path_glob,
Expand All @@ -127,7 +127,7 @@ impl AfterResolvePlugin for ExternalCjsModulesResolvePlugin {
false
}
ExternalPredicate::Only(externals) => {
let external_glob = packages_glob(*externals).await?;
let external_glob = packages_glob(**externals).await?;

if let Some(PackagesGlobs {
path_glob,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ impl EcmascriptChunkPlaceable for NextServerComponentModule {
exports,
star_exports: vec![module_reference],
}
.cell(),
.resolved_cell(),
)
.cell()
}
Expand Down
8 changes: 4 additions & 4 deletions crates/next-core/src/pages_structure.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use anyhow::Result;
use tracing::Instrument;
use turbo_tasks::{RcStr, ValueToString, Vc};
use turbo_tasks::{RcStr, ResolvedVc, ValueToString, Vc};
use turbo_tasks_fs::{
DirectoryContent, DirectoryEntry, FileSystemEntryType, FileSystemPath, FileSystemPathOption,
};
Expand All @@ -12,7 +12,7 @@ use crate::next_import_map::get_next_package;
pub struct PagesStructureItem {
pub base_path: Vc<FileSystemPath>,
pub extensions: Vc<Vec<RcStr>>,
pub fallback_path: Option<Vc<FileSystemPath>>,
pub fallback_path: Option<ResolvedVc<FileSystemPath>>,

/// Pathname of this item in the Next.js router.
pub next_router_path: Vc<FileSystemPath>,
Expand All @@ -29,7 +29,7 @@ impl PagesStructureItem {
fn new(
base_path: Vc<FileSystemPath>,
extensions: Vc<Vec<RcStr>>,
fallback_path: Option<Vc<FileSystemPath>>,
fallback_path: Option<ResolvedVc<FileSystemPath>>,
next_router_path: Vc<FileSystemPath>,
original_path: Vc<FileSystemPath>,
) -> Vc<Self> {
Expand All @@ -53,7 +53,7 @@ impl PagesStructureItem {
}
}
if let Some(fallback_path) = self.fallback_path {
Ok(fallback_path)
Ok(*fallback_path)
} else {
Ok(self.base_path)
}
Expand Down
Loading

0 comments on commit 9bd38dd

Please sign in to comment.