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

Commit 64bd846

Browse files
author
Henry Wong
committed
[elastic] Move the deps download into 'intialize' request handle
The original implementation relies on 'go list' to download the deps. 'go list' is called by other request handler indirectly. This approach is unpredictable and 'go list' is black box to us. So it's better download the deps manually in 'ManageDeps', i.e. inside 'initialize' request handler. For security, set 'GO111MODULE=off' by default, 'GO111MODULE=off' will disable the deps download by inconsistencies. NOTE:'GO111MODULE=off' break the go langserver ability in some extent. The official option for disable the network access is setting 'GOPROXY=off' under mod mode, however this option will break the go langserver totally. Related issue: golang/go#32337
1 parent 2702523 commit 64bd846

File tree

9 files changed

+31
-11
lines changed

9 files changed

+31
-11
lines changed

internal/lsp/cache/session.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func (s *session) Cache() source.Cache {
6565
return s.cache
6666
}
6767

68-
func (s *session) NewView(ctx context.Context, name string, folder span.URI) source.View {
68+
func (s *session) NewView(ctx context.Context, name string, folder span.URI, installGoDeps bool) source.View {
6969
index := atomic.AddInt64(&viewIndex, 1)
7070
s.viewMu.Lock()
7171
defer s.viewMu.Unlock()
@@ -93,6 +93,16 @@ func (s *session) NewView(ctx context.Context, name string, folder span.URI) sou
9393
},
9494
ignoredURIs: make(map[span.URI]struct{}),
9595
}
96+
97+
if installGoDeps {
98+
v.env = append(v.env, "GO111MODULE=on")
99+
} else {
100+
// Setting 'GO111MODULE=off' by default. 'GO111MODULE=off' is inconsistent with module mode, this will disable
101+
// the deps download.
102+
// TODO(henrywong) Use 'GOPROXY=off' to disable the network access
103+
v.env = append(v.env, "GO111MODULE=off")
104+
}
105+
96106
// Preemptively build the builtin package,
97107
// so we immediately add builtin.go to the list of ignored files.
98108
v.buildBuiltinPkg(ctx)

internal/lsp/elasticext_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func testLSPExt(t *testing.T, exporter packagestest.Exporter) {
6060

6161
cache := cache.New()
6262
session := cache.NewSession(ctx)
63-
view := session.NewView(cfg.Context, extViewName, span.FileURI(cfg.Dir))
63+
view := session.NewView(cfg.Context, extViewName, span.FileURI(cfg.Dir), false)
6464
view.SetEnv(cfg.Env)
6565
s := &Server{
6666
session: session,

internal/lsp/elasticserver.go

Lines changed: 2 additions & 4 deletions
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

Lines changed: 3 additions & 0 deletions
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/lsp_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func testLSP(t *testing.T, exporter packagestest.Exporter) {
4343

4444
cache := cache.New()
4545
session := cache.NewSession(ctx)
46-
view := session.NewView(ctx, viewName, span.FileURI(data.Config.Dir))
46+
view := session.NewView(ctx, viewName, span.FileURI(data.Config.Dir), false)
4747
view.SetEnv(data.Config.Env)
4848
for filename, content := range data.Config.Overlay {
4949
session.SetOverlay(span.FileURI(filename), content)

internal/lsp/protocol/elasticserver.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ type ElasticServer interface {
1111
Server
1212
EDefinition(context.Context, *TextDocumentPositionParams) ([]SymbolLocator, error)
1313
Full(context.Context, *FullParams) (FullResponse, error)
14-
ManageDeps(folders *[]WorkspaceFolder) error
14+
ManageDeps(*[]WorkspaceFolder) error
1515
}
1616

1717
type elasticServerHandler struct {

internal/lsp/server.go

Lines changed: 1 addition & 0 deletions
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/source/view.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ type Cache interface {
130130
// A session may have many active views at any given time.
131131
type Session interface {
132132
// NewView creates a new View and returns it.
133-
NewView(ctx context.Context, name string, folder span.URI) View
133+
NewView(ctx context.Context, name string, folder span.URI, installGoDeps bool) View
134134

135135
// Cache returns the cache that created this session.
136136
Cache() Cache

internal/lsp/workspace.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@ package lsp
66

77
import (
88
"context"
9-
109
"golang.org/x/tools/internal/lsp/protocol"
10+
"golang.org/x/tools/internal/lsp/telemetry/log"
1111
"golang.org/x/tools/internal/span"
1212
errors "golang.org/x/xerrors"
13+
"os/exec"
1314
)
1415

1516
func (s *Server) changeFolders(ctx context.Context, event protocol.WorkspaceFoldersChangeEvent) error {
@@ -31,7 +32,14 @@ func (s *Server) changeFolders(ctx context.Context, event protocol.WorkspaceFold
3132
}
3233

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

0 commit comments

Comments
 (0)