Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document debugging mage files in VSCode #280

Open
mirogta opened this issue Jan 9, 2020 · 9 comments
Open

Document debugging mage files in VSCode #280

mirogta opened this issue Jan 9, 2020 · 9 comments

Comments

@mirogta
Copy link
Member

mirogta commented Jan 9, 2020

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
Copy link
Member

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
Copy link

l50 commented Oct 27, 2021

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

@mirogta
Copy link
Member Author

mirogta commented Oct 29, 2021

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

@l50
Copy link

l50 commented Oct 29, 2021

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

@AndersonQ
Copy link

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
Copy link
Contributor

perj commented Jan 27, 2022

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

@AndersonQ
Copy link

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
Copy link
Contributor

perj commented Jan 27, 2022

@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
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants