Why do I get "identifier is undefined" or "not available" when inspecting a Rust variable in the VSCode debugger?

Viewed 1696

I've set up the Visual Studio Code debugger and run the following program.

pub fn main() {
    let mut chars = "test".chars();
    match chars.next() {
        Some(c) => {
            println!("What is the value of c, here?");
            if c == 'c' {
                println!("c");
            }
        }
        None => {}
    }
}

If I set a breakpoint at line 6, and look in the Variables and Watch panes, c does not evaluate, but rather passes the following message: identifier 'c' is undefined using cppvsdbg on Windows, or <not available> using lldb on Linux. I've confirmed that this happens both on Linux and Windows builds, for the current stable compiler version.

I've also added the following to Cargo.toml to no avail:

[profile.dev]
opt-level = 0
debug = true

For reference, here is my launch.json file, needed for the VS Code compiler:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(Windows) Launch",
            "type": "cppvsdbg",
            "request": "launch",
            "program": "${workspaceFolder}/target/debug/test.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": true
        }
    ]
}

Replace "(Windows) Launch" with your OS of choice.

Why is this the result? Is there a fix, or are there some compiler options that should be added?

2 Answers

This issue is not reproducible with recent release of CodeLLDB.

CodeLLDB v1.7.0
rust : 1.60.0 (7737e0b5c 2022-04-04)
vscode: v1.67.0

Everything works as expected the debug view shows

iter: {end:0x000055555559105f}
c: 't'

Upgrading the codelldb to specified version will resolve the issue.

Related