Skip to content

Commit 02b3377

Browse files
authored
fix: wasm plugin panic when use persistent cache (#12455)
* fix: wasm plugin panic when use persistent cache * fix: ci
1 parent 6fde634 commit 02b3377

File tree

8 files changed

+64
-123
lines changed

8 files changed

+64
-123
lines changed

Cargo.lock

Lines changed: 0 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/rspack_plugin_wasm/Cargo.toml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,14 @@ version.workspace = true
1111
[dependencies]
1212
async-trait = { workspace = true }
1313
cow-utils = { workspace = true }
14-
dashmap = { workspace = true }
1514
indexmap = { workspace = true }
16-
rayon = { workspace = true }
1715
rspack_cacheable = { workspace = true }
1816
rspack_collections = { workspace = true }
1917
rspack_core = { workspace = true }
2018
rspack_error = { workspace = true }
2119
rspack_hash = { workspace = true }
2220
rspack_hook = { workspace = true }
2321
rspack_util = { workspace = true }
24-
serde_json = { workspace = true }
2522
swc_core = { workspace = true, features = ["__ecma"] }
2623
tokio = { workspace = true }
2724
tracing = { workspace = true }

crates/rspack_plugin_wasm/src/lib.rs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,4 @@ mod runtime;
55
mod wasm_plugin;
66

77
pub use loading_plugin::{FetchCompileAsyncWasmPlugin, enable_wasm_loading_plugin};
8-
use rspack_core::AssetInfo;
98
pub use wasm_plugin::AsyncWasmPlugin;
10-
11-
// TODO(ahabhgk): remove this
12-
type ModuleIdToFileName = std::sync::Arc<
13-
dashmap::DashMap<
14-
rspack_core::ModuleIdentifier,
15-
(String, AssetInfo),
16-
std::hash::BuildHasherDefault<rspack_collections::IdentifierHasher>,
17-
>,
18-
>;

crates/rspack_plugin_wasm/src/parser_and_generator.rs

Lines changed: 16 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,24 @@
11
use std::borrow::Cow;
22

33
use indexmap::IndexMap;
4-
use rspack_cacheable::{
5-
cacheable, cacheable_dyn,
6-
with::{AsInner, AsMap},
7-
};
4+
use rspack_cacheable::{cacheable, cacheable_dyn};
85
use rspack_core::{
9-
AssetInfo, BoxDependency, BuildMetaExportsType, ChunkGraph, CodeGenerationData, Compilation,
10-
Dependency, DependencyId, DependencyType, Filename, GenerateContext, ImportPhase, Module,
11-
ModuleDependency, ModuleGraph, ModuleIdentifier, ModuleInitFragments, NormalModule, ParseContext,
12-
ParseResult, ParserAndGenerator, PathData, RuntimeGlobals, SourceType, StaticExportsDependency,
13-
StaticExportsSpec, TemplateContext,
6+
BoxDependency, BuildMetaExportsType, CodeGenerationData, Dependency, DependencyId,
7+
DependencyType, GenerateContext, ImportPhase, Module, ModuleDependency, ModuleGraph,
8+
ModuleIdentifier, ModuleInitFragments, ParseContext, ParseResult, ParserAndGenerator,
9+
RuntimeGlobals, SourceType, StaticExportsDependency, StaticExportsSpec, TemplateContext,
1410
rspack_sources::{BoxSource, RawStringSource, Source, SourceExt},
1511
};
1612
use rspack_error::{Diagnostic, IntoTWithDiagnosticArray, Result, TWithDiagnosticArray};
17-
use rspack_util::{hash_for_source, itoa, json_stringify};
13+
use rspack_util::{itoa, json_stringify};
1814
use swc_core::atoms::Atom;
1915
use wasmparser::{Import, Parser, Payload};
2016

21-
use crate::{ModuleIdToFileName, dependency::WasmImportDependency};
17+
use crate::dependency::WasmImportDependency;
2218

2319
#[cacheable]
2420
#[derive(Debug)]
25-
pub struct AsyncWasmParserAndGenerator {
26-
#[cacheable(with=AsInner<AsMap>)]
27-
pub(crate) module_id_to_filename: ModuleIdToFileName,
28-
}
21+
pub struct AsyncWasmParserAndGenerator;
2922

3023
pub(crate) static WASM_SOURCE_TYPE: &[SourceType; 2] = &[SourceType::Wasm, SourceType::JavaScript];
3124

@@ -141,17 +134,12 @@ impl ParserAndGenerator for AsyncWasmParserAndGenerator {
141134
runtime,
142135
..
143136
} = generate_context;
144-
let wasm_filename_template = &compilation.options.output.webassembly_module_filename;
145-
let hash = hash_for_source(source);
146-
let normal_module = module
147-
.as_normal_module()
148-
.expect("module should be a NormalModule in AsyncWasmParserAndGenerator::generate");
149-
let wasm_path_with_info =
150-
render_wasm_name(compilation, normal_module, wasm_filename_template, &hash).await?;
151-
152-
self
153-
.module_id_to_filename
154-
.insert(module.identifier(), wasm_path_with_info.clone());
137+
let hash = module
138+
.build_info()
139+
.hash
140+
.as_ref()
141+
.map(|hash| hash.rendered(16))
142+
.expect("should build info have hash");
155143

156144
match generate_context.requested_source_type {
157145
SourceType::JavaScript => {
@@ -271,11 +259,11 @@ impl ParserAndGenerator for AsyncWasmParserAndGenerator {
271259
};
272260

273261
let instantiate_call = format!(
274-
"{}(exports, module.id, {} {})",
262+
r#"{}(exports, module.id, "{}" {})"#,
275263
compilation
276264
.runtime_template
277265
.render_runtime_globals(&RuntimeGlobals::INSTANTIATE_WASM),
278-
serde_json::to_string(&hash).expect("should be ok"),
266+
&hash,
279267
imports_obj.unwrap_or_default()
280268
);
281269

@@ -325,24 +313,3 @@ impl ParserAndGenerator for AsyncWasmParserAndGenerator {
325313
Some("Module Concatenation is not implemented for AsyncWasmParserAndGenerator".into())
326314
}
327315
}
328-
329-
async fn render_wasm_name(
330-
compilation: &Compilation,
331-
normal_module: &NormalModule,
332-
wasm_filename_template: &Filename,
333-
hash: &str,
334-
) -> Result<(String, AssetInfo)> {
335-
compilation
336-
.get_asset_path_with_info(
337-
wasm_filename_template,
338-
PathData::default()
339-
.module_id_optional(
340-
ChunkGraph::get_module_id(&compilation.module_ids_artifact, normal_module.id())
341-
.map(|s| PathData::prepare_id(s.as_str()))
342-
.as_deref(),
343-
)
344-
.content_hash(hash)
345-
.hash(hash),
346-
)
347-
.await
348-
}

crates/rspack_plugin_wasm/src/wasm_plugin.rs

Lines changed: 38 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,18 @@
11
use std::fmt::Debug;
22

3-
use rayon::prelude::*;
43
use rspack_core::{
5-
ChunkUkey, Compilation, CompilationParams, CompilationRenderManifest, CompilerCompilation,
6-
DependencyType, ManifestAssetType, ModuleType, ParserAndGenerator, Plugin, RenderManifestEntry,
7-
SourceType,
4+
ChunkGraph, ChunkUkey, Compilation, CompilationParams, CompilationRenderManifest,
5+
CompilerCompilation, DependencyType, ManifestAssetType, ModuleType, ParserAndGenerator, PathData,
6+
Plugin, RenderManifestEntry, SourceType,
87
};
98
use rspack_error::{Diagnostic, Result};
109
use rspack_hook::{plugin, plugin_hook};
1110

12-
use crate::{ModuleIdToFileName, parser_and_generator::AsyncWasmParserAndGenerator};
11+
use crate::parser_and_generator::AsyncWasmParserAndGenerator;
1312

1413
#[plugin]
1514
#[derive(Debug, Default)]
16-
pub struct AsyncWasmPlugin {
17-
pub module_id_to_filename_without_ext: ModuleIdToFileName,
18-
}
15+
pub struct AsyncWasmPlugin {}
1916

2017
#[plugin_hook(CompilerCompilation for AsyncWasmPlugin)]
2118
async fn compilation(
@@ -42,45 +39,46 @@ async fn render_manifest(
4239
manifest: &mut Vec<RenderManifestEntry>,
4340
_diagnostics: &mut Vec<Diagnostic>,
4441
) -> Result<()> {
42+
let wasm_filename_template = &compilation.options.output.webassembly_module_filename;
4543
let chunk = compilation.chunk_by_ukey.expect_get(chunk_ukey);
4644
let module_graph = &compilation.get_module_graph();
4745

4846
let ordered_modules = compilation
4947
.chunk_graph
5048
.get_chunk_modules(chunk_ukey, module_graph);
5149

52-
let files = ordered_modules
53-
.par_iter()
54-
.filter(|m| *m.module_type() == ModuleType::WasmAsync)
55-
.map(|m| {
56-
let code_gen_result = compilation
57-
.code_generation_results
58-
.get(&m.identifier(), Some(chunk.runtime()));
59-
60-
let result = code_gen_result.get(&SourceType::Wasm).map(|source| {
61-
let (output_path, asset_info) = self
62-
.module_id_to_filename_without_ext
63-
.get(&m.identifier())
64-
.map(|s| s.clone())
65-
.expect("should have wasm_filename");
66-
67-
let asset_info = asset_info.with_asset_type(ManifestAssetType::Wasm);
68-
RenderManifestEntry {
69-
source: source.clone(),
70-
filename: output_path,
71-
has_filename: true,
72-
info: asset_info,
73-
auxiliary: false,
74-
}
75-
});
76-
77-
Ok(result)
50+
for m in ordered_modules {
51+
if m.module_type() != &ModuleType::WasmAsync {
52+
continue;
53+
}
54+
let Some(source) = compilation
55+
.code_generation_results
56+
.get(&m.identifier(), Some(chunk.runtime()))
57+
.get(&SourceType::Wasm)
58+
else {
59+
continue;
60+
};
61+
62+
let module_id = ChunkGraph::get_module_id(&compilation.module_ids_artifact, m.identifier())
63+
.map(|s| PathData::prepare_id(s.as_str()));
64+
let mut path_data = PathData::default().module_id_optional(module_id.as_deref());
65+
if let Some(hash) = &m.build_info().hash {
66+
let hash = hash.rendered(16);
67+
path_data = path_data.content_hash(hash).hash(hash);
68+
}
69+
let (output_path, asset_info) = compilation
70+
.get_asset_path_with_info(wasm_filename_template, path_data)
71+
.await?;
72+
73+
let asset_info = asset_info.with_asset_type(ManifestAssetType::Wasm);
74+
manifest.push(RenderManifestEntry {
75+
source: source.clone(),
76+
filename: output_path,
77+
has_filename: true,
78+
info: asset_info,
79+
auxiliary: false,
7880
})
79-
.collect::<Result<Vec<Option<RenderManifestEntry>>>>()?
80-
.into_iter()
81-
.flatten()
82-
.collect::<Vec<RenderManifestEntry>>();
83-
manifest.extend(files);
81+
}
8482

8583
Ok(())
8684
}
@@ -97,17 +95,9 @@ impl Plugin for AsyncWasmPlugin {
9795
.render_manifest
9896
.tap(render_manifest::new(self));
9997

100-
let module_id_to_filename_without_ext = self.module_id_to_filename_without_ext.clone();
101-
10298
ctx.register_parser_and_generator_builder(
10399
ModuleType::WasmAsync,
104-
Box::new(move |_, _| {
105-
Box::new({
106-
AsyncWasmParserAndGenerator {
107-
module_id_to_filename: module_id_to_filename_without_ext.clone(),
108-
}
109-
}) as Box<dyn ParserAndGenerator>
110-
}),
100+
Box::new(move |_, _| Box::new(AsyncWasmParserAndGenerator) as Box<dyn ParserAndGenerator>),
111101
);
112102

113103
Ok(())

tests/rspack-test/builtinCases/plugin-wasm/imports-multiple/__snapshots__/output.snap.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ __rspack_async_done();
6262
},
6363
"./wasm.wasm"(module, exports, __webpack_require__) {
6464
var __rspack_instantiate__ = function ([rspack_import_0, rspack_import_1]) {
65-
return __webpack_require__.v(exports, module.id, "e7320130ff8b397e" , {
65+
return __webpack_require__.v(exports, module.id, "e777786177103fda" , {
6666
"./module": {
6767
"getNumber": rspack_import_0.getNumber
6868
},
@@ -78,7 +78,7 @@ __webpack_require__.a(module, async function (__rspack_load_async_deps, __rspack
7878

7979
var __rspack_async_deps = __rspack_load_async_deps([rspack_import_0, rspack_import_1]);
8080
var [rspack_import_0, rspack_import_1] = __rspack_async_deps.then ? (await __rspack_async_deps)() : __rspack_async_deps;
81-
await __webpack_require__.v(exports, module.id, "e7320130ff8b397e" , {
81+
await __webpack_require__.v(exports, module.id, "e777786177103fda" , {
8282
"./module": {
8383
"getNumber": rspack_import_0.getNumber
8484
},

tests/rspack-test/builtinCases/plugin-wasm/v128/__snapshots__/output.snap.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ __rspack_async_done();
1515

1616
},
1717
"./v128.wasm"(module, exports, __webpack_require__) {
18-
module.exports = __webpack_require__.v(exports, module.id, "a92a2e151ead6e03" );
18+
module.exports = __webpack_require__.v(exports, module.id, "80508a12102fcf23" );
1919

2020
},
2121

tests/rspack-test/statsOutputCases/wasm-explorer-examples-sync/__snapshots__/stats.txt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
assets by path *.wasm xx KiB
2-
asset a0f3fcadb09a1a9b.module.wasm xx bytes [emitted] [immutable]
3-
asset 4d75fb9d421cb1ec.module.wasm xx bytes [emitted] [immutable]
4-
asset 448bdf8d3829b42d.module.wasm xx bytes [emitted] [immutable]
5-
asset 28c603ffa1d25701.module.wasm xx bytes [emitted] [immutable]
6-
asset 4f244726a350135b.module.wasm xx bytes [emitted] [immutable]
7-
asset 934e4a526f464eac.module.wasm xx bytes [emitted] [immutable]
2+
asset ad41658e96d8163b.module.wasm xx bytes [emitted] [immutable]
3+
asset 675b695aa295b810.module.wasm xx bytes [emitted] [immutable]
4+
asset d721b2404ff371da.module.wasm xx bytes [emitted] [immutable]
5+
asset 304f41ed8aa46cbb.module.wasm xx bytes [emitted] [immutable]
6+
asset 5fde5d910acc4fe1.module.wasm xx bytes [emitted] [immutable]
7+
asset c8b87a4f03233e44.module.wasm xx bytes [emitted] [immutable]
88
assets by path *.js xx KiB
99
asset bundle.js xx KiB [emitted] (name: main)
1010
asset 549.bundle.js xx KiB [emitted]
1111
asset 440.bundle.js xx bytes [emitted] (id hint: vendors)
1212
chunk (runtime: main) 440.bundle.js (id hint: vendors) xx bytes [rendered] split chunk (cache group: defaultVendors)
1313
./node_modules/env.js xx bytes [built] [code generated]
14-
chunk (runtime: main) 28c603ffa1d25701.module.wasm, 448bdf8d3829b42d.module.wasm, 4d75fb9d421cb1ec.module.wasm, 4f244726a350135b.module.wasm, 549.bundle.js, 934e4a526f464eac.module.wasm, a0f3fcadb09a1a9b.module.wasm xx KiB (javascript) xx KiB (wasm) [rendered]
14+
chunk (runtime: main) 304f41ed8aa46cbb.module.wasm, 549.bundle.js, 5fde5d910acc4fe1.module.wasm, 675b695aa295b810.module.wasm, ad41658e96d8163b.module.wasm, c8b87a4f03233e44.module.wasm, d721b2404ff371da.module.wasm xx KiB (javascript) xx KiB (wasm) [rendered]
1515
./Q_rsqrt.wasm xx bytes (wasm) xx bytes (javascript) [dependent] [built] [code generated]
1616
./duff.wasm xx bytes (wasm) xx bytes (javascript) [dependent] [built] [code generated]
1717
./fact.wasm xx bytes (wasm) xx bytes (javascript) [dependent] [built] [code generated]

0 commit comments

Comments
 (0)