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 25 commits into
base: main
Choose a base branch
from
Draft

Conversation

SyMind
Copy link
Member

@SyMind SyMind commented Jun 6, 2025

Summary

This PR resolves an issue where reassigning compilation.errors[0] caused error messages to degrade to unhelpful [object Object] output:

compilation.errors[0] = compilation.errors[0];

Error message became corrupted:

× Error: [object Object]

After this PR error message is preserved.

Rust RspackError should accept JavaScript Error objects and reads these properties:

interface RspackError {
	name: string;
	message: string;
	details?: string;
	module?: Module;
	loc?: string;
	file?: string;
	stack?: string;
	hideStack?: boolean;
        error?: RspackError;
}

When converting RspackError to Diagnostic, RspackError now maps name to specific Rust error types (e.g., ModuleBuildError for clearer categorization.

This PR modify the following test cases as they no longer align with current error handling behavior:

  1. packages/rspack-test-tools/tests/configCases/errors/hide-stack/

The hideStack property has no effect on RspackError objects. This test case might have been intended to verify hideStack behavior for StatsError and StatsWarning instead.

Checklist

  • Tests updated (or not required).
  • Documentation updated (or not required).

@github-actions github-actions bot added team The issue/pr is created by the member of Rspack. release: bug fix release: bug related release(mr only) labels Jun 6, 2025
Copy link

netlify bot commented Jun 6, 2025

Deploy Preview for rspack canceled.

Name Link
🔨 Latest commit 92df1d0
🔍 Latest deploy log https://app.netlify.com/projects/rspack/deploys/6853dda8208bed0008403f0b

Copy link

codspeed-hq bot commented Jun 6, 2025

CodSpeed Performance Report

Merging #10595 will not alter performance

Comparing fix-rspack-error (92df1d0) with main (77821f5)

Summary

✅ 16 untouched benchmarks

@SyMind SyMind force-pushed the fix-rspack-error branch from 7ed7c62 to be37ba1 Compare June 16, 2025 07:59
@SyMind SyMind force-pushed the fix-rspack-error branch from 3c0a232 to 669df4d Compare June 16, 2025 11:09
@SyMind SyMind force-pushed the fix-rspack-error branch from 669df4d to 281433a Compare June 16, 2025 11:09
@SyMind SyMind force-pushed the fix-rspack-error branch from 76227bb to d579051 Compare June 17, 2025 02:28
@SyMind SyMind marked this pull request as ready for review June 17, 2025 03:08
@SyMind SyMind requested review from h-a-n-a and Copilot June 17, 2025 03:08
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR refactors error handling to preserve messages and differentiate error types between JavaScript and Rust, cleans up obsolete hideStack behavior and tests, and updates Node binding modules to marshal errors and modules consistently.

  • Remove legacy hideStack tests that no longer align with current error handling.
  • Extend Diagnostic and introduce RspackError to carry message/details and map names to Rust error types.
  • Update Node-binding code (scheduler, context, module, error, compilation) for accurate marshalling of errors and modules.

Reviewed Changes

Copilot reviewed 36 out of 36 changed files in this pull request and generated no comments.

Show a summary per file
File Description
packages/rspack-test-tools/tests/diagnosticsCases/module-build-failed/loader-emit-hide-stack/my-loader.js Removed outdated loader hideStack test
packages/rspack-test-tools/tests/configCases/errors/hide-stack/** Removed obsolete hideStack config tests
crates/rspack_error/src/diagnostic.rs Added message/details fields and accessors to Diagnostic
crates/rspack_core/src/normal_module.rs Switched to ModuleBuildError::new(...) constructor usage
crates/rspack_core/src/diagnostics.rs Wrapped ModuleBuildError in a new struct with new method
crates/node_binding/src/plugins/js_loader/scheduler.rs Improved JS→Rust error message selection logic
crates/node_binding/src/plugins/js_loader/context.rs Renamed JS error type to RspackError
crates/node_binding/src/module.rs Adjusted ModuleObject sync/ownership handling
crates/node_binding/src/location.rs Derived Clone and added bidirectional conversions for types
crates/node_binding/src/error.rs Introduced RspackError for JS-Rust diagnostic marshalling
crates/node_binding/src/compilation/mod.rs Swapped JsRspackError for RspackError in compilation APIs
crates/node_binding/src/compilation/diagnostics.rs Updated diagnostics APIs to return RspackError
Comments suppressed due to low confidence (5)

crates/node_binding/src/plugins/js_loader/scheduler.rs:88

  • [nitpick] Comments are written in Chinese; consider translating them to English to maintain consistency with the rest of the codebase.
// 将 JS 传来的 Error message 转换为 Rust 需要的格式

crates/rspack_error/src/diagnostic.rs:172

  • [nitpick] The message() method strips the "Name: " prefix by string manipulation. It may be more robust to use miette APIs that separate error code and message, avoiding manual string hacks.
pub fn message(&self) -> String {

packages/rspack-test-tools/tests/configCases/errors/hide-stack/index.js:1

  • Removing these hide-stack tests reduces coverage for stack handling in StatsError and StatsWarning. Ensure you add or update tests to cover the new error formatting behavior.
-it("should hide stack in details when throw", function () {

crates/node_binding/src/module.rs:474

  • Implementing Sync for ModuleObject with an internal raw pointer may introduce race conditions if the underlying Module isn't thread-safe. Please verify that accesses to module across threads are properly synchronized or consider using a thread-safe wrapper.
unsafe impl Sync for ModuleObject {}

crates/node_binding/src/module.rs:616

  • Removing the .take() call changes ownership semantics of the raw module pointer, potentially leading to dangling pointers or memory leaks. Confirm the pointer’s lifetime remains valid and that no double-free scenarios are introduced.
module: normal_module.module.module,

@SyMind SyMind marked this pull request as draft June 17, 2025 03:41
@SyMind SyMind force-pushed the fix-rspack-error branch from 64c6799 to 9169c4e Compare June 18, 2025 03:22
@SyMind SyMind force-pushed the fix-rspack-error branch from e515b8a to 92df1d0 Compare June 19, 2025 09:51
@SyMind SyMind closed this Jun 19, 2025
@SyMind SyMind reopened this Jun 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release: bug fix release: bug related release(mr only) team The issue/pr is created by the member of Rspack.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant