Skip to content

fix: rspack error communicate between js and rust #10595

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 19 additions & 23 deletions crates/node_binding/binding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,16 @@ export interface ContextModule extends Module {
export interface ExternalModule extends Module {
readonly userRequest: string;
}

export interface RspackError {
name: string;
message: string;
module?: Module;
loc?: string;
file?: string;
stack?: string;
hideStack?: boolean;
}
/* -- banner.d.ts end -- */

/* -- napi-rs generated below -- */
Expand Down Expand Up @@ -140,10 +150,10 @@ export declare class Dependency {

export declare class Diagnostics {
get length(): number
values(): Array<JsRspackError>
get(index: number): JsRspackError | undefined
set(index: number, error: JsRspackError): void
spliceWithArray(index: number, deleteCount?: number | undefined | null, newItems?: Array<JsRspackError> | undefined | null): Array<JsRspackError>
values(): Array<RspackError>
get(index: number): RspackError | undefined
set(index: number, error: RspackError): void
spliceWithArray(index: number, deleteCount?: number | undefined | null, newItems?: Array<RspackError> | undefined | null): Array<RspackError>
}

export declare class EntryDataDto {
Expand Down Expand Up @@ -271,8 +281,8 @@ export declare class JsCompilation {
pushNativeDiagnostics(diagnostics: ExternalObject<'Diagnostic[]'>): void
get errors(): Diagnostics
get warnings(): Diagnostics
getErrors(): Array<JsRspackError>
getWarnings(): Array<JsRspackError>
getErrors(): Array<RspackError>
getWarnings(): Array<RspackError>
getStats(): JsStats
getAssetPath(filename: string, data: JsPathData): string
getAssetPathWithInfo(filename: string, data: JsPathData): PathWithInfo
Expand Down Expand Up @@ -398,11 +408,7 @@ export declare class JsStats {
}

export declare class KnownBuildInfo {
get _assets(): Assets
get _fileDependencies(): Array<string>
get _contextDependencies(): Array<string>
get _missingDependencies(): Array<string>
get _buildDependencies(): Array<string>

}

export declare class Module {
Expand Down Expand Up @@ -900,7 +906,7 @@ export interface JsLoaderContext {
loaderItems: Array<JsLoaderItem>
loaderIndex: number
loaderState: Readonly<JsLoaderState>
__internal__error?: JsRspackError
__internal__error?: RspackError
/**
* UTF-8 hint for `content`
* - Some(true): `content` is a `UTF-8` encoded sequence
Expand Down Expand Up @@ -1143,17 +1149,7 @@ export interface JsRsdoctorVariable {

export interface JsRspackDiagnostic {
severity: JsRspackSeverity
error: JsRspackError
}

export interface JsRspackError {
name: string
message: string
moduleIdentifier?: string
loc?: string
file?: string
stack?: string
hideStack?: boolean
error: RspackError
}

export declare enum JsRspackSeverity {
Expand Down
10 changes: 10 additions & 0 deletions crates/node_binding/scripts/banner.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,16 @@ export interface ContextModule extends Module {
export interface ExternalModule extends Module {
readonly userRequest: string;
}

export interface RspackError {
name: string;
message: string;
module?: Module;
loc?: string;
file?: string;
stack?: string;
hideStack?: boolean;
}
/* -- banner.d.ts end -- */

/* -- napi-rs generated below -- */
24 changes: 10 additions & 14 deletions crates/node_binding/src/compilation/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use napi::{bindgen_prelude::WeakReference, Either};
use rspack_core::Compilation;
use rspack_error::RspackSeverity;

use crate::{JsCompilation, JsRspackError};
use crate::{JsCompilation, RspackError};

#[napi]
pub struct Diagnostics {
Expand Down Expand Up @@ -54,21 +54,19 @@ impl Diagnostics {
}

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

let diagnostics = compilation.diagnostics();
diagnostics
.iter()
.filter(|diagnostic| diagnostic.severity() == self.severity)
.map(|diagnostic| {
JsRspackError::try_from_diagnostic(diagnostic, compilation.options.stats.colors)
})
.collect::<napi::Result<Vec<JsRspackError>>>()
.map(|diagnostic| RspackError::try_from_diagnostic(compilation, diagnostic))
.collect::<napi::Result<Vec<RspackError>>>()
}

#[napi]
pub fn get(&self, index: f64) -> napi::Result<Either<JsRspackError, ()>> {
pub fn get(&self, index: f64) -> napi::Result<Either<RspackError, ()>> {
if index < 0f64 || index.is_infinite() || index.abs() != index {
return Ok(Either::B(()));
}
Expand All @@ -81,16 +79,15 @@ impl Diagnostics {
.nth(index as usize);
Ok(match diagnostic {
Some(diagnostic) => {
let colors = compilation.options.stats.colors;
let js_rspack_error = JsRspackError::try_from_diagnostic(diagnostic, colors)?;
let js_rspack_error = RspackError::try_from_diagnostic(compilation, diagnostic)?;
Either::A(js_rspack_error)
}
None => Either::B(()),
})
}

#[napi]
pub fn set(&mut self, index: f64, error: JsRspackError) -> napi::Result<()> {
pub fn set(&mut self, index: f64, error: RspackError) -> napi::Result<()> {
if index < 0f64 || index.is_infinite() || index.abs() != index {
return Ok(());
}
Expand Down Expand Up @@ -132,11 +129,10 @@ impl Diagnostics {
&mut self,
index: f64,
delete_count: Option<f64>,
new_items: Option<Vec<JsRspackError>>,
) -> napi::Result<Vec<JsRspackError>> {
new_items: Option<Vec<RspackError>>,
) -> napi::Result<Vec<RspackError>> {
let severity = self.severity;
let compilation = self.as_mut()?;
let colors = compilation.options.stats.colors;

let diagnostics = compilation.diagnostics_mut();

Expand Down Expand Up @@ -197,7 +193,7 @@ impl Diagnostics {

removed
.into_iter()
.map(|d| JsRspackError::try_from_diagnostic(&d, colors))
.map(|d| RspackError::try_from_diagnostic(compilation, &d))
.collect::<napi::Result<Vec<_>>>()
}
}
17 changes: 7 additions & 10 deletions crates/node_binding/src/compilation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ use super::PathWithInfo;
use crate::{
entry::JsEntryOptions, utils::callbackify, AssetInfo, EntryDependency, ErrorCode,
JsAddingRuntimeModule, JsAsset, JsChunk, JsChunkGraph, JsChunkGroupWrapper, JsChunkWrapper,
JsCompatSource, JsFilename, JsModuleGraph, JsPathData, JsRspackDiagnostic, JsRspackError,
JsStats, JsStatsOptimizationBailout, ModuleObject, RspackResultToNapiResultExt, ToJsCompatSource,
COMPILER_REFERENCES,
JsCompatSource, JsFilename, JsModuleGraph, JsPathData, JsRspackDiagnostic, JsStats,
JsStatsOptimizationBailout, ModuleObject, RspackError, RspackResultToNapiResultExt,
ToJsCompatSource, COMPILER_REFERENCES,
};

#[napi]
Expand Down Expand Up @@ -441,25 +441,22 @@ impl JsCompilation {
}

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

let colored = compilation.options.stats.colors;
compilation
.get_errors_sorted()
.map(|d| JsRspackError::try_from_diagnostic(d, colored))
.map(|d| RspackError::try_from_diagnostic(compilation, d))
.collect()
}

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

let colored = compilation.options.stats.colors;

compilation
.get_warnings_sorted()
.map(|d| JsRspackError::try_from_diagnostic(d, colored))
.map(|d| RspackError::try_from_diagnostic(compilation, d))
.collect()
}

Expand Down
Loading
Loading