magefile / mage

a Make/rake-like dev tool using Go
https://magefile.org
Apache License 2.0
4.1k stars 251 forks source link

Document debugging mage files in VSCode #280

Open mirogta opened 4 years ago

mirogta commented 4 years ago

I can't find any info about debugging mage files.

I've tried to run mage -keep and then debug the generated mage_output_file.go to no avail. It's probably because of the // +build ignore restriction in it.

Could this be explained in the official docs/website with some examples?

natefinch commented 4 years ago

When you say debug, do you mean with delve to step through it? I haven't needed to do that, but it seems like your procedure should work. What happens when you try to debug the generated mainfile? The comment shouldn't matter if the go tool is passed the file explicitly (and you should be able to set build tags as well)

l50 commented 2 years ago

@mirogta did you ever figure this out? I'm having a bit of a time with this myself.

mirogta commented 2 years ago

@l50 I haven't figured out. How far did you get with it?

l50 commented 2 years ago

Gave up and decided to cheap out with print statements like a n00b :P

AndersonQ commented 2 years ago

Hey folks, I'd love to be able to debug the generated binaries as well. Perhaps there could be an option to make mage run the binary through delve and then we could connect to it remotely.

perj commented 2 years ago

This configuration worked for me: https://gist.github.com/perj/ec75ee651135ba0ba9aa64fc7eeb4033

AndersonQ commented 2 years ago

This configuration worked for me: https://gist.github.com/perj/ec75ee651135ba0ba9aa64fc7eeb4033

@perj, thanks! I don't really use VS code, so let me ask one thing. In short, you run mage --keep to generate mage_output_file.go, and then you run mage_output_file.go on the debugger. Is it?

perj commented 2 years ago

@AndersonQ Technically mage --keep followed by dlv debug --build-flags 'magefile.go' mage_output_file.go. But using --build-flags like that is just a workaround against VSCode limitations. I don't know if it's possible to generate mage_output_file.go without running it, perhaps using -l flag is appropriate.

gsantoro commented 2 years ago

@perj Thanks for the suggestion. I was searching for something similar but your solution wasn't optimal for me for various reasons:

  1. if I try to mage --keep <target> I can correctly generate a mage_output_file.go for that single target. What if I want to debug more than 1 target? do I need to repeat this for each target?
  2. I have multiple magefile split in subfolders, with multiple targets. compile them each separately will be a nightmare

Digging a little bit I found, this solution:

  1. I can generate a binary file with all the targets with mage -f -compile ./mage_debug_bin
  2. I can run a delve debugger with the same binary for each target without recompiling dlv --headless=true --listen=:56268 --api-version=2 --log exec mage_debug_bin <target>
  3. I can use VisualStudioCode to attach to the remote debugger
{
    "version": "0.2.0",
    "configurations": [
         {
            "name": "Connect to server",
            "type": "go",
            "request": "attach",
            "mode": "remote",
            "debugAdapter": "dlv-dap",
            "port": 56268,
            "host": "127.0.0.1",
            "showLog": true,
            "trace": "trace",
            "cwd": "${workspaceFolder}",
            "substitutePath": [
                { 
                    "from": "${workspaceFolder}", 
                    # NOTE: replace `<absolute_path>` with the absolute path to your VScode root folder
                    "to": "<absolute_path>"  
                }
            ]
        }

    ]
}