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

Commit 5d84904

Browse files
author
Henry Wong
authored
Merge 0a0f0d7 into 2702523
2 parents 2702523 + 0a0f0d7 commit 5d84904

File tree

4 files changed

+41
-6
lines changed

4 files changed

+41
-6
lines changed

internal/lsp/cache/load.go

+21-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"golang.org/x/tools/internal/lsp/telemetry/trace"
1717
"golang.org/x/tools/internal/span"
1818
errors "golang.org/x/xerrors"
19+
"path/filepath"
1920
)
2021

2122
func (v *view) loadParseTypecheck(ctx context.Context, f *goFile) ([]packages.Error, error) {
@@ -91,7 +92,7 @@ func (v *view) checkMetadata(ctx context.Context, f *goFile) (map[packageID]*met
9192

9293
ctx, done := trace.StartSpan(ctx, "packages.Load", telemetry.File.Of(f.filename()))
9394
defer done()
94-
pkgs, err := packages.Load(v.Config(ctx), fmt.Sprintf("file=%s", f.filename()))
95+
pkgs, err := loadPackages(ctx, f, v)
9596
if len(pkgs) == 0 {
9697
if err == nil {
9798
err = errors.Errorf("go/packages.Load: no packages found for %s", f.filename())
@@ -106,7 +107,6 @@ func (v *view) checkMetadata(ctx context.Context, f *goFile) (map[packageID]*met
106107
}
107108
// Track missing imports as we look at the package's errors.
108109
missingImports := make(map[packagePath]struct{})
109-
110110
log.Print(ctx, "go/packages.Load", tag.Of("packages", len(pkgs)))
111111
for _, pkg := range pkgs {
112112
log.Print(ctx, "go/packages.Load", tag.Of("package", pkg.PkgPath), tag.Of("files", pkg.CompiledGoFiles))
@@ -301,3 +301,22 @@ func filenamesIdentical(oldFiles, newFiles []string) bool {
301301
}
302302
return len(oldByName) == 0
303303
}
304+
305+
func loadPackages(ctx context.Context, f *goFile, v *view) ([]*packages.Package, error) {
306+
go111Module := false
307+
for i := range v.env {
308+
if v.env[len(v.env)-i-1] == "GO111MODULE=off" {
309+
go111Module = true
310+
break
311+
}
312+
}
313+
if go111Module {
314+
if dir, err := filepath.Rel(v.folder.Filename(), filepath.Dir(f.filename())); err != nil {
315+
return packages.Load(v.Config(ctx), fmt.Sprintf("file=%s", f.filename()))
316+
} else {
317+
return packages.Load(v.Config(ctx), fmt.Sprintf("pattern=%s", "."+string(filepath.Separator)+dir))
318+
}
319+
} else {
320+
return packages.Load(v.Config(ctx), fmt.Sprintf("file=%s", f.filename()))
321+
}
322+
}

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

+12
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"context"
1010
"fmt"
1111
"os"
12+
"os/exec"
1213
"path"
1314

1415
"golang.org/x/tools/internal/jsonrpc2"
@@ -32,12 +33,16 @@ func (s *Server) initialize(ctx context.Context, params *protocol.InitializePara
3233
s.state = serverInitializing
3334
s.stateMu.Unlock()
3435

36+
installGoDependency := false
3537
// TODO: Remove the option once we are certain there are no issues here.
3638
s.textDocumentSyncKind = protocol.Incremental
3739
if opts, ok := params.InitializationOptions.(map[string]interface{}); ok {
3840
if opt, ok := opts["noIncrementalSync"].(bool); ok && opt {
3941
s.textDocumentSyncKind = protocol.Full
4042
}
43+
if opt, ok := opts["installGoDependency"].(bool); ok && opt {
44+
installGoDependency = true
45+
}
4146
}
4247

4348
// Default to using synopsis as a default for hover information.
@@ -73,6 +78,13 @@ func (s *Server) initialize(ctx context.Context, params *protocol.InitializePara
7378
if err := s.addView(ctx, folder.Name, span.NewURI(folder.URI)); err != nil {
7479
return nil, err
7580
}
81+
if installGoDependency {
82+
cmd := exec.Command("go", "mod", "download")
83+
cmd.Dir = span.NewURI(folder.URI).Filename()
84+
if err := cmd.Run(); err != nil {
85+
log.Error(ctx, "failed to download the dependencies", err)
86+
}
87+
}
7688
}
7789
return &protocol.InitializeResult{
7890
Capabilities: protocol.ServerCapabilities{

0 commit comments

Comments
 (0)