Skip to content

Commit 84a23b1

Browse files
committed
service/dap: accept a string list as launch request's buildFlags
This change accepts both string type and []string (actually, []interface{} type due to Go's json decoding behavior. Fixes go-delve#2718 For golang/vscode-go#1831, golang/vscode-go#1027
1 parent f469a0a commit 84a23b1

File tree

3 files changed

+61
-7
lines changed

3 files changed

+61
-7
lines changed

pkg/gobuild/gobuild.go

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,11 @@ func GoBuild(debugname string, pkgs []string, buildflags string) error {
4141

4242
// GoBuildCombinedOutput builds non-test files in 'pkgs' with the specified 'buildflags'
4343
// and writes the output at 'debugname'.
44-
func GoBuildCombinedOutput(debugname string, pkgs []string, buildflags string) (string, []byte, error) {
45-
args := goBuildArgs(debugname, pkgs, buildflags, false)
44+
func GoBuildCombinedOutput(debugname string, pkgs []string, buildflags interface{}) (string, []byte, error) {
45+
args, err := goBuildArgs2(debugname, pkgs, buildflags, false)
46+
if err != nil {
47+
return "", nil, err
48+
}
4649
return gocommandCombinedOutput("build", args...)
4750
}
4851

@@ -55,8 +58,11 @@ func GoTestBuild(debugname string, pkgs []string, buildflags string) error {
5558

5659
// GoTestBuildCombinedOutput builds test files 'pkgs' with the specified 'buildflags'
5760
// and writes the output at 'debugname'.
58-
func GoTestBuildCombinedOutput(debugname string, pkgs []string, buildflags string) (string, []byte, error) {
59-
args := goBuildArgs(debugname, pkgs, buildflags, true)
61+
func GoTestBuildCombinedOutput(debugname string, pkgs []string, buildflags interface{}) (string, []byte, error) {
62+
args, err := goBuildArgs2(debugname, pkgs, buildflags, true)
63+
if err != nil {
64+
return "", nil, err
65+
}
6066
return gocommandCombinedOutput("test", args...)
6167
}
6268

@@ -84,6 +90,36 @@ func goBuildArgs(debugname string, pkgs []string, buildflags string, isTest bool
8490
return args
8591
}
8692

93+
// goBuildArgs2 is like goBuildArgs, but takes either string, []string, or []interface{}.
94+
func goBuildArgs2(debugname string, pkgs []string, buildflags interface{}, isTest bool) ([]string, error) {
95+
var args []string
96+
switch buildflags := buildflags.(type) {
97+
case string:
98+
return goBuildArgs(debugname, pkgs, buildflags, false), nil
99+
case nil:
100+
case []string:
101+
args = append(args, buildflags...)
102+
case []interface{}:
103+
for _, flag := range buildflags {
104+
switch flag := flag.(type) {
105+
case string:
106+
args = append(args, flag)
107+
default:
108+
return nil, fmt.Errorf("invalid buildflags element type %T (%q)", flag, flag)
109+
}
110+
}
111+
default:
112+
return nil, fmt.Errorf("invalid buildflags type %T", buildflags)
113+
}
114+
115+
args = append(args, "-o", debugname)
116+
if isTest {
117+
args = append([]string{"-c"}, args...)
118+
}
119+
args = append(args, "-gcflags", "all=-N -l")
120+
return append(args, pkgs...), nil
121+
}
122+
87123
func gocommandRun(command string, args ...string) error {
88124
_, goBuild := gocommandExecCmd(command, args...)
89125
goBuild.Stderr = os.Stdout

service/dap/server_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5594,6 +5594,18 @@ func TestLaunchRequestWithBuildFlags(t *testing.T) {
55945594
})
55955595
}
55965596

5597+
func TestLaunchRequestWithBuildFlags2(t *testing.T) {
5598+
runTest(t, "buildflagtest", func(client *daptest.Client, fixture protest.Fixture) {
5599+
runDebugSession(t, client, "launch", func() {
5600+
// We reuse the harness that builds, but ignore the built binary,
5601+
// only relying on the source to be built in response to LaunchRequest.
5602+
client.LaunchRequestWithArgs(map[string]interface{}{
5603+
"mode": "debug", "program": fixture.Source, "output": "__mybin",
5604+
"buildFlags": []string{"-ldflags", "-X main.Hello=World"}})
5605+
})
5606+
})
5607+
}
5608+
55975609
func TestLaunchRequestWithEnv(t *testing.T) {
55985610
// testenv fixture will lookup SOMEVAR with
55995611
// x, y := os.Lookup("SOMEVAR")

service/dap/types.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,15 @@ type LaunchConfig struct {
102102
// Relative paths used in BuildFlags will be interpreted as paths
103103
// relative to Delve's current working directory.
104104
//
105-
// It is like `dlv --build-flags`. For example,
106-
// "buildFlags": "-tags=integration -mod=vendor -cover -v"
107-
BuildFlags string `json:"buildFlags,omitempty"`
105+
// It should be a string like `dlv --build-flags`, or
106+
// an array of strings that is augmented when invoking the go build or
107+
// test command through os/exec.Command API.
108+
// For example, both forms are acceptible.
109+
// "buildFlags": "-tags=integration -ldflags='-X main.Hello=World'"
110+
// or
111+
// "buildFlags": ["-tags=integration", "-ldflags=-X main.Hello=World"]
112+
// Using other types is an error.
113+
BuildFlags interface{} `json:"buildFlags,omitempty"`
108114

109115
// Output path for the binary of the debuggee.
110116
// Relative path is interpreted as the path relative to

0 commit comments

Comments
 (0)