简体   繁体   中英

Debug Golang in VSCode but output path is wrong

I write a program in vscode, and the project path in D:\myapp. And I "F5" 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?

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}/",

        }
    ]
}```

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 :

https://code.visualstudio.com/assets/api/extension-guides/debugger-extension/debug-arch1.png

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM