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

Turbopack: store ChunkGroupInfo::chunk_groups as a Vec (#100935)

Order matters, use a vec

Using IndexSet gives us 'set equality' semantics (but deterministic
iteration order), this is broken for chunk groups and can cause queries
that depend on ordering to fail. Since in a wam build if the order of
items in a chunk group changes, then we will reuse the old order (the
cell update is considered equivalent)


I am looking at other places where this is a risk but this one
definitely is and is straightforward to fix

<!-- I am a human, why would i deceive you? -->

---------
Co-authored-by: 's avatarClaude Opus 5.5 <noreply@anthropic.com>
parent e1a69737
...@@ -48,8 +48,11 @@ pub struct ChunkGroupInfo { ...@@ -48,8 +48,11 @@ pub struct ChunkGroupInfo {
pub module_chunk_groups: ResolvedVc<ModuleToChunkGroups>, pub module_chunk_groups: ResolvedVc<ModuleToChunkGroups>,
/// For each async module, the chunk groups that contain its async loader. /// For each async module, the chunk groups that contain its async loader.
pub async_loader_chunk_groups: ResolvedVc<ModuleToChunkGroups>, pub async_loader_chunk_groups: ResolvedVc<ModuleToChunkGroups>,
#[bincode(with = "turbo_bincode::indexset")] /// Indexed by chunk group index. This must be a `Vec` (not an `IndexSet`) so that equality is
pub chunk_groups: FxIndexSet<ChunkGroup>, /// order-sensitive: `IndexSet` compares as a set, so a recomputation that only renumbers the
/// chunk groups would compare equal and keep the old order, while the bitsets in
/// `module_chunk_groups` and the indices in `chunk_group_indices` use the new numbering.
pub chunk_groups: Vec<ChunkGroup>,
#[turbo_tasks(unsafe_ignore)] #[turbo_tasks(unsafe_ignore)]
#[bincode(with = "turbo_bincode::indexset")] #[bincode(with = "turbo_bincode::indexset")]
pub chunk_group_keys: FxIndexSet<ChunkGroupKey>, pub chunk_group_keys: FxIndexSet<ChunkGroupKey>,
......
...@@ -3695,6 +3695,126 @@ pub mod tests { ...@@ -3695,6 +3695,126 @@ pub mod tests {
.await; .await;
} }
#[turbo_tasks::value]
struct EntryOrder {
swapped: turbo_tasks::State<bool>,
}
#[turbo_tasks::value(transparent)]
struct ChunkGroupDescriptions(Vec<RcStr>);
/// Builds a graph with two entries that each have an async child, in the order given by
/// `order`, and describes the chunk groups that `get_chunk_groups_for_module` returns for
/// `page1.js` and `lazy1.js`.
///
/// ```text
/// page1 -async-> lazy1
/// page2 -async-> lazy2
/// ```
#[turbo_tasks::function(operation, root)]
async fn chunk_groups_by_entry_order(
order: ResolvedVc<EntryOrder>,
) -> Result<Vc<ChunkGroupDescriptions>> {
let swapped = *order.await?.swapped.get();
let fs = VirtualFileSystem::new_with_name(rcstr!("test"));
let root = fs.root().await?;
let repo = TestRepo::new_with_chunking_types(
&root,
[
("page1.js", vec!["lazy1.js"]),
("page2.js", vec!["lazy2.js"]),
],
[
("page1.js", "lazy1.js", ChunkingType::Async),
("page2.js", "lazy2.js", ChunkingType::Async),
],
);
let module = |name: &str| {
Vc::upcast::<Box<dyn Module>>(MockModule::new(root.join(name).unwrap(), repo))
.to_resolved()
};
let page1 = module("page1.js").await?;
let page2 = module("page2.js").await?;
let lazy1 = module("lazy1.js").await?;
let entry = |module| ChunkGroupEntry::Entry {
modules: vec![module],
heuristics: EntryHeuristics::default(),
};
let mut entries = vec![entry(page1), entry(page2)];
if swapped {
entries.reverse();
}
let graph = SingleModuleGraph::new_with_entries(
GraphEntries::resolved_cell(GraphEntries::new(entries, vec![])),
false,
false,
);
let chunk_group_info = ModuleGraph::from_graphs(vec![graph], None)
.connect()
.chunk_group_info();
let mut descriptions = Vec::new();
for module in [page1, lazy1] {
let module_name = module.ident().await?.path.path.clone();
for group in chunk_group_info
.get_chunk_groups_for_module(*module)
.await?
.iter()
{
let kind = match &group.chunk_group {
ChunkGroup::Entry(_) => "entry",
ChunkGroup::Async(_) => "async",
_ => "other",
};
let entry_names = group
.chunk_group
.entries()
.map(async |entry| Ok(entry.ident().await?.path.path.clone()))
.try_join()
.await?;
descriptions
.push(format!("{module_name}: {kind} {}", entry_names.join(",")).into());
}
}
Ok(Vc::cell(descriptions))
}
/// Reordering the graph entries renumbers the chunk groups but keeps the same set of them.
/// The recomputed `ChunkGroupInfo` must not compare equal to the old one: if it did, the cell
/// would keep the old order while `module_chunk_groups` takes the new indices, and modules
/// would map to another entry's chunk groups.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_chunk_group_info_reordered_entries() {
let tt = turbo_tasks::TurboTasks::new(TurboTasksBackend::new(
BackendOptions::default(),
noop_backing_storage(),
));
tt.run_once(async move {
let order = EntryOrder {
swapped: turbo_tasks::State::new(false),
}
.resolved_cell();
let expected = vec![
rcstr!("page1.js: entry page1.js"),
rcstr!("lazy1.js: async lazy1.js"),
];
let descriptions = chunk_groups_by_entry_order(order)
.read_strongly_consistent()
.await?;
assert_eq!(*descriptions, expected);
order.await?.swapped.set(true);
let descriptions = chunk_groups_by_entry_order(order)
.read_strongly_consistent()
.await?;
assert_eq!(*descriptions, expected);
Ok(())
})
.await
.unwrap();
}
#[turbo_tasks::value(serialization = "skip", eq = "manual", cell = "new")] #[turbo_tasks::value(serialization = "skip", eq = "manual", cell = "new")]
struct ChunkGroupInfoTestGraph { struct ChunkGroupInfoTestGraph {
module_graph: ReadRef<ModuleGraph>, module_graph: ReadRef<ModuleGraph>,
...@@ -3809,7 +3929,7 @@ pub mod tests { ...@@ -3809,7 +3929,7 @@ pub mod tests {
.collect::<Vec<_>>() .collect::<Vec<_>>()
.join(",") .join(",")
}; };
match info.chunk_groups.get_index(index).unwrap() { match info.chunk_groups.get(index).unwrap() {
ChunkGroup::Entry(modules) => format!("entry {}", list(modules)), ChunkGroup::Entry(modules) => format!("entry {}", list(modules)),
ChunkGroup::Async(module) => format!("async {}", names[module]), ChunkGroup::Async(module) => format!("async {}", names[module]),
ChunkGroup::IsolatedMerged { ChunkGroup::IsolatedMerged {
......
...@@ -117,11 +117,11 @@ impl ModuleBatchesGraph { ...@@ -117,11 +117,11 @@ impl ModuleBatchesGraph {
.as_ref() .as_ref()
.and_then(|o| o.as_ref()) .and_then(|o| o.as_ref())
{ {
if let Some(chunk_group) = chunk_group_info.chunk_groups.get_index(idx) { if let Some(chunk_group) = chunk_group_info.chunk_groups.get(idx) {
debug_assert_eq!(ordered_entries.len(), chunk_group.entries_count()); debug_assert_eq!(ordered_entries.len(), chunk_group.entries_count());
} }
Either::Left(Either::Left(ordered_entries.iter().copied())) Either::Left(Either::Left(ordered_entries.iter().copied()))
} else if let Some(chunk_group) = chunk_group_info.chunk_groups.get_index(idx) { } else if let Some(chunk_group) = chunk_group_info.chunk_groups.get(idx) {
Either::Right(chunk_group.entries()) Either::Right(chunk_group.entries())
} else { } else {
Either::Left(Either::Right(std::iter::empty())) Either::Left(Either::Right(std::iter::empty()))
......
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