Skip to content
Prev Previous commit
Next Next commit
testutils: add custom backtrace frame filter to color-eyre
Filter backtrace frames to show only project code (jj_lib and jj_cli),
removing noise from dependencies and standard library to make error
reports easier to read during testing.
  • Loading branch information
glehmann committed Mar 18, 2026
commit dbdc0382bb975cb30260a54d439e65631e5d4e8e
14 changes: 13 additions & 1 deletion lib/testutils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,19 @@ pub mod test_backend;

#[ctor::ctor]
fn init_color_eyre() {
drop(color_eyre::install());
drop(
color_eyre::config::HookBuilder::default()
.add_frame_filter(Box::new(|frames| {
frames.retain(|frame| {
frame
.name
.as_ref()
.map(|name| name.starts_with("jj_"))
Copy link
Contributor

@ilyagr ilyagr Mar 18, 2026

Choose a reason for hiding this comment

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

It's nice that color-eyre has filtering functionality! That could be very helpful.

I don't know if this filter is correct for all use-cases, though. Ideally, there's be a message letting you know that you can set RUST_BACKTRACE=full to get the full backtrace in the cases where this filters too much. (I haven't checked how easy that is to do).

As per my other message, I'm thinking of panic-s, not TestResult use-cases.

Aside: My PR has an example of a more complicated-looking filter that's similar to default panic handler frame filtering logic, for comparison. I'm not sure that's necessarily better; this stricter filter could be good in 90% of the cases if there was a way to disable it in the other 10%.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There is an env var to set to disable the filtering, and it's displayed with the backtrace—see the screenshots in #9111 (comment)

image

The filtering is especially useful with #[tokio::test]: the backtraces are much longer in that case—29 frames instead of just 7 with the #[test] for the same test failure. #[pollster::test] is a bit more at 9 frames.

I like the way the error chains are displayed, but eyre alone already displays them well.

It may not be perfect every time, but it should work in most cases, produce much more compact and readable backtraces, also work with panics, and the filtering is easily deactivable when needed. I like it :)

Copy link
Contributor

Choose a reason for hiding this comment

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

Have you tested that it works, that your code doesn't override it?

It's fine if the filtering is not completely perfect, as long as it can be disabled; we can always fine-tune or even change our mind about using color_eyre later. So, I think that as long as COLORBT_SHOW_HIDDEN works, that's sufficient.

.unwrap_or(false)
});
}))
.install(),
);
}

/// Convenient return type for test functions.
Expand Down