gobanos / cargo-aoc

439 stars 47 forks source link

How do I debug with cargo-aoc #68

Open dantho opened 3 years ago

dantho commented 3 years ago

I'm having no luck debugging (using VS Code and ) while running with cargo-aoc. The debugger ignores my break-traps in dayN.rs I'm a bit of a newbie. This should work, right? Is there something in this .vscode launch.json file that's wrong? (I can debug Hello World with a similar setup.)

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(Windows) Launch",
            "type": "cppvsdbg",
            "request": "launch",
            "program": "${workspaceFolder}/target/debug/advent-of-code-2020.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false
        }
    ]
}
mjpieters commented 2 years ago

cargo aoc generates a new, nested Rust project under target/aoc; the binary you want to run is target/aoc/aoc-autobuild/target/release/aoc-autobuild. This is generated, built and run when you use cargo aoc on the command-line. Make sure you run cargo aoc before debugging; the generated code applies to a single day (by default, the most recent, use the command-line switches to change what day driver is generated).

I opened a feature request (two years ago) to help with debugging by only generating the binary, not also run it (see #64, PR in #69), but neither have seen movement unfortunately.

In the meantime, I use vscode-lldb features to trigger a build and extract the right binary path for the debugger, I don't know if cppvsdbg has something similar. For lldb, don't set program, but set cargo instead, and have it run cargo build --manifest-path target/aoc/aoc-autobuild/Cargo.toml --bin=aoc-autobuild; lldb will extract the right binary and you can then start debugging. This will only work if cargo aoc was run before to generate the nested project.

dantho commented 2 years ago

Thanks for the reply, @mjpieters !

Because of your reply, I finally dug in and got Rust debugging working in VS Code. I tried using whatever debugger I had installed, and the executable you pointed me to. It didn't work. But then I loaded the VSCode Add-in "CodeLLDB", ran its command "LLDB: Generate Launch Configurations from Cargo.toml" and I can now debug my code and my tests!

Love it. Thanks again.

MaddyGuthridge commented 10 months ago

For anyone else that comes across this issue, this is the launch.json that worked for me:

{
    // 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 current day",
            "cargo": {
                "args": [
                    "build",
                    "--manifest-path=target/aoc/aoc-autobuild/Cargo.toml",
                    "--bin=aoc-autobuild"
                ],
            },
            "args": [],
            "cwd": "${workspaceFolder}"
        }
    ]
}