Search code examples
govscode-debuggergo-cobra

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


Solution

  • 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: