Skip to content

Commit 537e8c5

Browse files
authored
fix: rspack error communicate between js and rust (#10595)
1 parent 1161892 commit 537e8c5

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+791
-357
lines changed

crates/node_binding/binding.d.ts

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ export const BUILD_INFO_MISSING_DEPENDENCIES_SYMBOL: unique symbol;
2323
export const BUILD_INFO_BUILD_DEPENDENCIES_SYMBOL: unique symbol;
2424
export const COMMIT_CUSTOM_FIELDS_SYMBOL: unique symbol;
2525

26+
export const RUST_ERROR_SYMBOL: unique symbol;
27+
2628
interface KnownBuildInfo {
2729
[BUILD_INFO_ASSETS_SYMBOL]: Assets,
2830
[BUILD_INFO_FILE_DEPENDENCIES_SYMBOL]: string[],
@@ -75,6 +77,18 @@ export interface ExternalModule extends Module {
7577
readonly userRequest: string;
7678
}
7779

80+
export interface RspackError extends Error {
81+
name: string;
82+
message: string;
83+
details?: string;
84+
module?: Module;
85+
loc?: DependencyLocation;
86+
file?: string;
87+
stack?: string;
88+
hideStack?: boolean;
89+
error?: Error;
90+
}
91+
7892
export type DependencyLocation = SyntheticDependencyLocation | RealDependencyLocation;
7993
/* -- banner.d.ts end -- */
8094

@@ -145,10 +159,10 @@ export declare class Dependency {
145159

146160
export declare class Diagnostics {
147161
get length(): number
148-
values(): Array<JsRspackError>
149-
get(index: number): JsRspackError | undefined
150-
set(index: number, error: JsRspackError): void
151-
spliceWithArray(index: number, deleteCount?: number | undefined | null, newItems?: Array<JsRspackError> | undefined | null): Array<JsRspackError>
162+
values(): Array<RspackError>
163+
get(index: number): RspackError | undefined
164+
set(index: number, error: RspackError): void
165+
spliceWithArray(index: number, deleteCount?: number | undefined | null, newItems?: Array<RspackError> | undefined | null): Array<RspackError>
152166
}
153167

154168
export declare class EntryDataDto {
@@ -277,8 +291,8 @@ export declare class JsCompilation {
277291
pushNativeDiagnostics(diagnostics: ExternalObject<'Diagnostic[]'>): void
278292
get errors(): Diagnostics
279293
get warnings(): Diagnostics
280-
getErrors(): Array<JsRspackError>
281-
getWarnings(): Array<JsRspackError>
294+
getErrors(): Array<RspackError>
295+
getWarnings(): Array<RspackError>
282296
getStats(): JsStats
283297
getAssetPath(filename: string, data: JsPathData): string
284298
getAssetPathWithInfo(filename: string, data: JsPathData): PathWithInfo
@@ -906,7 +920,7 @@ export interface JsLoaderContext {
906920
loaderItems: Array<JsLoaderItem>
907921
loaderIndex: number
908922
loaderState: Readonly<JsLoaderState>
909-
__internal__error?: JsRspackError
923+
__internal__error?: RspackError
910924
/**
911925
* UTF-8 hint for `content`
912926
* - Some(true): `content` is a `UTF-8` encoded sequence
@@ -1144,17 +1158,7 @@ export interface JsRsdoctorVariable {
11441158

11451159
export interface JsRspackDiagnostic {
11461160
severity: JsRspackSeverity
1147-
error: JsRspackError
1148-
}
1149-
1150-
export interface JsRspackError {
1151-
name: string
1152-
message: string
1153-
moduleIdentifier?: string
1154-
loc?: DependencyLocation
1155-
file?: string
1156-
stack?: string
1157-
hideStack?: boolean
1161+
error: RspackError
11581162
}
11591163

11601164
export declare enum JsRspackSeverity {

crates/node_binding/scripts/banner.d.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ export const BUILD_INFO_MISSING_DEPENDENCIES_SYMBOL: unique symbol;
2323
export const BUILD_INFO_BUILD_DEPENDENCIES_SYMBOL: unique symbol;
2424
export const COMMIT_CUSTOM_FIELDS_SYMBOL: unique symbol;
2525

26+
export const RUST_ERROR_SYMBOL: unique symbol;
27+
2628
interface KnownBuildInfo {
2729
[BUILD_INFO_ASSETS_SYMBOL]: Assets,
2830
[BUILD_INFO_FILE_DEPENDENCIES_SYMBOL]: string[],
@@ -75,6 +77,18 @@ export interface ExternalModule extends Module {
7577
readonly userRequest: string;
7678
}
7779

80+
export interface RspackError extends Error {
81+
name: string;
82+
message: string;
83+
details?: string;
84+
module?: Module;
85+
loc?: DependencyLocation;
86+
file?: string;
87+
stack?: string;
88+
hideStack?: boolean;
89+
error?: Error;
90+
}
91+
7892
export type DependencyLocation = SyntheticDependencyLocation | RealDependencyLocation;
7993
/* -- banner.d.ts end -- */
8094

crates/rspack_binding_api/src/compilation/diagnostics.rs

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use napi::{bindgen_prelude::WeakReference, Either};
22
use rspack_core::Compilation;
33
use rspack_error::RspackSeverity;
44

5-
use crate::{JsCompilation, JsRspackError};
5+
use crate::{JsCompilation, RspackError};
66

77
#[napi]
88
pub struct Diagnostics {
@@ -54,21 +54,19 @@ impl Diagnostics {
5454
}
5555

5656
#[napi]
57-
pub fn values(&self) -> napi::Result<Vec<JsRspackError>> {
57+
pub fn values(&self) -> napi::Result<Vec<RspackError>> {
5858
let compilation = self.as_ref()?;
5959

6060
let diagnostics = compilation.diagnostics();
6161
diagnostics
6262
.iter()
6363
.filter(|diagnostic| diagnostic.severity() == self.severity)
64-
.map(|diagnostic| {
65-
JsRspackError::try_from_diagnostic(diagnostic, compilation.options.stats.colors)
66-
})
67-
.collect::<napi::Result<Vec<JsRspackError>>>()
64+
.map(|diagnostic| RspackError::try_from_diagnostic(compilation, diagnostic))
65+
.collect::<napi::Result<Vec<RspackError>>>()
6866
}
6967

7068
#[napi]
71-
pub fn get(&self, index: f64) -> napi::Result<Either<JsRspackError, ()>> {
69+
pub fn get(&self, index: f64) -> napi::Result<Either<RspackError, ()>> {
7270
if index < 0f64 || index.is_infinite() || index.abs() != index {
7371
return Ok(Either::B(()));
7472
}
@@ -81,16 +79,15 @@ impl Diagnostics {
8179
.nth(index as usize);
8280
Ok(match diagnostic {
8381
Some(diagnostic) => {
84-
let colors = compilation.options.stats.colors;
85-
let js_rspack_error = JsRspackError::try_from_diagnostic(diagnostic, colors)?;
82+
let js_rspack_error = RspackError::try_from_diagnostic(compilation, diagnostic)?;
8683
Either::A(js_rspack_error)
8784
}
8885
None => Either::B(()),
8986
})
9087
}
9188

9289
#[napi]
93-
pub fn set(&mut self, index: f64, error: JsRspackError) -> napi::Result<()> {
90+
pub fn set(&mut self, index: f64, error: RspackError) -> napi::Result<()> {
9491
if index < 0f64 || index.is_infinite() || index.abs() != index {
9592
return Ok(());
9693
}
@@ -132,11 +129,10 @@ impl Diagnostics {
132129
&mut self,
133130
index: f64,
134131
delete_count: Option<f64>,
135-
new_items: Option<Vec<JsRspackError>>,
136-
) -> napi::Result<Vec<JsRspackError>> {
132+
new_items: Option<Vec<RspackError>>,
133+
) -> napi::Result<Vec<RspackError>> {
137134
let severity = self.severity;
138135
let compilation = self.as_mut()?;
139-
let colors = compilation.options.stats.colors;
140136

141137
let diagnostics = compilation.diagnostics_mut();
142138

@@ -197,7 +193,7 @@ impl Diagnostics {
197193

198194
removed
199195
.into_iter()
200-
.map(|d| JsRspackError::try_from_diagnostic(&d, colors))
196+
.map(|d| RspackError::try_from_diagnostic(compilation, &d))
201197
.collect::<napi::Result<Vec<_>>>()
202198
}
203199
}

crates/rspack_binding_api/src/compilation/mod.rs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ use super::PathWithInfo;
2727
use crate::{
2828
entry::JsEntryOptions, utils::callbackify, AssetInfo, EntryDependency, ErrorCode,
2929
JsAddingRuntimeModule, JsAsset, JsChunk, JsChunkGraph, JsChunkGroupWrapper, JsChunkWrapper,
30-
JsCompatSource, JsFilename, JsModuleGraph, JsPathData, JsRspackDiagnostic, JsRspackError,
31-
JsStats, JsStatsOptimizationBailout, ModuleObject, RspackResultToNapiResultExt, ToJsCompatSource,
32-
COMPILER_REFERENCES,
30+
JsCompatSource, JsFilename, JsModuleGraph, JsPathData, JsRspackDiagnostic, JsStats,
31+
JsStatsOptimizationBailout, ModuleObject, RspackError, RspackResultToNapiResultExt,
32+
ToJsCompatSource, COMPILER_REFERENCES,
3333
};
3434

3535
#[napi]
@@ -442,25 +442,22 @@ impl JsCompilation {
442442
}
443443

444444
#[napi]
445-
pub fn get_errors(&self) -> Result<Vec<JsRspackError>> {
445+
pub fn get_errors(&self) -> Result<Vec<RspackError>> {
446446
let compilation = self.as_ref()?;
447447

448-
let colored = compilation.options.stats.colors;
449448
compilation
450449
.get_errors_sorted()
451-
.map(|d| JsRspackError::try_from_diagnostic(d, colored))
450+
.map(|diagnostic| RspackError::try_from_diagnostic(compilation, diagnostic))
452451
.collect()
453452
}
454453

455454
#[napi]
456-
pub fn get_warnings(&self) -> Result<Vec<JsRspackError>> {
455+
pub fn get_warnings(&self) -> Result<Vec<RspackError>> {
457456
let compilation = self.as_ref()?;
458457

459-
let colored = compilation.options.stats.colors;
460-
461458
compilation
462459
.get_warnings_sorted()
463-
.map(|d| JsRspackError::try_from_diagnostic(d, colored))
460+
.map(|diagnostic| RspackError::try_from_diagnostic(compilation, diagnostic))
464461
.collect()
465462
}
466463

crates/rspack_binding_api/src/define_symbols.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ macro_rules! define_symbols {
1414
}
1515

1616
pub(super) fn export_symbols(mut exports: ::napi::bindgen_prelude::Object, env: ::napi::Env) -> ::napi::Result<()> {
17+
use napi::bindgen_prelude::JsObjectValue;
1718
$(
1819
let symbol = ::rspack_napi::OneShotRef::new(env.raw(), env.create_symbol(Some($name))?)?;
1920
exports.set_named_property($name, &symbol)?;

0 commit comments

Comments
 (0)