vadimcn / codelldb

A native debugger extension for VSCode based on LLDB
https://marketplace.visualstudio.com/items?itemName=vadimcn.vscode-lldb
MIT License
2.55k stars 246 forks source link

"cwd" not working in vscode debug #1136

Open AllanDaemon opened 1 month ago

AllanDaemon commented 1 month ago

Context

OS: Ubuntu 24.04.1 LTS noble VSCode version:

Version: 1.92.2
Commit: fee1edb8d6d72a0ddff41e5f71a671c23ed924b9
Date: 2024-08-14T17:29:30.058Z
Electron: 30.1.2
ElectronBuildId: 9870757
Chromium: 124.0.6367.243
Node.js: 20.14.0
V8: 12.4.254.20-electron.0
OS: Linux x64 6.8.0-41-generic

CodeLLDB version: v1.10.0 Compiler: ❯ rustc 1.80.1 (3f5fd8dd4 2024-08-06) cargo 1.80.1 (376290515 2024-07-16)

Debuggee: utility tool with GUI: https://github.com/AllanDaemon/uchoose/tree/oxidation

Issue

cwd parameter isn't being used when launching a cargo debug run. It's using the workspace root.

In the workspace root /data/code/uchoose, there is a folder called uchoose-rs where the rust code (including the Cargo.toml) is.

In the launcher.json (using a hardcoded path to ensure it's not an env variable substitution problem):

{
            "name": "Rust default",
            "type": "lldb",
            "request": "launch",
            "cwd": "/data/code/uchoose/uchoose-rs/",
            "cargo": {
                "args": ["run"]
            },
            "args": [],
        },

In the output, the first line says:

Running `cargo run --message-format=json`...
error: could not find `Cargo.toml` in `/data/code/uchoose` or any parent directory

It's taking /data/code/uchoose (the root of workspace) instead of the defined "cwd" /data/code/uchoose/uchoose-rs/.

Verbose log Output of terminal
 *  Executing task: CodeLLDB: cargo 

Running `cargo run --message-format=json`...
error: could not find `Cargo.toml` in `/data/code/uchoose` or any parent directory
Error: Cargo invocation failed.
        at t.Cargo.getCargoArtifacts (/home/user/.vscode/extensions/vadimcn.vscode-lldb-1.10.0/extension.js:1:14943)
        at async Object.open (/home/user/.vscode/extensions/vadimcn.vscode-lldb-1.10.0/extension.js:1:13253)
Caused by: Error: exit code: 101.
        at ChildProcess. (/home/user/.vscode/extensions/vadimcn.vscode-lldb-1.10.0/extension.js:1:16610)
        at ChildProcess.emit (node:events:519:28)
        at maybeClose (node:internal/child_process:1105:16)
        at Socket. (node:internal/child_process:457:11)
        at Socket.emit (node:events:519:28)
        at Pipe. (node:net:338:12)
 *  The terminal process failed to launch (exit code: 1). 
 *  Terminal will be reused by tasks, press any key to close it. 
Output of output (with verbose logging)
Initial debug configuration: {
  name: 'Rust default',
  type: 'lldb',
  request: 'launch',
  cwd: '/data/code/uchoose/uchoose-rs/',
  cargo: { args: [ 'run' ] },
  args: [],
  __configurationTarget: 6
}
Error: Cargo invocation failed.
    at t.Cargo.getCargoArtifacts (/home/user/.vscode/extensions/vadimcn.vscode-lldb-1.10.0/extension.js:1:14943)
    at async Object.open (/home/user/.vscode/extensions/vadimcn.vscode-lldb-1.10.0/extension.js:1:13253)
Caused by: Error: exit code: 101.
    at ChildProcess. (/home/user/.vscode/extensions/vadimcn.vscode-lldb-1.10.0/extension.js:1:16610)
    at ChildProcess.emit (node:events:519:28)
    at maybeClose (node:internal/child_process:1105:16)
    at Socket. (node:internal/child_process:457:11)
    at Socket.emit (node:events:519:28)
    at Pipe. (node:net:338:12)
AllanDaemon commented 1 month ago

Using the automaticly generated configuration for the cargo file above, when the workspace has only one place and it has the rust parts (like Cargo.toml), it works:

{
            "type": "lldb",
            "request": "launch",
            "name": "Debug executable 'uchoose'",
            "cargo": {
                "args": [
                    "build",
                    "--bin=uchoose",
                    "--package=uchoose"
                ],
                "filter": {
                    "name": "uchoose",
                    "kind": "bin"
                }
            },
            "args": [],
            "cwd": "${workspaceFolder}"
        },

Using the same config where the rust part isn't in the root of the workspace, even when the cwd is correctly set, has the same issue as before.

So the fact that I was writing the debug launch config from scratch wasn't the issue here, as the extension generated one has the same error.

stacktracer commented 1 month ago

A partial workaround is to pass --manifest-path=${workspaceFolder}/rust/Cargo.toml as a Cargo arg:

"cargo": {
  "args": [
    "build",
    "--manifest-path=${workspaceFolder}/rust/Cargo.toml",
    ...

HOWEVER, this won't pick up ${workspaceFolder}/rust/rust-toolchain.toml (... which is a frustrating issue for me at the moment).

stacktracer commented 1 month ago

An uglier (but more effective) workaround is to use a wrapper script that first calls cd and then calls cargo.

Step 1: Write the wrapper script

#!/bin/bash
set -euo pipefail

cd "$1"
shift
exec "$HOME/.cargo/bin/cargo" "$@"

Step 2: Open User Settings and change the lldb.cargo setting to point at the wrapper script.

Step 3: Edit launch.json so it passes the desired cwd as the first arg to the wrapper script:

    "configurations": [ {
        "type": "lldb",
        "request": "launch",
        "cargo": {
            "args": [
                "${workspaceFolder}/rust",
                "build",
                ...
            ],
            ...
AllanDaemon commented 1 month ago

My workaround was to create a separated workspace with the root on the rust dir just to run the debugger, and switch every time between the workspaces.

But the bug remains.

Barafu commented 2 weeks ago

I seemed to fix the problem by moving cargo call into tasks.conf. But now, the debugger crashes after a few seconds after breakpoint stop, and I don't know how to fix that.

launch.json

{
        "type": "lldb",
        "name": "Tauri Development Debug",
        "request": "launch",
        "program": "${workspaceFolder}/src-tauri/target/debug/dream_spinner.exe",        
        "preLaunchTask": "rust: cargo build devel",        
},

tasks.json

{
            "type": "shell",
            "command": "cargo build",
            "problemMatcher": {
                "base": "$rustc",
                "fileLocation": [
                    "relative",
                    "${workspaceFolder}/src-tauri"
                ]
            },
            "label": "rust: cargo build devel",
            "group": {
                "kind": "build",
                "isDefault": false
            },
            "options": {
                "cwd": "${workspaceFolder}/src-tauri"
            }
        },