Rust debugging in VS Code

I’m trying to set up a debugger in VS Code (actually Codium) on Linux. Does anyone already have this working?

If so, can you share your launch.json. I think I’m almost there but the debugger doesn’t seem to launch. I’m getting no errors I can find, when I ‘run’ the debug toolbar appears for about a second and then disappears.

Solution

Here’s a summary of how I solved this for Ubuntu 19.04 with VS Codium:
Summary of what worked for Ubuntu 19.04:

	sudo apt-get install lldb
	rustup component add rls rust-analysis rust-src

In VS Codium

  • Add extension: rust-lang
  • In settings:
    change rust server from RLS to rust-analyzer
    optional: change editor settings to ‘reformat on save’ (useful if you plan to work with Maidsafe code which requires this as well as using clippy).

That should be it. I recommend ignoring anything that tells you to create a cargo build task in tasks.json or a debugger config in settings.json as this seems to break the Rust-Lang extension!

I dont have a debugger going per se. I get type feedback etc w the rust package with rust-analyzer for running check (for my money its way better than RLS).

I know that’s not really what you’re asking, but in case it’s helpful :+1:

1 Like

All tips welcome, thanks Josh. I have that installed and may even have seen its output but tbh is hard to know what is generating the output I’ve seen. I can see the simple cargo build output, and I’ve seen a very long list of warnings (maybe from analyser, maybe from clippy) but I’m not sure how to control all this goodness yet.

Main issue is the debugger though.

As Josh mentioned, rust-analyzer is pretty good. Debugging depends on what you want to debug. If it’s a library then you’ll need a test to do debugging. If it’s a binary then that can be debugged easily.

Once you’ve installed rust-analyzer and it has completed the build. You’ll know it’s done when this cargo check [...] goes away:
image

Then above the test function or the main function of your binary you should see Run | Debug options.
Clicking to the left of the line number places a break point (a red dot).

1 Like

Thanks Lionel, do you have a launch.json you can share?

I have lldb-mi in my launch.json config (for syncer which is a command line app). I can set a breakpoint but from what I am seeing I don’t think LLDB is starting. It looks to me as if VS Code tries to start it, waits for a connection and times out after a second, but there are no errors anywhere. So it might help to see a working config.

You don’t really need a launch.json when using rust-analyser. I remember configuring a debugger using launch.json back when I was using RLS (which is now deprecated).

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "type": "lldb",
            "request": "launch",
            "name": "Debug",
            "program": "${workspaceFolder}/target/debug/safe_core-as576asd57asdf4",
             "args": ["create_login_packet", "--" ,"--nocapture"],
            "cwd": "${workspaceFolder}"
        }
    ]
}

Here’s an old launch config that was lying around

Well there’s a clue - I’ve followed instructions which say I need to create the config. Maybe that’s the problem. Thanks, gives me some things to try, starting with deleting my launch.json! :smile:

Having tried a few things I’m puzzled.

I’m using VSCodium on Linux and was originally set up with the native debugger ennabled (which includes LLDB) and had added the rust-lang extention which includes support for build tasks and the docs say:

Rust support is powered by a separate language server - either by the official Rust Language Server (RLS) or rust-analyzer, depending on the user’s preference. If you don’t have it installed, the extension will install it for you (with permission).

I thought I had rust-analyzer installed because I wasn’t prompted for it, but it isn’t in my extensions list so I’ve been trying to install it as an extension but it doesn’t show up (instructions here don’t work - they just paste a search into the extensions list which doesn’t find rust-analyzer). So I don’t think it is in the market place.

This may explain why I don’t get an option to debug unless I create a launch.config, but when I do that (just tried using safe-vault) I can run the debugger but get the result above (debug toolbar appears for a second and then disappears).

So I’m puzzled as to how VS Codium is supposed to be set up (seems you guys are using an extension I can’t install. Maybe superceded by the rust-lang extension which I can’t get to work.

I don’t think save_vault is being run, and if I mess with the config (e.g. change the name of the executable to safe_NOTHING) the behaviour doesn’t change. I can set a breakpoint in safe_vault.js but it has no effect.

Looking in VSC settings I had RLS selected, so I switched to rust-analyzer. No difference!

UPDATE:
Seems like this might be the magic needed: sudo apt-get install lldb
I’ve not seen any instructions to do that!

VS Codium has now prompted me to install rust-analyzer, so fingers crossed.

BINGO! That’s the missing step, installing lldb

It is still not quite as promised. It only works with a launch.json config, which is fine.

If I use the little |> Run|Debug buttons inside the code I get an error popup and it doesn’t run the debugger:
command 'rust-analyzer.runSingle' not found

But if I create a simple config in launch.json and run that it’s fine, and I hit my breakpoint.

Solution

Here’s a summary of how I solved this for Ubuntu 19.04 with VS Codium:
Summary of what worked for Ubuntu 19.04:

	sudo apt-get install lldb
	rustup component add rls rust-analysis rust-src

In VS Codium

  • Add extension: rust-lang
  • In settings:
    change rust server from RLS to rust-analyzer
    optional: change editor settings to ‘reformat on save’ (useful if you plan to work with Maidsafe code which requires this as well as using clippy).

That should be it. I recommend ignoring anything that tells you to create a cargo build task in tasks.json or a debugger config in settings.json as this seems to break the Rust-Lang extension!

1 Like