Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Turbopack] Low hanging fixes and improvement on module graph #75106

Merged
merged 12 commits into from
Jan 20, 2025
1 change: 1 addition & 0 deletions crates/next-api/src/pages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,7 @@ impl PagesProject {
false,
None,
)
.await?
.first_module()
.await?
.context("expected Next.js client runtime to resolve to a module")?;
Expand Down
4 changes: 2 additions & 2 deletions turbopack/crates/turbo-tasks/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,8 @@ pub use turbo_tasks_macros::{function, value_impl, value_trait, KeyValuePair, Ta
pub use value::{TransientInstance, TransientValue, Value};
pub use value_type::{TraitMethod, TraitType, ValueType};
pub use vc::{
Dynamic, NonLocalValue, OperationValue, OperationVc, ResolvedVc, TypedForInput, Upcast,
ValueDefault, Vc, VcCast, VcCellNewMode, VcCellSharedMode, VcDefaultRead, VcRead,
Dynamic, NonLocalValue, OperationValue, OperationVc, OptionVcExt, ResolvedVc, TypedForInput,
Upcast, ValueDefault, Vc, VcCast, VcCellNewMode, VcCellSharedMode, VcDefaultRead, VcRead,
VcTransparentRead, VcValueTrait, VcValueTraitCast, VcValueType, VcValueTypeCast,
};

Expand Down
20 changes: 20 additions & 0 deletions turbopack/crates/turbo-tasks/src/vc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -664,3 +664,23 @@ where
T::value_default()
}
}

pub trait OptionVcExt<T>
where
T: VcValueType,
{
fn to_resolved(self) -> impl Future<Output = Result<Option<ResolvedVc<T>>>> + Send;
}

