From 17ffacd810dba8326e6ced6bdd90916f38f2c8f2 Mon Sep 17 00:00:00 2001 From: ahabhgk Date: Thu, 13 Feb 2025 15:50:00 +0800 Subject: [PATCH 1/3] fix: dyanmic entry remove --- .../src/build_chunk_graph/incremental.rs | 36 ++++++++++++------ .../old_cache/local/code_splitting_cache.rs | 8 +++- .../dynamic-entries-remove/0/index0.js | 13 +++++++ .../dynamic-entries-remove/0/index1.js | 1 + .../dynamic-entries-remove/1/index0.js | 8 ++++ .../dynamic-entries-remove/rspack.config.js | 37 +++++++++++++++++++ .../dynamic-entries-remove/test.config.js | 5 +++ 7 files changed, 96 insertions(+), 12 deletions(-) create mode 100644 packages/rspack-test-tools/tests/watchCases/entries/dynamic-entries-remove/0/index0.js create mode 100644 packages/rspack-test-tools/tests/watchCases/entries/dynamic-entries-remove/0/index1.js create mode 100644 packages/rspack-test-tools/tests/watchCases/entries/dynamic-entries-remove/1/index0.js create mode 100644 packages/rspack-test-tools/tests/watchCases/entries/dynamic-entries-remove/rspack.config.js create mode 100644 packages/rspack-test-tools/tests/watchCases/entries/dynamic-entries-remove/test.config.js diff --git a/crates/rspack_core/src/build_chunk_graph/incremental.rs b/crates/rspack_core/src/build_chunk_graph/incremental.rs index 7735f1fa0e36..ec61d1688b1b 100644 --- a/crates/rspack_core/src/build_chunk_graph/incremental.rs +++ b/crates/rspack_core/src/build_chunk_graph/incremental.rs @@ -1,7 +1,9 @@ use std::{collections::HashSet, hash::BuildHasherDefault}; use num_bigint::BigUint; -use rspack_collections::{IdentifierHasher, IdentifierIndexSet, IdentifierMap, UkeySet}; +use rspack_collections::{ + IdentifierHasher, IdentifierIndexSet, IdentifierMap, IdentifierSet, UkeySet, +}; use rspack_error::Result; use tracing::instrument; @@ -443,26 +445,38 @@ impl CodeSplitter { self.stat_cache_miss_by_available_modules = 0; self.stat_cache_miss_by_cant_rebuild = 0; - let modules = if let Some(mutations) = compilation + let (affected_modules, removed_modules) = if let Some(mutations) = compilation .incremental .mutations_read(IncrementalPasses::BUILD_CHUNK_GRAPH) { - mutations.get_affected_modules_with_module_graph(&module_graph) + let affected_modules = mutations.get_affected_modules_with_module_graph(&module_graph); + let removed_modules: IdentifierSet = mutations + .iter() + .filter_map(|mutation| match mutation { + Mutation::ModuleRemove { module } => Some(*module), + _ => None, + }) + .collect(); + dbg!(&removed_modules); + (affected_modules, removed_modules) } else { - compilation - .get_module_graph() - .modules() - .keys() - .copied() - .collect() + ( + compilation + .get_module_graph() + .modules() + .keys() + .copied() + .collect(), + Default::default(), + ) }; let mut edges = vec![]; // collect invalidate caches before we do anything to the chunk graph - let dirty_blocks = self.collect_dirty_caches(compilation, modules.iter().copied()); + let dirty_blocks = self.collect_dirty_caches(compilation, affected_modules.iter().copied()); - for m in modules { + for m in affected_modules { for module_map in self.block_modules_runtime_map.values_mut() { module_map.swap_remove(&DependenciesBlockIdentifier::Module(m)); } diff --git a/crates/rspack_core/src/old_cache/local/code_splitting_cache.rs b/crates/rspack_core/src/old_cache/local/code_splitting_cache.rs index c30b03c69b2d..43e8982bdc97 100644 --- a/crates/rspack_core/src/old_cache/local/code_splitting_cache.rs +++ b/crates/rspack_core/src/old_cache/local/code_splitting_cache.rs @@ -45,7 +45,13 @@ where && compilation .incremental .can_read_mutations(IncrementalPasses::BUILD_CHUNK_GRAPH); - let no_change = !compilation.has_module_import_export_change(); + let no_change = !compilation.has_module_import_export_change() + && compilation.code_splitting_cache.entrypoints.len() == compilation.entries.len() + && compilation + .code_splitting_cache + .entrypoints + .keys() + .eq(compilation.entries.keys()); if incremental_code_splitting || no_change { let cache = &mut compilation.code_splitting_cache; diff --git a/packages/rspack-test-tools/tests/watchCases/entries/dynamic-entries-remove/0/index0.js b/packages/rspack-test-tools/tests/watchCases/entries/dynamic-entries-remove/0/index0.js new file mode 100644 index 000000000000..0d364631fa20 --- /dev/null +++ b/packages/rspack-test-tools/tests/watchCases/entries/dynamic-entries-remove/0/index0.js @@ -0,0 +1,13 @@ +it("should have correct entrypoints", function() { + expect(Object.keys(__STATS__.entrypoints)).toEqual(["bundle0", "bundle1"]); + + const index0 = __STATS__.modules.find(m => m.name === "./index0.js"); + expect(index0.built).toBe(true); + expect(index0.reasons.length).toBe(1); + expect(index0.reasons[0].type).toBe("entry"); + + const index1 = __STATS__.modules.find(m => m.name === "./index1.js"); + expect(index1.built).toBe(true); + expect(index1.reasons.length).toBe(1); + expect(index1.reasons[0].type).toBe("entry"); +}) diff --git a/packages/rspack-test-tools/tests/watchCases/entries/dynamic-entries-remove/0/index1.js b/packages/rspack-test-tools/tests/watchCases/entries/dynamic-entries-remove/0/index1.js new file mode 100644 index 000000000000..cee8bccd679e --- /dev/null +++ b/packages/rspack-test-tools/tests/watchCases/entries/dynamic-entries-remove/0/index1.js @@ -0,0 +1 @@ +console.log.bind(console, 1); diff --git a/packages/rspack-test-tools/tests/watchCases/entries/dynamic-entries-remove/1/index0.js b/packages/rspack-test-tools/tests/watchCases/entries/dynamic-entries-remove/1/index0.js new file mode 100644 index 000000000000..d72639b4c65b --- /dev/null +++ b/packages/rspack-test-tools/tests/watchCases/entries/dynamic-entries-remove/1/index0.js @@ -0,0 +1,8 @@ +it("should have correct entrypoints", function() { + expect(Object.keys(__STATS__.entrypoints)).toEqual(["bundle0"]); + + const index0 = __STATS__.modules.find(m => m.name === "./index0.js"); + expect(index0.built).toBe(true); + expect(index0.reasons.length).toBe(1); + expect(index0.reasons[0].type).toBe("entry"); +}) diff --git a/packages/rspack-test-tools/tests/watchCases/entries/dynamic-entries-remove/rspack.config.js b/packages/rspack-test-tools/tests/watchCases/entries/dynamic-entries-remove/rspack.config.js new file mode 100644 index 000000000000..cf9d3c9d188a --- /dev/null +++ b/packages/rspack-test-tools/tests/watchCases/entries/dynamic-entries-remove/rspack.config.js @@ -0,0 +1,37 @@ +const path = require("path"); +const fs = require("fs"); + +let compiler; +let step = 0; +let entries; + +/** @type {import("@rspack/core").Configuration} */ +module.exports = { + entry: async () => { + const context = compiler.context; + if (step === 0) { + const files = await fs.promises.readdir(context); + entries = files.filter(f => f.startsWith("index")); + entries.sort(); + } else if (step === 1) { + entries.pop(); + } else { + throw new Error(`unreachable step: ${step}`); + } + return entries.reduce((acc, e, i) => { + acc[`bundle${i}`] = path.resolve(context, e); + return acc; + }, {}); + }, + output: { + filename: "[name].js" + }, + plugins: [ + function (c) { + compiler = c; + c.hooks.done.tap("test", () => { + step += 1; + }); + } + ] +}; diff --git a/packages/rspack-test-tools/tests/watchCases/entries/dynamic-entries-remove/test.config.js b/packages/rspack-test-tools/tests/watchCases/entries/dynamic-entries-remove/test.config.js new file mode 100644 index 000000000000..49fd95b26864 --- /dev/null +++ b/packages/rspack-test-tools/tests/watchCases/entries/dynamic-entries-remove/test.config.js @@ -0,0 +1,5 @@ +module.exports = { + findBundle(i) { + return ["bundle0.js"]; + } +}; From 04ed23ea6d3f86ecc0f5f1e47ff2047eb55c2390 Mon Sep 17 00:00:00 2001 From: ahabhgk Date: Fri, 21 Feb 2025 12:28:31 +0800 Subject: [PATCH 2/3] fix --- .../src/build_chunk_graph/incremental.rs | 17 +++++++++++++++-- .../src/old_cache/local/code_splitting_cache.rs | 1 - 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/crates/rspack_core/src/build_chunk_graph/incremental.rs b/crates/rspack_core/src/build_chunk_graph/incremental.rs index ec61d1688b1b..ba18cb1386ef 100644 --- a/crates/rspack_core/src/build_chunk_graph/incremental.rs +++ b/crates/rspack_core/src/build_chunk_graph/incremental.rs @@ -457,7 +457,6 @@ impl CodeSplitter { _ => None, }) .collect(); - dbg!(&removed_modules); (affected_modules, removed_modules) } else { ( @@ -474,7 +473,21 @@ impl CodeSplitter { let mut edges = vec![]; // collect invalidate caches before we do anything to the chunk graph - let dirty_blocks = self.collect_dirty_caches(compilation, affected_modules.iter().copied()); + let dirty_blocks = self.collect_dirty_caches( + compilation, + affected_modules + .iter() + .chain(removed_modules.iter()) + .copied(), + ); + + for m in removed_modules { + for module_map in self.block_modules_runtime_map.values_mut() { + module_map.swap_remove(&DependenciesBlockIdentifier::Module(m)); + } + + self.invalidate_from_module(m, compilation)?; + } for m in affected_modules { for module_map in self.block_modules_runtime_map.values_mut() { diff --git a/crates/rspack_core/src/old_cache/local/code_splitting_cache.rs b/crates/rspack_core/src/old_cache/local/code_splitting_cache.rs index 43e8982bdc97..faadbeff44bf 100644 --- a/crates/rspack_core/src/old_cache/local/code_splitting_cache.rs +++ b/crates/rspack_core/src/old_cache/local/code_splitting_cache.rs @@ -46,7 +46,6 @@ where .incremental .can_read_mutations(IncrementalPasses::BUILD_CHUNK_GRAPH); let no_change = !compilation.has_module_import_export_change() - && compilation.code_splitting_cache.entrypoints.len() == compilation.entries.len() && compilation .code_splitting_cache .entrypoints From 1b365b5009c82222d7604f31b026425779a32604 Mon Sep 17 00:00:00 2001 From: ahabhgk Date: Fri, 21 Feb 2025 13:36:07 +0800 Subject: [PATCH 3/3] fix --- .../src/compiler/make/cutout/has_module_graph_change.rs | 4 +++- .../src/old_cache/local/code_splitting_cache.rs | 7 +------ 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/crates/rspack_core/src/compiler/make/cutout/has_module_graph_change.rs b/crates/rspack_core/src/compiler/make/cutout/has_module_graph_change.rs index 9f6bb9344b2f..90f1e317f187 100644 --- a/crates/rspack_core/src/compiler/make/cutout/has_module_graph_change.rs +++ b/crates/rspack_core/src/compiler/make/cutout/has_module_graph_change.rs @@ -120,7 +120,9 @@ impl HasModuleGraphChange { if self.disabled { return; } - if artifact.built_modules.len() != self.expect_built_modules_len { + if artifact.built_modules.len() != self.expect_built_modules_len + || artifact.built_modules.len() != artifact.revoked_modules.len() + { // contain unexpected module built artifact.has_module_graph_change = true; return; diff --git a/crates/rspack_core/src/old_cache/local/code_splitting_cache.rs b/crates/rspack_core/src/old_cache/local/code_splitting_cache.rs index faadbeff44bf..c30b03c69b2d 100644 --- a/crates/rspack_core/src/old_cache/local/code_splitting_cache.rs +++ b/crates/rspack_core/src/old_cache/local/code_splitting_cache.rs @@ -45,12 +45,7 @@ where && compilation .incremental .can_read_mutations(IncrementalPasses::BUILD_CHUNK_GRAPH); - let no_change = !compilation.has_module_import_export_change() - && compilation - .code_splitting_cache - .entrypoints - .keys() - .eq(compilation.entries.keys()); + let no_change = !compilation.has_module_import_export_change(); if incremental_code_splitting || no_change { let cache = &mut compilation.code_splitting_cache;