golang / vscode-go

Go extension for Visual Studio Code
https://marketplace.visualstudio.com/items?itemName=golang.Go
Other
3.89k stars 753 forks source link

VSCode debugging a Go app with dlv exits unexpectedly #2183

Closed mipnw closed 2 years ago

mipnw commented 2 years ago

What version of Go, VS Code & VS Code Go extension are you using?

Version Information
* Run `go version` to get version of Go from _the VS Code integrated terminal_. - 1.17.8 * Run `gopls -v version` to get version of Gopls from _the VS Code integrated terminal_. - 0.8.3 * Run `code -v` or `code-insiders -v` to get version of VS Code or VS Code Insiders. - 1.66.2 * Check your installed extensions to get the version of the VS Code Go extension - 0.32.0 * Run Ctrl+Shift+P (Cmd+Shift+P on Mac OS) > `Go: Locate Configured Go Tools` command. ``` Checking configured tools.... GOBIN: undefined toolsGopath: gopath: /Users/mipnw/go GOROOT: /usr/local/Cellar/go/1.17.8/libexec PATH: /usr/local/opt/coreutils/libexec/gnubin:/usr/local/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/TeX/texbin:/usr/local/go/bin:/Users/mipnw/bin/google-cloud-sdk/bin:/Users/mipnw/go/bin:/Applications/Visual Studio Code.app/Contents/Resources/app/bin:/Users/mipnw/Library/Python/3.8/bin/ go: /usr/local/bin/go: go version go1.17.8 darwin/amd64 go-outline: /Users/mipnw/go/bin/go-outline (version: v0.0.0-20210608161538-9736a4bde949 built with go: go1.17) gotests: /Users/mipnw/go/bin/gotests (version: v1.6.0 built with go: go1.17) gomodifytags: /Users/mipnw/go/bin/gomodifytags (version: v1.14.0 built with go: go1.17) impl: /Users/mipnw/go/bin/impl (version: v1.1.0 built with go: go1.17) goplay: /Users/mipnw/go/bin/goplay (version: v1.0.0 built with go: go1.17) dlv: /Users/mipnw/go/bin/dlv (version: v1.7.1 built with go: go1.17) staticcheck: /Users/mipnw/go/bin/staticcheck (version: v0.2.1 built with go: go1.17) gopls: /Users/mipnw/go/bin/gopls (version: v0.8.3 built with go: go1.17.8) go env Workspace Folder (checkimage): /Users/mipnw/src/mipnw/checkimage GO111MODULE="" GOARCH="amd64" GOBIN="" GOCACHE="/Users/mipnw/Library/Caches/go-build" GOENV="/Users/mipnw/Library/Application Support/go/env" GOEXE="" GOEXPERIMENT="" GOFLAGS="" GOHOSTARCH="amd64" GOHOSTOS="darwin" GOINSECURE="" GOMODCACHE="/Users/mipnw/go/pkg/mod" GONOPROXY="" GONOSUMDB="" GOOS="darwin" GOPATH="/Users/mipnw/go" GOPRIVATE="" GOPROXY="https://proxy.golang.org,direct" GOROOT="/usr/local/Cellar/go/1.17.8/libexec" GOSUMDB="sum.golang.org" GOTMPDIR="" GOTOOLDIR="/usr/local/Cellar/go/1.17.8/libexec/pkg/tool/darwin_amd64" GOVCS="" GOVERSION="go1.17.8" GCCGO="gccgo" AR="ar" CC="clang" CXX="clang++" CGO_ENABLED="1" GOMOD="/Users/mipnw/src/mipnw/checkimage/go.mod" CGO_CFLAGS="-g -O2" CGO_CPPFLAGS="" CGO_CXXFLAGS="-g -O2" CGO_FFLAGS="-g -O2" CGO_LDFLAGS="-g -O2" PKG_CONFIG="pkg-config" GOGCCFLAGS="-fPIC -arch x86_64 -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/mv/13x0c0p510119w7702_qf73s9rwdhc/T/go-build638434252=/tmp/go-build -gno-record-gcc-switches -fno-common" ```

Share the Go related settings you have added/edited

