visual-studio-coderust

Is it possible to Debug a Rust Integration test and see the output of println as I debug?


It has been a while since I touched Rust in Visual Studio Code. I am fiddling with the Rust exercises from the Exercism site. Each exercise has a lib.rs under src and a [exercise name].rs under the tests.

I believe that I used to be able to see the output of println statements in the Terminal window as I debugged a single Integration Test using the CodeLens Debug link. I currently cannot. If I use the CodeLens Run link for a test, output from a println in lib.rs functions shows up in the Terminal.

Should I be able to see println output in the Terminal window as I debug an integration test? If so, can anyone provide suggestions on how to identify or document the problem?

The extensions I am using are: CodeLLDB, rust-analyzer and WSL.

I uninstalled each extension, deleted the Code directory, reinstalled Code and loaded each extension again.


Solution

  • By default, Rust tests silence their output. Rust-analyzer, by default, adds --show-output which means the tests will print all output once all the tests have finished. If you want the tests to print while they are running, you need to pass --nocapture. Rust-analyzer used to use --nocapture but this was changed a few months ago.

    The easiest way to fix this is to change the rust-analyzer.runnables.extraTestBinaryArgs VS Code setting to use --nocapture instead of --show-output, which should give you the old behavior you're used to. This applies to both the "Run Test" and "Debug" buttons, which may not be ideal, but I couldn't find a way to pass different arguments to each.

    The other way is to use a launch.json file. The downside is this only affects one test command at a time, and it doesn't apply to the CodeLens buttons. You can generate most of the file by running "LLDB: Generate Launch Configurations from Cargo.toml" in the command palette. Then, find the entry named "Debug integration test 'test-name'" and add "--nocapture" to the top-level args array (not the cargo.args array; those are only used for compilation). Save this file as ".vscode/launch.json" in the VS Code workspace root. You can then use this configuration in the "Run and Debug" tab.