Skip to content

Commit

Permalink
add goTool param
Browse files Browse the repository at this point in the history
  • Loading branch information
xushiwei committed Jan 9, 2024
1 parent a7dfb48 commit 2444f11
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 27 deletions.
9 changes: 7 additions & 2 deletions cmd/dlv/cmds/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,11 @@ func dapCmd(cmd *cobra.Command, args []string) {
os.Exit(status)
}

// gopdlv: add `goTool`
func goTool(cmd *cobra.Command) string {
return "gop" // TODO
}

func buildBinary(cmd *cobra.Command, args []string, isTest bool) (string, bool) {
outputFlag := cmd.Flag("output").Value.String()
var debugname string
Expand All @@ -561,9 +566,9 @@ func buildBinary(cmd *cobra.Command, args []string, isTest bool) (string, bool)
}

if isTest {
err = gobuild.GoTestBuild(debugname, args, buildFlags)
err = gobuild.GoTestBuild(goTool(cmd), debugname, args, buildFlags)
} else {
err = gobuild.GoBuild(debugname, args, buildFlags)
err = gobuild.GoBuild(goTool(cmd), debugname, args, buildFlags)
}
if err != nil {
if outputFlag == "" {
Expand Down
38 changes: 22 additions & 16 deletions pkg/gobuild/gobuild.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,36 +34,40 @@ func Remove(path string) {

// GoBuild builds non-test files in 'pkgs' with the specified 'buildflags'
// and writes the output at 'debugname'.
func GoBuild(debugname string, pkgs []string, buildflags string) error {
// gopdlv: add `goTool` param
func GoBuild(goTool, debugname string, pkgs []string, buildflags string) error {
args := goBuildArgs(debugname, pkgs, buildflags, false)
return gocommandRun("build", args...)
return gocommandRun(goTool, "build", args...)
}

// GoBuildCombinedOutput builds non-test files in 'pkgs' with the specified 'buildflags'
// and writes the output at 'debugname'.
func GoBuildCombinedOutput(debugname string, pkgs []string, buildflags interface{}) (string, []byte, error) {
// gopdlv: add `goTool` param
func GoBuildCombinedOutput(goTool, debugname string, pkgs []string, buildflags interface{}) (string, []byte, error) {
args, err := goBuildArgs2(debugname, pkgs, buildflags, false)
if err != nil {
return "", nil, err
}
return gocommandCombinedOutput("build", args...)
return gocommandCombinedOutput(goTool, "build", args...)
}

// GoTestBuild builds test files 'pkgs' with the specified 'buildflags'
// and writes the output at 'debugname'.
func GoTestBuild(debugname string, pkgs []string, buildflags string) error {
// gopdlv: add `goTool` param
func GoTestBuild(goTool, debugname string, pkgs []string, buildflags string) error {
args := goBuildArgs(debugname, pkgs, buildflags, true)
return gocommandRun("test", args...)
return gocommandRun(goTool, "test", args...)
}

// GoTestBuildCombinedOutput builds test files 'pkgs' with the specified 'buildflags'
// and writes the output at 'debugname'.
func GoTestBuildCombinedOutput(debugname string, pkgs []string, buildflags interface{}) (string, []byte, error) {
// gopdlv: add `goTool` param
func GoTestBuildCombinedOutput(goTool, debugname string, pkgs []string, buildflags interface{}) (string, []byte, error) {
args, err := goBuildArgs2(debugname, pkgs, buildflags, true)
if err != nil {
return "", nil, err
}
return gocommandCombinedOutput("test", args...)
return gocommandCombinedOutput(goTool, "test", args...)
}

func goBuildArgs(debugname string, pkgs []string, buildflags string, isTest bool) []string {
Expand Down Expand Up @@ -111,25 +115,27 @@ func goBuildArgs2(debugname string, pkgs []string, buildflags interface{}, isTes
return append(args, pkgs...), nil
}

func gocommandRun(command string, args ...string) error {
_, goBuild := gocommandExecCmd(command, args...)
// gopdlv: add `goTool` param
func gocommandRun(goTool, command string, args ...string) error {
_, goBuild := gocommandExecCmd(goTool, command, args...)
goBuild.Stderr = os.Stdout
goBuild.Stdout = os.Stderr
return goBuild.Run()
}

func gocommandCombinedOutput(command string, args ...string) (string, []byte, error) {
buildCmd, goBuild := gocommandExecCmd(command, args...)
// gopdlv: add `goTool` param
func gocommandCombinedOutput(goTool, command string, args ...string) (string, []byte, error) {
buildCmd, goBuild := gocommandExecCmd(goTool, command, args...)
out, err := goBuild.CombinedOutput()
return buildCmd, out, err
}

func gocommandExecCmd(command string, args ...string) (string, *exec.Cmd) {
func gocommandExecCmd(goTool, command string, args ...string) (string, *exec.Cmd) {
allargs := []string{command}
allargs = append(allargs, args...)
// gopdlv: Go+
// gopdlv: add `goTool` param
// goBuild := exec.Command("go", allargs...)
// return strings.Join(append([]string{"go"}, allargs...), " "), goBuild
goBuild := exec.Command("gop", allargs...)
return strings.Join(append([]string{"gop"}, allargs...), " "), goBuild
goBuild := exec.Command(goTool, allargs...)
return strings.Join(append([]string{goTool}, allargs...), " "), goBuild
}
4 changes: 2 additions & 2 deletions service/dap/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -989,9 +989,9 @@ func (s *Session) onLaunchRequest(request *dap.LaunchRequest) {

switch args.Mode {
case "debug":
cmd, out, err = gobuild.GoBuildCombinedOutput(args.Output, []string{args.Program}, args.BuildFlags.value)
cmd, out, err = gobuild.GoBuildCombinedOutput(args.goTool(), args.Output, []string{args.Program}, args.BuildFlags.value)
case "test":
cmd, out, err = gobuild.GoTestBuildCombinedOutput(args.Output, []string{args.Program}, args.BuildFlags.value)
cmd, out, err = gobuild.GoTestBuildCombinedOutput(args.goTool(), args.Output, []string{args.Program}, args.BuildFlags.value)
}
args.DlvCwd, _ = filepath.Abs(args.DlvCwd)
s.config.log.Debugf("building from %q: [%s]", args.DlvCwd, cmd)
Expand Down
11 changes: 11 additions & 0 deletions service/dap/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ type LaunchConfig struct {
// Default is "debug".
Mode string `json:"mode,omitempty"`

// goxdlv: add `goTool`
// Tool is the command to build/test packages. Default is `gop` (not `go`).
Tool string `json:"tool,omitempty"`

// Path to the program folder (or any go file within that folder)
// when in `debug` or `test` mode, and to the pre-built binary file
// to debug in `exec` mode.
Expand Down Expand Up @@ -160,6 +164,13 @@ type LaunchConfig struct {
LaunchAttachCommonConfig
}

func (p *LaunchConfig) goTool() string {
if p.Tool == "" {
return "gop"
}
return p.Tool
}

// LaunchAttachCommonConfig is the attributes common in both launch/attach requests.
type LaunchAttachCommonConfig struct {
// Automatically stop program after launch or attach.
Expand Down
15 changes: 13 additions & 2 deletions service/debugger/debugger.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,10 @@ type Config struct {
// TTY for that process.
TTY string

// goxdlv: add `goTool`
// Tool is the command to build/test packages. Default is `gop` (not `go`).
Tool string

// Packages contains the packages that we are debugging.
Packages []string

Expand All @@ -160,6 +164,13 @@ type Config struct {
RrOnProcessPid int
}

func (p *Config) goTool() string {
if p.Tool == "" {
return "gop"
}
return p.Tool
}

// New creates a new Debugger. ProcessArgs specify the commandline arguments for the
// new process.
func New(config *Config, processArgs []string) (*Debugger, error) {
Expand Down Expand Up @@ -515,12 +526,12 @@ func (d *Debugger) Restart(rerecord bool, pos string, resetArgs bool, newArgs []
if rebuild {
switch d.config.ExecuteKind {
case ExecutingGeneratedFile:
err = gobuild.GoBuild(d.processArgs[0], d.config.Packages, d.config.BuildFlags)
err = gobuild.GoBuild(d.config.goTool(), d.processArgs[0], d.config.Packages, d.config.BuildFlags)
if err != nil {
return nil, fmt.Errorf("could not rebuild process: %s", err)
}
case ExecutingGeneratedTest:
err = gobuild.GoTestBuild(d.processArgs[0], d.config.Packages, d.config.BuildFlags)
err = gobuild.GoTestBuild(d.config.goTool(), d.processArgs[0], d.config.Packages, d.config.BuildFlags)
if err != nil {
return nil, fmt.Errorf("could not rebuild process: %s", err)
}
Expand Down
10 changes: 7 additions & 3 deletions service/debugger/debugger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,17 @@ import (
"github.com/go-delve/delve/service/api"
)

const (
goTool = "go" // gopdlv: add `goTool`
)

func TestDebugger_LaunchNoMain(t *testing.T) {
fixturesDir := protest.FindFixturesDir()
nomaindir := filepath.Join(fixturesDir, "nomaindir")
debugname := "debug"
exepath := filepath.Join(nomaindir, debugname)
defer os.Remove(exepath)
if err := gobuild.GoBuild(debugname, []string{nomaindir}, fmt.Sprintf("-o %s", exepath)); err != nil {
if err := gobuild.GoBuild(goTool, debugname, []string{nomaindir}, fmt.Sprintf("-o %s", exepath)); err != nil {
t.Fatalf("go build error %v", err)
}

Expand Down Expand Up @@ -51,7 +55,7 @@ func TestDebugger_LaunchInvalidFormat(t *testing.T) {
}
t.Setenv("GOOS", switchOS[runtime.GOOS])
exepath := filepath.Join(buildtestdir, debugname)
if err := gobuild.GoBuild(debugname, []string{buildtestdir}, fmt.Sprintf("-o %s", exepath)); err != nil {
if err := gobuild.GoBuild(goTool, debugname, []string{buildtestdir}, fmt.Sprintf("-o %s", exepath)); err != nil {
t.Fatalf("go build error %v", err)
}
defer os.Remove(exepath)
Expand Down Expand Up @@ -81,7 +85,7 @@ func TestDebugger_LaunchCurrentDir(t *testing.T) {
t.Fatalf("error removing executable %v", err)
}
}()
if err := gobuild.GoBuild(debugname, []string{testDir}, fmt.Sprintf("-o %s", exepath)); err != nil {
if err := gobuild.GoBuild(goTool, debugname, []string{testDir}, fmt.Sprintf("-o %s", exepath)); err != nil {
t.Fatalf("go build error %v", err)
}

Expand Down
4 changes: 2 additions & 2 deletions service/debugger/debugger_unix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func TestDebugger_LaunchNoExecutablePerm(t *testing.T) {
t.Setenv("GOOS", switchOS[runtime.GOOS])
exepath := filepath.Join(buildtestdir, debugname)
defer os.Remove(exepath)
if err := gobuild.GoBuild(debugname, []string{buildtestdir}, fmt.Sprintf("-o %s", exepath)); err != nil {
if err := gobuild.GoBuild(goTool, debugname, []string{buildtestdir}, fmt.Sprintf("-o %s", exepath)); err != nil {
t.Fatalf("go build error %v", err)
}
if err := os.Chmod(exepath, 0644); err != nil {
Expand Down Expand Up @@ -74,7 +74,7 @@ func TestDebugger_LaunchWithTTY(t *testing.T) {
buildtestdir := filepath.Join(fixturesDir, "buildtest")
debugname := "debugtty"
exepath := filepath.Join(buildtestdir, debugname)
if err := gobuild.GoBuild(debugname, []string{buildtestdir}, fmt.Sprintf("-o %s", exepath)); err != nil {
if err := gobuild.GoBuild(goTool, debugname, []string{buildtestdir}, fmt.Sprintf("-o %s", exepath)); err != nil {
t.Fatalf("go build error %v", err)
}
defer os.Remove(exepath)
Expand Down

0 comments on commit 2444f11

Please sign in to comment.