delve/pkg/gobuild/defaultexe.go
Alessandro Arzilli 84b757ad57
cmd/dlv,service/dap: use randomized name as default output binary (#3366)
Using a fixed path as the default output binary means that executing
Delve twice in the same directory will cause the second invocation to
overwrite the output binary of the first instance of Delve, making the
restart command not work correctly.

Fixes #3345
2023-05-16 09:36:15 -07:00

29 lines
612 B
Go

package gobuild
import (
"io/ioutil"
"runtime"
"github.com/go-delve/delve/pkg/logflags"
)
// DefaultDebugBinaryPath returns an unused file path in the current
// directory named 'name' followed by a random string
func DefaultDebugBinaryPath(name string) string {
pattern := name
if runtime.GOOS == "windows" {
pattern += "*.exe"
}
f, err := ioutil.TempFile(".", pattern)
if err != nil {
logflags.DebuggerLogger().Errorf("could not create temporary file for build output: %v", err)
if runtime.GOOS == "windows" {
return name + ".exe"
}
return name
}
r := f.Name()
f.Close()
return r
}