I'm currently working on debugging a Go program using dlv
(Delve) alongside VSCode, and I've encountered a problem related to path configurations. The program needs to run as root inside a Docker container, with the project's root directory being /project
within the container and /home/user/project
on my host machine. The main.go
file is located at cmd/project/main.go
, relative to the project root.
To initiate debugging, I run dlv
within the container from the /project
directory using the command:
dlv dap --listen=:4000
My launch.json
configuration in VSCode is as follows:
{
"version": "0.2.0",
"configurations": [
{
"name": "Connect to server",
"type": "go",
"debugAdapter": "dlv-dap",
"request": "launch",
"mode": "debug",
"port": 4000,
"host": "172.18.0.2",
"program": "/project/cmd/project/main.go",
"showLog": true,
"substitutePath": [
{
"from": "${workspaceFolder}",
"to": "/project"
}
]
}
]
}
Upon attempting to start the debugging session, I encountered an error stating:
The program attribute '/project/cmd/project/main.go' must be a valid directory or .go file in debug/test/auto modes.
My understanding was that the program
attribute should reflect the path on the remote machine (i.e., inside the container). Not surprisingly, changing the "program"
path to "/home/user/project/cmd/project/main.go"
results in a build error from dlv
:
Build Error: go build -o /__debug_bin2715112943 -gcflags all=-N -l /home/user/project/cmd/project/main.go
go: go.mod file not found in current directory or any parent directory; see 'go help modules' (exit status 1)
As a temporary workaround, I've matched the project root paths inside and outside the container. Could you help me understand what might be wrong with my configuration, or suggest how to correctly set up debugging in this scenario?
Thank you for your assistance.