Consider debugging the following code:
package main
import "os"
func main() {
os.Args = append(os.Args, "help")
}
To test it with command line arguments I create launch.json as follows:
{
"version": "0.2.0",
"configurations": [
{
"name": "Test args",
"type": "go",
"request": "launch",
"mode": "test",
"program": "${file}",
"env": {},
"args": ["-test.v","--","arg1","arg2","arg3"]
}
]
}
When I run Test args configuration from Run and Debug in VS Code I get an error message
Failed to launch: could not launch process: open /mypath/__debug_bin: no such file or directory
This error is because for some reason dlv fails to create 'intermediate' binary file __debug_bin from under VS Code.
However, when I run dlv directly from command line
dlv debug -- arg1 arg2 arg3
it works as expected, creates __debug_bin in /mypath directory and all further debugging features (setting breakpoints, stepping over/into, printing variables etc) are available.
Moreover, if I first run dlv debug from command line (creating thus __debug_bin) and then (without quitting dlv) switch to VS Code and run Test args from Run and Debug, it works too. Meaning that VS Code (and dlv somewhere under the hood) catches this __debug_bin and uses it for debugging.
So, the trouble is that dlv fails to build __debug_bin when called from VS Code context.
Please advise how can I fix this issue?
EDIT. The trouble happened because of using wrong "mode" and "args" parameters in my launch.json file. Proper debug configuration looks as follows:
{
"version": "0.2.0",
"configurations": [
{
"name": "Launch file",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${file}",
"env": {},
"args": ["--","arg1","arg2","arg3"]
}
]
}
I changed "mode" from "test" to "auto", and removed "-test.v" flag from "args". Configuration with "mode": "test" and "-test.v" in "args" works for testing scenarios. Say, when I have main_test.go and main.go, and I want to debug main_test.go. If I debug a 'normal' (not 'test') go file, I need to adjust launch.json as described.