Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to configure VSCode with Rust for binary execution?

Assuming that I'm editing the lession_1/src/main.rs file, I want to Ctrl+F5 (or just F5) keyboard shortcut and have the following command executed (as if I was running it from lession_1 directory):

cargo run

My project structure

├── lesson_1
│   ├── Cargo.toml
│   └── src
│       └── main.rs
├── lesson_2
│   ├── Cargo.toml
│   └── src
│       └── main.rs
├── lesson_3
│   ├── Cargo.toml
│   └── src
│       └── main.rs
├── .git
├── .gitignore
├── .pre-commit-config.yaml
├── .pre-commit-hooks.yaml
├── README.md
├── .secrets.baseline
└── Untitled.ipynb

What I tried

I followed up this article, however, I do get an error:

error: could not find `Cargo.toml` in `/home/me/projects/my_rust_project` or any parent directory

My config

.vscode/launch.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "lldb",
            "request": "launch",
            "name": "Debug",
            "program": "/usr/bin/cargo",
            "args": ["run", "${workspaceFolder}/<executable file>"],
            "cwd": "${workspaceFolder}"
        }
    ]
}

.vscode/tasks.json

{
    "version": "2.0.0",
    "tasks": [{
     "label": "cargo build",
     "type": "shell",
     "command": "cargo build",
     "args": [],
     "group": {
       "kind": "build",
       "isDefault": true
     }
    },
    {
        "label": "cargo run",
        "type": "shell",
        "command": "cargo",
        "args": [
          "run"
          // "--release",
          // "--",
          // "arg1"
        ],
        "group": {
          "kind": "build",
          "isDefault": true
        }
       }]
  }
like image 418
Artur Barseghyan Avatar asked Jan 27 '26 04:01

Artur Barseghyan


2 Answers

Due to the structure of your project, you have several Cargo package in your workspace.

In your launch.json, cwd is set to the workspace folder and Cargo doesn't know which package to run.

Replace "cwd": "${workspaceFolder}" by "cwd": "${relativeFileDirname}" and Cargo will be run from the folder containing the Rust file you are currently editing, and should easily find the associated Cargo.toml.

For the tasks.json, the same solution can apply, but it means you are only building the Cargo package you are currently editing and not all of them.

like image 57
mrBen Avatar answered Jan 29 '26 04:01

mrBen


Answer suggested by @eggyal

Changing the .vscode/tasks.json as follows does the job:

{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "lldb",
            "request": "launch",
            "name": "Debug",
            "program": "/usr/bin/cargo",
            "args": ["run", "${workspaceFolder}/<executable file>"],
            "cwd": "${fileDirname}"
        }
    ]
}

The key is changing the cwd from "${workspaceFolder}" to "${fileDirname}".

As suggested, see VSCode's Variables Reference for more information.

like image 42
Artur Barseghyan Avatar answered Jan 29 '26 03:01

Artur Barseghyan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!