dap: create temporary binary in the current working directory (#2868)

This is a partial revert of PR #2660.

Some users develop in environments where execution of binaries
in temp directory is prohibited or undesirable. Moreover, the
current implementation creates binaries with random names
and that made it more difficult to set up firewall exclusion
rules. Revert it and always use the default debug output name.
(The debug binary will be created in the delve's working directory
and an editor does not have any promise on working directory
unless user specifies it through dlvCwd. So users still need to
chase them unless they set the exclusion rule purely based on
the file base name).

Fixes golang/vscode-go#1955
This commit is contained in:
Hyang-Ah Hana Kim 2022-01-19 14:02:49 -05:00 committed by GitHub
parent b3e528e4fb
commit 0005eb9f58
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -17,7 +17,6 @@ import (
"go/constant" "go/constant"
"go/parser" "go/parser"
"io" "io"
"io/ioutil"
"math" "math"
"net" "net"
"os" "os"
@ -864,29 +863,10 @@ func (s *Session) setClientCapabilities(args dap.InitializeRequestArguments) {
s.clientCapabilities.supportsVariableType = args.SupportsVariableType s.clientCapabilities.supportsVariableType = args.SupportsVariableType
} }
// Default output file pathname for the compiled binary in debug or test modes // 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. // This is relative to the current working directory of the server.
const defaultDebugBinary string = "./__debug_bin" 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)
}
name := f.Name()
if err := f.Close(); err != nil {
s.config.log.Errorf("failed to create a temporary binary (%v), falling back to %q", err, defaultDebugBinary)
return cleanExeName(defaultDebugBinary)
}
return name
}
func cleanExeName(name string) string { func cleanExeName(name string) string {
if runtime.GOOS == "windows" && filepath.Ext(name) != ".exe" { if runtime.GOOS == "windows" && filepath.Ext(name) != ".exe" {
return name + ".exe" return name + ".exe"
@ -982,7 +962,7 @@ func (s *Session) onLaunchRequest(request *dap.LaunchRequest) {
debugbinary := args.Program debugbinary := args.Program
if args.Mode == "debug" || args.Mode == "test" { if args.Mode == "debug" || args.Mode == "test" {
if args.Output == "" { if args.Output == "" {
args.Output = s.tempDebugBinary() args.Output = cleanExeName(defaultDebugBinary)
} else { } else {
args.Output = cleanExeName(args.Output) args.Output = cleanExeName(args.Output)
} }