Skip to content

Commit

Permalink
internal/imports: change processEnv to use buildflags
Browse files Browse the repository at this point in the history
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 <[email protected]>
Run-TryBot: Heschi Kreinick <[email protected]>
TryBot-Result: Gobot Gobot <[email protected]>
(cherry picked from commit 3868802)
Reviewed-on: https://go-review.googlesource.com/c/tools/+/219124
  • Loading branch information
ridersofrohan authored and stamblerre committed Feb 12, 2020
1 parent fc53639 commit 7aafd4d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 16 deletions.
11 changes: 9 additions & 2 deletions internal/imports/fix.go
Original file line number Diff line number Diff line change
Expand Up @@ -750,6 +750,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
Expand Down Expand Up @@ -822,8 +824,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
Expand Down
23 changes: 9 additions & 14 deletions internal/lsp/cache/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,8 +370,9 @@ func (v *view) refreshProcessEnv() {

func (v *view) buildProcessEnv(ctx context.Context) (*imports.ProcessEnv, error) {
cfg := v.Config(ctx)
env := &imports.ProcessEnv{
processEnv := &imports.ProcessEnv{
WorkingDir: cfg.Dir,
BuildFlags: cfg.BuildFlags,
Logf: func(format string, args ...interface{}) {
log.Print(ctx, fmt.Sprintf(format, args...))
},
Expand All @@ -385,26 +386,20 @@ func (v *view) buildProcessEnv(ctx context.Context) (*imports.ProcessEnv, error)
}
switch split[0] {
case "GOPATH":
env.GOPATH = split[1]
processEnv.GOPATH = split[1]
case "GOROOT":
env.GOROOT = split[1]
processEnv.GOROOT = split[1]
case "GO111MODULE":
env.GO111MODULE = split[1]
processEnv.GO111MODULE = split[1]
case "GOPROXY":
env.GOPROXY = split[1]
processEnv.GOPROXY = split[1]
case "GOFLAGS":
env.GOFLAGS = split[1]
processEnv.GOFLAGS = split[1]
case "GOSUMDB":
env.GOSUMDB = split[1]
processEnv.GOSUMDB = split[1]
}
}
if len(cfg.BuildFlags) > 0 {
if env.GOFLAGS != "" {
env.GOFLAGS += " "
}
env.GOFLAGS += strings.Join(cfg.BuildFlags, " ")
}
return env, nil
return processEnv, nil
}

func (v *view) fileVersion(filename string) string {
Expand Down

0 comments on commit 7aafd4d

Please sign in to comment.