无法使用 VSCode 中的断点调试 Golang

Can't debug Golang with breakpoints in VSCode

我在 VS Code 中使用断点调试 Golang 应用程序。 调试器抱怨无法找到存在的文件。

有谁知道如何在 VS Code 中为 Go 应用程序启用断点吗?

调试器日志:

Debuggee is not running. Setting breakpoints without halting.
All cleared
Creating on: /home/gbajson/Sync/clickr/clickr-node-api/clickr-node-api.go:63
Creating on: /home/gbajson/Sync/clickr/clickr-node-api/clickr-node-api.go:84
All cleared
All set:[]
SetBreakPointsResponse
2021-10-21T12:52:17+02:00 debug layer=rpc <- RPCServer.CreateBreakpoint(rpc2.CreateBreakpointIn{"Breakpoint":{"id":0,"name":"","addr":0,"addrs":null,"file":"/home/gbajson/Sync/clickr/clickr-node-api/clickr-node-api.go","line":63,"Cond":"","HitCond":"","continue":false,"traceReturn":false,"goroutine":false,"stacktrace":0,"LoadArgs":{"FollowPointers":true,"MaxVariableRecurse":1,"MaxStringLen":64,"MaxArrayValues":64,"MaxStructFields":-1},"LoadLocals":{"FollowPointers":true,"MaxVariableRecurse":1,"MaxStringLen":64,"MaxArrayValues":64,"MaxStructFields":-1},"WatchExpr":"","WatchType":0,"hitCount":null,"totalHitCount":0,"disabled":false}})
2021-10-21T12:52:17+02:00 debug layer=rpc -> *rpc2.CreateBreakpointOut{"Breakpoint":{"id":0,"name":"","addr":0,"addrs":null,"file":"","line":0,"Cond":"","HitCond":"","continue":false,"traceReturn":false,"goroutine":false,"stacktrace":0,"LoadArgs":null,"LoadLocals":null,"WatchExpr":"","WatchType":0,"hitCount":null,"totalHitCount":0,"disabled":false}} error: "could not find file /home/gbajson/Sync/clickr/clickr-node-api/clickr-node-api.go"
2021-10-21T12:52:17+02:00 debug layer=rpc <- RPCServer.CreateBreakpoint(rpc2.CreateBreakpointIn{"Breakpoint":{"id":0,"name":"","addr":0,"addrs":null,"file":"/home/gbajson/Sync/clickr/clickr-node-api/clickr-node-api.go","line":84,"Cond":"","HitCond":"","continue":false,"traceReturn":false,"goroutine":false,"stacktrace":0,"LoadArgs":{"FollowPointers":true,"MaxVariableRecurse":1,"MaxStringLen":64,"MaxArrayValues":64,"MaxStructFields":-1},"LoadLocals":{"FollowPointers":true,"MaxVariableRecurse":1,"MaxStringLen":64,"MaxArrayValues":64,"MaxStructFields":-1},"WatchExpr":"","WatchType":0,"hitCount":null,"totalHitCount":0,"disabled":false}})
2021-10-21T12:52:17+02:00 debug layer=rpc -> *rpc2.CreateBreakpointOut{"Breakpoint":{"id":0,"name":"","addr":0,"addrs":null,"file":"","line":0,"Cond":"","HitCond":"","continue":false,"traceReturn":false,"goroutine":false,"stacktrace":0,"LoadArgs":null,"LoadLocals":null,"WatchExpr":"","WatchType":0,"hitCount":null,"totalHitCount":0,"disabled":false}} error: "could not find file /home/gbajson/Sync/clickr/clickr-node-api/clickr-node-api.go"
2
Error on CreateBreakpoint: could not find file /home/gbajson/Sync/clickr/clickr-node-api/clickr-node-api.go

文件存在:

gbajson@misio:~$ ls -l /home/gbajson/Sync/clickr/clickr-node-api/clickr-node-api.go
-rw-r--r-- 1 gbajson gbajson 2961 Oct 21 12:22 /home/gbajson/Sync/clickr/clickr-node-api/clickr-node-api.go

我已经检查过这不是文件权限的问题。 我还跟进了dlv lagacy版本的过程: https://github.com/golang/vscode-go/blob/master/docs/debugging-legacy.md#selecting-legacy-debug-adapter

调试器配置

{
    "go.delveConfig": {
        "debugAdapter": "legacy",
    },

我发现了问题。 VS Code 不能很好地处理符号链接。 当我在真实路径中的 VS Code 中设置项目时,调试器开始正常工作。

gbajson@misio:~$ realpath /home/gbajson/Sync/clickr/clickr-node-api/clickr-node-api.go
/storage/amoje/Sync/clickr/clickr-node-api/clickr-node-api.go

这个问题也描述在:https://github.com/golang/vscode-go/issues/1677

想在这里分享我的解决方案。 在阅读了一些 github issues vs-code-go 之后,问题似乎是由 remotePathsubstitutePath 引起的。经过几次尝试,将 remotePath 设置为 "" 终于成功了。

    "version": "0.2.0",
    "configurations": [
        {
            "name": "Connect to server",
            "type": "go",
            "request": "attach",
            "mode": "remote",
            "remotePath": "",
            "port": 40000,
            "host": "<host>",
        }
    ]
}

这是 gbajson 回答的后续。

符号链接对我来说是个问题。在 launch.json 中使用此配置解决了问题,同时允许我保留符号链接:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Launch",
            "type": "go",
            "request": "launch",
            "mode": "auto",
            "program": "${workspaceFolder}/cmd/server/main.go",
            "envFile": "${workspaceFolder}/.env",
            "substitutePath": [
                {
                    "from": "/home/me/go-workspaces",
                    "to": "/data/projects/workspaces"
                }
            ]
        }
    ]
}

具体来说就是“substitutePath”部分。

我的工作区目录实际上是从“/home/me/go-workspaces”到实际目录“/data/projects/workspaces”的符号链接。

参见:https://github.com/golang/vscode-go/blob/master/docs/debugging.md#debugging-symlink-directories