Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to debug golang cobra cli app in vscode

I have a golang cobra cli app. have configured my vscode for debugging. I want to debug a specific command using vscode for my application.

I am using this launch.json

        {
            "name": "Launch Package",
            "type": "go",
            "request": "launch",
            "mode": "debug",
            "program": "${fileDirname}"
        }

If I select main.go and start debugging it just prints the help for my command. How can I debug a particular cli subcommand in vscode? Like say abc create or abc version

If I select a subcommand package and then debug it says : Failed to launch :could not launch process:not an executalbe file

like image 965
Chetan Avatar asked Nov 16 '25 00:11

Chetan


1 Answers

You can configure the VSCode launch configuration to pass arguments using:

        {
            "name": "Launch Package",
            "type": "go",
            "request": "launch",
            "mode": "debug",
            "program": "${fileDirname}"
            "args": ["arg1", ...]
        }

Note that you may prefer to write the path to your main go file in the program field so that you can run/debug no matter what file you are currently on.

Reference:

  • launch.json: https://code.visualstudio.com/docs/editor/debugging#_launchjson-attributes
  • VSCode Variables: https://code.visualstudio.com/docs/editor/variables-reference
like image 134
saraf.gahl Avatar answered Nov 18 '25 19:11

saraf.gahl



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!