Skip to content

Commit 58a8a8b

Browse files
lukesandberghuozhi
authored andcommitted
[turbopack] Change where cells are created in resolve_raw to make cell allocation order deterministic. (#85525)
Constructing cells within a `try_flat_join` will lead to racy creation. Instead we just need to do a second pass after the fact Also eliminate a clone while we are there.
1 parent 9d63118 commit 58a8a8b

File tree

1 file changed

+21
-20
lines changed
  • turbopack/crates/turbopack-core/src/resolve

1 file changed

+21
-20
lines changed

turbopack/crates/turbopack-core/src/resolve/mod.rs

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -575,30 +575,25 @@ impl ResolveResult {
575575
}
576576

577577
pub fn source(source: ResolvedVc<Box<dyn Source>>) -> ResolvedVc<Self> {
578-
Self::source_with_key(RequestKey::default(), source)
578+
Self::source_with_key(RequestKey::default(), source).resolved_cell()
579579
}
580580

581-
pub fn source_with_key(
582-
request_key: RequestKey,
583-
source: ResolvedVc<Box<dyn Source>>,
584-
) -> ResolvedVc<Self> {
581+
fn source_with_key(request_key: RequestKey, source: ResolvedVc<Box<dyn Source>>) -> Self {
585582
ResolveResult {
586583
primary: vec![(request_key, ResolveResultItem::Source(source))].into_boxed_slice(),
587584
affecting_sources: Default::default(),
588585
}
589-
.resolved_cell()
590586
}
591587

592-
pub fn source_with_affecting_sources(
588+
fn source_with_affecting_sources(
593589
request_key: RequestKey,
594590
source: ResolvedVc<Box<dyn Source>>,
595591
affecting_sources: Vec<ResolvedVc<Box<dyn Source>>>,
596-
) -> ResolvedVc<Self> {
592+
) -> Self {
597593
ResolveResult {
598594
primary: vec![(request_key, ResolveResultItem::Source(source))].into_boxed_slice(),
599595
affecting_sources: affecting_sources.into_boxed_slice(),
600596
}
601-
.resolved_cell()
602597
}
603598
}
604599

@@ -1416,17 +1411,17 @@ pub async fn resolve_raw(
14161411
) -> Result<Vc<ResolveResult>> {
14171412
async fn to_result(
14181413
request: RcStr,
1419-
path: FileSystemPath,
1414+
path: &FileSystemPath,
14201415
collect_affecting_sources: bool,
1421-
) -> Result<Vc<ResolveResult>> {
1416+
) -> Result<ResolveResult> {
14221417
let result = &*path.realpath_with_links().await?;
14231418
let path = match &result.path_result {
14241419
Ok(path) => path,
1425-
Err(e) => bail!(e.as_error_message(&path, result)),
1420+
Err(e) => bail!(e.as_error_message(path, result)),
14261421
};
14271422
let request_key = RequestKey::new(request);
14281423
let source = ResolvedVc::upcast(FileSource::new(path.clone()).to_resolved().await?);
1429-
Ok(*if collect_affecting_sources {
1424+
Ok(if collect_affecting_sources {
14301425
ResolveResult::source_with_affecting_sources(
14311426
request_key,
14321427
source,
@@ -1449,17 +1444,22 @@ pub async fn resolve_raw(
14491444
matches: &[PatternMatch],
14501445
collect_affecting_sources: bool,
14511446
) -> Result<Vec<Vc<ResolveResult>>> {
1452-
matches
1447+
Ok(matches
14531448
.iter()
14541449
.map(|m| async move {
14551450
Ok(if let PatternMatch::File(request, path) = m {
1456-
Some(to_result(request.clone(), path.clone(), collect_affecting_sources).await?)
1451+
Some(to_result(request.clone(), path, collect_affecting_sources).await?)
14571452
} else {
14581453
None
14591454
})
14601455
})
14611456
.try_flat_join()
1462-
.await
1457+
.await?
1458+
// Construct all the cells after resolving the results to ensure they are constructed in
1459+
// a deterministic order.
1460+
.into_iter()
1461+
.map(|res| res.cell())
1462+
.collect())
14631463
}
14641464

14651465
let mut results = Vec::new();
@@ -2847,8 +2847,8 @@ async fn resolved(
28472847
.to_resolved()
28482848
.await?,
28492849
);
2850-
if options_value.collect_affecting_sources {
2851-
Ok(*ResolveResult::source_with_affecting_sources(
2850+
Ok(if options_value.collect_affecting_sources {
2851+
ResolveResult::source_with_affecting_sources(
28522852
request_key,
28532853
source,
28542854
result
@@ -2861,10 +2861,11 @@ async fn resolved(
28612861
})
28622862
.try_join()
28632863
.await?,
2864-
))
2864+
)
28652865
} else {
2866-
Ok(*ResolveResult::source_with_key(request_key, source))
2866+
ResolveResult::source_with_key(request_key, source)
28672867
}
2868+
.cell())
28682869
}
28692870

28702871
async fn handle_exports_imports_field(

0 commit comments

Comments
 (0)