Skip to content

Commit 9729f37

Browse files
authored
feat: improve add include error msg (#9529)
1 parent 832bc69 commit 9729f37

File tree

3 files changed

+44
-26
lines changed

3 files changed

+44
-26
lines changed

crates/node_binding/src/compilation/mod.rs

+30-25
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use rspack_core::BoxDependency;
1515
use rspack_core::Compilation;
1616
use rspack_core::CompilationId;
1717
use rspack_core::EntryOptions;
18+
use rspack_core::FactorizeInfo;
1819
use rspack_core::ModuleIdentifier;
1920
use rspack_error::Diagnostic;
2021
use rspack_napi::napi::bindgen_prelude::*;
@@ -760,49 +761,53 @@ impl JsCompilation {
760761
let module_graph = compilation.get_module_graph();
761762
let results = dependency_ids
762763
.into_iter()
763-
.map(
764-
|dependency_id| match module_graph.get_module_by_dependency_id(&dependency_id) {
764+
.map(|dependency_id| {
765+
if let Some(dependency) = module_graph.dependency_by_id(&dependency_id) {
766+
if let Some(factorize_info) = FactorizeInfo::get_from(dependency) {
767+
if let Some(diagnostic) = factorize_info.diagnostics().first() {
768+
return Either::A(diagnostic.to_string());
769+
}
770+
}
771+
}
772+
773+
match module_graph.get_module_by_dependency_id(&dependency_id) {
765774
Some(module) => {
766775
let js_module =
767776
JsModuleWrapper::new(module.identifier(), None, compilation.compiler_id());
768-
(Either::B(()), Either::B(js_module))
777+
Either::B(js_module)
769778
}
770-
None => (
771-
Either::A(format!(
772-
"Module created by {:?} cannot be found",
773-
dependency_id
774-
)),
775-
Either::A(()),
776-
),
777-
},
778-
)
779-
.collect::<Vec<(Either<String, ()>, Either<(), JsModuleWrapper>)>>();
779+
None => Either::A("build failed with unknown error".to_string()),
780+
}
781+
})
782+
.collect::<Vec<Either<String, JsModuleWrapper>>>();
780783

781784
Ok(JsAddIncludeCallbackArgs(results))
782785
})
783786
}
784787
}
785788

786-
pub struct JsAddIncludeCallbackArgs(Vec<(Either<String, ()>, Either<(), JsModuleWrapper>)>);
789+
pub struct JsAddIncludeCallbackArgs(Vec<Either<String, JsModuleWrapper>>);
787790

788791
impl ToNapiValue for JsAddIncludeCallbackArgs {
789792
unsafe fn to_napi_value(env: sys::napi_env, val: Self) -> Result<sys::napi_value> {
790793
let env_wrapper = Env::from_raw(env);
791794
let mut js_array = env_wrapper.create_array(0)?;
792-
for (error, module) in val.0 {
793-
let js_error = match error {
794-
Either::A(val) => env_wrapper.create_string(&val)?.into_unknown(),
795-
Either::B(_) => env_wrapper.get_undefined()?.into_unknown(),
796-
};
797-
let js_module = match module {
798-
Either::A(_) => env_wrapper.get_undefined()?.into_unknown(),
799-
Either::B(val) => {
800-
let napi_val = ToNapiValue::to_napi_value(env, val)?;
801-
Unknown::from_napi_value(env, napi_val)?
795+
796+
for result in val.0 {
797+
let js_result = match result {
798+
Either::A(msg) => vec![
799+
env_wrapper.create_string(&msg)?.into_unknown(),
800+
env_wrapper.get_undefined()?.into_unknown(),
801+
],
802+
Either::B(module) => {
803+
let napi_val = ToNapiValue::to_napi_value(env, module)?;
804+
let js_module = Unknown::from_napi_value(env, napi_val)?;
805+
vec![env_wrapper.get_undefined()?.into_unknown(), js_module]
802806
}
803807
};
804-
js_array.insert(vec![js_error, js_module])?;
808+
js_array.insert(js_result)?;
805809
}
810+
806811
ToNapiValue::to_napi_value(env, js_array)
807812
}
808813
}

packages/rspack-test-tools/tests/configCases/compilation/add-include/rspack.config.js

+13
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,19 @@ class Plugin {
5959
);
6060
})
6161
);
62+
tasks.push(
63+
new Promise(resolve => {
64+
compilation.addInclude(
65+
compiler.context,
66+
EntryPlugin.createDependency(path.resolve(__dirname, "no-exist.js")),
67+
{},
68+
(err, module) => {
69+
expect(err.message).toMatch(/Can't resolve/);
70+
resolve(module);
71+
}
72+
);
73+
})
74+
);
6275
return Promise.all(tasks);
6376
});
6477

packages/rspack/src/Compilation.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1264,7 +1264,7 @@ class AddIncludeDispatcher {
12641264
const cb = cbs[i];
12651265
cb(
12661266
errMsg ? new WebpackError(errMsg) : null,
1267-
Module.__from_binding(moduleBinding)
1267+
moduleBinding ? Module.__from_binding(moduleBinding) : undefined
12681268
);
12691269
}
12701270
});

0 commit comments

Comments
 (0)