VS Code, test panic, can't click and go to stack trace locations

I thought this used to work. I run a unit test in VS Code, and it panics. In the terminal I see stuff like:

1: core::panicking::panic_fmt
             at /rustc/[...]\core\src\panicking.rs:80
2: bambu_ui::doc_editor::model::tests::check_model_xml
             at .\src\doc_editor\model\tests\mod.rs:14
3: bambu_ui::doc_editor::model::tests::sections::create_section1
             at .\src\doc_editor\model\tests\sections.rs:20
...

But if I control-click on those locations, it just copies them up to the command palette area and says "No matching results". I can thn manually edit it so instead of .\src\... it starts with my package name, bambu-ui\src\..., it works. Actually I can just remove the leading . and it also matches and finds it.

Is something misconfigured? I'm in a workspace, so I'm running tests in one of the packages inside the workspace. I'm running by clicking on the ":play_button: Run Test" in the editor above the test fn.

Please open a new issue at Sign in to GitHub Β· GitHub. Follow the template and be mindful of the AI policy.

This has nothing to do with rust-analyzer.

Are you using a cargo workspace in the root of the vscode workspace? Paths in panic messages should be relative to the cargo workspace root.

Yes, I have a cargo workspace. But it's producing that path in the stack trace as a path relative to one of the members. When I press that ":play_button: Run Test", I can see it puts this command in the Terminal:

 C:\Users\rober\.cargo\bin\cargo.EXE test --package bambu-ui --lib -- doc_editor::model::tests::xml::test_panic_and_vs_code_paths --exact --nocapture --include-ignored 

That test function is simply a panic!. I then get this, with the paths not relative to the workspace root. Well, the first path is – it starts with bambu-ui\src, but the ones in the stack are not.

thread 'doc_editor::model::tests::xml::test_panic_and_vs_code_paths' (35788) panicked at bambu-ui\src\doc_editor\model\tests\xml.rs:89:5:
Test panic
stack backtrace:
   0: std::panicking::panic_handler
             at /rustc/b940084d7eb6a299eb4bfeb8e34901bc051e7ac4/library\std\src\panicking.rs:679
   1: core::panicking::panic_fmt
             at /rustc/b940084d7eb6a299eb4bfeb8e34901bc051e7ac4/library\core\src\panicking.rs:80
   2: bambu_ui::doc_editor::model::tests::xml::test_panic_and_vs_code_paths
             at .\src\doc_editor\model\tests\xml.rs:89
   3: bambu_ui::doc_editor::model::tests::xml::test_panic_and_vs_code_paths::closure$0
             at .\src\doc_editor\model\tests\xml.rs:88
   4: core::ops::function::FnOnce::call_once<bambu_ui::doc_editor::model::tests::xml::test_panic_and_vs_code_paths::closure_env$0,tuple$<> >
             at C:\Users\rober\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\src\rust\library\core\src\ops\function.rs:250
   5: core::ops::function::FnOnce::call_once
             at /rustc/b940084d7eb6a299eb4bfeb8e34901bc051e7ac4/library\core\src\ops\function.rs:250
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
test doc_editor::model::tests::xml::test_panic_and_vs_code_paths ... FAILED

I had a similar problem and fixed it by providing an absolute path to code instead of a relative as I used in beginning. I do not use Cargo, so I can't be helpful, but for my tool the following change fixed the problem:

76fec65 using full source path
diff --git a/comm-build.7b b/comm-build.7b
index 5f43cf7..249219c 100644
--- a/comm-build.7b
+++ b/comm-build.7b
@@ -45,7 +45,7 @@ target clippy:. {
        comp opts,
        --edition, 2024,
        -Cpanic=abort,
-       ${main}.rs
+       ${~cwd~}${~/~}${main}.rs
     )
     if {
         neq(~~, 0)
@@ -72,7 +72,7 @@ target build:. {
        --edition, 2024,
        -o,
        project,
-       ${main}.rs
+       ${~cwd~}${~/~}${main}.rs
    )
      if {
          neq(~~, 0)

Oh. Right.

the first location is captured when the panic!() macro gets expanded at compile time (through, but the stacktrace is resolved by the unwinding library at runtime. so the panic location is always there, but the backtrace might not be available, e.g. if the debug info is stripped from the binary.

the fact they use paths with different relative roots seems odd to me. either libunwind resolved the backtrace incorrectly, or it has something to do with unit tests and libtest.

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

Could it be relative to the working directory of the executable when running it?

wow, you nailed it!

I just tried to run the test executable directly, and indeed the paths are relative to the working directory.

and this is actually documented behavior:

in hindsight, that's so obvious.

but now what about editor integration? can rust-analyzer do something about it on the client side? can a vscode plugin intercept hyperlink click events in the terminal window?

I guess this is something rust-analyzer can do something about after all. I think it can run cargo test from the workspace root rather than package root.

changing the cwd is indeed a simpler fix than handling the link by ra itself.

but actually we should do the opposite, it should set the cwd to the package root instead of the (default) workspace root.

the cargo test command will always run the tests from the package root, so the stacktrace printer "normalize" the paths relative to it.

however, the vscode auto file link detection will try to resolve relative paths based on the cwd when the task is initially launched[1], not some child processe's (the test binary's, in this situation) runtime cwd when the message is printed.

as long as the launching cwd is consistent with the runtime behavior, vscode will resolve the relative path to the correct uri and open it.

I think ra already has a code path to set the cwd for code lens actions, e.g. I found this pr related to cwd and the debug code lens:

for this one, I think we just need a small change for the test code lens case.


  1. though this is the behavior I observed, I'm not sure if this is documented by vscode. this might be related, or at least interesting to dig into β†©οΈŽ