Skip to content

Commit

Permalink
fix(incremental): wrong runtime module when rebuild (#8504)
Browse files Browse the repository at this point in the history
  • Loading branch information
ahabhgk authored Nov 25, 2024
1 parent 6dfaba3 commit 4a2ac52
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 42 deletions.
71 changes: 33 additions & 38 deletions crates/rspack_core/src/compiler/compilation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -963,7 +963,9 @@ impl Compilation {
self
.chunk_render_results
.retain(|chunk, _| self.chunk_by_ukey.contains(chunk));
mutations.get_affected_chunks_with_chunk_graph(self)
let mut chunks = mutations.get_affected_chunks_with_chunk_graph(self);
chunks.extend(self.get_chunk_graph_entries());
chunks
} else {
self.chunk_by_ukey.keys().copied().collect()
};
Expand Down Expand Up @@ -1332,33 +1334,36 @@ impl Compilation {
plugin_driver.clone(),
)
.await?;
let runtime_chunks = self.get_chunk_graph_entries().collect();
let process_runtime_requirements_chunks = if let Some(mutations) = self
.incremental
.mutations_read(IncrementalPasses::CHUNKS_RUNTIME_REQUIREMENTS)
{
let removed_chunks = mutations.iter().filter_map(|mutation| match mutation {
Mutation::ChunkRemove { chunk } => Some(*chunk),
Mutation::ChunkRemove { chunk } => Some(chunk),
_ => None,
});
for removed_chunk in removed_chunks {
self.cgc_runtime_requirements_results.remove(&removed_chunk);
self.cgc_runtime_requirements_results.remove(removed_chunk);
}
let affected_chunks = mutations.get_affected_chunks_with_chunk_graph(self);
for affected_chunk in &affected_chunks {
self.cgc_runtime_requirements_results.remove(affected_chunk);
}
for runtime_chunk in &runtime_chunks {
self.cgc_runtime_requirements_results.remove(runtime_chunk);
}
self
.cgc_runtime_requirements_results
.retain(|chunk, _| self.chunk_by_ukey.contains(chunk));
mutations.get_affected_chunks_with_chunk_graph(self)
affected_chunks
} else {
self.chunk_by_ukey.keys().copied().collect()
};
self
.process_chunks_runtime_requirements(
process_runtime_requirements_chunks,
plugin_driver.clone(),
)
.await?;
self
.process_entries_runtime_requirements(
self.get_chunk_graph_entries().into_iter(),
runtime_chunks,
plugin_driver.clone(),
)
.await?;
Expand Down Expand Up @@ -1457,7 +1462,7 @@ impl Compilation {
}
}

