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

huangapple go评论93阅读模式
英文:

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 go extension v0.29.0. delve v1.7.1.

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

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

它启动了应用程序,我可以从控制台看到它正在工作,并且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的屏幕截图:

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

请注意,我在main的第一行的打印语句上设置了一个断点。

这是我按下F5后看到的情况:

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

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

更新

这是我的目录结构视图:

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 go extension v0.29.0. delve v1.7.1.

I'm new to vscode and Go. I have many years of experience debugging Java apps in Eclipse.

I've constructed a small multi-module Go app. I can set a breakpoint in main and other functions in other modules. Inside main.go, I select "Start Debugging".

It starts the application, and I can tell it's working from the console, and that the REST endpoint responds with my dummy response.

However, it will NOT stop at breakpoints. As soon as I start the session, the red breakpoint markers suddenly become hollow, and hovering on one of them shows a message "Could not find file ...", which prints the full path to the source file in question.

When I start it, it shows the following in the console:

> 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

I haven't modified the launch.json (I hope someday a friendlier interface to editing launch configurations is provided).

What else could I be doing wrong?

Update:

This is a screenshot showing main.go just before I press F5 (Start Debugging):

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

Notice that I have a breakpoint on the print statement, on the first line of main.

This is what I see after I press F5:

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

Notice that it printed "At start of main" in the console. It didn't stop at the breakpoint. Also notice message in tooltip when hovering over the breakpoint.

Update:

This is a view of my directory structure:

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

答案1

得分: 2

首先,请确保你使用go mod init voltagems初始化了你的项目:这可以解释为什么会有import "voltagems/xxx",同时也有助于delve在调试时找到你的main.go文件。
main.go旁边应该有go.modgo.sum文件。

其次,请检查你的go env输出,确保GOPATHGOROOT设置为默认路径。

OP David M. Karr评论中提到:

> 我在创建项目时运行了"go mod init",但我意识到我不喜欢根模块的名称,所以我将其改为"voltagems"

我相信你可以直接编辑go.mod文件的第一行,确保它是这样的:

module voltagems

然后运行go mod verify + go mod tidy

最后,运行go build .。重新启动你的VSCode(或者使用命令Reload Window),看看问题是否仍然存在。


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

> 我的项目路径中有符号链接
>
> 在VSCode-Go中有一个名为"substitutePath"的配置,用于映射到绝对路径。

你可以在使用旧版调试适配器进行调试中看到这个参数的提及。

> ## substitutePath
>
> 路径映射,用于从编辑器中的路径到编译程序中的路径(默认值:[])。

这来自问题622debug: 使用符号链接时断点不起作用”。
以及提交93f32bb

> ## src/debugAdapter: 添加用于调试的substitutePath配置
>
> 此更改向启动和附加请求添加了一个新的配置选项。
substituePath接受一个从字符串到字符串的映射数组,用于将路径传递给调试器,然后转换回客户端。
>
> 这允许用户将其符号链接的目录转换为实际用于构建二进制文件的文件。
此外,这也可以用于远程调试,以及当文件的位置自构建程序以来发生变化时。

例如:你需要一个fromto键:

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

First, just make sure you have initiated your project with go mod init voltagems: that would explain the import "voltagems/xxx", but also helps delve to find your main.go file at debug time.
You should have go.mod and go.sum files beside main.go.

Second, check your go env output, making sure GOPATH and GOROOT are set to default paths.

The OP David M. Karr adds in 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"

I believe you can edit directly go.mod first line, and make sure it says:

module voltagems

Then go mod verify + go mod tidy

Finally, go build .. Restart your VSCode (or the command Reload Window), and see if the issue persists.


The OP David M. Karr points out to a root cause:

> There are symbolic links in my project path.
>
> There is a "substitutePath" configuration in VSCode-Go that is used to map to absolute paths.

You can see this parameter mentioned in 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: []).

That comes from issue 622 "debug: breakpoints don't work when working with symlink".
And 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.

Example: you need a from and to key:

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

huangapple
  • 本文由 发表于 2021年11月12日 07:15:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/69936099.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定