Run Preferences: Open Settings (JSON) command to open your settings.json file. Share all the settings with the go. or ["go"] or gopls prefixes.

 "[go]": {
        "editor.insertSpaces": false,
        "editor.formatOnSave": true,
        "editor.codeActionsOnSave": {
            "source.organizeImports": true
        },
    },
    "go.formatTool": "goimports",
    "go.toolsManagement.autoUpdate": true,

Describe the bug

VSCode debugger attached to a headless dlv debugger errors when stepping over a line of source code. The VSCode's debug console shows this error

**Unhandled error in debug adapter: TypeError: Cannot read properties of undefined (reading 'addr')
    at GoDebugSession.convertDebugVariableToProtocolVariable (/Users/mipnw/.vscode/extensions/golang.go-0.32.0/dist/debugAdapter.js:16651:25)
    at /Users/mipnw/.vscode/extensions/golang.go-0.32.0/dist/debugAdapter.js:16219:55
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async Promise.all (index 3)**

Steps to reproduce the behavior:

Clone https://github.com/mipnw/checkImage and follow the README.md instructions which are essentially copied here.

That git repo contains the VSCode launch configuration, the Dockerfile specifying how to build the docker image with a trivial go app and dlv. make docker-build builds that image, make debug runs the container with dlv listening 2345.

Set a breakpoint on [$GOPATH]/pkg/mod/github.com/google/go-containerregistry@v0.8.0/pkg/v1/remote/descriptor.go line 106, and launch the VSCode launch configuration to debug. The breakpoint will hit, step over, VScode's debug console should show the error pasted above, and the headless dlv will receive debugger detaching which exits the container.

Dockerfile

FROM golang:1.17.8-alpine3.15 as dev
ENV GO111MODULE=on
WORKDIR /usr/src/app
COPY ./go.mod ./go.sum ./
RUN go mod download
COPY . .

FROM dev as build-dbg
ENV CGO_ENABLED=0
RUN go get github.com/go-delve/delve/cmd/dlv
RUN go build -gcflags="-N -l" -o /usr/local/bin/checkimage cmd/checkimage/main.go

FROM alpine:3.15.0 as deploy-dbg
COPY --from=build-dbg /go/bin/dlv /usr/local/bin/dlv
COPY --from=build-dbg /usr/local/bin/checkimage /usr/local/bin/checkimage

Makefile

.PHONY: docker-build
docker-build:
    DOCKER_BUILDKIT=1 docker build \
        -f Dockerfile \
        -t checkimage:latest \
        .

.PHONY: debug
debug:
    docker run \
    --rm \
    --entrypoint dlv \
    -p 2345:2345 \
    checkimage:latest \
        --listen=:2345 --headless --api-version=2 --log=true --log-output=debugger,debuglineerr,gdbwire,lldbout,rpc exec /usr/local/bin/checkimage -- myimage:mytag

launch.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Attach delve",
            "type": "go",
            "request": "attach",
            "mode": "remote",
            "port":2345,
            "host":"127.0.0.1",
            "showLog": true,
            "trace": "log",
            "substitutePath": [
                {
                    "from": "${workspaceFolder}", 
                    "to": "/usr/src/app"
                },
                {
                    "from": "/Users/${env:USER}/go",
                    "to": "/go"
                }
            ],
        }
    ]
}

Screenshots or recordings

n/a

hyangah commented 2 years ago

Thanks for the report. Looks like the result from delve doesn't seem to match the assumption made here

https://github.com/golang/vscode-go/blob/e599c1ab941c35b986c55771bb9dd73d2f7f58b2/src/debugAdapter/goDebug.ts#L2329

This is a bug in the legacy debug adapter (which we no longer actively maintain). Can you test if the new debug adapter handles this better?

Adding "debugAdapter": "dlv-dap" to the launch configuration will make the extension to use the new adapter for remote/attach mode (See https://github.com/golang/vscode-go/blob/master/docs/debugging.md#connecting-to-headless-delve-with-target-specified-at-server-start-up).

If it doesn't work, can you please capture the delve's log (--log-output=dap) and share? Thanks!