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

[elastic] Move the deps downloading into 'intialize' request handle #79

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions go/internal/packagesdriver/sizes.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"encoding/json"
"fmt"
"go/types"
"golang.org/x/tools/internal/lsp/protocol"
"log"
"os"
"os/exec"
Expand Down Expand Up @@ -125,6 +126,7 @@ func InvokeGo(ctx context.Context, env []string, dir string, usesExportData bool
cmd.Dir = dir
cmd.Stdout = stdout
cmd.Stderr = stderr
protocol.AdjustGoListForVendorMode(&(cmd.Env), &(cmd.Args))
if err := cmd.Run(); err != nil {
exitErr, ok := err.(*exec.ExitError)
if !ok {
Expand Down
3 changes: 2 additions & 1 deletion go/packages/golist.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"encoding/json"
"fmt"
"go/types"
"golang.org/x/tools/internal/lsp/protocol"
"hash/fnv"
"io/ioutil"
"log"
Expand Down Expand Up @@ -860,7 +861,7 @@ func invokeGo(cfg *Config, args ...string) (*bytes.Buffer, error) {
defer func(start time.Time) {
cfg.Logf("%s for %v, stderr: <<%s>>\n", time.Since(start), cmdDebugStr(cmd, args...), stderr)
}(time.Now())

protocol.AdjustGoListForVendorMode(&(cmd.Env), &(cmd.Args))
if err := cmd.Run(); err != nil {
// Check for 'go' executable not being found.
if ee, ok := err.(*exec.Error); ok && ee.Err == exec.ErrNotFound {
Expand Down
5 changes: 5 additions & 0 deletions internal/lsp/cache/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ func (s *session) NewView(ctx context.Context, name string, folder span.URI) sou
},
ignoredURIs: make(map[span.URI]struct{}),
}
if enableVendor, ok := ctx.Value("ENABLEVENDOR").(bool); ok && enableVendor {
// Set 'GOPROXY=off' to disable the network access
v.env = append(v.env, "GOPROXY=off")
v.env = append(v.env, "ENABLEVENDOR=on")
}
// Preemptively build the builtin package,
// so we immediately add builtin.go to the list of ignored files.
v.buildBuiltinPkg(ctx)
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
16 changes: 16 additions & 0 deletions internal/lsp/protocol/elasticserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,22 @@ type ElasticServer interface {
ManageDeps(folders *[]WorkspaceFolder) error
}

func AdjustGoListForVendorMode(env *[]string, args *[]string) {
l := len(*env)
for i := range *env {
if (*env)[l-i-1] == "ENABLEVENDOR=on" {
// If 'ENABLEVENDOR' is on, append '-mod=vendor' to go list command
for j := range *args {
if (j+2 < len(*args)) && (*args)[j] == "go" && (*args)[j+1] == "list" {
*args = append((*args)[:j+2], append([]string{"-mod=vendor"}, (*args)[j+2:]...)...)
break
}
}
return
}
}
}

type elasticServerHandler struct {
canceller
server ElasticServer
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
12 changes: 12 additions & 0 deletions internal/lsp/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ 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,6 +33,16 @@ func (s *Server) changeFolders(ctx context.Context, event protocol.WorkspaceFold
}

func (s *Server) addView(ctx context.Context, name string, uri span.URI) error {
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)
}
} else {
// If we disable the go dependency download, trying to find the deps from the vendor folder.
ctx = context.WithValue(ctx, "ENABLEVENDOR", true)
}
view := s.session.NewView(ctx, name, uri)
s.stateMu.Lock()
state := s.state
Expand Down