Reputation: 22292
I'm developing code for the Codingame problems using VS Code on Windows with Rust and the Visual Studio toolchain.
I have found multiple guides explaining how to debug the executable generated by cargo build
, the best being Debug Rust on Windows with Visual Studio Code and the MSVC Debugger.
However, when I face problems, I tend to write unit tests (I've done that in Java, JavaScript, Ruby, ...), which I then debug. Unfortunately, I can't find any way to do that in Rust. How do I configure my environment to debug my tests?
I'm not talking about adding println!
statements in my tests, as I already know how to do that. I'm also not talking about adding new assertions, because those reside in the test, not in the tested code.
What I want is to use the VS Code Debugger on the code called by my test.
Upvotes: 22
Views: 13729
Reputation: 103
When using WinDBG, consider using rust-windbg. It's script will do all the heavy lifting of setting up the WinDBG environment such that you can concentrate on debugging your source's issue.
Upvotes: 0
Reputation: 1190
Expanding on Shepmaster's answer you could automate the process of finding the right executable.
In nushell you could do something like this.
export def open-in-windbg [executable: path] {
# NOTE: change it to your executable location if it differs
~/AppData/Local/Microsoft/WindowsApps/WinDbgX.exe $executable
}
export def --wrapped "cargo test-windbg" [...args: string] {
let executable = (cargo t ...$args --no-run e>| parse --regex 'Executable.*\((?<name>.+)\)' | get 0.name)
open-in-windbg $executable
}
And then call
cargo test-windb
Extra cargo test flags should be fine too
cargo test-windbg --features myfeat
NOTE: I did not manage to make Windbg open the source file yet.
Upvotes: 0
Reputation: 430671
Rust unit tests are compiled as separate binaries, which means you debug them exactly the same as any other binary. Once compiled, they are located at ./target/debug/$name-$hash
.
Here's modified versions of the VS Code configuration files that allow me to debug a unit test.
{
"type": "shell",
"label": "cargo test build",
"command": "cargo",
"args": [
"test", "--no-run"
],
"problemMatcher": [
"$rustc"
]
}
{
"name": "Run Test Debugger",
"type": "cppvsdbg",
"request": "launch",
"program": "${workspaceFolder}/target/debug/buggin-70708b3916187eeb.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"preLaunchTask": "cargo test build",
}
Build your tests:
cargo test --no-run
Open the built executable in Windbg and open the source file.
Finding the hash is the most annoying aspect. The best solution I know of is to write a small script that builds the tests and then finds the test executable based on which is newest. My Powershell skills are not adequate to the task, nor do I know how to directly integrate this with VS Code or Windbg.
There are open issues for Cargo to help with identifying the file:
build --test
and test --no-run
(#1924)Upvotes: 23