Skip to content

Commit

Permalink
Remove backtraces on error when running rerun binary (#4746)
Browse files Browse the repository at this point in the history
### What
* Closes #4738

Starting in [`anyhow
1.0.77`](https://github.com/dtolnay/anyhow/releases/tag/1.0.77), a
backtrace will be printed on error if `RUST_BACKTRACE` is set, and we
set `RUST_BACKTRACE=1` in:


https://github.com/rerun-io/rerun/blob/d204936e4a186b6f264425f7a3d3849f94b7b86f/crates/re_log/src/setup.rs#L32-L35

We do this because we want to print stack traces on panics.

However, for `anyhow::Error` our error messages _should_ be good enough
to not require a stack trace,
so with this PR we now explicitly print out the error instead of relying
in the formatting that happens when returning `anyhow::Result` from
`main`.

### Result
```
❯ cargo run -p rerun-cli --quiet -- .github/workflows/documentation.yaml    
[2024-01-09T10:09:31Z INFO  re_data_source::load_file] Loading ".github/workflows/documentation.yaml"…
Error: ".github/workflows/documentation.yaml" -> No data-loader support for ".github/workflows/documentation.yaml"
```

### Checklist
* [x] I have read and agree to [Contributor
Guide](https://github.com/rerun-io/rerun/blob/main/CONTRIBUTING.md) and
the [Code of
Conduct](https://github.com/rerun-io/rerun/blob/main/CODE_OF_CONDUCT.md)
* [x] I've included a screenshot or gif (if applicable)
* [x] I have tested the web demo (if applicable):
* Using newly built examples:
[app.rerun.io](https://app.rerun.io/pr/4746/index.html)
* Using examples from latest `main` build:
[app.rerun.io](https://app.rerun.io/pr/4746/index.html?manifest_url=https://app.rerun.io/version/main/examples_manifest.json)
* Using full set of examples from `nightly` build:
[app.rerun.io](https://app.rerun.io/pr/4746/index.html?manifest_url=https://app.rerun.io/version/nightly/examples_manifest.json)
* [x] The PR title and labels are set such as to maximize their
usefulness for the next release's CHANGELOG

- [PR Build Summary](https://build.rerun.io/pr/4746)
- [Docs
preview](https://rerun.io/preview/dfadee45bb4767f1e81e2459b8e3f93aabcb5c39/docs)
<!--DOCS-PREVIEW-->
- [Examples
preview](https://rerun.io/preview/dfadee45bb4767f1e81e2459b8e3f93aabcb5c39/examples)
<!--EXAMPLES-PREVIEW-->
- [Recent benchmark results](https://build.rerun.io/graphs/crates.html)
- [Wasm size tracking](https://build.rerun.io/graphs/sizes.html)
  • Loading branch information
emilk authored Jan 9, 2024
1 parent d204936 commit 97acc3e
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 10 deletions.
5 changes: 3 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ emath = "0.24.1"

# All of our direct external dependencies should be found here:
ahash = "0.8"
anyhow = "1.0"
anyhow = { version = "1.0", default-features = false }
arboard = { version = "3.2", default-features = false }
argh = "0.1.12"
array-init = "2.1"
Expand Down
2 changes: 1 addition & 1 deletion crates/re_build_tools/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ all-features = true


[dependencies]
anyhow.workspace = true
anyhow = { workspace = true, default-features = true }
cargo_metadata.workspace = true
glob.workspace = true
sha2.workspace = true
Expand Down
11 changes: 9 additions & 2 deletions crates/re_log/src/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,15 @@ pub fn default_log_filter() -> String {
/// Directs [`log`] calls to stderr.
#[cfg(not(target_arch = "wasm32"))]
pub fn setup_native_logging() {
if std::env::var("RUST_BACKTRACE").is_err() {
// Make sure we always produce backtraces for the (hopefully rare) cases when we crash!
if cfg!(debug_assertions) && std::env::var("RUST_BACKTRACE").is_err() {
// In debug build, default `RUST_BACKTRACE` to `1` if it is not set.
// This ensures sure we produce backtraces if our examples (etc) panics.

// Our own crash handler (`re_crash_handler`) always prints a backtraces
// (currently ignoring `RUST_BACKTRACE`) but we only use that for `rerun-cli`, our main binary.

// `RUST_BACKTRACE` also turns on printing backtraces for `anyhow::Error`s that
// are returned from `main` (i.e. if `main` returns `anyhow::Result`).
std::env::set_var("RUST_BACKTRACE", "1");
}

Expand Down
1 change: 1 addition & 0 deletions crates/rerun-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ web_viewer = ["rerun/web_viewer", "rerun/sdk"]

[dependencies]
re_build_info.workspace = true
re_error.workspace = true
re_log.workspace = true
re_memory.workspace = true
rerun = { workspace = true, features = [
Expand Down
17 changes: 13 additions & 4 deletions crates/rerun-cli/src/bin/rerun.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ static GLOBAL: AccountingAllocator<mimalloc::MiMalloc> =
AccountingAllocator::new(mimalloc::MiMalloc);

#[tokio::main]
async fn main() -> anyhow::Result<std::process::ExitCode> {
async fn main() -> std::process::ExitCode {
re_log::setup_native_logging();

// Name the rayon threads for the benefit of debuggers and profilers:
Expand All @@ -27,7 +27,16 @@ async fn main() -> anyhow::Result<std::process::ExitCode> {
.unwrap();

let build_info = re_build_info::build_info!();
rerun::run(build_info, rerun::CallSource::Cli, std::env::args())
.await
.map(std::process::ExitCode::from)

let result = rerun::run(build_info, rerun::CallSource::Cli, std::env::args()).await;

match result {
Ok(exit_code) => std::process::ExitCode::from(exit_code),
Err(err) => {
// Note: we do not print the backtrace here, because our error messages should be short, readable, and actionable.
// If we instead return an `anyhow::Result` from `main`, then the backtrace will be printed if `RUST_BACKTRACE=1`.
eprintln!("Error: {}", re_error::format(err));
std::process::ExitCode::FAILURE
}
}
}

0 comments on commit 97acc3e

Please sign in to comment.