WasTabon
WasTabon

Reputation: 143

Cannot debug Rust in Visual Studio Code?

I am trying to debug a Rust program in VS Code, but I get an error: https://i.sstatic.net/QapPo.png

After clicking OK, VS Code opens "settings.json": https://i.sstatic.net/X17Ss.png

I have these extensions installed:

https://i.sstatic.net/HPpZ6.png

My program is a simple "hello world" app.

Upvotes: 13

Views: 22445

Answers (3)

Francis King
Francis King

Reputation: 1732

Visual Studio Code is a general editor, but it can be configured to debug rust code.

Step 1.

Assuming that Visual Code, rust and cargo are installed, the first step is to enable the required extensions:

  1. rust-analyzer
  2. CodeLLDB

These only need to be installed one.

Step 2

The second step is to create the rust code. I have a folder called Rust, in which I keep the rust code. Changing to that folder, I use cargo new hello_world to create a new rust project.

Step 3

The third step is to change the folder for the project. There are two plausible options, but only one of them will work.

I changed to my Rust folder, and then I can edit the source code by following hello_world - src.

enter image description here

To debug the code, it is necessary to create a launch.json file, using Run - Add configuration... However, the file isn't correct, with <your program> where the correct name should be. This is the wrong approach.

{
// 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}/<your program>",
        "args": [],
        "cwd": "${workspaceFolder}"
    }
]

}

The documentation is a bit thin at this point. The correct approach is to pick a different folder, the top level of the project hello_world. The Cargo.toml file is available.

enter image description here

Now, when Run - Add configuration... is used, and the option of LLDB is selected -

enter image description here

the Cargo.toml file can be picked up -

enter image description here

and then the Cargo.toml file is used to correctly build the launch.json file -

  {
  // 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 executable 'hello_world'",
        "cargo": {
            "args": [
                "build",
                "--bin=hello_world",
                "--package=hello_world"
            ],
            "filter": {
                "name": "hello_world",
                "kind": "bin"
            }
        },
        "args": [],
        "cwd": "${workspaceFolder}"
    },
    {
        "type": "lldb",
        "request": "launch",
        "name": "Debug unit tests in executable 'hello_world'",
        "cargo": {
            "args": [
                "test",
                "--no-run",
                "--bin=hello_world",
                "--package=hello_world"
            ],
            "filter": {
                "name": "hello_world",
                "kind": "bin"
            }
        },
        "args": [],
        "cwd": "${workspaceFolder}"
    }
]
} 

Now, both Run - Start debugging and Run - Run without Debugging both work properly.

Upvotes: 10

Granger
Granger

Reputation: 4379

From your screenshots, you're on Windows, so here's how to proceed. This assumes you already took care of the basics:

  1. VsCode has the recommended rust-analyzer extension installed.
  2. Your project folder was initialized with cargo init. (Your project's folder name must be the same as the name of your package in Cargo.toml.)
  3. You can cargo run from within your project directory and it works.

As indicated by various locations on the 'Net, you need to install another VsCode extension to make it so you can debug. Since you're on Windows, you want to use the MS C++ DevTools extension for VsCode, instead of the CodeLLDB one.

Next, you need a "launch" configuration setup. Select VsCode's Run >>> Add Configuration... menu item. Choose the C/C++: (Windows) launch option in the drop-down. You'll now have a launch.json file with a single configuration object.

You'll need to change the program property to "${workspaceFolder}/target/debug/${workspaceFolderBasename}.exe"; this depends on your package name being the same as the project folder's name. (I also changed the cwd property to "${workspaceFolder}", though I'm not sure it matters.) To be clearer, here's the configuration I have presently in my launch.json file (the preLaunchTask property is for later):

{
    "name": "(Windows) Launch",
    "type": "cppvsdbg",
    "request": "launch",
    "program": "${workspaceFolder}/target/debug/${workspaceFolderBasename}.exe",
    "preLaunchTask": "rust: cargo build",
    "args": [],
    "stopAtEntry": false,
    "cwd": "${workspaceFolder}",
    "environment": [],
    "console": "externalTerminal"
}

At this point, as long as you've already built your project at least once, you can hit F5 and debug.

If you want F5 to also save your changes and rebuild your project before debugging, then you also have to add a build task and configure it to run before debugging starts.

To do that, add the build task by opening the Show All Commands box (either F1 or Ctrl+Shift+p) and choosing Tasks: Configure Task. Select rust: cargo build. It'll create a tasks.json file next to your launch.json; the defaults are all you need. My file looks like this:

{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "cargo",
            "command": "build",
            "problemMatcher": [
                "$rustc"
            ],
            "group": "build",
            "label": "rust: cargo build"
        }
    ]
}

Then, to hook everything up, you just need to manually add the preLaunchTask property to your launch configuration with a value equal to the label in your task. E.g. "preLaunchTask": "rust: cargo build",, like what I have in my example launch.json up above.

At this point, whenever you press F5, VsCode will save your work, rebuild your project, then start debugging it.

Upvotes: 14

Saurav Rao
Saurav Rao

Reputation: 674

Unfortunately VS Code can't debug Rust out of the box :( But no need to worry, just few steps of configuration will do the work :)

Steps

  1. Install C/C++ extension if you are on windows and CodeLLDB if on OS X/Linux

  2. Click Debug -> Add Configuration, a launch.json file should open, you need to change the program name here manually

     {
         "version": "0.2.0",
         "configurations": [
             {
                 "name": "(Windows) Launch",
                 "type": "cppvsdbg",
                 "request": "launch",
                 "program": "${workspaceRoot}/target/debug/foo.exe",
                 "args": [],
                 "stopAtEntry": false,
                 "cwd": "${workspaceRoot}",
                 "environment": [],
                 "externalConsole": true
             },
             {
                 "name": "(OSX) Launch",
                 "type": "lldb",
                 "request": "launch",
                 "program": "${workspaceRoot}/target/debug/foo",
                 "args": [],
                 "cwd": "${workspaceRoot}",
             }
         ]
     }
    
  3. Make sure Allow setting breakpoints in any file is checkend under File -> Preferences -> Settings

For detailed steps and more you can refer the article I used to answer this

Credits- Forrest Smith

Upvotes: 22

Related Questions