implement PanicTracker to track t panics

Trying to understand panics triggered by `t` macro calls is very exhausting (especially on CI failures)
because it doesn't provide any information about where the macro was originally invoked. This change adds
that missing information when an inner call inside the `t` macro panics.

Signed-off-by: onur-ozkan <work@onurozkan.dev>
This commit is contained in:
onur-ozkan 2025-05-04 16:41:42 +03:00
parent 6e23095adf
commit de44231d22
2 changed files with 26 additions and 5 deletions

View file

@ -53,6 +53,7 @@ use tracing::{instrument, span};
pub use utils::change_tracker::{
CONFIG_CHANGE_HISTORY, find_recent_config_change_ids, human_readable_changes,
};
pub use utils::helpers::PanicTracker;
use crate::core::build_steps::vendor::VENDOR_DIR;

View file

@ -7,8 +7,9 @@ use std::ffi::OsStr;
use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};
use std::sync::OnceLock;
use std::thread::panicking;
use std::time::{Instant, SystemTime, UNIX_EPOCH};
use std::{env, fs, io, str};
use std::{env, fs, io, panic, str};
use build_helper::util::fail;
use object::read::archive::ArchiveFile;
@ -22,6 +23,23 @@ pub use crate::utils::shared_helpers::{dylib_path, dylib_path_var};
#[cfg(test)]
mod tests;
/// A wrapper around `std::panic::Location` used to track the location of panics
/// triggered by `t` macro usage.
pub struct PanicTracker<'a>(pub &'a panic::Location<'a>);
impl Drop for PanicTracker<'_> {
fn drop(&mut self) {
if panicking() {
eprintln!(
"Panic was initiated from {}:{}:{}",
self.0.file(),
self.0.line(),
self.0.column()
);
}
}
}
/// A helper macro to `unwrap` a result except also print out details like:
///
/// * The file/line of the panic
@ -32,19 +50,21 @@ mod tests;
/// using a `Result` with `try!`, but this may change one day...
#[macro_export]
macro_rules! t {
($e:expr) => {
($e:expr) => {{
let _panic_guard = $crate::PanicTracker(std::panic::Location::caller());
match $e {
Ok(e) => e,
Err(e) => panic!("{} failed with {}", stringify!($e), e),
}
};
}};
// it can show extra info in the second parameter
($e:expr, $extra:expr) => {
($e:expr, $extra:expr) => {{
let _panic_guard = $crate::PanicTracker(std::panic::Location::caller());
match $e {
Ok(e) => e,
Err(e) => panic!("{} failed with {} ({:?})", stringify!($e), e, $extra),
}
};
}};
}
pub use t;