Unverified Commit c5f0da7b authored by Luke Sandberg's avatar Luke Sandberg Committed by GitHub

[turbopack] Remove turbotask functions from `trait ResolveOrigin` (#94324)

### What?

Removes the `#[turbo_tasks::function]` annotations from the
`ResolveOrigin` trait methods (`origin_path`, `asset_context`,
`resolve_options`), turning them into plain synchronous trait methods.
Callers obtain the value once via `Vc::into_trait_ref().await?` and then
call the methods directly on the resulting `TraitRef`.

This mirrors the earlier de-functionification of the `Issue` trait
(#92623).

### Why?

The `ResolveOrigin` implementations are all trivial — they return a
stored field or delegate a single call.

| Task (misses, before → after) | Misses removed |
| --- | --- |
| `ResolveOrigin::resolve_options` (dyn) | 92,608 → 0 |
| `EcmascriptModuleAsset::origin_path` | 64,779 → 0 |
| `ResolveOriginWithTransition::asset_context` | 52,292 → 0 |
| `ResolveOriginWithTransition::origin_path` | 52,292 → 0 |
| `EcmascriptModuleAsset::asset_context` | 41,164 → 0 |
| `PlainResolveOrigin::{origin_path, asset_context}` | 105 + 105 → 0 |
| `EcmascriptCssModule::{origin_path, asset_context}` | 32 + 32 → 0 |
| `CssModule::{origin_path, asset_context}` | 19 + 19 → 0 |

Net effect in that run: **~303k fewer cache misses** (total misses
6,197,068 → 5,893,545) — The ~1.35M eliminated cached-task *hits*
additionally remove the per-call task lookup/scheduling overhead.

The caching that genuinely matters is preserved one layer down —
`AssetContext::resolve_options` (which `ResolveOrigin::resolve_options`
delegated to) is still a cached task; its miss count is unchanged
(39,668) since those distinct results were always computed, and it
simply absorbs the call volume directly now (hit rate 57% → 92%).

## Build Performance: `ResolveOrigin` optimization

Benchmark across 10 runs per branch.

| Metric | Base | This branch | Δ |
|---|---|---|---|
| **Wall time** | 43.302 s | 42.692 s | **−0.61 s (−1.4%)** |
| **User time** | 314.249 s | 310.919 s | **−3.33 s (−1.1%)** |
| **Sys time** | 84.225 s | 84.618 s | +0.39 s (+0.5%) |
| **MaxRSS** | 14106.1 MB | 13883.4 MB | **−222.8 MB (−1.6%)** |

### Notes on significance

- **User time** is the cleanest signal: −1.1% with very tight variance
(base σ 0.4%, branch σ 0.3%). The drop (~3.3 s) is well outside the
noise — a real reduction in CPU work.
- **MaxRSS** is down a solid 1.6% (~223 MB) with tight variance on both
sides (σ ≈ 0.5–0.6%) — a clear, reproducible memory win.
- **Wall time** improves 1.4%, but variance is large (base σ 6.6%,
branch σ 4.5%, with one 50 s outlier in the base set), so treat this as
directional. It tracks the user-time improvement.
- **Sys time** is essentially flat (+0.5%, within its ~10% run-to-run
noise) — no meaningful change, as expected for a CPU/allocation
optimization.

### Invalidation tradeoff

The main behavioral risk is invalidation granularity. Removing the
per-method tasks removes their invalidation boundary: a consumer that
previously depended only on the `origin_path()` (or `asset_context()`)
task output now reads the value off the origin directly, so it is tied
to the origin cell rather than to that one method's cached output.

In practice this is narrow, because every `ResolveOrigin` implementor is
an immutable, content-addressed `#[turbo_tasks::value]` (only
`ResolvedVc`/owned fields) — a different field value means a different
cell, so "reading the whole value vs. one field" does not by itself
widen invalidation.

<!-- NEXT_JS_LLM_PR -->
parent 74bb0d9a
# yaml-language-server: $schema=https://raw.githubusercontent.com/ast-grep/ast-grep/main/schemas/rule.json
id: resolved-vc-in-return-type
message: Returning a `ResolvedVc` in a turbo task is not recommended. Consider replacing `$TYPE` in `$FN_NAME` with `$RESOLVED_VC` instead.
message: Returning a `ResolvedVc` in a turbo task is not recommended. Consider replacing `ResolvedVc<$A>` in `$FN_NAME` with `Vc<$A>` instead.
severity: warning # error, warning, info, hint
language: Rust
rule:
any:
# turbo tasks function
- all:
- pattern:
context: 'let x: ResolvedVc<$A> = 1;'
selector: generic_type
- pattern: $TYPE
inside:
kind: function_item
field: return_type
stopBy: end
follows:
pattern: '#[turbo_tasks::function]'
has:
kind: identifier
pattern: $FN_NAME
# turbo tasks value trait
- all:
- pattern:
context: 'let x: ResolvedVc<$A> = 1;'
selector: generic_type
- pattern: $TYPE
inside:
kind: function_signature_item
field: return_type
stopBy: end
inside:
inside:
kind: trait_item
follows:
pattern: '#[turbo_tasks::value_trait]'
has:
kind: identifier
pattern: $FN_NAME
rewriters:
- id: rewrite-vc
rule:
pattern: $TYPE
fix: Vc<$A>
transform:
RESOLVED_VC:
rewrite:
rewriters: [rewrite-vc]
source: $TYPE
fix: $RESOLVED_VC
# The `ResolvedVc<$A>` generic type itself, captured as $TYPE.
pattern:
context: 'let x: ResolvedVc<$A> = 1;'
selector: generic_type
# ...appearing in the return type of a `#[turbo_tasks::function]`.
inside:
kind: function_item
field: return_type
stopBy: end
follows:
pattern: '#[turbo_tasks::function]'
stopBy: end
has:
kind: identifier
pattern: $FN_NAME
fix: Vc<$A>
......@@ -5,7 +5,6 @@ use turbo_rcstr::RcStr;
use turbo_tasks::{ResolvedVc, Vc, trace::TraceRawVcs};
use turbo_tasks_fs::FileSystemPath;
use turbopack::module_options::ModuleRule;
use turbopack_core::{context::AssetContext, resolve::origin::ResolveOrigin};
use turbopack_ecmascript::{CustomTransformer, TransformPlugin};
use crate::next_config::NextConfig;
......@@ -46,33 +45,6 @@ pub async fn get_swc_ecma_transform_plugin_rule(
}
}
/// A resolve origin without any asset_context, intended for handle_resolve_error
#[turbo_tasks::value]
pub struct DummyResolveOrigin {
origin_path: FileSystemPath,
}
#[turbo_tasks::value_impl]
impl DummyResolveOrigin {
#[turbo_tasks::function]
pub fn new(origin_path: FileSystemPath) -> Vc<Self> {
DummyResolveOrigin { origin_path }.cell()
}
}
#[turbo_tasks::value_impl]
impl ResolveOrigin for DummyResolveOrigin {
#[turbo_tasks::function]
fn origin_path(&self) -> Vc<FileSystemPath> {
self.origin_path.clone().cell()
}
#[turbo_tasks::function]
fn asset_context(&self) -> Result<Vc<Box<dyn AssetContext>>> {
anyhow::bail!("DummyResolveOrigin has no asset context");
}
}
pub async fn get_swc_ecma_transform_rule_impl(
project_path: FileSystemPath,
plugin_configs: &[(RcStr, serde_json::Value)],
......@@ -126,7 +98,7 @@ pub async fn get_swc_ecma_transform_rule_impl(
.as_raw_module_result(),
ReferenceType::CommonJs(CommonJsReferenceSubType::Undefined),
// TODO proper error location
Vc::upcast(DummyResolveOrigin::new(project_path.clone())),
project_path.clone(),
request,
resolve_options,
ResolveErrorMode::Error,
......
......@@ -28,6 +28,7 @@ use turbopack_core::{
ChunkingConfig, ChunkingContext, ChunkingContextExt, ContentHashing, EvaluatableAsset,
MangleType, MinifyType, SourceMapsType, availability_info::AvailabilityInfo,
},
context::AssetContext,
environment::{BrowserEnvironment, Environment, ExecutionEnvironment, NodeJsEnvironment},
ident::AssetIdent,
issue::{IssueReporter, IssueSeverity, handle_issues},
......@@ -40,7 +41,7 @@ use turbopack_core::{
output::{OutputAsset, OutputAssets, OutputAssetsWithReferenced},
reference_type::{EntryReferenceSubType, ReferenceType},
resolve::{
origin::{PlainResolveOrigin, ResolveOrigin, ResolveOriginExt},
origin::{PlainResolveOrigin, ResolveOrigin},
parse::Request,
},
};
......@@ -283,27 +284,33 @@ async fn build_internal(
.await?)
.to_vec();
let origin = PlainResolveOrigin::new(asset_context, project_fs.root().await?.join("_")?);
let origin =
PlainResolveOrigin::new(asset_context, project_fs.root().await?.join("_")?).await?;
let resolve_options = origin.resolve_options();
let asset_context = origin.asset_context();
let origin_path = origin.origin_path();
let project_dir = &project_dir;
let entries = async move {
entry_requests
.into_iter()
.map(|request_vc| async move {
let ty = ReferenceType::Entry(EntryReferenceSubType::Undefined);
let request = request_vc.await?;
origin
.resolve_asset(request_vc, origin.resolve_options(), ty)
.await?
.await?
.first_module()
.await?
.with_context(|| {
format!(
"Unable to resolve entry {} from directory {}.",
request.request().unwrap(),
project_dir
)
})
.map(|request_vc| {
let origin_path = origin_path.clone();
async move {
let ty = ReferenceType::Entry(EntryReferenceSubType::Undefined);
let request = request_vc.await?;
asset_context
.resolve_asset(origin_path, request_vc, resolve_options, ty)
.await?
.first_module()
.await?
.with_context(|| {
format!(
"Unable to resolve entry {} from directory {}.",
request.request().unwrap(),
project_dir
)
})
}
})
.try_join()
.await
......
......@@ -9,6 +9,7 @@ use turbopack_core::{
chunk::{
ChunkableModule, ChunkingContext, EvaluatableAsset, SourceMapSourceType, SourceMapsType,
},
context::AssetContext,
environment::Environment,
file_source::FileSource,
module::Module,
......@@ -17,7 +18,7 @@ use turbopack_core::{
},
reference_type::{EntryReferenceSubType, ReferenceType},
resolve::{
origin::{PlainResolveOrigin, ResolveOrigin, ResolveOriginExt},
origin::{PlainResolveOrigin, ResolveOrigin},
parse::Request,
},
};
......@@ -135,17 +136,22 @@ pub async fn create_web_entry_source(
let runtime_entries = entries.resolve_entries(asset_context);
let origin = PlainResolveOrigin::new(asset_context, root_path.join("_")?);
let origin = PlainResolveOrigin::new(asset_context, root_path.join("_")?).await?;
let resolve_options = origin.resolve_options();
let asset_context = origin.asset_context();
let origin_path = origin.origin_path();
let entries = entry_requests
.into_iter()
.map(|request| async move {
let ty = ReferenceType::Entry(EntryReferenceSubType::Web);
origin
.resolve_asset(request, origin.resolve_options(), ty)
.await?
.await?
.first_module()
.await
.map(|request| {
let origin_path = origin_path.clone();
async move {
let ty = ReferenceType::Entry(EntryReferenceSubType::Web);
asset_context
.resolve_asset(origin_path, request, resolve_options, ty)
.await?
.first_module()
.await
}
})
.try_flat_join()
.await?;
......
......@@ -12,14 +12,14 @@ use crate::{
reference_type::ReferenceType,
resolve::{
ModuleResolveResult, ResolveErrorMode, ResolveResult, options::ResolveOptions,
origin::ResolveOrigin, parse::Request,
parse::Request,
},
};
pub async fn handle_resolve_error(
result: Vc<ModuleResolveResult>,
reference_type: ReferenceType,
origin: Vc<Box<dyn ResolveOrigin>>,
origin_path: FileSystemPath,
request: Vc<Request>,
resolve_options: Vc<ResolveOptions>,
error_mode: ResolveErrorMode,
......@@ -31,7 +31,7 @@ pub async fn handle_resolve_error(
if result_ref.is_unresolvable() {
emit_unresolvable_issue(
error_mode,
origin,
&origin_path,
reference_type,
request,
resolve_options,
......@@ -40,14 +40,14 @@ pub async fn handle_resolve_error(
.await?;
}
handle_item_issues(result_ref.errors(), origin, source).await?;
handle_item_issues(result_ref.errors(), &origin_path, source).await?;
result
}
Err(err) => {
emit_resolve_error_issue(
error_mode,
origin,
&origin_path,
reference_type,
request,
resolve_options,
......@@ -63,7 +63,7 @@ pub async fn handle_resolve_error(
pub async fn handle_resolve_source_error(
result: Vc<ResolveResult>,
reference_type: ReferenceType,
origin: Vc<Box<dyn ResolveOrigin>>,
origin_path: FileSystemPath,
request: Vc<Request>,
resolve_options: Vc<ResolveOptions>,
error_mode: ResolveErrorMode,
......@@ -74,7 +74,7 @@ pub async fn handle_resolve_source_error(
if result_ref.is_unresolvable() {
emit_unresolvable_issue(
error_mode,
origin,
&origin_path,
reference_type,
request,
resolve_options,
......@@ -83,14 +83,14 @@ pub async fn handle_resolve_source_error(
.await?;
}
handle_item_issues(result_ref.errors(), origin, source).await?;
handle_item_issues(result_ref.errors(), &origin_path, source).await?;
result
}
Err(err) => {
emit_resolve_error_issue(
error_mode,
origin,
&origin_path,
reference_type,
request,
resolve_options,
......@@ -105,12 +105,11 @@ pub async fn handle_resolve_source_error(
async fn handle_item_issues(
items: impl Iterator<Item = ResolvedVc<Box<dyn Issue>>>,
origin: Vc<Box<dyn ResolveOrigin>>,
origin_path: &FileSystemPath,
source: Option<IssueSource>,
) -> Result<()> {
let mut items = items.peekable();
if items.peek().is_some() {
let file_path = origin.origin_path().owned().await?;
for item in items {
let trait_ref = item.into_trait_ref().await?;
ResolvingIssueWithLocation {
......@@ -118,7 +117,7 @@ async fn handle_item_issues(
severity: trait_ref.severity(),
stage: trait_ref.stage(),
documentation_link: trait_ref.documentation_link(),
file_path: file_path.clone(),
file_path: origin_path.clone(),
source,
}
.resolved_cell()
......@@ -130,7 +129,7 @@ async fn handle_item_issues(
async fn emit_resolve_error_issue(
error_mode: ResolveErrorMode,
origin: Vc<Box<dyn ResolveOrigin>>,
origin_path: &FileSystemPath,
reference_type: ReferenceType,
request: Vc<Request>,
resolve_options: Vc<ResolveOptions>,
......@@ -147,7 +146,7 @@ async fn emit_resolve_error_issue(
};
ResolvingIssue {
severity,
file_path: origin.origin_path().owned().await?,
file_path: origin_path.clone(),
request_type: format!("{reference_type} request"),
request: request.to_resolved().await?,
resolve_options: resolve_options.to_resolved().await?,
......@@ -161,8 +160,7 @@ async fn emit_resolve_error_issue(
async fn emit_unresolvable_issue(
error_mode: ResolveErrorMode,
origin: Vc<Box<dyn ResolveOrigin>>,
origin_path: &FileSystemPath,
reference_type: ReferenceType,
request: Vc<Request>,
resolve_options: Vc<ResolveOptions>,
......@@ -178,7 +176,7 @@ async fn emit_unresolvable_issue(
};
ResolvingIssue {
severity,
file_path: origin.origin_path().owned().await?,
file_path: origin_path.clone(),
request_type: format!("{reference_type} request"),
request: request.to_resolved().await?,
resolve_options: resolve_options.to_resolved().await?,
......
......@@ -1666,9 +1666,11 @@ pub async fn url_resolve(
issue_source: Option<IssueSource>,
error_mode: ResolveErrorMode,
) -> Result<Vc<ModuleResolveResult>> {
let resolve_options = origin.resolve_options();
let origin_ref = origin.into_trait_ref().await?;
let resolve_options = origin_ref.resolve_options();
let rel_request = request.as_relative();
let origin_path_parent = origin.origin_path().await?.parent();
let origin_path = origin_ref.origin_path();
let origin_path_parent = origin_path.parent();
let rel_result = resolve(
origin_path_parent.clone(),
reference_type.clone(),
......@@ -1697,13 +1699,13 @@ pub async fn url_resolve(
} else {
rel_result
};
let result = origin
let result = origin_ref
.asset_context()
.process_resolve_result(result, reference_type.clone());
handle_resolve_error(
result,
reference_type,
origin,
origin_path,
*request,
resolve_options,
error_mode,
......
......@@ -16,20 +16,15 @@ pub trait ResolveOrigin {
/// since that might be needed to infer custom resolving options for that
/// specific file. But usually only the directory is relevant for the real
/// resolving.
#[turbo_tasks::function]
fn origin_path(self: Vc<Self>) -> Vc<FileSystemPath>;
fn origin_path(&self) -> FileSystemPath;
/// The AssetContext that carries the configuration for building that
/// subgraph.
#[turbo_tasks::function]
fn asset_context(self: Vc<Self>) -> Vc<Box<dyn AssetContext>>;
fn asset_context(&self) -> ResolvedVc<Box<dyn AssetContext>>;
/// Get the resolve options that apply for this origin.
#[turbo_tasks::function]
async fn resolve_options(self: Vc<Self>) -> Result<Vc<ResolveOptions>> {
Ok(self
.asset_context()
.resolve_options(self.origin_path().owned().await?))
fn resolve_options(&self) -> Vc<ResolveOptions> {
self.asset_context().resolve_options(self.origin_path())
}
}
......@@ -47,7 +42,10 @@ pub trait ResolveOriginExt: Send {
) -> impl Future<Output = Result<Vc<ModuleResolveResult>>> + Send;
/// Adds a transition that is used for resolved assets.
fn with_transition(self: ResolvedVc<Self>, transition: RcStr) -> Vc<Box<dyn ResolveOrigin>>;
fn with_transition(
self: ResolvedVc<Self>,
transition: RcStr,
) -> impl Future<Output = Result<Vc<Box<dyn ResolveOrigin>>>> + Send;
}
impl<T> ResolveOriginExt for T
......@@ -60,22 +58,35 @@ where
options: Vc<ResolveOptions>,
reference_type: ReferenceType,
) -> Result<Vc<ModuleResolveResult>> {
Ok(self.asset_context().to_resolved().await?.resolve_asset(
self.origin_path().owned().await?,
let origin = Vc::upcast_non_strict::<Box<dyn ResolveOrigin>>(self)
.into_trait_ref()
.await?;
Ok(origin.asset_context().resolve_asset(
origin.origin_path(),
*request.to_resolved().await?,
*options.to_resolved().await?,
reference_type,
))
}
fn with_transition(self: ResolvedVc<Self>, transition: RcStr) -> Vc<Box<dyn ResolveOrigin>> {
Vc::upcast(
async fn with_transition(
self: ResolvedVc<Self>,
transition: RcStr,
) -> Result<Vc<Box<dyn ResolveOrigin>>> {
let origin = Vc::upcast_non_strict::<Box<dyn ResolveOrigin>>(*self)
.into_trait_ref()
.await?;
Ok(Vc::upcast(
ResolveOriginWithTransition {
previous: ResolvedVc::upcast_non_strict(self),
transition,
origin_path: origin.origin_path(),
asset_context: origin
.asset_context()
.with_transition(transition)
.to_resolved()
.await?,
}
.cell(),
)
))
}
}
......@@ -103,35 +114,33 @@ impl PlainResolveOrigin {
#[turbo_tasks::value_impl]
impl ResolveOrigin for PlainResolveOrigin {
#[turbo_tasks::function]
fn origin_path(&self) -> Vc<FileSystemPath> {
self.origin_path.clone().cell()
fn origin_path(&self) -> FileSystemPath {
self.origin_path.clone()
}
#[turbo_tasks::function]
fn asset_context(&self) -> Vc<Box<dyn AssetContext>> {
*self.asset_context
fn asset_context(&self) -> ResolvedVc<Box<dyn AssetContext>> {
self.asset_context
}
}
/// Wraps a ResolveOrigin to add a transition.
///
/// The transition is applied to the wrapped origin's [`AssetContext`] eagerly at
/// construction (see [`ResolveOriginExt::with_transition`]) so that the trait
/// methods can be synchronous.
#[turbo_tasks::value]
struct ResolveOriginWithTransition {
previous: ResolvedVc<Box<dyn ResolveOrigin>>,
transition: RcStr,
origin_path: FileSystemPath,
asset_context: ResolvedVc<Box<dyn AssetContext>>,
}
#[turbo_tasks::value_impl]
impl ResolveOrigin for ResolveOriginWithTransition {
#[turbo_tasks::function]
fn origin_path(&self) -> Vc<FileSystemPath> {
self.previous.origin_path()
fn origin_path(&self) -> FileSystemPath {
self.origin_path.clone()
}
#[turbo_tasks::function]
fn asset_context(&self) -> Vc<Box<dyn AssetContext>> {
self.previous
.asset_context()
.with_transition(self.transition.clone())
fn asset_context(&self) -> ResolvedVc<Box<dyn AssetContext>> {
self.asset_context
}
}
......@@ -43,28 +43,31 @@ pub struct CssModule {
ty: CssModuleType,
environment: Option<ResolvedVc<Environment>>,
lightningcss_features: LightningCssFeatureFlags,
/// The path of `source`, precomputed so that `ResolveOrigin::origin_path` is synchronous.
origin_path: FileSystemPath,
}
#[turbo_tasks::value_impl]
impl CssModule {
/// Creates a new CSS asset.
#[turbo_tasks::function]
pub fn new(
pub async fn new(
source: ResolvedVc<Box<dyn Source>>,
asset_context: ResolvedVc<Box<dyn AssetContext>>,
ty: CssModuleType,
import_context: Option<ResolvedVc<ImportContext>>,
environment: Option<ResolvedVc<Environment>>,
lightningcss_features: LightningCssFeatureFlags,
) -> Vc<Self> {
Self::cell(CssModule {
) -> Result<Vc<Self>> {
Ok(Self::cell(CssModule {
origin_path: source.ident().await?.path.clone(),
source,
asset_context,
import_context,
ty,
environment,
lightningcss_features,
})
}))
}
/// Returns the asset ident of the source without the "css" modifier
......@@ -202,14 +205,12 @@ impl CssChunkPlaceable for CssModule {}
#[turbo_tasks::value_impl]
impl ResolveOrigin for CssModule {
#[turbo_tasks::function]
async fn origin_path(&self) -> Result<Vc<FileSystemPath>> {
Ok(self.source.ident().await?.path.clone().cell())
fn origin_path(&self) -> FileSystemPath {
self.origin_path.clone()
}
#[turbo_tasks::function]
fn asset_context(&self) -> Vc<Box<dyn AssetContext>> {
*self.asset_context
fn asset_context(&self) -> ResolvedVc<Box<dyn AssetContext>> {
self.asset_context
}
}
......
......@@ -41,19 +41,22 @@ use crate::{
pub struct EcmascriptCssModule {
pub source: ResolvedVc<Box<dyn Source>>,
pub asset_context: ResolvedVc<Box<dyn AssetContext>>,
/// The path of `source`, precomputed so that `ResolveOrigin::origin_path` is synchronous.
origin_path: FileSystemPath,
}
#[turbo_tasks::value_impl]
impl EcmascriptCssModule {
#[turbo_tasks::function]
pub fn new(
pub async fn new(
source: ResolvedVc<Box<dyn Source>>,
asset_context: ResolvedVc<Box<dyn AssetContext>>,
) -> Vc<Self> {
Self::cell(EcmascriptCssModule {
) -> Result<Vc<Self>> {
Ok(Self::cell(EcmascriptCssModule {
origin_path: source.ident().await?.path.clone(),
source,
asset_context,
})
}))
}
}
......@@ -357,14 +360,12 @@ impl EcmascriptChunkPlaceable for EcmascriptCssModule {
#[turbo_tasks::value_impl]
impl ResolveOrigin for EcmascriptCssModule {
#[turbo_tasks::function]
async fn origin_path(&self) -> Result<Vc<FileSystemPath>> {
Ok(self.source.ident().await?.path.clone().cell())
fn origin_path(&self) -> FileSystemPath {
self.origin_path.clone()
}
#[turbo_tasks::function]
fn asset_context(&self) -> Vc<Box<dyn AssetContext>> {
*self.asset_context
fn asset_context(&self) -> ResolvedVc<Box<dyn AssetContext>> {
self.asset_context
}
}
......
......@@ -47,7 +47,7 @@ impl ModuleReference for CssModuleComposeReference {
);
let resolved = result.await?.first_module().await?;
let file_path = self.origin.origin_path().to_resolved().await?;
let file_path = self.origin.into_trait_ref().await?.origin_path();
if let Some(module) = resolved {
if ResolvedVc::try_downcast_type::<EcmascriptCssModule>(module).is_none() {
CssModuleComposesIssue {
......@@ -92,7 +92,7 @@ impl ModuleReference for CssModuleComposeReference {
#[turbo_tasks::value(shared)]
struct CssModuleComposesIssue {
severity: IssueSeverity,
file_path: ResolvedVc<FileSystemPath>,
file_path: FileSystemPath,
message: RcStr,
}
......@@ -114,7 +114,7 @@ impl Issue for CssModuleComposesIssue {
}
async fn file_path(&self) -> Result<FileSystemPath> {
self.file_path.owned().await
Ok(self.file_path.clone())
}
async fn description(&self) -> Result<Option<StyledString>> {
......
......@@ -426,6 +426,8 @@ pub struct EcmascriptModuleAsset {
pub compile_time_info: ResolvedVc<CompileTimeInfo>,
pub side_effect_free_packages: Option<ResolvedVc<Glob>>,
pub inner_assets: Option<ResolvedVc<InnerAssets>>,
/// The path of `source`, precomputed so that `ResolveOrigin::origin_path` is synchronous.
origin_path: FileSystemPath,
}
#[turbo_tasks::value_trait]
......@@ -704,7 +706,7 @@ async fn determine_module_type_for_directory(
#[turbo_tasks::value_impl]
impl EcmascriptModuleAsset {
#[turbo_tasks::function]
fn new(
async fn new(
source: ResolvedVc<Box<dyn Source>>,
asset_context: ResolvedVc<Box<dyn AssetContext>>,
ty: EcmascriptModuleAssetType,
......@@ -712,8 +714,9 @@ impl EcmascriptModuleAsset {
options: ResolvedVc<EcmascriptOptions>,
compile_time_info: ResolvedVc<CompileTimeInfo>,
side_effect_free_packages: Option<ResolvedVc<Glob>>,
) -> Vc<Self> {
Self::cell(EcmascriptModuleAsset {
) -> Result<Vc<Self>> {
Ok(Self::cell(EcmascriptModuleAsset {
origin_path: source.ident().await?.path.clone(),
source,
asset_context,
ty,
......@@ -722,7 +725,7 @@ impl EcmascriptModuleAsset {
compile_time_info,
side_effect_free_packages,
inner_assets: None,
})
}))
}
#[turbo_tasks::function]
......@@ -748,6 +751,7 @@ impl EcmascriptModuleAsset {
))
} else {
Ok(Self::cell(EcmascriptModuleAsset {
origin_path: source.ident().await?.path.clone(),
source,
asset_context,
ty,
......@@ -810,7 +814,7 @@ impl EcmascriptModuleAsset {
SpecifiedModuleType::Automatic => {}
}
determine_module_type_for_directory(self.origin_path().await?.parent()).await
determine_module_type_for_directory(this.origin_path.parent()).await
}
}
......@@ -953,14 +957,12 @@ impl EvaluatableAsset for EcmascriptModuleAsset {}
#[turbo_tasks::value_impl]
impl ResolveOrigin for EcmascriptModuleAsset {
#[turbo_tasks::function]
async fn origin_path(&self) -> Result<Vc<FileSystemPath>> {
Ok(self.source.ident().await?.path.clone().cell())
fn origin_path(&self) -> FileSystemPath {
self.origin_path.clone()
}
#[turbo_tasks::function]
fn asset_context(&self) -> Vc<Box<dyn AssetContext>> {
*self.asset_context
fn asset_context(&self) -> ResolvedVc<Box<dyn AssetContext>> {
self.asset_context
}
}
......
......@@ -39,7 +39,6 @@ use crate::{
pub struct EsmAsyncAssetReference {
pub origin: ResolvedVc<Box<dyn ResolveOrigin>>,
pub request: ResolvedVc<Request>,
pub annotations: ImportAnnotations,
pub issue_source: IssueSource,
pub error_mode: ResolveErrorMode,
pub import_externals: bool,
......@@ -51,17 +50,8 @@ pub struct EsmAsyncAssetReference {
}
impl EsmAsyncAssetReference {
fn get_origin(&self) -> Vc<Box<dyn ResolveOrigin>> {
if let Some(transition) = self.annotations.transition() {
self.origin.with_transition(transition.into())
} else {
*self.origin
}
}
}
impl EsmAsyncAssetReference {
pub fn new(
#[allow(clippy::too_many_arguments)]
pub async fn new(
origin: ResolvedVc<Box<dyn ResolveOrigin>>,
request: ResolvedVc<Request>,
issue_source: IssueSource,
......@@ -70,17 +60,27 @@ impl EsmAsyncAssetReference {
import_externals: bool,
export_usage: ExportUsage,
resolve_override: Option<ResolvedVc<Box<dyn Module>>>,
) -> Self {
EsmAsyncAssetReference {
) -> Result<Self> {
// Apply any annotation-driven transition eagerly so the stored origin is final and the
// `annotations` don't need to be retained on the reference.
let origin = if let Some(transition) = annotations.transition() {
origin
.with_transition(transition.into())
.await?
.to_resolved()
.await?
} else {
origin
};
Ok(EsmAsyncAssetReference {
origin,
request,
issue_source,
annotations,
error_mode,
import_externals,
export_usage,
resolve_override,
}
})
}
}
......@@ -93,7 +93,7 @@ impl ModuleReference for EsmAsyncAssetReference {
}
esm_resolve(
*self.get_origin().to_resolved().await?,
*self.origin,
*self.request,
EcmaScriptModulesReferenceSubType::DynamicImport,
self.error_mode,
......
......@@ -142,6 +142,7 @@ pub async fn compute_ecmascript_module_exports(
options.tree_shaking_mode,
resolve_override,
)
.await?
.resolved_cell();
import_references.push(reference);
......
......@@ -307,10 +307,11 @@ impl Module for CachedExternalModule {
.await?
}
CachedExternalType::Global | CachedExternalType::Script => {
let resolve_options = origin.into_trait_ref().await?.resolve_options();
origin
.resolve_asset(
Request::parse_string(self.request.clone()),
origin.resolve_options(),
resolve_options,
ReferenceType::Undefined,
)
.await?
......
......@@ -413,7 +413,7 @@ impl ImportMetaGlobMap {
issue_source: Option<IssueSource>,
error_mode: ResolveErrorMode,
) -> Result<Vc<Self>> {
let origin_path = origin.origin_path().await?.parent();
let origin_path = origin.into_trait_ref().await?.origin_path().parent();
// Use read_glob for efficient directory-pruning file discovery.
let glob_result = base_dir.read_glob(positive_glob).await?;
......@@ -594,7 +594,7 @@ impl ImportMetaGlobAsset {
#[turbo_tasks::function]
pub async fn map(&self) -> Result<Vc<ImportMetaGlobMap>> {
let origin = *self.origin;
let origin_dir = origin.origin_path().await?.parent();
let origin_dir = origin.into_trait_ref().await?.origin_path().parent();
// Compute the base directory for glob scanning.
// With `base`, patterns are resolved relative to origin + base.
......@@ -662,7 +662,7 @@ impl ImportMetaGlobAsset {
impl Module for ImportMetaGlobAsset {
#[turbo_tasks::function]
async fn ident(&self) -> Result<Vc<AssetIdent>> {
let origin_path = self.origin.origin_path().owned().await?;
let origin_path = self.origin.into_trait_ref().await?.origin_path();
Ok(AssetIdent::from_path(origin_path)
.with_modifier(modifier(
&self.patterns,
......
......@@ -354,7 +354,7 @@ async fn to_single_pattern_mapping(
.into(),
)
.resolved_cell(),
path: origin.origin_path().owned().await?,
path: origin.into_trait_ref().await?.origin_path(),
source: None,
}
.resolved_cell()
......@@ -386,7 +386,7 @@ async fn to_single_pattern_mapping(
"asset is not placeable in ESM chunks, so it doesn't have a module id"
))
.resolved_cell(),
path: origin.origin_path().owned().await?,
path: origin.into_trait_ref().await?.origin_path(),
source: None,
}
.resolved_cell()
......
......@@ -186,7 +186,7 @@ impl RequireContextMap {
issue_source: Option<IssueSource>,
error_mode: ResolveErrorMode,
) -> Result<Vc<Self>> {
let origin_path = origin.origin_path().await?.parent();
let origin_path = origin.into_trait_ref().await?.origin_path().parent();
let list = &*FlatDirList::read(dir, recursive, filter).await?;
......@@ -260,7 +260,12 @@ impl RequireContextAssetReference {
) -> Result<Self> {
let map = RequireContextMap::generate(
*origin,
origin.origin_path().await?.parent().join(&dir)?,
origin
.into_trait_ref()
.await?
.origin_path()
.parent()
.join(&dir)?,
include_subdirs,
filter,
issue_source,
......
......@@ -71,16 +71,10 @@ impl TsReferencePathAssetReference {
impl ModuleReference for TsReferencePathAssetReference {
#[turbo_tasks::function]
async fn resolve_reference(&self) -> Result<Vc<ModuleResolveResult>> {
let origin = self.origin.into_trait_ref().await?;
Ok(
if let Some(path) = self
.origin
.origin_path()
.await?
.parent()
.try_join(&self.path)
{
let module = self
.origin
if let Some(path) = origin.origin_path().parent().try_join(&self.path) {
let module = origin
.asset_context()
.process(
Vc::upcast(FileSource::new(path.clone())),
......
......@@ -116,7 +116,8 @@ impl WorkerAssetReference {
impl ModuleReference for WorkerAssetReference {
#[turbo_tasks::function]
async fn resolve_reference(&self) -> Result<Vc<ModuleResolveResult>> {
let asset_context = self.origin.asset_context().to_resolved().await?;
let origin = self.origin.into_trait_ref().await?;
let asset_context = origin.asset_context();
let result = match (&self.worker_type, &self.request) {
(WorkerType::WebWorker | WorkerType::SharedWebWorker, WorkerRequest::Url(request)) => {
......@@ -151,9 +152,9 @@ impl ModuleReference for WorkerAssetReference {
handle_resolve_error(
result,
reference_type.clone(),
*self.origin,
origin.origin_path(),
Request::parse(path.owned().await?),
self.origin.resolve_options(),
origin.resolve_options(),
self.error_mode,
Some(self.issue_source),
)
......@@ -195,7 +196,7 @@ impl ModuleReference for WorkerAssetReference {
.await?,
)
.resolved_cell(),
path: self.origin.origin_path().owned().await?,
path: origin.origin_path(),
source: Some(self.issue_source),
}
.resolved_cell()
......@@ -224,7 +225,7 @@ impl ModuleReference for WorkerAssetReference {
.await?,
)
.resolved_cell(),
path: self.origin.origin_path().owned().await?,
path: origin.origin_path(),
source: Some(self.issue_source),
}
.resolved_cell()
......@@ -273,7 +274,13 @@ impl WorkerAssetReference {
async fn get_module_type_issue_severity(&self) -> Result<IssueSeverity> {
Ok(
if self.error_mode != ResolveErrorMode::Error
|| self.origin.resolve_options().await?.loose_errors
|| self
.origin
.into_trait_ref()
.await?
.resolve_options()
.await?
.loose_errors
{
IssueSeverity::Warning
} else {
......
......@@ -56,7 +56,7 @@ impl Module for TsConfigModuleAsset {
let configs = read_tsconfigs(
self.source.content().file_content(),
self.source,
apply_cjs_specific_options(self.origin.resolve_options()),
apply_cjs_specific_options(self.origin.into_trait_ref().await?.resolve_options()),
)
.await?;
references.extend(
......
......@@ -193,12 +193,13 @@ pub struct WebpackRuntimeAssetReference {
impl ModuleReference for WebpackRuntimeAssetReference {
#[turbo_tasks::function]
async fn resolve_reference(&self) -> Result<Vc<ModuleResolveResult>> {
let options = self.origin.resolve_options();
let origin = self.origin.into_trait_ref().await?;
let options = origin.resolve_options();
let options = apply_cjs_specific_options(options);
let resolved = resolve(
self.origin.origin_path().await?.parent(),
origin.origin_path().parent(),
ReferenceType::CommonJs(CommonJsReferenceSubType::Undefined),
*self.request,
options,
......
use anyhow::Result;
use turbo_rcstr::rcstr;
use turbo_tasks::{ResolvedVc, Vc};
use turbo_tasks::{ResolvedVc, TraitRef, Vc};
use turbopack_core::{
context::AssetContext,
issue::IssueSource,
reference_type::{CommonJsReferenceSubType, EcmaScriptModulesReferenceSubType, ReferenceType},
resolve::{
......@@ -11,7 +12,7 @@ use turbopack_core::{
ConditionValue, ResolutionConditions, ResolveInPackage, ResolveIntoPackage,
ResolveOptions,
},
origin::{ResolveOrigin, ResolveOriginExt},
origin::ResolveOrigin,
parse::Request,
resolve,
},
......@@ -101,10 +102,11 @@ pub async fn esm_resolve(
issue_source: Option<IssueSource>,
) -> Result<Vc<ModuleResolveResult>> {
let ty = ReferenceType::EcmaScriptModules(ty);
let options = *apply_esm_specific_options(origin.resolve_options(), &ty)
let origin_ref = origin.into_trait_ref().await?;
let options = *apply_esm_specific_options(origin_ref.resolve_options(), &ty)
.to_resolved()
.await?;
specific_resolve(origin, request, options, ty, error_mode, issue_source).await
specific_resolve(origin_ref, request, options, ty, error_mode, issue_source).await
}
#[turbo_tasks::function]
......@@ -116,10 +118,11 @@ pub async fn cjs_resolve(
error_mode: ResolveErrorMode,
) -> Result<Vc<ModuleResolveResult>> {
let ty = ReferenceType::CommonJs(ty);
let options = *apply_cjs_specific_options(origin.resolve_options())
let origin_ref = origin.into_trait_ref().await?;
let options = *apply_cjs_specific_options(origin_ref.resolve_options())
.to_resolved()
.await?;
specific_resolve(origin, request, options, ty, error_mode, issue_source).await
specific_resolve(origin_ref, request, options, ty, error_mode, issue_source).await
}
#[turbo_tasks::function]
......@@ -131,20 +134,17 @@ pub async fn cjs_resolve_source(
error_mode: ResolveErrorMode,
) -> Result<Vc<ResolveResult>> {
let ty = ReferenceType::CommonJs(ty);
let options = *apply_cjs_specific_options(origin.resolve_options())
let origin_ref = origin.into_trait_ref().await?;
let options = *apply_cjs_specific_options(origin_ref.resolve_options())
.to_resolved()
.await?;
let result = resolve(
origin.origin_path().await?.parent(),
ty.clone(),
*request,
options,
);
let origin_path = origin_ref.origin_path();
let result = resolve(origin_path.parent(), ty.clone(), *request, options);
handle_resolve_source_error(
result,
ty,
*origin,
origin_path,
*request,
options,
error_mode,
......@@ -154,21 +154,24 @@ pub async fn cjs_resolve_source(
}
async fn specific_resolve(
origin: Vc<Box<dyn ResolveOrigin>>,
origin: TraitRef<Box<dyn ResolveOrigin>>,
request: Vc<Request>,
options: Vc<ResolveOptions>,
reference_type: ReferenceType,
error_mode: ResolveErrorMode,
issue_source: Option<IssueSource>,
) -> Result<Vc<ModuleResolveResult>> {
let result = origin
.resolve_asset(request, options, reference_type.clone())
.await?;
let result = origin.asset_context().resolve_asset(
origin.origin_path(),
request,
options,
reference_type.clone(),
);
handle_resolve_error(
result,
reference_type,
origin,
origin.origin_path(),
request,
options,
error_mode,
......
......@@ -394,8 +394,9 @@ pub async fn type_resolve(
request: Vc<Request>,
) -> Result<Vc<ModuleResolveResult>> {
let ty = ReferenceType::TypeScript(TypeScriptReferenceSubType::Undefined);
let context_path = origin.origin_path().await?.parent();
let options = origin.resolve_options();
let origin_ref = origin.into_trait_ref().await?;
let context_path = origin_ref.origin_path().parent();
let options = origin_ref.resolve_options();
let options = apply_typescript_types_options(options);
let types_request = if let Request::Module {
module: m,
......@@ -446,14 +447,14 @@ pub async fn type_resolve(
)
};
let result = as_typings_result(
origin
origin_ref
.asset_context()
.process_resolve_result(result, ty.clone()),
);
handle_resolve_error(
result,
ty,
origin,
origin_ref.origin_path(),
request,
options,
ResolveErrorMode::Error,
......
......@@ -36,19 +36,22 @@ use crate::{
pub struct WebAssemblyModuleAsset {
source: ResolvedVc<WebAssemblySource>,
asset_context: ResolvedVc<Box<dyn AssetContext>>,
/// The path of `source`, precomputed so that `ResolveOrigin::origin_path` is synchronous.
origin_path: FileSystemPath,
}
#[turbo_tasks::value_impl]
impl WebAssemblyModuleAsset {
#[turbo_tasks::function]
pub fn new(
pub async fn new(
source: ResolvedVc<WebAssemblySource>,
asset_context: ResolvedVc<Box<dyn AssetContext>>,
) -> Vc<Self> {
Self::cell(WebAssemblyModuleAsset {
) -> Result<Vc<Self>> {
Ok(Self::cell(WebAssemblyModuleAsset {
origin_path: source.ident().await?.path.clone(),
source,
asset_context,
})
}))
}
#[turbo_tasks::function]
......@@ -209,13 +212,11 @@ impl EcmascriptChunkPlaceable for WebAssemblyModuleAsset {
#[turbo_tasks::value_impl]
impl ResolveOrigin for WebAssemblyModuleAsset {
#[turbo_tasks::function]
async fn origin_path(&self) -> Result<Vc<FileSystemPath>> {
Ok(self.source.ident().await?.path.clone().cell())
fn origin_path(&self) -> FileSystemPath {
self.origin_path.clone()
}
#[turbo_tasks::function]
fn asset_context(&self) -> Vc<Box<dyn AssetContext>> {
*self.asset_context
fn asset_context(&self) -> ResolvedVc<Box<dyn AssetContext>> {
self.asset_context
}
}
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