Skip to content
This repository was archived by the owner on Jan 31, 2024. It is now read-only.

Commit ecb8026

Browse files
author
Henry Wong
authored
Merge 59b5269 into 2702523
2 parents 2702523 + 59b5269 commit ecb8026

File tree

6 files changed

+36
-4
lines changed

6 files changed

+36
-4
lines changed

go/packages/golist.go

+15
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,13 @@ func addNeededOverlayPackages(cfg *Config, driver driver, response *responseDedu
240240
}
241241

242242
func runContainsQueries(cfg *Config, driver driver, response *responseDeduper, queries []string, rootDirs func() map[string]string) error {
243+
go111ModuleOff := false
244+
for i := range cfg.Env {
245+
if cfg.Env[len(cfg.Env)-i-1] == "GO111MODULE=off" {
246+
go111ModuleOff = true
247+
break
248+
}
249+
}
243250
for _, query := range queries {
244251
// TODO(matloob): Do only one query per directory.
245252
fdir := filepath.Dir(query)
@@ -249,6 +256,14 @@ func runContainsQueries(cfg *Config, driver driver, response *responseDeduper, q
249256
if err != nil {
250257
return fmt.Errorf("could not determine absolute path of file= query path %q: %v", query, err)
251258
}
259+
if go111ModuleOff {
260+
// Given that langserver converts the repos to modules explicitly, 'go list' has no idea where to find the
261+
// packages with 'GO111MODULE=off'. Setting the list pattern to relative local import will guide 'go list'
262+
// to find the part of the packages inside current repo.
263+
if dir, err := filepath.Rel(cfg.Dir, fdir); err == nil {
264+
pattern = "." + string(filepath.Separator) + dir
265+
}
266+
}
252267
dirResponse, err := driver(cfg, pattern)
253268
if err != nil || (len(dirResponse.Packages) == 1 && len(dirResponse.Packages[0].Errors) == 1) {
254269
// There was an error loading the package. Try to load the file as an ad-hoc package.

internal/lsp/cache/session.go

+6
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,12 @@ func (s *session) NewView(ctx context.Context, name string, folder span.URI) sou
9393
},
9494
ignoredURIs: make(map[span.URI]struct{}),
9595
}
96+
97+
// Setting 'GO111MODULE=off' by default. 'GO111MODULE=off' is inconsistent with module mode, this will disable
98+
// the deps download.
99+
// TODO(henrywong) Use 'GOPROXY=off' to disable the network access
100+
v.env = append(v.env, "GO111MODULE=off")
101+
96102
// Preemptively build the builtin package,
97103
// so we immediately add builtin.go to the list of ignored files.
98104
v.buildBuiltinPkg(ctx)

internal/lsp/elasticserver.go

+2-4
Original file line numberDiff line numberDiff line change
@@ -189,10 +189,8 @@ type WorkspaceFolderMeta struct {
189189
// manageDeps will try its best to convert the folders to modules. The core functions, like deps downloading and deps
190190
// management, will be implemented in the package 'cache'.
191191
func (s ElasticServer) ManageDeps(folders *[]protocol.WorkspaceFolder) error {
192-
// Note: For the upstream go langserver, granularity of the workspace folders is repository. But for the elastic go
193-
// language server, there are repositories contain multiple modules. In order to handle the modules separately, we
194-
// consider different modules as different workspace folders, so we can manage the dependency of different modules
195-
// separately.
192+
// In order to handle the modules separately, we consider different modules as different workspace folders, so we
193+
// can manage the dependency of different modules separately.
196194
for _, folder := range *folders {
197195
metadata := &WorkspaceFolderMeta{}
198196
if folder.URI != "" {

internal/lsp/general.go

+3
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ func (s *Server) initialize(ctx context.Context, params *protocol.InitializePara
3838
if opt, ok := opts["noIncrementalSync"].(bool); ok && opt {
3939
s.textDocumentSyncKind = protocol.Full
4040
}
41+
if opt, ok := opts["installGoDependency"].(bool); ok && opt {
42+
s.installGoDependency = true
43+
}
4144
}
4245

4346
// Default to using synopsis as a default for hover information.

internal/lsp/server.go

+1
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ type Server struct {
8888
preferredContentFormat protocol.MarkupKind
8989
disabledAnalyses map[string]struct{}
9090
wantSuggestedFixes bool
91+
installGoDependency bool
9192

9293
supportedCodeActions map[source.FileKind]map[protocol.CodeActionKind]bool
9394

internal/lsp/workspace.go

+9
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ import (
88
"context"
99

1010
"golang.org/x/tools/internal/lsp/protocol"
11+
"golang.org/x/tools/internal/lsp/telemetry/log"
1112
"golang.org/x/tools/internal/span"
1213
errors "golang.org/x/xerrors"
14+
"os/exec"
1315
)
1416

1517
func (s *Server) changeFolders(ctx context.Context, event protocol.WorkspaceFoldersChangeEvent) error {
@@ -31,6 +33,13 @@ func (s *Server) changeFolders(ctx context.Context, event protocol.WorkspaceFold
3133
}
3234

3335
func (s *Server) addView(ctx context.Context, name string, uri span.URI) error {
36+
if s.installGoDependency {
37+
cmd := exec.Command("go", "mod", "download")
38+
cmd.Dir = uri.Filename()
39+
if err := cmd.Run(); err != nil {
40+
log.Error(ctx, "failed to download the dependencies", err)
41+
}
42+
}
3443
view := s.session.NewView(ctx, name, uri)
3544
s.stateMu.Lock()
3645
state := s.state

0 commit comments

Comments
 (0)