gpt4 book ai didi

go - 在vscode中调试go不会在断点处停止,调试器启动时说 "Could not find file ..."

转载 作者:行者123 更新时间:2023-12-05 05:53:13 24 4
gpt4 key购买 nike

Ubuntu。代码 1.62.1。 go1.17.3。 vscode 去扩展 v0.29.0。 Delve v1.7.1.

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

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

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

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

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

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 的屏幕截图:

Before pressing F5

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

这是我按 F5 后看到的:

After pressing F5

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

更新:

这是我的目录结构的 View :

project directory structure

最佳答案

首先,只需确保您已使用 go mod init voltagems 启动您的项目:这将解释 import "voltagems/xxx",但也有助于 delve在调试时找到您的 main.go 文件。
main.go 旁边应该有 go.modgo.sum 文件。

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

OP David M. Karr添加 the comments :

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

最后,开始构建。。重新启动 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 622调试:使用符号链接(symbolic link)时断点不起作用”。
commit 93f32bb

src/debugAdapter: add substitutePath config for debugging

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

This allows users to translate their symlinked directories to thefiles 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",
},

关于go - 在vscode中调试go不会在断点处停止,调试器启动时说 "Could not find file ...",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69936099/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com