Skip to content

Commit 1017ecd

Browse files
committed
fix
1 parent 8095e71 commit 1017ecd

File tree

10 files changed

+53
-22
lines changed

10 files changed

+53
-22
lines changed

crates/node_binding/src/error.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ impl RspackError {
215215
compilation: &rspack_core::Compilation,
216216
diagnostic: &Diagnostic,
217217
) -> napi::Result<Self> {
218-
let error = match diagnostic.source() {
218+
let mut error = match diagnostic.source() {
219219
Some(source) => match source.downcast_ref::<RspackError>() {
220220
Some(rspack_error) => Some(Box::new(rspack_error.clone())),
221221
None => Some(Box::new(RspackError {
@@ -235,7 +235,7 @@ impl RspackError {
235235
};
236236

237237
if let Some(e) = diagnostic.downcast_ref::<ModuleNotFoundError>() {
238-
return Ok(RspackError {
238+
error = Some(Box::new(RspackError {
239239
name: "Error".to_string(),
240240
message: format!("{}", e).replace("Module not found: ", ""),
241241
severity: None,
@@ -246,7 +246,7 @@ impl RspackError {
246246
stack: None,
247247
hide_stack: None,
248248
error: None,
249-
});
249+
}));
250250
}
251251

252252
if let Some(error) = diagnostic.downcast_ref::<RspackError>() {

crates/rspack_core/src/compiler/make/repair/factorize.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use rspack_sources::BoxSource;
55

66
use super::{add::AddTask, MakeTaskContext};
77
use crate::{
8-
diagnostics::ModuleNotFoundError,
98
module_graph::ModuleGraphModule,
109
utils::task_loop::{Task, TaskResult, TaskType},
1110
BoxDependency, CompilationId, CompilerId, CompilerOptions, Context, ExportInfoData,

crates/rspack_core/src/context_module_factory.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@ use std::{borrow::Cow, fs, sync::Arc};
22

33
use cow_utils::CowUtils;
44
use derive_more::Debug;
5-
use rspack_error::{error, miette::IntoDiagnostic, Result, ToStringResultToRspackResultExt};
5+
use rspack_error::{
6+
error,
7+
miette::{self, Diagnostic, IntoDiagnostic},
8+
Result, ToStringResultToRspackResultExt,
9+
};
610
use rspack_hook::define_hook;
711
use rspack_paths::{AssertUtf8, Utf8Path, Utf8PathBuf};
812
use rspack_regex::RspackRegex;
@@ -324,10 +328,15 @@ impl ContextModuleFactory {
324328
data.add_missing_dependencies(missing_dependencies);
325329
return Ok((ModuleFactoryResult::new_with_module(raw_module), None));
326330
}
327-
Err(err) => {
331+
Err(mut e) => {
328332
data.add_file_dependencies(file_dependencies);
329333
data.add_missing_dependencies(missing_dependencies);
330-
return Err(err);
334+
if e.source_code().is_none() {
335+
if let Some(source) = &data.original_module_source {
336+
e = e.with_source_code(source.source());
337+
}
338+
}
339+
return Err(miette::Error::from(e));
331340
}
332341
};
333342

crates/rspack_core/src/diagnostics.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{fmt::Display, path::PathBuf};
1+
use std::{fmt::Display, path::PathBuf, sync::Arc};
22

33
use itertools::Itertools;
44
use rspack_error::{
@@ -19,9 +19,10 @@ use crate::{BoxLoader, DependencyRange};
1919
pub struct EmptyDependency(Box<dyn Diagnostic + Send + Sync>);
2020

2121
impl EmptyDependency {
22-
pub fn new(span: DependencyRange) -> Self {
22+
pub fn new(source_code: Option<String>, span: DependencyRange) -> Self {
2323
Self(
24-
TraceableError::from_lazy_file(
24+
TraceableError::from_arc_string(
25+
source_code.map(Arc::new),
2526
span.start as usize,
2627
span.end as usize,
2728
"Empty dependency".to_string(),

crates/rspack_core/src/normal_module_factory.rs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@ use std::{
55

66
use regex::Regex;
77
use rspack_cacheable::cacheable;
8-
use rspack_error::{error, Result};
8+
use rspack_error::{
9+
error,
10+
miette::{self, Diagnostic},
11+
Result,
12+
};
913
use rspack_hook::define_hook;
1014
use rspack_loader_runner::{get_scheme, Loader, Scheme};
1115
use rspack_paths::Utf8PathBuf;
@@ -235,7 +239,16 @@ impl NormalModuleFactory {
235239

236240
if first_char.is_none() {
237241
let span = dependency.source_span().unwrap_or_default();
238-
return Err(EmptyDependency::new(DependencyRange::new(span.start, span.end)).into());
242+
return Err(
243+
EmptyDependency::new(
244+
data
245+
.original_module_source
246+
.as_ref()
247+
.map(|s| s.source().to_string()),
248+
DependencyRange::new(span.start, span.end),
249+
)
250+
.into(),
251+
);
239252
}
240253

241254
// See: https://webpack.js.org/concepts/loaders/#inline
@@ -356,10 +369,15 @@ impl NormalModuleFactory {
356369

357370
return Ok(Some(ModuleFactoryResult::new_with_module(raw_module)));
358371
}
359-
Err(e) => {
372+
Err(mut e) => {
360373
data.add_file_dependencies(file_dependencies);
361374
data.add_missing_dependencies(missing_dependencies);
362-
return Err(e);
375+
if e.source_code().is_none() {
376+
if let Some(source) = &data.original_module_source {
377+
e = e.with_source_code(source.source());
378+
}
379+
}
380+
return Err(miette::Error::from(e));
363381
}
364382
}
365383
}

crates/rspack_core/src/resolver/mod.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use std::{
99
};
1010

1111
use regex::Regex;
12-
use rspack_error::Error;
1312
use rspack_loader_runner::{DescriptionData, ResourceData};
1413
use rspack_paths::{AssertUtf8, Utf8PathBuf};
1514
use rspack_util::identifier::insert_zero_width_space_for_fragment;
@@ -21,8 +20,8 @@ pub use self::{
2120
resolver_impl::{ResolveInnerOptions, Resolver},
2221
};
2322
use crate::{
24-
Context, DependencyCategory, DependencyType, ErrorSpan, ModuleIdentifier, Resolve,
25-
SharedPluginDriver,
23+
diagnostics::ModuleNotFoundError, Context, DependencyCategory, DependencyType, ErrorSpan,
24+
ModuleIdentifier, Resolve, SharedPluginDriver,
2625
};
2726

2827
static RELATIVE_PATH_REGEX: LazyLock<Regex> =
@@ -306,7 +305,7 @@ if its extension was not listed in the `resolve.extensions`. Here're some possib
306305
pub async fn resolve(
307306
args: ResolveArgs<'_>,
308307
plugin_driver: &SharedPluginDriver,
309-
) -> Result<ResolveResult, Error> {
308+
) -> Result<ResolveResult, ModuleNotFoundError> {
310309
let dep = ResolveOptionsWithDependencyType {
311310
resolve_options: args
312311
.resolve_options
@@ -346,5 +345,5 @@ pub async fn resolve(
346345
result = result.map_err(|err| err.with_help(hint))
347346
};
348347

349-
result.map_err(|e| Error::from(e))
348+
result
350349
}

packages/rspack-test-tools/tests/__snapshots__/StatsOutput.test.js.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,7 @@ runtime modules 274 bytes 1 module
405405
./index.js 39 bytes [built] [code generated] [1 error]
406406
407407
ERROR in ./index.js 1:0-22
408-
× Invalid "exports" target "../../index.js" defined for '.' in the package config <TEST_TOOLS_ROOT>/tests/statsOutputCases/resolve-unexpected-exports-in-pkg-error/node_modules/pkg-a/package.json
408+
× Module not found: Invalid "exports" target "../../index.js" defined for '.' in the package config <TEST_TOOLS_ROOT>/tests/statsOutputCases/resolve-unexpected-exports-in-pkg-error/node_modules/pkg-a/package.json
409409
410410
Rspack x.x.x compiled with 1 error in X s
411411
`;

packages/rspack-test-tools/tests/diagnosticsCases/factorize/cannot-resolve-with-bom/stats.err

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
ERROR in ./index.js 1:8-20
2-
× JSON parse error: BOM character found in '<TEST_TOOLS_ROOT>/tests/diagnosticsCases/factorize/cannot-resolve-with-bom/node_modules/bommodule/package.json'
2+
× Module not found: JSON parse error: BOM character found in '<TEST_TOOLS_ROOT>/tests/diagnosticsCases/factorize/cannot-resolve-with-bom/node_modules/bommodule/package.json'
33
╭─[1:0]
44
1 │ {
55
· ▲

packages/rspack-test-tools/tests/diagnosticsCases/factorize/cannot-resolve-with-broken-json/stats.err

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
ERROR in ./index.js 1:8-14
2-
× JSON parse error: control character (/u0000-/u001F) found while parsing a string at line 3 column 0 in '<TEST_TOOLS_ROOT>/tests/diagnosticsCases/factorize/cannot-resolve-with-broken-json/node_modules/foo/package.json'
2+
× Module not found: JSON parse error: control character (/u0000-/u001F) found while parsing a string at line 3 column 0 in '<TEST_TOOLS_ROOT>/tests/diagnosticsCases/factorize/cannot-resolve-with-broken-json/node_modules/foo/package.json'
33
╭─[3:0]
44
1 │ {
55
2 │ "name": "foo

packages/rspack-test-tools/tests/errorCases/error-map.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ module.exports = {
2323
expect(errors).toMatchInlineSnapshot(`
2424
Array [
2525
Object {
26+
error: Object {
27+
"message": "Can't resolve './answer' in '<TEST_TOOLS_ROOT>/tests/fixtures/errors/resolve-fail-esm'",
28+
"name": "Error",
29+
"stack": "Error: Can't resolve './answer' in '<TEST_TOOLS_ROOT>/tests/fixtures/errors/resolve-fail-esm'\\n at Proxy.map (<RSPACK_ROOT>/dist/index.js<LINE_COL>)\\n at <TEST_TOOLS_ROOT>/tests/errorCases/error-map.js<LINE_COL>\\n at Object.fn (<RSPACK_ROOT>/dist/index.js<LINE_COL>)\\n at next (<ROOT>/node_modules/<PNPM_INNER>/@rspack/lite-tapable/dist/index.js<LINE_COL>)\\n at AsyncSeriesHook.callAsyncStageRange (<ROOT>/node_modules/<PNPM_INNER>/@rspack/lite-tapable/dist/index.js<LINE_COL>)\\n at AsyncSeriesHook.callAsync (<ROOT>/node_modules/<PNPM_INNER>/@rspack/lite-tapable/dist/index.js<LINE_COL>)\\n at <RSPACK_ROOT>/dist/index.js<LINE_COL>",
30+
},
2631
index: 0,
2732
loc: 1:0-33,
2833
module: NormalModule {

0 commit comments

Comments
 (0)