在VSCode中调试Golang时,但输出路径错误。

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

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 :
    &quot;version&quot;: &quot;0.2.0&quot;,
    &quot;configurations&quot;: [
        {
            &quot;name&quot;: &quot;Launch Package&quot;,
            &quot;type&quot;: &quot;go&quot;,
            &quot;request&quot;: &quot;launch&quot;,
            &quot;mode&quot;: &quot;auto&quot;,
            &quot;program&quot;: &quot;${workspaceFolder}/&quot;,
            &quot;cwd&quot;: &quot;${workspaceFolder}/&quot;,

        }
    ]
}

答案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的意思是“调试适配器协议”(在此处介绍):

在VSCode中调试Golang时,但输出路径错误。

我们称这个中间件为调试适配器(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标志,或者使用dlvFlagsoutput键值对。将其设置为./__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:

在VSCode中调试Golang时,但输出路径错误。

> 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 = &quot;./__debug_bin&quot;

func (s *Session) tempDebugBinary() string {
	binaryPattern := &quot;__debug_bin&quot;
	if runtime.GOOS == &quot;windows&quot; {
		binaryPattern = &quot;__debug_bin*.exe&quot;
	}
	f, err := ioutil.TempFile(&quot;&quot;, binaryPattern)
	if err != nil {
		s.config.log.Errorf(&quot;failed to create a temporary binary (%v), falling back to %q&quot;, 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 == &quot;debug&quot; || args.Mode == &quot;test&quot; {
		if args.Output == &quot;&quot; {
			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.

huangapple
  • 本文由 发表于 2021年11月30日 10:34:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/70163625.html
匿名

发表评论

匿名网友

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

确定