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