Commit 5aa98765 authored by Luke Sandberg's avatar Luke Sandberg

Turbopack: Fix non-deterministic chunk item ordering after merge_chunks integration

After integrating merge_chunks into production.rs, chunk items were being
collected in the order that groups were merged, which is non-deterministic
due to hash map iteration order and heap operations in the merge algorithm.

The merge algorithm extends group_indices and batch_group_ids in whatever
order groups are encountered during merging. When production.rs iterates
over these unsorted indices to collect chunk items, the same set of modules
produces different output orders across builds.

Fix by sorting group_indices and batch_group_ids before iterating over them
to collect chunk items. This ensures deterministic output regardless of the
merge algorithm's internal ordering.
Co-Authored-By: 's avatarClaude Sonnet 4.5 <noreply@anthropic.com>
parent b45c24af
......@@ -197,13 +197,19 @@ pub async fn make_production_chunks(
span.record("chunks", merged.len());
let mut total_size = 0;
for merged_group in merged {
for mut merged_group in merged {
total_size += merged_group.total_size;
// Collect chunk items from all merged groups
let mut chunk_items_out: Vec<&ChunkItemOrBatchWithInfo> = Vec::new();
let mut batch_groups_out: Vec<ResolvedVc<ChunkItemBatchGroup>> = Vec::new();
// Sort group indices to ensure deterministic chunk item ordering.
// Without this, the same set of modules can produce different output
// depending on the order groups were merged during the algorithm.
merged_group.group_indices.sort_unstable();
merged_group.batch_group_ids.sort_unstable();
for &group_idx in &merged_group.group_indices {
chunk_items_out.extend(groups_data[group_idx].chunk_items.iter());
}
......
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