Skip to content

Commit 31e90b9

Browse files
authored
refactor(cli): use rolldown's disable_panic_hook feature for panic hook (#1330)
Instead of the fragile double `take_hook()` workaround that removed rolldown's panic hook at runtime, use rolldown's new `disable_panic_hook` compile-time feature to prevent it from being installed in the first place. Also moves the panic hook setup into `#[module_init]` for cleaner initialization. Closes #1285
1 parent e0ac321 commit 31e90b9

File tree

2 files changed

+13
-24
lines changed

2 files changed

+13
-24
lines changed

packages/cli/binding/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ vite_static_config = { workspace = true }
3131
vite_str = { workspace = true }
3232
vite_task = { workspace = true }
3333
vite_workspace = { workspace = true }
34-
rolldown_binding = { workspace = true, optional = true }
34+
rolldown_binding = { workspace = true, optional = true, features = ["disable_panic_hook"] }
3535

3636
[build-dependencies]
3737
napi-build = { workspace = true }

packages/cli/binding/src/lib.rs

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,21 @@ use crate::cli::{
2828
BoxedResolverFn, CliOptions as ViteTaskCliOptions, ResolveCommandResult, ViteConfigResolverFn,
2929
};
3030

31-
/// Module initialization - sets up tracing for debugging
31+
/// Module initialization - sets up tracing and panic hook
3232
#[napi_derive::module_init]
33+
#[allow(clippy::disallowed_macros)]
3334
pub fn init() {
3435
crate::cli::init_tracing();
36+
37+
// Install a Vite+ panic hook so panics are correctly attributed to Vite+.
38+
let default_hook = std::panic::take_hook();
39+
std::panic::set_hook(Box::new(move |info| {
40+
eprintln!("Vite+ panicked. This is a bug in Vite+, not your code.");
41+
default_hook(info);
42+
eprintln!(
43+
"\nPlease report this issue at: https://github.com/voidzero-dev/vite-plus/issues/new?template=bug_report.yml"
44+
);
45+
}));
3546
}
3647

3748
/// Configuration options passed from JavaScript to Rust.
@@ -123,35 +134,13 @@ fn format_error_message(error: &(dyn StdError + 'static)) -> String {
123134
message
124135
}
125136

126-
/// Install a Vite+ panic hook so panics are correctly attributed to Vite+.
127-
///
128-
/// Discards any previously set hook (e.g. rolldown's) via double `take_hook`:
129-
/// first call removes the current hook, second captures the restored default.
130-
/// Safe to call regardless of whether a custom hook was installed.
131-
#[allow(clippy::disallowed_macros)]
132-
fn setup_panic_hook() {
133-
static ONCE: std::sync::Once = std::sync::Once::new();
134-
ONCE.call_once(|| {
135-
let _ = std::panic::take_hook();
136-
let default_hook = std::panic::take_hook();
137-
std::panic::set_hook(Box::new(move |info| {
138-
eprintln!("Vite+ panicked. This is a bug in Vite+, not your code.");
139-
default_hook(info);
140-
eprintln!(
141-
"\nPlease report this issue at: https://github.com/voidzero-dev/vite-plus/issues/new?template=bug_report.yml"
142-
);
143-
}));
144-
});
145-
}
146-
147137
/// Main entry point for the CLI, called from JavaScript.
148138
///
149139
/// This is an async function that spawns a new thread for the non-Send async code
150140
/// from vite_task, while allowing the NAPI async context to continue running
151141
/// and process JavaScript callbacks (via ThreadsafeFunction).
152142
#[napi]
153143
pub async fn run(options: CliOptions) -> Result<i32> {
154-
setup_panic_hook();
155144
// Use provided cwd or current directory
156145
let mut cwd = current_dir()?;
157146
if let Some(options_cwd) = options.cwd {

0 commit comments

Comments
 (0)