magefile / mage

a Make/rake-like dev tool using Go

Home Page:https://magefile.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Document debugging mage files in VSCode

mirogta opened this issue · comments

commented

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?

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)

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

commented

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

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

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.

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?

@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.

@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>"  
                }
            ]
        }
      
    ]
}