Skip to content
Merged
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
2 changes: 1 addition & 1 deletion packages/cli/binding/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ vite_static_config = { workspace = true }
vite_str = { workspace = true }
vite_task = { workspace = true }
vite_workspace = { workspace = true }
rolldown_binding = { workspace = true, optional = true }
rolldown_binding = { workspace = true, optional = true, features = ["disable_panic_hook"] }

[build-dependencies]
napi-build = { workspace = true }
Expand Down
35 changes: 12 additions & 23 deletions packages/cli/binding/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,21 @@ use crate::cli::{
BoxedResolverFn, CliOptions as ViteTaskCliOptions, ResolveCommandResult, ViteConfigResolverFn,
};

/// Module initialization - sets up tracing for debugging
/// Module initialization - sets up tracing and panic hook
#[napi_derive::module_init]
#[allow(clippy::disallowed_macros)]
pub fn init() {
crate::cli::init_tracing();

// Install a Vite+ panic hook so panics are correctly attributed to Vite+.
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"
);
}));
}

/// Configuration options passed from JavaScript to Rust.
Expand Down Expand Up @@ -123,35 +134,13 @@ 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();
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