pub fn get_chunk_graph_entries(&self) -> UkeySet<ChunkUkey> {
pub fn get_chunk_graph_entries(&self) -> impl Iterator<Item = ChunkUkey> + use<'_> {
let entries = self.entrypoints.values().map(|entrypoint_ukey| {
let entrypoint = self.chunk_group_by_ukey.expect_get(entrypoint_ukey);
entrypoint.get_runtime_chunk(&self.chunk_group_by_ukey)
Expand All @@ -1466,7 +1471,7 @@ impl Compilation {
let entrypoint = self.chunk_group_by_ukey.expect_get(entrypoint_ukey);
entrypoint.get_runtime_chunk(&self.chunk_group_by_ukey)
});
entries.chain(async_entries).collect()
entries.chain(async_entries)
}

#[instrument(skip_all)]
Expand Down Expand Up @@ -1527,18 +1532,19 @@ impl Compilation {
pub async fn process_chunks_runtime_requirements(
&mut self,
chunks: UkeySet<ChunkUkey>,
entries: UkeySet<ChunkUkey>,
plugin_driver: SharedPluginDriver,
) -> Result<()> {
let logger = self.get_logger("rspack.Compilation");
let start = logger.time("runtime requirements.chunks");
let mut chunk_requirements = HashMap::default();
for chunk_ukey in chunks {
for chunk_ukey in chunks.iter().chain(entries.iter()) {
let mut set = RuntimeGlobals::default();
for module in self
.chunk_graph
.get_chunk_modules(&chunk_ukey, &self.get_module_graph())
.get_chunk_modules(chunk_ukey, &self.get_module_graph())
{
let chunk = self.chunk_by_ukey.expect_get(&chunk_ukey);
let chunk = self.chunk_by_ukey.expect_get(chunk_ukey);
if let Some(runtime_requirements) =
ChunkGraph::get_module_runtime_requirements(self, module.identifier(), chunk.runtime())
{
Expand All @@ -1551,24 +1557,15 @@ impl Compilation {
plugin_driver
.compilation_hooks
.additional_chunk_runtime_requirements
.call(self, &chunk_ukey, &mut set)?;
.call(self, chunk_ukey, &mut set)?;

ChunkGraph::set_chunk_runtime_requirements(self, chunk_ukey, set);
ChunkGraph::set_chunk_runtime_requirements(self, *chunk_ukey, set);
}
logger.time_end(start);
Ok(())
}

#[instrument(skip_all)]
pub async fn process_entries_runtime_requirements(
&mut self,
chunk_graph_entries: impl Iterator<Item = ChunkUkey>,
plugin_driver: SharedPluginDriver,
) -> Result<()> {
let logger = self.get_logger("rspack.Compilation");
let start = logger.time("runtime requirements.entries");
for entry_ukey in chunk_graph_entries {
let entry = self.chunk_by_ukey.expect_get(&entry_ukey);
for entry_ukey in &entries {
let entry = self.chunk_by_ukey.expect_get(entry_ukey);
let mut set = RuntimeGlobals::default();
for chunk_ukey in entry
.get_all_referenced_chunks(&self.chunk_group_by_ukey)
Expand All @@ -1581,7 +1578,7 @@ impl Compilation {
plugin_driver
.compilation_hooks
.additional_tree_runtime_requirements
.call(self, &entry_ukey, &mut set)
.call(self, entry_ukey, &mut set)
.await?;

process_runtime_requirement_hook(
Expand All @@ -1592,7 +1589,7 @@ impl Compilation {
.runtime_requirement_in_tree
.call(
self,
&entry_ukey,
entry_ukey,
all_runtime_requirements,
runtime_requirements,
runtime_requirements_mut,
Expand All @@ -1601,25 +1598,23 @@ impl Compilation {
},
)?;

ChunkGraph::set_tree_runtime_requirements(self, entry_ukey, set);
ChunkGraph::set_tree_runtime_requirements(self, *entry_ukey, set);
}

// NOTE: webpack runs hooks.runtime_module in compilation.add_runtime_module
// and overwrite the runtime_module.generate() to get new source in create_chunk_assets
// this needs full runtime requirements, so run hooks.runtime_module after runtime_requirements_in_tree
for entry_ukey in self.get_chunk_graph_entries() {
for entry_ukey in &entries {
let runtime_module_ids: Vec<_> = self
.chunk_graph
.get_chunk_runtime_modules_iterable(&entry_ukey)
.get_chunk_runtime_modules_iterable(entry_ukey)
.copied()
.collect();
for runtime_module_id in runtime_module_ids {
self
.plugin_driver
.clone()
plugin_driver
.compilation_hooks
.runtime_module
.call(self, &runtime_module_id, &entry_ukey)
.call(self, &runtime_module_id, entry_ukey)
.await?;
}
}
Expand Down Expand Up @@ -1653,7 +1648,7 @@ impl Compilation {
Ok(())
}

let unordered_runtime_chunks = self.get_chunk_graph_entries();
let unordered_runtime_chunks: UkeySet<ChunkUkey> = self.get_chunk_graph_entries().collect();
let start = logger.time("hashing: hash chunks");
let other_chunks: Vec<_> = create_hash_chunks
.iter()
Expand Down
1 change: 0 additions & 1 deletion crates/rspack_core/src/compiler/hmr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ impl Compiler {
let all_old_runtime = old
.compilation
.get_chunk_graph_entries()
.into_iter()
.filter_map(|entry_ukey| old.compilation.chunk_by_ukey.get(&entry_ukey))
.flat_map(|entry_chunk| entry_chunk.runtime().clone())
.collect();
Expand Down
4 changes: 1 addition & 3 deletions crates/rspack_core/src/compiler/module_executor/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,13 +166,11 @@ impl Task<MakeTaskContext> for ExecuteTask {
.await?;
compilation
.process_chunks_runtime_requirements(
UkeySet::from_iter([chunk_ukey]),
UkeySet::from_iter([chunk_ukey]),
compilation.plugin_driver.clone(),
)
.await?;
compilation
.process_entries_runtime_requirements(once(chunk_ukey), compilation.plugin_driver.clone())
.await?;
let runtime_modules = compilation
.chunk_graph
.get_chunk_runtime_modules_iterable(&chunk_ukey)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import "./b";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
global.value = 42;
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
it("should have correct result", () => {
import("./a").then(() => {
if (WATCH_STEP === '1') {
expect(global.value).toBe("main");
expect(__webpack_require__.j).toBe("main");
} else {
expect(global.value).toBe(42);
expect(__webpack_require__.j).toBe(undefined);
}
});
})
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
global.value = __webpack_runtime_id__;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
global.value = 42;

2 comments on commit 4a2ac52

@rspack-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 Benchmark detail: Open

Name Base (2024-11-25 6dfaba3) Current Change
10000_big_production-mode_disable-minimize + exec 40 s ± 1.05 s 39.8 s ± 1.25 s -0.49 %
10000_development-mode + exec 1.8 s ± 35 ms 1.79 s ± 36 ms -0.30 %
10000_development-mode_hmr + exec 643 ms ± 14 ms 642 ms ± 6.4 ms -0.28 %
10000_production-mode + exec 2.38 s ± 32 ms 2.37 s ± 39 ms -0.47 %
arco-pro_development-mode + exec 1.76 s ± 59 ms 1.76 s ± 75 ms -0.32 %
arco-pro_development-mode_hmr + exec 428 ms ± 0.73 ms 428 ms ± 1.6 ms 0.00 %
arco-pro_production-mode + exec 3.1 s ± 105 ms 3.12 s ± 61 ms +0.58 %
arco-pro_production-mode_generate-package-json-webpack-plugin + exec 3.15 s ± 47 ms 3.17 s ± 71 ms +0.62 %
threejs_development-mode_10x + exec 1.62 s ± 18 ms 1.62 s ± 21 ms +0.32 %
threejs_development-mode_10x_hmr + exec 806 ms ± 7 ms 809 ms ± 8.9 ms +0.27 %
threejs_production-mode_10x + exec 4.91 s ± 29 ms 4.9 s ± 37 ms -0.12 %
10000_big_production-mode_disable-minimize + rss memory 12487 MiB ± 63.7 MiB 12535 MiB ± 186 MiB +0.38 %
10000_development-mode + rss memory 762 MiB ± 54.1 MiB 757 MiB ± 20.7 MiB -0.68 %
10000_development-mode_hmr + rss memory 1572 MiB ± 426 MiB 1637 MiB ± 332 MiB +4.16 %
10000_production-mode + rss memory 665 MiB ± 33.2 MiB 675 MiB ± 34 MiB +1.53 %
arco-pro_development-mode + rss memory 696 MiB ± 23.1 MiB 683 MiB ± 52.1 MiB -1.94 %
arco-pro_development-mode_hmr + rss memory 888 MiB ± 94.6 MiB 896 MiB ± 78.7 MiB +0.91 %
arco-pro_production-mode + rss memory 871 MiB ± 59.8 MiB 846 MiB ± 47.7 MiB -2.86 %
arco-pro_production-mode_generate-package-json-webpack-plugin + rss memory 862 MiB ± 60.1 MiB 872 MiB ± 59.5 MiB +1.17 %
threejs_development-mode_10x + rss memory 784 MiB ± 60.8 MiB 792 MiB ± 50.8 MiB +1.02 %
threejs_development-mode_10x_hmr + rss memory 1496 MiB ± 268 MiB 1532 MiB ± 464 MiB +2.36 %
threejs_production-mode_10x + rss memory 1018 MiB ± 46.3 MiB 1091 MiB ± 59.1 MiB +7.08 %

@rspack-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 Ran ecosystem CI: Open

suite result
modernjs ❌ failure
_selftest ✅ success
rspress ✅ success
rslib ✅ success
rsbuild ✅ success
examples ✅ success
devserver ✅ success
nuxt ✅ success

Please sign in to comment.