Skip to content
Open
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
22 changes: 22 additions & 0 deletions packages/cli/binding/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,35 @@ fn format_error_message(error: &(dyn StdError + 'static)) -> String {
message
}

/// Install a Vite+ panic hook so panics are correctly attributed to Vite+.
///
/// Discards any previously set hook (e.g. rolldown's) via double `take_hook`:
/// first call removes the current hook, second captures the restored default.
/// Safe to call regardless of whether a custom hook was installed.
#[allow(clippy::disallowed_macros)]
fn setup_panic_hook() {
static ONCE: std::sync::Once = std::sync::Once::new();
ONCE.call_once(|| {
let _ = std::panic::take_hook();
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

#1285 (comment) It would be better if there was a way to disable rolldown's panic hook

let default_hook = std::panic::take_hook();
std::panic::set_hook(Box::new(move |info| {
eprintln!("Vite+ panicked. This is a bug in Vite+, not your code.");
default_hook(info);
eprintln!(
"\nPlease report this issue at: https://github.com/voidzero-dev/vite-plus/issues/new?template=bug_report.yml"
);
}));
});
}

/// Main entry point for the CLI, called from JavaScript.
///
/// This is an async function that spawns a new thread for the non-Send async code
/// from vite_task, while allowing the NAPI async context to continue running
/// and process JavaScript callbacks (via ThreadsafeFunction).
#[napi]
pub async fn run(options: CliOptions) -> Result<i32> {
setup_panic_hook();
// Use provided cwd or current directory
let mut cwd = current_dir()?;
if let Some(options_cwd) = options.cwd {
Expand Down
Loading