I believe there might be an issue in this scenario. Here's what I've done:
- Create an empty folder
c:\repos\demo
- In that folder I run
go mod init demo
, here is the mod filemodule demo go 1.22.0
- Add a simple
main.go
file in the folderpackage main import ( "fmt" "net/http" ) func main() { http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "Hello, World!") }) http.ListenAndServe(":8080", nil) }
- Test the VScode debugging works fine, and I can put break point in my code and use browser access the
http://localhost:8080
to hit my breakpoint. - Add a Dockerfile to build the debugging image
FROM golang:1.22.0 WORKDIR /app COPY . . RUN go install github.com/go-delve/delve/cmd/dlv@latest EXPOSE 40000 8080 CMD ["dlv", "debug", "--headless", "--listen=:40000", "--api-version=2", "--accept-multiclient", "--log"]
- Add VS code remote debugging
launch.json
{ "version": "0.2.0", "configurations": [ { "name": "Connect to server", "type": "go", "request": "attach", "mode": "remote", "port": 40000, "host": "127.0.0.1" } ] }
- Build the docker image and run it
docker build -t demo . docker run -p 40000:40000 -p 8080:8080 demo
- Now I can see the dlv is running and click the VScode
Connect to server
to remote debugPS C:\repos\demo> docker run -p 40000:40000 -p 8080:8080 demo 2024-02-16T04:46:42Z warning layer=rpc Listening for remote connections (connections are not authenticated nor encrypted) 2024-02-16T04:46:42Z info layer=debugger launching process with args: [./__debug_bin4263375465] API server listening at: [::]:40000 2024-02-16T04:46:42Z debug layer=debugger Adding target 1534 "/app/__debug_bin4263375465" 2024-02-16T04:46:50Z debug layer=debugger continuing 2024-02-16T04:46:50Z debug layer=debugger ContinueOnce
- Now my code is running the remote debugger is attached. I can use my browser to hit the my "Hello, world!" end-point. However, I cannot set break point on my code, the VScode said could not find code c:\repos\demo\main.go, If click in the VScode try to set a breakpoint, the docker log said:
2024-02-16T04:46:59Z debug layer=debugger halting 2024-02-16T04:46:59Z warning layer=debugger gnu_debuglink link "ce4e6e4ef08fa58a3535f7437bd3e592db5ac0.debug" not found in any debug info directory 2024-02-16T04:46:59Z warning layer=debugger gnu_debuglink link "e7d4a67acf053c794b3b8094e6900b5163f37d.debug" not found in any debug info directory 2024-02-16T04:46:59Z debug layer=debugger callInjection protocol on: 2024-02-16T04:46:59Z debug layer=debugger 1534 PC=0x405e8e 2024-02-16T04:46:59Z debug layer=debugger 1542 PC=0x4795a3 2024-02-16T04:46:59Z debug layer=debugger 1543 PC=0x4795a3 2024-02-16T04:46:59Z debug layer=debugger 1544 PC=0x4795a3 2024-02-16T04:46:59Z debug layer=debugger 1545 PC=0x4795a3 2024-02-16T04:46:59Z debug layer=debugger continuing (direction congruent) 2024-02-16T04:46:59Z debug layer=debugger ContinueOnce
I attempted to run the same code about a month ago, and everything worked perfectly. However, this time it seems to be broken. I'm not certain if anything has changed recently.
That worked for me - open VSCode user's settings.json file and add there: