I did some tests, it seems the standard library resolves backtrace differently in a test than in a binary. I'm not sure why. but the backtrace crate does resolve the full path correctly, both in unittests and in main.
the workspace:
βββ Cargo.lock
βββ Cargo.toml
βββ helloworld
βββ Cargo.toml
βββ src
βββ main.rs
the source code
fn main() {
let trace = backtrace::Backtrace::new();
let stdtrace = std::backtrace::Backtrace::capture();
dbg!(trace);
dbg!(stdtrace);
dbg!(file!());
panic!();
}
#[test]
fn test() {
let trace = backtrace::Backtrace::new();
let stdtrace = std::backtrace::Backtrace::capture();
dbg!(trace);
dbg!(stdtrace);
dbg!(file!());
panic!();
}
with RUST_BACKTRACE=1 cargo run, output abbreviated, all is expected:
Running `target/debug/helloworld`
[helloworld/src/main.rs:4:2] trace = 0: 0x556833034448 - helloworld[425c6445f077211c]::main
at /tmp/workspace/helloworld/src/main.rs:2:14
1: 0x556833034e5b - <fn() as core[6fcd33f5c444871b]::ops::function::FnOnce<()>>::call_once
at /home/nerditation/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/ops/function.rs:250:5
...
10: 0x0 - <unknown>
[helloworld/src/main.rs:5:2] stdtrace = Backtrace [
{ fn: "helloworld::main", file: "./helloworld/src/main.rs", line: 3 },
...
{ fn: "_start", file: "/home/abuild/rpmbuild/BUILD/glibc-2.44-build/glibc-2.44/csu/../sysdeps/x86_64/start.S", line: 115 },
]
[helloworld/src/main.rs:6:2] file!() = "helloworld/src/main.rs"
thread 'main' (39703) panicked at helloworld/src/main.rs:7:5:
explicit panic
stack backtrace:
0: __rustc::rust_begin_unwind
...
3: helloworld::main
at ./helloworld/src/main.rs:7:2
...
with RUST_BACKTRACE=1 cargo test, output abbreviated, the std backtrace is relative to the package root, not the workspace root:
Running unittests src/main.rs (/tmp/cargo-build/debug/deps/helloworld-b1be72998f585a02)
running 1 test
test test ... FAILED
failures:
---- test stdout ----
[helloworld/src/main.rs:14:2] trace = 0: 0x560b292c1858 - helloworld[197f6df0611322d7]::test
at /tmp/workspace/helloworld/src/main.rs:12:14
1: 0x560b292c0fc7 - helloworld[197f6df0611322d7]::test::{closure#0}
at /tmp/workspace/helloworld/src/main.rs:11:10
...
10: 0x0 - <unknown>
[helloworld/src/main.rs:15:2] stdtrace = Backtrace [
{ fn: "helloworld::test", file: "./src/main.rs", line: 13 },
{ fn: "helloworld::test::{closure#0}", file: "./src/main.rs", line: 11 },
...
{ fn: "start_thread" },
{ fn: "__clone3" },
]
[helloworld/src/main.rs:16:2] file!() = "helloworld/src/main.rs"
thread 'test' (40015) panicked at helloworld/src/main.rs:17:5:
explicit panic
stack backtrace:
0: __rustc::rust_begin_unwind
at /rustc/d7f14d3d89c39b2c12ed4b9864a0eb2c205d20bc/library/std/src/
...
3: helloworld::test
at ./src/main.rs:17:2
4: helloworld::test::{closure#0}
at ./src/main.rs:11:10
...
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
failures:
test
test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.11s