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
├── 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
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
.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
}
}]
}
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With