From 3868802a52a81fee73b9d36e7328ade976cd4665 Mon Sep 17 00:00:00 2001 From: Rohan Challa Date: Mon, 10 Feb 2020 13:32:08 -0500 Subject: [PATCH] internal/imports: change processEnv to use buildflags This change adds a buildFlags variable to the processEnv struct rather than appending them to the GOFLAGS by using a space separator. Fixes golang/go#37108 Change-Id: I4331066c30fa51f0133504d723132527b00ce74a Reviewed-on: https://go-review.googlesource.com/c/tools/+/218857 Reviewed-by: Heschi Kreinick Run-TryBot: Heschi Kreinick TryBot-Result: Gobot Gobot --- internal/imports/fix.go | 11 +++++++++-- internal/lsp/cache/view.go | 7 +------ 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/internal/imports/fix.go b/internal/imports/fix.go index f95d0f4409..ee01d34b1b 100644 --- a/internal/imports/fix.go +++ b/internal/imports/fix.go @@ -749,6 +749,8 @@ type ProcessEnv struct { LocalPrefix string Debug bool + BuildFlags []string + // If non-empty, these will be used instead of the // process-wide values. GOPATH, GOROOT, GO111MODULE, GOPROXY, GOFLAGS, GOSUMDB string @@ -821,8 +823,13 @@ func (e *ProcessEnv) buildContext() *build.Context { return &ctx } -func (e *ProcessEnv) invokeGo(args ...string) (*bytes.Buffer, error) { - cmd := exec.Command("go", args...) +func (e *ProcessEnv) invokeGo(verb string, args ...string) (*bytes.Buffer, error) { + goArgs := []string{verb} + if verb != "env" { + goArgs = append(goArgs, e.BuildFlags...) + } + goArgs = append(goArgs, args...) + cmd := exec.Command("go", goArgs...) stdout := &bytes.Buffer{} stderr := &bytes.Buffer{} cmd.Stdout = stdout diff --git a/internal/lsp/cache/view.go b/internal/lsp/cache/view.go index 4525853edc..c034ea5f77 100644 --- a/internal/lsp/cache/view.go +++ b/internal/lsp/cache/view.go @@ -333,6 +333,7 @@ func (v *view) buildProcessEnv(ctx context.Context) (*imports.ProcessEnv, error) env, buildFlags := v.env() processEnv := &imports.ProcessEnv{ WorkingDir: v.folder.Filename(), + BuildFlags: buildFlags, Logf: func(format string, args ...interface{}) { log.Print(ctx, fmt.Sprintf(format, args...)) }, @@ -359,12 +360,6 @@ func (v *view) buildProcessEnv(ctx context.Context) (*imports.ProcessEnv, error) processEnv.GOSUMDB = split[1] } } - if len(buildFlags) > 0 { - if processEnv.GOFLAGS != "" { - processEnv.GOFLAGS += " " - } - processEnv.GOFLAGS += strings.Join(buildFlags, " ") - } return processEnv, nil }