Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add debug info under panic with debug build #4940

Merged
merged 3 commits into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
14 changes: 12 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,15 @@ 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;
eprintln!("{}", DEBUG_INFO.lock().unwrap());
}
panic_handler(panic);
}))
}
7 changes: 7 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,13 @@
{
#[instrument(skip_all)]
pub fn new(options: CompilerOptions, plugins: Vec<BoxPlugin>, output_filesystem: T) -> Self {
#[cfg(debug_assertions)]
{
crate::debug_info::DEBUG_INFO

Check failure on line 57 in crates/rspack_core/src/compiler/mod.rs

View workflow job for this annotation

GitHub Actions / Rust check

used `unwrap()` on a `Result` value
.lock()
.unwrap()
.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!("{tt}: <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
Loading