impl<T> OptionVcExt<T> for Option<Vc<T>>
where
T: VcValueType,
{
async fn to_resolved(self) -> Result<Option<ResolvedVc<T>>> {
if let Some(vc) = self {
Ok(Some(vc.to_resolved().await?))
} else {
Ok(None)
}
}
}
1 change: 1 addition & 0 deletions turbopack/crates/turbopack-cli/src/build/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,7 @@ async fn build_internal(
let request = request_vc.await?;
origin
.resolve_asset(request_vc, origin.resolve_options(ty.clone()), ty)
.await?
.first_module()
.await?
.with_context(|| {
Expand Down
1 change: 1 addition & 0 deletions turbopack/crates/turbopack-cli/src/dev/web_entry_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ pub async fn create_web_entry_source(
let ty = Value::new(ReferenceType::Entry(EntryReferenceSubType::Web));
Ok(origin
.resolve_asset(request, origin.resolve_options(ty.clone()), ty)
.await?
.resolve()
.await?
.primary_modules()
Expand Down
18 changes: 9 additions & 9 deletions turbopack/crates/turbopack-core/src/resolve/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ use serde::{Deserialize, Serialize};
use tracing::{Instrument, Level};
use turbo_rcstr::RcStr;
use turbo_tasks::{
fxindexmap, trace::TraceRawVcs, FxIndexMap, FxIndexSet, NonLocalValue, ResolvedVc, TaskInput,
TryJoinIterExt, Value, ValueToString, Vc,
fxindexmap, trace::TraceRawVcs, FxIndexMap, FxIndexSet, NonLocalValue, OptionVcExt, ResolvedVc,
TaskInput, TryJoinIterExt, Value, ValueToString, Vc,
};
use turbo_tasks_fs::{
util::normalize_request, FileSystemEntryType, FileSystemPath, RealPathResult,
Expand Down Expand Up @@ -1539,7 +1539,7 @@ pub async fn url_resolve(
origin: Vc<Box<dyn ResolveOrigin>>,
request: Vc<Request>,
reference_type: Value<ReferenceType>,
issue_source: Option<ResolvedVc<IssueSource>>,
issue_source: Option<Vc<IssueSource>>,
is_optional: bool,
) -> Result<Vc<ModuleResolveResult>> {
let resolve_options = origin.resolve_options(reference_type.clone());
Expand Down Expand Up @@ -2902,7 +2902,7 @@ pub async fn handle_resolve_error(
request: Vc<Request>,
resolve_options: Vc<ResolveOptions>,
is_optional: bool,
source: Option<ResolvedVc<IssueSource>>,
source: Option<Vc<IssueSource>>,
) -> Result<Vc<ModuleResolveResult>> {
async fn is_unresolvable(result: Vc<ModuleResolveResult>) -> Result<bool> {
Ok(*result.resolve().await?.is_unresolvable().await?)
Expand Down Expand Up @@ -2946,7 +2946,7 @@ pub async fn handle_resolve_source_error(
request: Vc<Request>,
resolve_options: Vc<ResolveOptions>,
is_optional: bool,
source: Option<ResolvedVc<IssueSource>>,
source: Option<Vc<IssueSource>>,
) -> Result<Vc<ResolveResult>> {
async fn is_unresolvable(result: Vc<ResolveResult>) -> Result<bool> {
Ok(*result.resolve().await?.is_unresolvable().await?)
Expand Down Expand Up @@ -2990,7 +2990,7 @@ async fn emit_resolve_error_issue(
request: Vc<Request>,
resolve_options: Vc<ResolveOptions>,
err: anyhow::Error,
source: Option<ResolvedVc<IssueSource>>,
source: Option<Vc<IssueSource>>,
) -> Result<()> {
let severity = if is_optional || resolve_options.await?.loose_errors {
IssueSeverity::Warning.resolved_cell()
Expand All @@ -3004,7 +3004,7 @@ async fn emit_resolve_error_issue(
request: request.to_resolved().await?,
resolve_options: resolve_options.to_resolved().await?,
error_message: Some(format!("{}", PrettyPrintError(&err))),
source,
source: source.to_resolved().await?,
}
.resolved_cell()
.emit();
Expand All @@ -3017,7 +3017,7 @@ async fn emit_unresolvable_issue(
reference_type: Value<ReferenceType>,
request: Vc<Request>,
resolve_options: Vc<ResolveOptions>,
source: Option<ResolvedVc<IssueSource>>,
source: Option<Vc<IssueSource>>,
) -> Result<()> {
let severity = if is_optional || resolve_options.await?.loose_errors {
IssueSeverity::Warning.resolved_cell()
Expand All @@ -3031,7 +3031,7 @@ async fn emit_unresolvable_issue(
request: request.to_resolved().await?,
resolve_options: resolve_options.to_resolved().await?,
error_message: None,
source,
source: source.to_resolved().await?,
}
.resolved_cell()
.emit();
Expand Down
11 changes: 6 additions & 5 deletions turbopack/crates/turbopack-core/src/resolve/origin.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::future::Future;

use anyhow::Result;
use turbo_rcstr::RcStr;
use turbo_tasks::{ResolvedVc, Upcast, Value, Vc};
Expand Down Expand Up @@ -39,7 +41,7 @@ pub trait ResolveOriginExt: Send {
request: Vc<Request>,
options: Vc<ResolveOptions>,
reference_type: Value<ReferenceType>,
) -> Vc<ModuleResolveResult>;
) -> impl Future<Output = Result<Vc<ModuleResolveResult>>> + Send;

/// Get the resolve options that apply for this origin.
fn resolve_options(self: Vc<Self>, reference_type: Value<ReferenceType>) -> Vc<ResolveOptions>;
Expand All @@ -57,7 +59,7 @@ where
request: Vc<Request>,
options: Vc<ResolveOptions>,
reference_type: Value<ReferenceType>,
) -> Vc<ModuleResolveResult> {
) -> impl Future<Output = Result<Vc<ModuleResolveResult>>> + Send {
resolve_asset(Vc::upcast(self), request, options, reference_type)
}

Expand All @@ -77,7 +79,6 @@ where
}
}

#[turbo_tasks::function]
async fn resolve_asset(
resolve_origin: Vc<Box<dyn ResolveOrigin>>,
request: Vc<Request>,
Expand All @@ -93,8 +94,8 @@ async fn resolve_asset(
.await?
.resolve_asset(
resolve_origin.origin_path().resolve().await?,
request,
options,
request.resolve().await?,
options.resolve().await?,
reference_type,
))
}
Expand Down
67 changes: 38 additions & 29 deletions turbopack/crates/turbopack-ecmascript/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,36 @@ impl EcmascriptAnalyzable for EcmascriptModuleAsset {
}
}

#[turbo_tasks::function]
async fn determine_module_type_for_directory(
context_path: Vc<FileSystemPath>,
) -> Result<Vc<ModuleTypeResult>> {
let find_package_json =
find_context_file(context_path, package_json().resolve().await?).await?;
let FindContextFileResult::Found(package_json, _) = *find_package_json else {
return Ok(ModuleTypeResult::new(SpecifiedModuleType::Automatic));
};

// analysis.add_reference(PackageJsonReference::new(package_json));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

might be relevant for nft...
Not sure why it's commented out...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But I only moved the code. It was commented out before

if let FileJsonContent::Content(content) = &*package_json.read_json().await? {
if let Some(r#type) = content.get("type") {
return Ok(ModuleTypeResult::new_with_package_json(
match r#type.as_str() {
Some("module") => SpecifiedModuleType::EcmaScript,
Some("commonjs") => SpecifiedModuleType::CommonJs,
_ => SpecifiedModuleType::Automatic,
},
*package_json,
));
}
}

Ok(ModuleTypeResult::new_with_package_json(
SpecifiedModuleType::Automatic,
*package_json,
))
}

#[turbo_tasks::value_impl]
impl EcmascriptModuleAsset {
#[turbo_tasks::function]
Expand Down Expand Up @@ -473,53 +503,32 @@ impl EcmascriptModuleAsset {
pub fn parse(&self) -> Vc<ParseResult> {
parse(*self.source, Value::new(self.ty), *self.transforms)
}
}

#[turbo_tasks::function]
pub(crate) async fn determine_module_type(self: Vc<Self>) -> Result<Vc<ModuleTypeResult>> {
impl EcmascriptModuleAsset {
#[tracing::instrument(level = "trace", skip_all)]
pub(crate) async fn determine_module_type(self: Vc<Self>) -> Result<ReadRef<ModuleTypeResult>> {
let this = self.await?;

match this.options.await?.specified_module_type {
SpecifiedModuleType::EcmaScript => {
return Ok(ModuleTypeResult::new(SpecifiedModuleType::EcmaScript))
return ModuleTypeResult::new(SpecifiedModuleType::EcmaScript).await
}
SpecifiedModuleType::CommonJs => {
return Ok(ModuleTypeResult::new(SpecifiedModuleType::CommonJs))
return ModuleTypeResult::new(SpecifiedModuleType::CommonJs).await
}
SpecifiedModuleType::Automatic => {}
}

let find_package_json = find_context_file(
determine_module_type_for_directory(
self.origin_path()
.resolve()
.await?
.parent()
.resolve()
.await?,
package_json().resolve().await?,
)
.await?;
let FindContextFileResult::Found(package_json, _) = *find_package_json else {
return Ok(ModuleTypeResult::new(SpecifiedModuleType::Automatic));
};

// analysis.add_reference(PackageJsonReference::new(package_json));
if let FileJsonContent::Content(content) = &*package_json.read_json().await? {
if let Some(r#type) = content.get("type") {
return Ok(ModuleTypeResult::new_with_package_json(
match r#type.as_str() {
Some("module") => SpecifiedModuleType::EcmaScript,
Some("commonjs") => SpecifiedModuleType::CommonJs,
_ => SpecifiedModuleType::Automatic,
},
*package_json,
));
}
}

Ok(ModuleTypeResult::new_with_package_json(
SpecifiedModuleType::Automatic,
*package_json,
))
.await
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,7 @@ impl EsmAssetReference {
}
}

#[turbo_tasks::value_impl]
impl EsmAssetReference {
#[turbo_tasks::function]
pub fn new(
origin: ResolvedVc<Box<dyn ResolveOrigin>>,
request: ResolvedVc<Request>,
Expand All @@ -143,7 +141,10 @@ impl EsmAssetReference {
import_externals,
})
}
}

#[turbo_tasks::value_impl]
impl EsmAssetReference {
#[turbo_tasks::function]
pub(crate) fn get_referenced_asset(self: Vc<Self>) -> Vc<ReferencedAsset> {
ReferencedAsset::from_resolve_result(self.resolve_reference())
Expand Down Expand Up @@ -188,7 +189,8 @@ impl ModuleReference for EsmAssetReference {
Value::new(ty),
false,
Some(*self.issue_source),
);
)
.await?;

if let Some(part) = self.export_name {
let part = part.await?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@ pub struct EsmBindings {
pub bindings: Vec<EsmBinding>,
}

#[turbo_tasks::value_impl]
impl EsmBindings {
#[turbo_tasks::function]
pub fn new(bindings: Vec<EsmBinding>) -> Vc<Self> {
EsmBindings { bindings }.cell()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,14 @@ impl EsmAsyncAssetReference {
impl ModuleReference for EsmAsyncAssetReference {
#[turbo_tasks::function]
async fn resolve_reference(&self) -> Result<Vc<ModuleResolveResult>> {
Ok(esm_resolve(
esm_resolve(
self.get_origin().resolve().await?,
*self.request,
Value::new(EcmaScriptModulesReferenceSubType::DynamicImport),
self.in_try,
Some(*self.issue_source),
))
)
.await
}
}

Expand Down Expand Up @@ -126,7 +127,8 @@ impl CodeGenerateable for EsmAsyncAssetReference {
Value::new(EcmaScriptModulesReferenceSubType::DynamicImport),
self.in_try,
Some(*self.issue_source),
),
)
.await?,
if matches!(
*chunking_context.environment().chunk_loading().await?,
ChunkLoading::Edge
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ pub struct EsmModuleItem {
pub path: ResolvedVc<AstPath>,
}

#[turbo_tasks::value_impl]
impl EsmModuleItem {
#[turbo_tasks::function]
pub fn new(path: ResolvedVc<AstPath>) -> Vc<Self> {
Self::cell(EsmModuleItem { path })
}
Expand Down
Loading
Loading