Skip to content

Commit be332ea

Browse files
committed
gopls/internal/lsp/cache: avoid problematic use of goCommandInvocation
Avoid using goCommandInvocation from populateProcessEnvFromSnapshot, just to get the BuildFlags and environment. Copy the small bit of logic instead. Change-Id: Ie7ed1c76b29dbd12c251fdf9e8659c9c34141268 Reviewed-on: https://go-review.googlesource.com/c/tools/+/542623 Reviewed-by: Alan Donovan <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent dfa5cbb commit be332ea

File tree

1 file changed

+5
-21
lines changed

1 file changed

+5
-21
lines changed

gopls/internal/lsp/cache/imports.go

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,15 @@ package cache
77
import (
88
"context"
99
"fmt"
10+
"os"
1011
"reflect"
1112
"strings"
1213
"sync"
1314
"time"
1415

1516
"golang.org/x/tools/gopls/internal/file"
16-
"golang.org/x/tools/gopls/internal/lsp/source"
1717
"golang.org/x/tools/internal/event"
1818
"golang.org/x/tools/internal/event/keys"
19-
"golang.org/x/tools/internal/gocommand"
2019
"golang.org/x/tools/internal/imports"
2120
)
2221

@@ -127,33 +126,18 @@ func populateProcessEnvFromSnapshot(ctx context.Context, pe *imports.ProcessEnv,
127126
pe.Logf = nil
128127
}
129128

130-
// Extract invocation details from the snapshot to use with goimports.
131-
//
132-
// TODO(rfindley): refactor to extract the necessary invocation logic into
133-
// separate functions. Using goCommandInvocation is unnecessarily indirect,
134-
// and has led to memory leaks in the past, when the snapshot was
135-
// unintentionally held past its lifetime.
136-
_, inv, cleanupInvocation, err := snapshot.goCommandInvocation(ctx, source.LoadWorkspace, &gocommand.Invocation{
137-
WorkingDir: snapshot.view.goCommandDir.Path(),
138-
})
139-
if err != nil {
140-
return err
141-
}
142-
143-
pe.BuildFlags = inv.BuildFlags
129+
pe.WorkingDir = snapshot.view.goCommandDir.Path()
144130
pe.ModFlag = "readonly" // processEnv operations should not mutate the modfile
145131
pe.Env = map[string]string{}
146-
for _, kv := range inv.Env {
132+
pe.BuildFlags = append([]string{}, snapshot.Options().BuildFlags...)
133+
env := append(append(os.Environ(), snapshot.Options().EnvSlice()...), "GO111MODULE="+snapshot.view.GO111MODULE())
134+
for _, kv := range env {
147135
split := strings.SplitN(kv, "=", 2)
148136
if len(split) != 2 {
149137
continue
150138
}
151139
pe.Env[split[0]] = split[1]
152140
}
153-
// We don't actually use the invocation, so clean it up now.
154-
cleanupInvocation()
155-
// TODO(rfindley): should this simply be inv.WorkingDir?
156-
pe.WorkingDir = snapshot.view.goCommandDir.Path()
157141
return nil
158142
}
159143

0 commit comments

Comments
 (0)