Unverified Commit 72f3bab2 authored by Benjamin Woodruff's avatar Benjamin Woodruff Committed by GitHub

refactor(turbopack-core): Migrate `Sources` type (and related types) to `ResolvedVc` (#72382)

A hand refactor of some struct fields from `Vc` to `ResolvedVc` to unblock local tasks.

Closes PACK-3392
parent 9fdfe2a8
use anyhow::Result; use anyhow::Result;
use indoc::formatdoc; use indoc::formatdoc;
use turbo_tasks::Vc; use turbo_tasks::{ResolvedVc, Vc};
use turbo_tasks_fs::{File, FileSystemPath}; use turbo_tasks_fs::{File, FileSystemPath};
use turbopack_core::{ use turbopack_core::{
asset::AssetContent, asset::AssetContent,
...@@ -62,9 +62,9 @@ impl ImportMappingReplacement for NextEdgeUnsupportedModuleReplacer { ...@@ -62,9 +62,9 @@ impl ImportMappingReplacement for NextEdgeUnsupportedModuleReplacer {
"# "#
}; };
let content = AssetContent::file(File::from(code).into()); let content = AssetContent::file(File::from(code).into());
let source = VirtualSource::new(root_path, content); let source = VirtualSource::new(root_path, content).to_resolved().await?;
return Ok(ImportMapResult::Result( return Ok(ImportMapResult::Result(
ResolveResult::source(Vc::upcast(source)).resolved_cell(), ResolveResult::source(ResolvedVc::upcast(source)).resolved_cell(),
) )
.cell()); .cell());
}; };
......
...@@ -133,11 +133,11 @@ impl NextFontGoogleReplacer { ...@@ -133,11 +133,11 @@ impl NextFontGoogleReplacer {
.into(), .into(),
) )
.cell()), .cell()),
); ).to_resolved().await?;
Ok( Ok(ImportMapResult::Result(
ImportMapResult::Result(ResolveResult::source(Vc::upcast(js_asset)).resolved_cell()) ResolveResult::source(ResolvedVc::upcast(js_asset)).resolved_cell(),
.cell(),
) )
.cell())
} }
} }
...@@ -267,7 +267,7 @@ impl NextFontGoogleCssModuleReplacer { ...@@ -267,7 +267,7 @@ impl NextFontGoogleCssModuleReplacer {
.await?; .await?;
Ok(ImportMapResult::Result( Ok(ImportMapResult::Result(
ResolveResult::source(*ResolvedVc::upcast(css_asset)).resolved_cell(), ResolveResult::source(ResolvedVc::upcast(css_asset)).resolved_cell(),
) )
.cell()) .cell())
} }
...@@ -386,12 +386,14 @@ impl ImportMappingReplacement for NextFontGoogleFontFileReplacer { ...@@ -386,12 +386,14 @@ impl ImportMappingReplacement for NextFontGoogleFontFileReplacer {
let font_source = VirtualSource::new( let font_source = VirtualSource::new(
font_virtual_path, font_virtual_path,
AssetContent::file(FileContent::Content(font.await?.0.as_slice().into()).cell()), AssetContent::file(FileContent::Content(font.await?.0.as_slice().into()).cell()),
); )
.to_resolved()
.await?;
Ok( Ok(ImportMapResult::Result(
ImportMapResult::Result(ResolveResult::source(Vc::upcast(font_source)).resolved_cell()) ResolveResult::source(ResolvedVc::upcast(font_source)).resolved_cell(),
.cell(),
) )
.cell())
} }
} }
......
use anyhow::{bail, Context, Result}; use anyhow::{bail, Context, Result};
use indoc::formatdoc; use indoc::formatdoc;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use turbo_tasks::{RcStr, Value, Vc}; use turbo_tasks::{RcStr, ResolvedVc, Value, Vc};
use turbo_tasks_fs::{ use turbo_tasks_fs::{
glob::Glob, json::parse_json_with_source_context, FileContent, FileSystemPath, glob::Glob, json::parse_json_with_source_context, FileContent, FileSystemPath,
}; };
...@@ -174,10 +174,12 @@ impl BeforeResolvePlugin for NextFontLocalResolvePlugin { ...@@ -174,10 +174,12 @@ impl BeforeResolvePlugin for NextFontLocalResolvePlugin {
.into(), .into(),
), ),
AssetContent::file(FileContent::Content(file_content.into()).into()), AssetContent::file(FileContent::Content(file_content.into()).into()),
); )
.to_resolved()
.await?;
Ok(ResolveResultOption::some( Ok(ResolveResultOption::some(
ResolveResult::source(Vc::upcast(js_asset)).into(), ResolveResult::source(ResolvedVc::upcast(js_asset)).cell(),
)) ))
} }
"@vercel/turbopack-next/internal/font/local/cssmodule.module.css" => { "@vercel/turbopack-next/internal/font/local/cssmodule.module.css" => {
...@@ -202,11 +204,13 @@ impl BeforeResolvePlugin for NextFontLocalResolvePlugin { ...@@ -202,11 +204,13 @@ impl BeforeResolvePlugin for NextFontLocalResolvePlugin {
let css_asset = VirtualSource::new( let css_asset = VirtualSource::new(
css_virtual_path, css_virtual_path,
AssetContent::file(FileContent::Content(stylesheet.into()).into()), AssetContent::file(FileContent::Content(stylesheet.into()).cell()),
); )
.to_resolved()
.await?;
Ok(ResolveResultOption::some( Ok(ResolveResultOption::some(
ResolveResult::source(Vc::upcast(css_asset)).into(), ResolveResult::source(ResolvedVc::upcast(css_asset)).cell(),
)) ))
} }
"@vercel/turbopack-next/internal/font/local/font" => { "@vercel/turbopack-next/internal/font/local/font" => {
...@@ -233,10 +237,12 @@ impl BeforeResolvePlugin for NextFontLocalResolvePlugin { ...@@ -233,10 +237,12 @@ impl BeforeResolvePlugin for NextFontLocalResolvePlugin {
let font_file = lookup_path.join(path.clone()).read(); let font_file = lookup_path.join(path.clone()).read();
let font_source = let font_source =
VirtualSource::new(font_virtual_path, AssetContent::file(font_file)); VirtualSource::new(font_virtual_path, AssetContent::file(font_file))
.to_resolved()
.await?;
Ok(ResolveResultOption::some( Ok(ResolveResultOption::some(
ResolveResult::source(Vc::upcast(font_source)).into(), ResolveResult::source(ResolvedVc::upcast(font_source)).cell(),
)) ))
} }
_ => Ok(ResolveResultOption::none()), _ => Ok(ResolveResultOption::none()),
......
...@@ -2,7 +2,7 @@ use std::collections::HashMap; ...@@ -2,7 +2,7 @@ use std::collections::HashMap;
use anyhow::Result; use anyhow::Result;
use lazy_static::lazy_static; use lazy_static::lazy_static;
use turbo_tasks::{RcStr, Value, Vc}; use turbo_tasks::{RcStr, ResolvedVc, Value, Vc};
use turbo_tasks_fs::{glob::Glob, FileSystemPath}; use turbo_tasks_fs::{glob::Glob, FileSystemPath};
use turbopack_core::{ use turbopack_core::{
diagnostics::DiagnosticExt, diagnostics::DiagnosticExt,
...@@ -319,7 +319,10 @@ impl AfterResolvePlugin for NextNodeSharedRuntimeResolvePlugin { ...@@ -319,7 +319,10 @@ impl AfterResolvePlugin for NextNodeSharedRuntimeResolvePlugin {
.join(format!("{base}/{resource_request}").into()); .join(format!("{base}/{resource_request}").into());
Ok(Vc::cell(Some( Ok(Vc::cell(Some(
ResolveResult::source(Vc::upcast(FileSource::new(new_path))).into(), ResolveResult::source(ResolvedVc::upcast(
FileSource::new(new_path).to_resolved().await?,
))
.cell(),
))) )))
} }
} }
...@@ -418,7 +421,10 @@ impl AfterResolvePlugin for NextSharedRuntimeResolvePlugin { ...@@ -418,7 +421,10 @@ impl AfterResolvePlugin for NextSharedRuntimeResolvePlugin {
let modified_path = raw_fs_path.path.replace("next/dist/esm/", "next/dist/"); let modified_path = raw_fs_path.path.replace("next/dist/esm/", "next/dist/");
let new_path = fs_path.root().join(modified_path.into()); let new_path = fs_path.root().join(modified_path.into());
Ok(Vc::cell(Some( Ok(Vc::cell(Some(
ResolveResult::source(Vc::upcast(FileSource::new(new_path))).into(), ResolveResult::source(ResolvedVc::upcast(
FileSource::new(new_path).to_resolved().await?,
))
.cell(),
))) )))
} }
} }
use anyhow::Result; use anyhow::Result;
use turbo_tasks::Vc; use turbo_tasks::{ResolvedVc, Vc};
use turbo_tasks_fs::{self, FileJsonContent, FileSystemPath}; use turbo_tasks_fs::{self, FileJsonContent, FileSystemPath};
use turbopack::{ use turbopack::{
module_options::{ module_options::{
...@@ -19,20 +19,18 @@ use crate::{mode::NextMode, next_config::NextConfig}; ...@@ -19,20 +19,18 @@ use crate::{mode::NextMode, next_config::NextConfig};
async fn get_typescript_options( async fn get_typescript_options(
project_path: Vc<FileSystemPath>, project_path: Vc<FileSystemPath>,
) -> Option<Vec<(Vc<FileJsonContent>, Vc<Box<dyn Source>>)>> { ) -> Result<Option<Vec<(Vc<FileJsonContent>, ResolvedVc<Box<dyn Source>>)>>> {
let tsconfig = find_context_file(project_path, tsconfig()); let tsconfig = find_context_file(project_path, tsconfig());
match *tsconfig.await.ok()? { Ok(match tsconfig.await.ok().as_deref() {
FindContextFileResult::Found(path, _) => Some( Some(FindContextFileResult::Found(path, _)) => read_tsconfigs(
read_tsconfigs( path.read(),
path.read(), ResolvedVc::upcast(FileSource::new(**path).to_resolved().await?),
Vc::upcast(FileSource::new(*path)), node_cjs_resolve_options(path.root()),
node_cjs_resolve_options(path.root()), )
) .await
.await .ok(),
.ok()?, Some(FindContextFileResult::NotFound(_)) | None => None,
), })
FindContextFileResult::NotFound(_) => None,
}
} }
/// Build the transform options for specifically for the typescript's runtime /// Build the transform options for specifically for the typescript's runtime
...@@ -41,7 +39,7 @@ async fn get_typescript_options( ...@@ -41,7 +39,7 @@ async fn get_typescript_options(
pub async fn get_typescript_transform_options( pub async fn get_typescript_transform_options(
project_path: Vc<FileSystemPath>, project_path: Vc<FileSystemPath>,
) -> Result<Vc<TypescriptTransformOptions>> { ) -> Result<Vc<TypescriptTransformOptions>> {
let tsconfig = get_typescript_options(project_path).await; let tsconfig = get_typescript_options(project_path).await?;
let use_define_for_class_fields = if let Some(tsconfig) = tsconfig { let use_define_for_class_fields = if let Some(tsconfig) = tsconfig {
read_from_tsconfigs(&tsconfig, |json, _| { read_from_tsconfigs(&tsconfig, |json, _| {
...@@ -66,7 +64,7 @@ pub async fn get_typescript_transform_options( ...@@ -66,7 +64,7 @@ pub async fn get_typescript_transform_options(
pub async fn get_decorators_transform_options( pub async fn get_decorators_transform_options(
project_path: Vc<FileSystemPath>, project_path: Vc<FileSystemPath>,
) -> Result<Vc<DecoratorsOptions>> { ) -> Result<Vc<DecoratorsOptions>> {
let tsconfig = get_typescript_options(project_path).await; let tsconfig = get_typescript_options(project_path).await?;
let decorators_transform_options = if let Some(tsconfig) = tsconfig { let decorators_transform_options = if let Some(tsconfig) = tsconfig {
read_from_tsconfigs(&tsconfig, |json, _| { read_from_tsconfigs(&tsconfig, |json, _| {
...@@ -126,7 +124,7 @@ pub async fn get_jsx_transform_options( ...@@ -126,7 +124,7 @@ pub async fn get_jsx_transform_options(
is_rsc_context: bool, is_rsc_context: bool,
next_config: Vc<NextConfig>, next_config: Vc<NextConfig>,
) -> Result<Vc<JsxTransformOptions>> { ) -> Result<Vc<JsxTransformOptions>> {
let tsconfig = get_typescript_options(project_path).await; let tsconfig = get_typescript_options(project_path).await?;
let enable_react_refresh = if let Some(resolve_options_context) = resolve_options_context { let enable_react_refresh = if let Some(resolve_options_context) = resolve_options_context {
assert_can_resolve_react_refresh(project_path, resolve_options_context) assert_can_resolve_react_refresh(project_path, resolve_options_context)
......
...@@ -145,7 +145,7 @@ pub async fn referenced_modules_and_affecting_sources( ...@@ -145,7 +145,7 @@ pub async fn referenced_modules_and_affecting_sources(
.affecting_sources_iter() .affecting_sources_iter()
.map(|source| async move { .map(|source| async move {
Ok(ResolvedVc::upcast( Ok(ResolvedVc::upcast(
RawModule::new(source).to_resolved().await?, RawModule::new(*source).to_resolved().await?,
)) ))
}) })
.try_join() .try_join()
......
use turbo_tasks::Vc; use turbo_tasks::{ResolvedVc, Vc};
use crate::{asset::Asset, ident::AssetIdent}; use crate::{asset::Asset, ident::AssetIdent};
...@@ -12,9 +12,9 @@ pub trait Source: Asset { ...@@ -12,9 +12,9 @@ pub trait Source: Asset {
} }
#[turbo_tasks::value(transparent)] #[turbo_tasks::value(transparent)]
pub struct OptionSource(Option<Vc<Box<dyn Source>>>); pub struct OptionSource(Option<ResolvedVc<Box<dyn Source>>>);
#[turbo_tasks::value(transparent)] #[turbo_tasks::value(transparent)]
pub struct Sources(Vec<Vc<Box<dyn Source>>>); pub struct Sources(Vec<ResolvedVc<Box<dyn Source>>>);
// TODO All Vc::try_resolve_downcast::<Box<dyn Source>> calls should be removed // TODO All Vc::try_resolve_downcast::<Box<dyn Source>> calls should be removed
...@@ -3067,7 +3067,7 @@ async fn resolve_as_webpack_runtime( ...@@ -3067,7 +3067,7 @@ async fn resolve_as_webpack_runtime(
); );
if let Some(source) = *resolved.first_source().await? { if let Some(source) = *resolved.first_source().await? {
Ok(webpack_runtime(source, transforms)) Ok(webpack_runtime(*source, transforms))
} else { } else {
Ok(WebpackRuntime::None.into()) Ok(WebpackRuntime::None.into())
} }
......
...@@ -124,7 +124,9 @@ async fn resolve_reference_from_dir( ...@@ -124,7 +124,9 @@ async fn resolve_reference_from_dir(
PatternMatch::File(matched_path, file) => { PatternMatch::File(matched_path, file) => {
let realpath = file.realpath_with_links().await?; let realpath = file.realpath_with_links().await?;
for &symlink in &realpath.symlinks { for &symlink in &realpath.symlinks {
affecting_sources.push(Vc::upcast(FileSource::new(*symlink))); affecting_sources.push(ResolvedVc::upcast(
FileSource::new(*symlink).to_resolved().await?,
));
} }
results.push(( results.push((
RequestKey::new(matched_path.clone()), RequestKey::new(matched_path.clone()),
......
...@@ -25,14 +25,17 @@ use turbopack_resolve::{ ...@@ -25,14 +25,17 @@ use turbopack_resolve::{
#[turbo_tasks::value] #[turbo_tasks::value]
pub struct TsConfigModuleAsset { pub struct TsConfigModuleAsset {
pub source: Vc<Box<dyn Source>>, pub origin: ResolvedVc<Box<dyn ResolveOrigin>>,
pub origin: Vc<Box<dyn ResolveOrigin>>, pub source: ResolvedVc<Box<dyn Source>>,
} }
#[turbo_tasks::value_impl] #[turbo_tasks::value_impl]
impl TsConfigModuleAsset { impl TsConfigModuleAsset {
#[turbo_tasks::function] #[turbo_tasks::function]
pub fn new(origin: Vc<Box<dyn ResolveOrigin>>, source: Vc<Box<dyn Source>>) -> Vc<Self> { pub fn new(
origin: ResolvedVc<Box<dyn ResolveOrigin>>,
source: ResolvedVc<Box<dyn Source>>,
) -> Vc<Self> {
Self::cell(TsConfigModuleAsset { origin, source }) Self::cell(TsConfigModuleAsset { origin, source })
} }
} }
...@@ -56,7 +59,7 @@ impl Module for TsConfigModuleAsset { ...@@ -56,7 +59,7 @@ impl Module for TsConfigModuleAsset {
) )
.await?; .await?;
for (_, config_asset) in configs[1..].iter() { for (_, config_asset) in configs[1..].iter() {
references.push(Vc::upcast(TsExtendsReference::new(*config_asset))); references.push(Vc::upcast(TsExtendsReference::new(**config_asset)));
} }
// ts-node options // ts-node options
{ {
...@@ -71,7 +74,7 @@ impl Module for TsConfigModuleAsset { ...@@ -71,7 +74,7 @@ impl Module for TsConfigModuleAsset {
.unwrap_or_else(|| "typescript".to_string()) .unwrap_or_else(|| "typescript".to_string())
.into(); .into();
references.push(Vc::upcast(CompilerReference::new( references.push(Vc::upcast(CompilerReference::new(
self.origin, *self.origin,
Request::parse(Value::new(compiler.into())), Request::parse(Value::new(compiler.into())),
))); )));
let require = read_from_tsconfigs(&configs, |json, source| { let require = read_from_tsconfigs(&configs, |json, source| {
...@@ -90,7 +93,7 @@ impl Module for TsConfigModuleAsset { ...@@ -90,7 +93,7 @@ impl Module for TsConfigModuleAsset {
if let Some(require) = require { if let Some(require) = require {
for (_, request) in require { for (_, request) in require {
references.push(Vc::upcast(TsNodeRequireReference::new( references.push(Vc::upcast(TsNodeRequireReference::new(
self.origin, *self.origin,
Request::parse(Value::new(request.into())), Request::parse(Value::new(request.into())),
))); )));
} }
...@@ -140,7 +143,7 @@ impl Module for TsConfigModuleAsset { ...@@ -140,7 +143,7 @@ impl Module for TsConfigModuleAsset {
}; };
for (_, name) in types { for (_, name) in types {
references.push(Vc::upcast(TsConfigTypesReference::new( references.push(Vc::upcast(TsConfigTypesReference::new(
self.origin, *self.origin,
Request::module( Request::module(
name, name,
Value::new(RcStr::default().into()), Value::new(RcStr::default().into()),
......
...@@ -180,7 +180,7 @@ impl ModuleReference for WebpackRuntimeAssetReference { ...@@ -180,7 +180,7 @@ impl ModuleReference for WebpackRuntimeAssetReference {
.await? .await?
.map_module(|source| async move { .map_module(|source| async move {
Ok(ModuleResolveResultItem::Module(ResolvedVc::upcast( Ok(ModuleResolveResultItem::Module(ResolvedVc::upcast(
WebpackModuleAsset::new(source, self.runtime, self.transforms) WebpackModuleAsset::new(*source, self.runtime, self.transforms)
.to_resolved() .to_resolved()
.await?, .await?,
))) )))
......
...@@ -209,8 +209,11 @@ pub async fn resolve_node_pre_gyp_files( ...@@ -209,8 +209,11 @@ pub async fn resolve_node_pre_gyp_files(
.await?, .await?,
affecting_paths affecting_paths
.into_iter() .into_iter()
.map(|p| Vc::upcast(FileSource::new(p))) .map(|p| async move {
.collect(), anyhow::Ok(ResolvedVc::upcast(FileSource::new(p).to_resolved().await?))
})
.try_join()
.await?,
) )
.cell()); .cell());
} }
......
...@@ -42,14 +42,14 @@ async fn json_only(resolve_options: Vc<ResolveOptions>) -> Result<Vc<ResolveOpti ...@@ -42,14 +42,14 @@ async fn json_only(resolve_options: Vc<ResolveOptions>) -> Result<Vc<ResolveOpti
Ok(opts.cell()) Ok(opts.cell())
} }
type TsConfigs = Vec<(Vc<FileJsonContent>, Vc<Box<dyn Source>>)>; type TsConfig = (Vc<FileJsonContent>, ResolvedVc<Box<dyn Source>>);
#[tracing::instrument(skip_all)] #[tracing::instrument(skip_all)]
pub async fn read_tsconfigs( pub async fn read_tsconfigs(
mut data: Vc<FileContent>, mut data: Vc<FileContent>,
mut tsconfig: Vc<Box<dyn Source>>, mut tsconfig: ResolvedVc<Box<dyn Source>>,
resolve_options: Vc<ResolveOptions>, resolve_options: Vc<ResolveOptions>,
) -> Result<TsConfigs> { ) -> Result<Vec<TsConfig>> {
let mut configs = Vec::new(); let mut configs = Vec::new();
let resolve_options = json_only(resolve_options); let resolve_options = json_only(resolve_options);
loop { loop {
...@@ -90,7 +90,7 @@ pub async fn read_tsconfigs( ...@@ -90,7 +90,7 @@ pub async fn read_tsconfigs(
FileJsonContent::Content(json) => { FileJsonContent::Content(json) => {
configs.push((parsed_data, tsconfig)); configs.push((parsed_data, tsconfig));
if let Some(extends) = json["extends"].as_str() { if let Some(extends) = json["extends"].as_str() {
let resolved = resolve_extends(tsconfig, extends, resolve_options).await?; let resolved = resolve_extends(*tsconfig, extends, resolve_options).await?;
if let Some(source) = *resolved.await? { if let Some(source) = *resolved.await? {
data = source.content().file_content(); data = source.content().file_content();
tsconfig = source; tsconfig = source;
...@@ -200,11 +200,9 @@ async fn resolve_extends_rooted_or_relative( ...@@ -200,11 +200,9 @@ async fn resolve_extends_rooted_or_relative(
Ok(result) Ok(result)
} }
type Config = (Vc<FileJsonContent>, Vc<Box<dyn Source>>);
pub async fn read_from_tsconfigs<T>( pub async fn read_from_tsconfigs<T>(
configs: &[Config], configs: &[TsConfig],
accessor: impl Fn(&JsonValue, Vc<Box<dyn Source>>) -> Option<T>, accessor: impl Fn(&JsonValue, ResolvedVc<Box<dyn Source>>) -> Option<T>,
) -> Result<Option<T>> { ) -> Result<Option<T>> {
for (config, source) in configs.iter() { for (config, source) in configs.iter() {
if let FileJsonContent::Content(json) = &*config.await? { if let FileJsonContent::Content(json) = &*config.await? {
...@@ -240,7 +238,7 @@ pub async fn tsconfig_resolve_options( ...@@ -240,7 +238,7 @@ pub async fn tsconfig_resolve_options(
) -> Result<Vc<TsConfigResolveOptions>> { ) -> Result<Vc<TsConfigResolveOptions>> {
let configs = read_tsconfigs( let configs = read_tsconfigs(
tsconfig.read(), tsconfig.read(),
Vc::upcast(FileSource::new(tsconfig)), ResolvedVc::upcast(FileSource::new(tsconfig).to_resolved().await?),
node_cjs_resolve_options(tsconfig.root()), node_cjs_resolve_options(tsconfig.root()),
) )
.await?; .await?;
......
...@@ -729,9 +729,9 @@ impl AssetContext for ModuleAssetContext { ...@@ -729,9 +729,9 @@ impl AssetContext for ModuleAssetContext {
let reference_type = reference_type.clone(); let reference_type = reference_type.clone();
async move { async move {
let process_result = if let Some(transition) = transition { let process_result = if let Some(transition) = transition {
transition.process(source, self, reference_type) transition.process(*source, self, reference_type)
} else { } else {
self.process_with_transition_rules(source, reference_type) self.process_with_transition_rules(*source, reference_type)
}; };
Ok(match *process_result.await? { Ok(match *process_result.await? {
ProcessResult::Module(m) => { ProcessResult::Module(m) => {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment