Skip to content

Commit

Permalink
fix: circular dependencies rebuild panic (#3001)
Browse files Browse the repository at this point in the history
* fix: circular dependencies rebuild panic

* add changeset

* test: add test case for hmr circular dependencies
  • Loading branch information
jerrykingxyz authored May 5, 2023
1 parent 1907257 commit ada51e2
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 20 deletions.
5 changes: 5 additions & 0 deletions .changeset/cuddly-fans-glow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@rspack/binding": patch
---

fix: circular dependencies rebuild panic
38 changes: 18 additions & 20 deletions crates/rspack_core/src/compiler/hmr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::{
path::PathBuf,
};

use rayon::prelude::*;
use rspack_error::Result;
use rspack_fs::AsyncWritableFileSystem;
use rspack_identifier::{IdentifierMap, IdentifierSet};
Expand Down Expand Up @@ -55,26 +56,23 @@ where
compilation: &Compilation,
) -> (IdentifierMap<(u64, String)>, IdentifierMap<String>) {
let modules_map = compilation
.module_graph
.modules()
.values()
.map(|module| {
let identifier = module.identifier();
(
identifier,
(
compilation
.module_graph
.get_module_hash(&identifier)
.expect("Module hash expected"),
compilation
.chunk_graph
.get_module_id(identifier)
.as_deref()
.expect("should has module id")
.to_string(),
),
)
.chunk_graph
.chunk_graph_module_by_module_identifier
.par_iter()
.filter_map(|(identifier, cgm)| {
let module_hash = compilation.module_graph.get_module_hash(identifier);
let cid = cgm.id.as_deref();
if let Some(module_hash) = module_hash && let Some(cid) = cid {
Some((
*identifier,
(
module_hash,
cid.to_string(),
),
))
} else {
None
}
})
.collect::<IdentifierMap<_>>();

Expand Down
2 changes: 2 additions & 0 deletions packages/rspack/tests/hotCases/runtime/hmr-circular/a.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import "./b";
export default "a.js";
2 changes: 2 additions & 0 deletions packages/rspack/tests/hotCases/runtime/hmr-circular/b.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import "./a";
export default "b.js";
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// TODO: remove this file after cache.
const path = require("path");

module.exports = [path.resolve(__dirname, "./entry.js")];
4 changes: 4 additions & 0 deletions packages/rspack/tests/hotCases/runtime/hmr-circular/entry.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import './a';
export default "entry.js"
---
export default "new_entry.js"
10 changes: 10 additions & 0 deletions packages/rspack/tests/hotCases/runtime/hmr-circular/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import entry from "./entry";

it("should not throw error when hmr remove circular dependencies", done => {
expect(entry).toBe("entry.js");
module.hot.accept("./entry", () => {
expect(entry).toBe("new_entry.js");
done();
});
NEXT(require("../../update")(done));
});

0 comments on commit ada51e2

Please sign in to comment.