Skip to content

Commit dde546f

Browse files
committed
fix(cli): override rolldown panic hook with vite-plus branding (#1287)
Since rolldown_binding is bundled into the same NAPI binary, its module_init panic hook catches all panics and misleadingly shows "Rolldown panicked" with a link to rolldown's issue tracker. Replace it at the start of run() with a vite-plus specific hook that correctly attributes panics and links to the vite-plus bug report. Closes #1285
1 parent d7b9b6a commit dde546f

File tree

1 file changed

+22
-0
lines changed
  • packages/cli/binding/src

1 file changed

+22
-0
lines changed

packages/cli/binding/src/lib.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,13 +123,35 @@ fn format_error_message(error: &(dyn StdError + 'static)) -> String {
123123
message
124124
}
125125

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+
126147
/// Main entry point for the CLI, called from JavaScript.
127148
///
128149
/// This is an async function that spawns a new thread for the non-Send async code
129150
/// from vite_task, while allowing the NAPI async context to continue running
130151
/// and process JavaScript callbacks (via ThreadsafeFunction).
131152
#[napi]
132153
pub async fn run(options: CliOptions) -> Result<i32> {
154+
setup_panic_hook();
133155
// Use provided cwd or current directory
134156
let mut cwd = current_dir()?;
135157
if let Some(options_cwd) = options.cwd {

0 commit comments

Comments
 (0)