Skip to content

Commit

Permalink
chore: add debug info under panic with debug build (#4940)
Browse files Browse the repository at this point in the history
* chore: add debug info under panic with debug build

* fix

* fix
  • Loading branch information
h-a-n-a authored Dec 7, 2023
1 parent 321956f commit e064fb6
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 2 deletions.
16 changes: 14 additions & 2 deletions crates/node_binding/src/panic.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use color_backtrace::{default_output_stream, BacktracePrinter};

pub fn install_panic_handler() {
BacktracePrinter::default()
let panic_handler = BacktracePrinter::default()
.message("Panic occurred at runtime. Please file an issue on GitHub with the backtrace below: https://github.com/web-infra-dev/rspack/issues")
.add_frame_filter(Box::new(|frames| {
static NAME_PREFIXES: &[&str] = &[
Expand Down Expand Up @@ -48,5 +48,17 @@ pub fn install_panic_handler() {
.verbosity(color_backtrace::Verbosity::Medium)
.lib_verbosity(color_backtrace::Verbosity::Medium)
.print_addresses(false)
.install(default_output_stream());
// .install(default_output_stream());
.into_panic_handler(default_output_stream());

std::panic::set_hook(Box::new(move |panic| {
#[cfg(debug_assertions)]
{
use rspack_core::debug_info::DEBUG_INFO;
if let Ok(info) = DEBUG_INFO.lock() {
eprintln!("{}", info);
}
}
panic_handler(panic);
}))
}
6 changes: 6 additions & 0 deletions crates/rspack_core/src/compiler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ where
{
#[instrument(skip_all)]
pub fn new(options: CompilerOptions, plugins: Vec<BoxPlugin>, output_filesystem: T) -> Self {
#[cfg(debug_assertions)]
{
if let Ok(mut debug_info) = crate::debug_info::DEBUG_INFO.lock() {
debug_info.with_context(options.context.to_string());
}
}
let new_resolver = options.experiments.rspack_future.new_resolver;
let resolver_factory = Arc::new(ResolverFactory::new(new_resolver, options.resolve.clone()));
let loader_resolver_factory = Arc::new(ResolverFactory::new(
Expand Down
44 changes: 44 additions & 0 deletions crates/rspack_core/src/debug_info.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
use std::fmt::Display;
use std::sync::Mutex;

/// Debug info used when programs panics
/// Only works with #[cfg(debug_assertions)]
pub struct DebugInfo {
/// The base directory. See [options.context](https://webpack.js.org/configuration/entry-context/#context)
pub(crate) context: Option<String>,
}

impl DebugInfo {
const fn new() -> Self {
Self { context: None }
}

pub(crate) fn with_context(&mut self, context: String) -> &mut Self {
self.context = Some(context);
self
}
}

macro_rules! write_debug_info {
($f:ident,$tt:tt,$expr:expr) => {
let tt = $tt.to_string();
if let Some(e) = &$expr {
let e = format!("\u{001b}[1m\u{001b}[33m{tt}\u{001b}[0m: {e}");
writeln!($f, "{}", e)?;
} else {
let e = format!("\u{001b}[1m\u{001b}[33m{tt}\u{001b}[0m: <empty>");
writeln!($f, "{}", e)?;
}
};
}

impl Display for DebugInfo {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
writeln!(f, "DebugInfo:\n")?;
write_debug_info!(f, "context", &self.context);

Ok(())
}
}

pub static DEBUG_INFO: Mutex<DebugInfo> = Mutex::new(DebugInfo::new());
3 changes: 3 additions & 0 deletions crates/rspack_core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ pub mod tree_shaking;
pub use rspack_loader_runner::{get_scheme, ResourceData, Scheme, BUILTIN_LOADER_PREFIX};
pub use rspack_sources;

#[cfg(debug_assertions)]
pub mod debug_info;

#[derive(Default, Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum SourceType {
JavaScript,
Expand Down

0 comments on commit e064fb6

Please sign in to comment.