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

Commit b948c72

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.
1 parent 2702523 commit b948c72

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

internal/lsp/elasticserver.go

+11-5
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"golang.org/x/tools/internal/jsonrpc2"
1212
"golang.org/x/tools/internal/lsp/protocol"
1313
"golang.org/x/tools/internal/lsp/source"
14+
"golang.org/x/tools/internal/lsp/telemetry/log"
1415
"golang.org/x/tools/internal/semver"
1516
"golang.org/x/tools/internal/span"
1617
"io/ioutil"
@@ -188,11 +189,9 @@ type WorkspaceFolderMeta struct {
188189
// manageDeps will explore the workspace folders sent from the client and give a whole picture of them. Besides that,
189190
// manageDeps will try its best to convert the folders to modules. The core functions, like deps downloading and deps
190191
// management, will be implemented in the package 'cache'.
191-
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+
func (s ElasticServer) ManageDeps(ctx context.Context, folders *[]protocol.WorkspaceFolder) error {
193+
// In order to handle the modules separately, we consider different modules as different workspace folders, so we
194+
// can manage the dependency of different modules separately.
196195
for _, folder := range *folders {
197196
metadata := &WorkspaceFolderMeta{}
198197
if folder.URI != "" {
@@ -215,6 +214,13 @@ func (s ElasticServer) ManageDeps(folders *[]protocol.WorkspaceFolder) error {
215214
}
216215
}
217216
}
217+
for _, folder := range *folders {
218+
cmd := exec.Command("go", "mod", "download")
219+
cmd.Dir = span.NewURI(folder.URI).Filename()
220+
if err := cmd.Run(); err != nil {
221+
log.Error(ctx, "failed to download the dependencies", err)
222+
}
223+
}
218224
return nil
219225
}
220226

internal/lsp/protocol/elasticserver.go

+3-3
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(context.Context, *[]WorkspaceFolder) error
1515
}
1616

1717
type elasticServerHandler struct {
@@ -39,7 +39,7 @@ func (h elasticServerHandler) Deliver(ctx context.Context, r *jsonrpc2.Request,
3939
sendParseError(ctx, r, err)
4040
return true
4141
}
42-
if err := h.server.ManageDeps(&params.Event.Added); err != nil {
42+
if err := h.server.ManageDeps(ctx, &params.Event.Added); err != nil {
4343
log.Error(ctx, "", err)
4444
}
4545
if err := h.server.DidChangeWorkspaceFolders(ctx, &params); err != nil {
@@ -223,7 +223,7 @@ func (h elasticServerHandler) Deliver(ctx context.Context, r *jsonrpc2.Request,
223223
sendParseError(ctx, r, err)
224224
return true
225225
}
226-
if err := h.server.ManageDeps(&params.WorkspaceFolders); err != nil {
226+
if err := h.server.ManageDeps(ctx, &params.WorkspaceFolders); err != nil {
227227
log.Error(ctx, "", err)
228228
}
229229
resp, err := h.server.Initialize(ctx, &params)

0 commit comments

Comments
 (0)