Home > Software engineering >  Debug Golang in VSCode but output path is wrong
Debug Golang in VSCode but output path is wrong

Time:12-01

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

        }
    ]
}```

CodePudding user response:

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.

  • Related