英文:
Debug Golang in VSCode but output path is wrong
问题
我在VSCode中编写了一个程序,项目路径在D:\myapp
。我使用"F5"进行调试。通常情况下,我会在项目文件夹中得到一个名为"__debug_bin
"的文件。
上周,我更新了VSC并重新安装了所有工具(例如dlv
)。然后我得到了一个新的调试文件"__debug_bin1167246115.exe
"。但是该文件没有输出到我的项目文件夹中,而是绝对路径为"C:\Users\xxx\AppData\Local\Temp\__debug_bin1167246115.exe
"。
问题是:
我需要将调试文件输出并运行在我的项目根文件夹中;我该如何做到这一点?
- VS Code版本:1.62.3(用户安装)
- Golang版本:1.17.3
- launch.json:
"version": "0.2.0",
"configurations": [
{
"name": "Launch Package",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${workspaceFolder}/",
"cwd": "${workspaceFolder}/",
}
]
}
英文:
I write a program in VSCode, and the project path in D:\myapp
. And I "<kbd>F5</kbd>" to debug.
Normally, I will get a file names "__debug_bin
" in my project folder.
Last week, I updated VSC and reinstall all tool (dlv
or something).
And I get a new debug file "__debug_bin1167246115.exe
".
But the file not output in my project folder, the absolute path is "C:\Users\xxx\AppData\Local\Temp\__debug_bin1167246115.exe
".
The question is:
I need the debug file output and run in my project root folder; How can I do that?
- VS Code Version : 1.62.3 (user setup)
- Golang Version : 1.17.3
- launch.json :
"version": "0.2.0",
"configurations": [
{
"name": "Launch Package",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${workspaceFolder}/",
"cwd": "${workspaceFolder}/",
}
]
}
答案1
得分: 3
上周(2021年11月底),我更新了VSC并重新安装了所有工具(例如dlv
)。然后我得到了一个新的调试文件“__debug_bin1167246115.exe
”。
VSCode调试文档提到了一个最近的变化(2021年第四季度):
Go扩展允许您启动或附加到Go程序进行调试。您可以检查变量和堆栈,设置断点,并使用VS Code的调试界面进行其他调试活动。
这些调试功能是通过使用Go调试器Delve实现的。Go扩展通过自定义调试适配器程序(遗留模式)与Delve进行通信。随着新的Delve原生DAP实现的推出,Go扩展正在过渡到跳过遗留调试适配器,直接与Delve进行本地调试通信。
📣 我们很高兴地宣布,现在默认启用了这种新的Delve集成模式(dlv-dap模式)进行本地调试!
注意:DAP的意思是“调试适配器协议”(在此处介绍):
我们称这个中间件为调试适配器(DA),DA与VS Code之间使用的抽象协议是调试适配器协议(DAP)。
由于调试适配器协议与VS Code无关,它有自己的网站,您可以在其中找到介绍和概述、详细的规范以及一些已知实现和支持工具的列表。DAP的历史和动机在这篇博文中有解释。
作为这种新的DAP支持的一部分,您有一个提交fa10cec,它首先尝试创建一个临时文件,然后再退回到您所知道的方式:
// Default output file pathname for the compiled binary in debug or test modes
// when temporary debug binary creation fails.
// This is relative to the current working directory of the server.
const defaultDebugBinary string = "./__debug_bin"
func (s *Session) tempDebugBinary() string {
binaryPattern := "__debug_bin"
if runtime.GOOS == "windows" {
binaryPattern = "__debug_bin*.exe"
}
f, err := ioutil.TempFile("", binaryPattern)
if err != nil {
s.config.log.Errorf("failed to create a temporary binary (%v), falling back to %q", err, defaultDebugBinary)
return cleanExeName(defaultDebugBinary)
}
...
}
这是由以下代码调用的:
// Prepare the debug executable filename, building it if necessary
debugbinary := args.Program
if args.Mode == "debug" || args.Mode == "test" {
if args.Output == "" {
args.Output = s.tempDebugBinary()
} else {
args.Output = cleanExeName(args.Output)
}
args.Output, err = filepath.Abs(args.Output)
因此,请尝试在launch.json属性中设置output
标志,或者使用dlvFlags
和output
键值对。将其设置为./__debug_bin
。
英文:
> Last week (end Nov. 2021), I updated VSC and reinstall all tool (dlv
or something).
And I get a new debug file "__debug_bin1167246115.exe
".
VSCode Debugging documentation mentions a recent (Q4 2021) change
> The Go extension allows you to launch or attach to Go programs for debugging. You can inspect variables and stacks, setting breakpoints, and do other debugging activities using VS Code’s Debugging UI.
>
> These debugging features are possible by using Delve, the Go debugger. The Go extension has been communicating with Delve through a custom debug adapter program (legacy mode). As the new Delve's native DAP implementation becomes available, the Go extension is transitioning to skip the legacy debug adapter and directly communicate with Delve for local debugging.
>
> 📣 We are happy to announce that now this new mode of Delve integration (dlv-dap mode) is enabled for local debugging by default!
Note: DAP means "Debug Adaptor Protocol" presented here:
> We call this intermediary the Debug Adapter (or DA for short) and the abstract protocol that is used between the DA and VS Code is the Debug Adapter Protocol (DAP for short).
>
> Since the Debug Adapter Protocol is independent from VS Code, it has its own web site where you can find an introduction and overview, the detailed specification, and some lists with known implementations and supporting tools.
The history of and motivation behind DAP is explained in this blog post.
As part of this new DAP support, you have commit fa10cec, which tries first to create a temp file, before falling back to what you knew:
// Default output file pathname for the compiled binary in debug or test modes
// when temporary debug binary creation fails.
// This is relative to the current working directory of the server.
const defaultDebugBinary string = "./__debug_bin"
func (s *Session) tempDebugBinary() string {
binaryPattern := "__debug_bin"
if runtime.GOOS == "windows" {
binaryPattern = "__debug_bin*.exe"
}
f, err := ioutil.TempFile("", binaryPattern)
if err != nil {
s.config.log.Errorf("failed to create a temporary binary (%v), falling back to %q", err, defaultDebugBinary)
return cleanExeName(defaultDebugBinary)
}
...
}
This is [called by][6]:
```go
// Prepare the debug executable filename, building it if necessary
debugbinary := args.Program
if args.Mode == "debug" || args.Mode == "test" {
if args.Output == "" {
args.Output = s.tempDebugBinary()
} else {
args.Output = cleanExeName(args.Output)
}
args.Output, err = filepath.Abs(args.Output)
So try and set the output
flag in launch.json attributes, or dlvFlags
with a output
key value.
Set it to ./__debug_bin
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论