调试进入 vscode 不会在断点处停止,当调试器启动时说 "Could not find file ..."

Debugging go in vscode doesn't stop at breakpoints, says "Could not find file ..." when debugger starts

Ubuntu。 vscode 1.62.1。 go1.17.3。 vscode 去扩展 v0.29.0。钻研 v1.7.1.

我是 vscode 和 Go 的新手。我有多年在 Eclipse 中调试 Java 应用程序的经验。

我构建了一个小型多模块 Go 应用程序。我可以在其他模块的 main 和其他函数中设置断点。在 main.go 中,我 select“开始调试”。

它启动了应用程序,我可以从控制台得知它正在运行,并且 REST 端点响应我的虚拟响应。

但是,它不会在断点处停止。我一开始会话,红色断点标记突然变成空心,将鼠标悬停在其中一个上会显示一条消息“找不到文件...”,它会打印出相关源文件的完整路径。

当我启动它时,它在控制台中显示以下内容:

Starting: /home/.../go/bin/dlv-dap dap --check-go-version=false --listen=127.0.0.1:43347 --log-dest=3 from /home/.../... DAP server listening at: 127.0.0.1:43347

我还没有修改 launch.json(我希望有一天能提供一个更友好的界面来编辑启动配置)。

我还有什么地方做错了吗?

更新:

这是我按 F5(开始调试)之前 main.go 的屏幕截图:

注意我在 print 语句上有一个断点,在 main 的第一行。

这是我按 F5 后看到的:

请注意,它在控制台中打印了“At start of main”。它没有在断点处停止。还要注意将鼠标悬停在断点上时工具提示中的消息。

更新:

这是我的目录结构视图:

首先,请确保您已使用 go mod init voltagems 启动您的项目:这将解释 import "voltagems/xxx",但也有助于 delve 在以下位置找到您的 main.go 文件调试时间。
main.go.

旁边应该有 go.modgo.sum 文件

其次,检查您的 go env 输出,确保 GOPATH and GOROOT 设置为默认路径。

OP David M. Karr adds in :

I did run "go mod init" when I first created the project, but I realized that I didn't like the root module name, so I changed it to "voltagems"

我相信你可以直接编辑 go.mod 第一行,并确保它说:

module voltagems

然后 go mod verify + go mod tidy

最后,go build .。重新启动您的 VSCode(或命令 Reload Window),然后查看问题是否仍然存在。


OP David M. Karr 指出了根本原因:

There are symbolic links in my project path.

There is a "substitutePath" configuration in VSCode-Go that is used to map to absolute paths.

可以看到Debugging with Legacy Debug Adapter

中提到的这个参数

substitutePath

Path mappings to apply to get from a path in the editor to a path in the compiled program (default: []).

来自 issue 622debug:使用符号链接时断点不起作用”。
并且 commit 93f32bb

src/debugAdapter: add substitutePath config for debugging

This change adds a new configuration option to both launch and attach requests.
substituePath takes an array that maps from string to string that is used to translate paths passed to the debugger and then back to the client.

This allows users to translate their symlinked directories to the files that were actually used to build the binary.
In addition this can also be used for remote debugging, and when the location of the files has moved since the program was built.

示例:您需要 fromto 密钥:

    "substitutePath": [
        {
            "from": "/symlink/path/dir/on/local/machine",
            "to": "/absolute/path/dir/on/local/machine",
        },