mage icon indicating copy to clipboard operation
mage copied to clipboard

Document debugging mage files in VSCode

Open mirogta opened this issue 5 years ago • 9 comments

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?

mirogta avatar Jan 09 '20 11:01 mirogta

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)

natefinch avatar Jan 13 '20 17:01 natefinch

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

l50 avatar Oct 27 '21 17:10 l50

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

mirogta avatar Oct 29 '21 17:10 mirogta

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

l50 avatar Oct 29 '21 17:10 l50

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.

AndersonQ avatar Jan 26 '22 14:01 AndersonQ

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

perj avatar Jan 27 '22 08:01 perj

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 avatar Jan 27 '22 09:01 AndersonQ

@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 avatar Jan 27 '22 09:01 perj

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

gsantoro avatar May 24 '22 22:05 gsantoro