Skip to content

Commit 09a4d48

Browse files
authored
fix: should handle loader error with empty stack trace (#12213)
1 parent dceb4c7 commit 09a4d48

File tree

20 files changed

+175
-112
lines changed

20 files changed

+175
-112
lines changed

β€Žcrates/rspack_binding_api/src/error.rsβ€Ž

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,9 @@ impl std::fmt::Display for RspackError {
254254
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
255255
if self.parent_error_name.as_deref() == Some("ModuleBuildError") {
256256
// https://github.com/webpack/webpack/blob/93743d233ab4fa36738065ebf8df5f175323b906/lib/ModuleBuildError.js
257-
if let Some(stack) = &self.stack {
257+
if let Some(stack) = &self.stack
258+
&& !stack.is_empty()
259+
{
258260
if self.hide_stack != Some(true) {
259261
write!(f, "{stack}")
260262
} else {

β€Žcrates/rspack_binding_api/src/plugins/js_loader/scheduler.rsβ€Ž

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -103,13 +103,6 @@ pub(crate) fn merge_loader_context(
103103
.collect();
104104

105105
if let Some(error) = from.error {
106-
let details = if let Some(stack) = &error.stack
107-
&& error.hide_stack.unwrap_or(false)
108-
{
109-
Some(stack.to_string())
110-
} else {
111-
None
112-
};
113106
return Err(error.with_parent_error_name("ModuleBuildError").into());
114107
}
115108

β€Žcrates/rspack_core/src/diagnostics.rsβ€Ž

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,28 @@ impl From<EmptyDependency> for Error {
3131

3232
///////////////////// Module /////////////////////
3333

34-
#[derive(Debug)]
35-
pub struct ModuleBuildError(pub Error);
34+
#[derive(Debug, Clone)]
35+
pub struct ModuleBuildError {
36+
error: Error,
37+
from: Option<String>,
38+
}
3639

3740
impl ModuleBuildError {
38-
pub fn new(error: Error) -> Self {
39-
Self(error)
41+
pub fn new(error: Error, from: Option<String>) -> Self {
42+
Self { error, from }
4043
}
4144
}
4245

4346
impl From<ModuleBuildError> for Error {
4447
fn from(value: ModuleBuildError) -> Error {
45-
let source = value.0;
48+
let source = value.error;
4649

47-
let mut err = Error::error("Module build failed:".into());
50+
let message = if let Some(from) = value.from {
51+
format!("Module build failed (from {from}):")
52+
} else {
53+
"Module build failed:".to_string()
54+
};
55+
let mut err = Error::error(message);
4856
let details = source
4957
.hide_stack
5058
.unwrap_or(false)

β€Žcrates/rspack_core/src/normal_module.rsβ€Ž

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,17 @@ impl Module for NormalModule {
510510
.collect();
511511

512512
inner.source = None;
513-
let diagnostic = Diagnostic::from(rspack_error::Error::from(ModuleBuildError::new(err)));
513+
514+
let current_loader = loader_result.current_loader.map(|current_loader| {
515+
contextify(
516+
build_context.compiler_options.context.as_path(),
517+
current_loader.as_str(),
518+
)
519+
});
520+
let diagnostic = Diagnostic::from(rspack_error::Error::from(ModuleBuildError::new(
521+
err,
522+
current_loader,
523+
)));
514524
inner.diagnostics.push(diagnostic);
515525

516526
self.inner_mut().build_info.hash = Some(self.init_build_hash(

β€Žcrates/rspack_loader_runner/src/runner.rsβ€Ž

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use std::{fmt::Debug, path::PathBuf, sync::Arc};
22

33
use rspack_error::{Diagnostic, Error, Result, error};
44
use rspack_fs::ReadableFileSystem;
5+
use rspack_paths::Utf8PathBuf;
56
use rspack_sources::SourceMap;
67
use rustc_hash::FxHashSet as HashSet;
78
use tracing::{Instrument, info_span};
@@ -223,6 +224,7 @@ pub struct LoaderResult<Context> {
223224
pub source_map: Option<SourceMap>,
224225
pub additional_data: Option<AdditionalData>,
225226
pub parse_meta: ParseMeta,
227+
pub current_loader: Option<Utf8PathBuf>,
226228
}
227229

228230
impl<Context: Send> LoaderResult<Context> {
@@ -241,6 +243,14 @@ impl<Context: Send> LoaderResult<Context> {
241243
source_map: loader_context.source_map,
242244
additional_data: loader_context.additional_data,
243245
parse_meta: loader_context.parse_meta,
246+
current_loader: (loader_context.loader_index >= 0)
247+
.then(|| {
248+
loader_context
249+
.loader_items
250+
.get(loader_context.loader_index as usize)
251+
})
252+
.flatten()
253+
.map(|loader| loader.path().to_path_buf()),
244254
}
245255
}
246256
}

β€Žtests/rspack-test/diagnosticsCases/builtins/recoverable_syntax_error/stats.errβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
ERROR in ./index.tsx
2-
Γ— Module build failed:
2+
Γ— Module build failed (from builtin:swc-loader):
33
β”œβ”€β–Ά Γ— x Unexpected token. Did you mean `{'>'}` or `&gt;`?
44
β”‚ β”‚ ,-[<TEST_ROOT>/diagnosticsCases/builtins/recoverable_syntax_error/index.tsx<LINE_COL>]
55
β”‚ β”‚ 1 | let c = <test>() => {}</test>;

β€Žtests/rspack-test/diagnosticsCases/module-build-failed/asset-module-build-failed/stats.errβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
ERROR in ./logo.svg
2-
Γ— Module build failed:
2+
Γ— Module build failed (from ./my-loader.js):
33
╰─▢ Γ— Error: Failed to load
44
β”‚ at xxx
55
β”‚ at xxx

β€Žtests/rspack-test/diagnosticsCases/module-build-failed/loader-emit-diagnostic/stats.errβ€Ž

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ ERROR in (./with-multi-byte-char.js!) 1:0-13
4949
╰────
5050

5151
ERROR in (./with-multi-byte-char.js!)
52-
Γ— Module build failed:
52+
Γ— Module build failed (from ./with-multi-byte-char.js):
5353
╰─▢ Γ— Error: Format diagnostic failed: Invalid char boundary. Did you pass the correct line, column and length?
5454
β”‚ at xxx
5555
β”‚ at xxx
@@ -76,7 +76,7 @@ ERROR in (./with-multiple-line.js!) 1:0-2:4
7676
╰────
7777

7878
ERROR in (./with-multiple-line.js!)
79-
Γ— Module build failed:
79+
Γ— Module build failed (from ./with-multiple-line.js):
8080
╰─▢ Γ— Error: Format diagnostic failed: Invalid `length` in location.
8181
β”‚ at xxx
8282
β”‚ at xxx
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
it("should handle loader error with empty stack trace", () => {
2+
expect(() => {
3+
require("./lib");
4+
}).toThrow("Failed to load");
5+
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const lib = "lib";

0 commit comments

Comments
Β (0)