Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Debugging tests via VSCode Code Lens fails #16010

Closed
fnuecke opened this issue Dec 4, 2023 · 5 comments · Fixed by #16024
Closed

Debugging tests via VSCode Code Lens fails #16010

fnuecke opened this issue Dec 4, 2023 · 5 comments · Fixed by #16024
Labels
A-vscode vscode plugin issues C-bug Category: bug

Comments

@fnuecke
Copy link

fnuecke commented Dec 4, 2023

rust-analyzer version: 0.3.1756-standalone (e402c49 2023-12-01)
rustc version: 1.74.0 (79e9716c9 2023-11-13)
cargo version: 1.74.0 (ecb9851af 2023-10-18)
os: Windows 11 Version 10.0.22631 Build 22631
additional vs code extensions: cpptools

Reproduction steps

  • Open a new directory in VSCode.
  • Run cargo init in the directory.
  • Open main.rs and add an empty test.
  • Click the "Debug" Code Lens option on the test function or test module.

Details

Sample main.rs:

fn main() {
    println!("Hello, world!");
}

#[cfg(test)]
mod tests {
    #[test]
    fn test() {}
}

Attempting to run a test in debug configuration leads to the following error:
image
(Dialog box with error "Invalid 'cwd' value 'H:\Projects\test-debug\Cargo.toml'. The directory name is invalid.")

Output in vscode's debug output for the generated launch configuration:

    Finished test [unoptimized + debuginfo] target(s) in 0.00s
Launching debug configuration:
{
  "type": "cppvsdbg",
  "request": "launch",
  "name": "test-mod tests",
  "program": "H:\\Projects\\test-debug\\target\\debug\\deps\\test_debug-53b7fdbe7fc8a241.exe",
  "args": [
    "tests",
    "--nocapture"
  ],
  "cwd": "H:\\Projects\\test-debug\\Cargo.toml",
  "sourceFileMap": {},
  "env": {
    ...
  }
}

Manually putting this into launch.json results in the same error. Removing the trailing "\\Cargo.toml" from the "cwd" setting solves the issue.

Output of cargo test --message-format=json --no-run:

> cargo test --message-format=json --no-run
{"reason":"compiler-artifact","package_id":"test-debug 0.1.0 (path+file:///H:/Projects/test-debug)","manifest_path":"H:\\Projects\\test-debug\\Cargo.toml","target":{"kind":["bin"],"crate_types":["bin"],"name":"test-debug","src_path":"H:\\Projects\\test-debug\\src\\main.rs","edition":"2021","doc":true,"doctest":false,"test":true},"profile":{"opt_level":"0","debuginfo":2,"debug_assertions":true,"overflow_checks":true,"test":true},"features":[],"filenames":["H:\\Projects\\test-debug\\target\\debug\\deps\\test_debug-53b7fdbe7fc8a241.exe","H:\\Projects\\test-debug\\target\\debug\\deps\\test_debug-53b7fdbe7fc8a241.pdb"],"executable":"H:\\Projects\\test-debug\\target\\debug\\deps\\test_debug-53b7fdbe7fc8a241.exe","fresh":true}
{"reason":"build-finished","success":true}
    Finished test [unoptimized + debuginfo] target(s) in 0.00s

Notably, the returned manifest_path uses backslashes as path separator.

Digging into how the launch configurations are generated, it looks like rust-analyzer currently assumes paths will always use slashes, since it tries to strip the trailing "/Cargo.toml" (note the slash).

If this can alternatively be solved by adjusting my configuration somewhere, guidance would be highly appreciated.

@fnuecke fnuecke added the C-bug Category: bug label Dec 4, 2023
@fnuecke fnuecke changed the title Debugging tests via Code Lens fails Debugging tests via VSCode Code Lens fails Dec 4, 2023
@Rampagy
Copy link

Rampagy commented Dec 4, 2023

I have this issue as well, same versions as listed above. Took me down for an hour during advent of code, trying to figure out what was going on. I was able to revert to the previous release and everything worked fine.

Here's the repo I initially noticed in it, but I tried it in a bunch of my repos and none of the tests worked. IIRC I also tried debugging main and that also didn't work.
https://github.com/Rampagy/AdventOfCode2023/blob/main/Day04/src/main.rs#L123

Curious if this PR is involved.
#15993

@lnicola lnicola added the A-vscode vscode plugin issues label Dec 4, 2023
@Rampagy
Copy link

Rampagy commented Dec 4, 2023

I've never use typescript before, but based on a quick search it appears that the .replace() method can use regular expressions.
https://github.com/rust-lang/rust-analyzer/blob/master/editors/code/src/toolchain.ts#L77

Would this work?
https://regex101.com/r/T91UQI/4

@lnicola
Copy link
Member

lnicola commented Dec 5, 2023

Can anyone install the pre-release version that just came out (there's a button in VS Code) and check if this is now working?

@codewing
Copy link

codewing commented Dec 5, 2023

I had the same/similar issue with the LLDB extension and this is now resolved with the pre-release version 🎉

@fnuecke
Copy link
Author

fnuecke commented Dec 5, 2023

Can also confirm the issue is fixed in the pre-release version. Thanks a lot for the quick fix!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-vscode vscode plugin issues C-bug Category: bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants