Getting debugger setup in VSCode
from trymeout@lemmy.world to rust@programming.dev on 07 Oct 2024 00:48
https://lemmy.world/post/20581829

Just started learning Rust today and got Rust installed, got the hello world example compiled and running. I installed rust-analyzer and CodeLLDB extensions in VSCode. Enable the debug.allowBreakpointsEverywhere settings to VSCode to be true. Setup a debug configuration in VSCode.

However I keep getting errors from rust-analyzer when I run the debugger…

2024-10-06T22:16:04.808655Z ERROR FetchWorkspaceError: rust-analyzer failed to load workspace: Failed to load the project at /home/john/Documents/Snippets/Rust/Cargo.toml: Failed to read Cargo metadata from Cargo.toml file /home/john/Documents/Snippets/Rust/Cargo.toml, Some(Version { major: 1, minor: 81, patch: 0 }): Failed to run `cd "/home/john/Documents/Snippets/Rust" && RUSTUP_TOOLCHAIN="/home/john/.rustup/toolchains/stable-x86_64-unknown-linux-gnu" "/home/john/.cargo/bin/cargo" "metadata" "--format-version" "1" "--manifest-path" "/home/john/Documents/Snippets/Rust/Cargo.toml" "--filter-platform" "x86_64-unknown-linux-gnu"`: `cargo metadata` exited with an error: error: failed to parse manifest at `/home/john/Documents/Snippets/Rust/Cargo.toml`

Caused by:
  no targets specified in the manifest
  either src/lib.rs, src/main.rs, a [lib] section, or [[bin]] section must be present

I not sure how to fix this.

I would like to get the VSCode debugger to work for launch debugging, attach debugging and launch and attach debugging for rust running inside a docker container. This will be a good setup for getting started I believe.

This is my VSCode debugger configuration…

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

Any help and advice will be most appreciated.

#rust

threaded - newest

BB_C@programming.dev on 07 Oct 2024 01:08 next collapse

  • Why do you think this is a debugger issue?
  • May I suggest you try to learn how to use the rust toolchain directly first?
  • The error message looks informative to me. What part of this line did you not get?
     either src/lib.rs, src/main.rs, a [lib] section, or [[bin]] section must be present 
    

I think you’re tying yourself too close to VSCode. Reading this Cargo guide is a minimal requirement. And you should learn how to use cargo (the command), at least for basic operations. This is not hard. If anything, it’s much simpler than those bloated and opaque editor/IDE setups.

Ephera@lemmy.ml on 07 Oct 2024 01:19 collapse

Maybe should be mentioned, if you just run cargo run in the project repo, you should get the same error without all the noise surrounding it…

hawgietonight@lemmy.world on 07 Oct 2024 02:20 next collapse

I’m not sure, but in the debug config try to point “program” to the actual executable in /target/debug/hello_world

FizzyOrange@programming.dev on 07 Oct 2024 20:20 collapse

My advice is that you debug via tests. If you install Rust-analyzer (you probably already have) then any tests you add have a little “Run test” and “Debug test” button next to them that you can just click. It magically works without having to faff around with JSON files.

As for your current setup, as other said you have an issue in Cargo.toml which is unrelated to debugging, and the target binary is in target/debug, not in the workspace root.