Skip to content

Commit

Permalink
[elastic] Move the deps download into 'intialize' request handle
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Henry Wong committed Aug 21, 2019
1 parent 2702523 commit 64bd846
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 11 deletions.
12 changes: 11 additions & 1 deletion internal/lsp/cache/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (s *session) Cache() source.Cache {
return s.cache
}

func (s *session) NewView(ctx context.Context, name string, folder span.URI) source.View {
func (s *session) NewView(ctx context.Context, name string, folder span.URI, installGoDeps bool) source.View {
index := atomic.AddInt64(&viewIndex, 1)
s.viewMu.Lock()
defer s.viewMu.Unlock()
Expand Down Expand Up @@ -93,6 +93,16 @@ func (s *session) NewView(ctx context.Context, name string, folder span.URI) sou
},
ignoredURIs: make(map[span.URI]struct{}),
}

if installGoDeps {
v.env = append(v.env, "GO111MODULE=on")
} else {
// Setting 'GO111MODULE=off' by default. 'GO111MODULE=off' is inconsistent with module mode, this will disable
// the deps download.
// TODO(henrywong) Use 'GOPROXY=off' to disable the network access
v.env = append(v.env, "GO111MODULE=off")
}

// Preemptively build the builtin package,
// so we immediately add builtin.go to the list of ignored files.
v.buildBuiltinPkg(ctx)
Expand Down
2 changes: 1 addition & 1 deletion internal/lsp/elasticext_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func testLSPExt(t *testing.T, exporter packagestest.Exporter) {

cache := cache.New()
session := cache.NewSession(ctx)
view := session.NewView(cfg.Context, extViewName, span.FileURI(cfg.Dir))
view := session.NewView(cfg.Context, extViewName, span.FileURI(cfg.Dir), false)
view.SetEnv(cfg.Env)
s := &Server{
session: session,
Expand Down
6 changes: 2 additions & 4 deletions internal/lsp/elasticserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,10 +189,8 @@ type WorkspaceFolderMeta struct {
// manageDeps will try its best to convert the folders to modules. The core functions, like deps downloading and deps
// management, will be implemented in the package 'cache'.
func (s ElasticServer) ManageDeps(folders *[]protocol.WorkspaceFolder) error {
// Note: For the upstream go langserver, granularity of the workspace folders is repository. But for the elastic go
// language server, there are repositories contain multiple modules. In order to handle the modules separately, we
// consider different modules as different workspace folders, so we can manage the dependency of different modules
// separately.
// In order to handle the modules separately, we consider different modules as different workspace folders, so we
// can manage the dependency of different modules separately.
for _, folder := range *folders {
metadata := &WorkspaceFolderMeta{}
if folder.URI != "" {
Expand Down
3 changes: 3 additions & 0 deletions internal/lsp/general.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ func (s *Server) initialize(ctx context.Context, params *protocol.InitializePara
if opt, ok := opts["noIncrementalSync"].(bool); ok && opt {
s.textDocumentSyncKind = protocol.Full
}
if opt, ok := opts["installGoDependency"].(bool); ok && opt {
s.installGoDependency = true
}
}

// Default to using synopsis as a default for hover information.
Expand Down
2 changes: 1 addition & 1 deletion internal/lsp/lsp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func testLSP(t *testing.T, exporter packagestest.Exporter) {

cache := cache.New()
session := cache.NewSession(ctx)
view := session.NewView(ctx, viewName, span.FileURI(data.Config.Dir))
view := session.NewView(ctx, viewName, span.FileURI(data.Config.Dir), false)
view.SetEnv(data.Config.Env)
for filename, content := range data.Config.Overlay {
session.SetOverlay(span.FileURI(filename), content)
Expand Down
2 changes: 1 addition & 1 deletion internal/lsp/protocol/elasticserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type ElasticServer interface {
Server
EDefinition(context.Context, *TextDocumentPositionParams) ([]SymbolLocator, error)
Full(context.Context, *FullParams) (FullResponse, error)
ManageDeps(folders *[]WorkspaceFolder) error
ManageDeps(*[]WorkspaceFolder) error
}

type elasticServerHandler struct {
Expand Down
1 change: 1 addition & 0 deletions internal/lsp/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ type Server struct {
preferredContentFormat protocol.MarkupKind
disabledAnalyses map[string]struct{}
wantSuggestedFixes bool
installGoDependency bool

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

Expand Down
2 changes: 1 addition & 1 deletion internal/lsp/source/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ type Cache interface {
// A session may have many active views at any given time.
type Session interface {
// NewView creates a new View and returns it.
NewView(ctx context.Context, name string, folder span.URI) View
NewView(ctx context.Context, name string, folder span.URI, installGoDeps bool) View

// Cache returns the cache that created this session.
Cache() Cache
Expand Down
12 changes: 10 additions & 2 deletions internal/lsp/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ package lsp

import (
"context"

"golang.org/x/tools/internal/lsp/protocol"
"golang.org/x/tools/internal/lsp/telemetry/log"
"golang.org/x/tools/internal/span"
errors "golang.org/x/xerrors"
"os/exec"
)

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

func (s *Server) addView(ctx context.Context, name string, uri span.URI) error {
view := s.session.NewView(ctx, name, uri)
if s.installGoDependency {
cmd := exec.Command("go", "mod", "download")
cmd.Dir = uri.Filename()
if err := cmd.Run(); err != nil {
log.Error(ctx, "failed to download the dependencies", err)
}
}
view := s.session.NewView(ctx, name, uri, s.installGoDependency)
s.stateMu.Lock()
state := s.state
s.stateMu.Unlock()
Expand Down

0 comments on commit 64bd846

Please sign in to comment.