From 3c1b287bbdbbf510eec19c7bd3e927bb27899e1d Mon Sep 17 00:00:00 2001 From: Rebecca Stambler Date: Wed, 10 Jun 2020 16:13:25 -0400 Subject: [PATCH] internal/lsp: await the initial workspace load in ModHandle ModHandle races with the initial workspace load if the go.mod file does not yet exist. We should await for the initial workspace load to complete before proceeding with update codelenses, etc. Part of trying to figure out the flakes in golang/go#39504. Also a few staticcheck fixes, and fix the Windows line endings in fill_struct.go, because `git gofmt` complains. Change-Id: Ide21a47137390792d1afb924740cff0bb6f0b764 Reviewed-on: https://go-review.googlesource.com/c/tools/+/237419 Run-TryBot: Rebecca Stambler TryBot-Result: Gobot Gobot Reviewed-by: Heschi Kreinick --- internal/lsp/cache/mod.go | 10 ++++++---- internal/lsp/link.go | 6 +++++- internal/lsp/mod/code_lens.go | 6 +++++- internal/lsp/mod/format.go | 6 +++++- internal/lsp/mod/hover.go | 6 +++++- internal/lsp/source/fill_struct.go | 1 - internal/lsp/source/view.go | 2 +- 7 files changed, 27 insertions(+), 10 deletions(-) diff --git a/internal/lsp/cache/mod.go b/internal/lsp/cache/mod.go index 1fb4413158..abbd98b3ca 100644 --- a/internal/lsp/cache/mod.go +++ b/internal/lsp/cache/mod.go @@ -128,12 +128,14 @@ func (mh *modHandle) Why(ctx context.Context) (*modfile.File, *protocol.ColumnMa return data.origParsedFile, data.origMapper, data.why, data.err } -func (s *snapshot) ModHandle(ctx context.Context, fh source.FileHandle) source.ModHandle { +func (s *snapshot) ModHandle(ctx context.Context, fh source.FileHandle) (source.ModHandle, error) { + if err := s.awaitLoaded(ctx); err != nil { + return nil, err + } uri := fh.URI() if handle := s.getModHandle(uri); handle != nil { - return handle + return handle, nil } - realURI, tempURI := s.view.ModFiles() folder := s.View().Folder().Filename() cfg := s.Config(ctx) @@ -202,7 +204,7 @@ func (s *snapshot) ModHandle(ctx context.Context, fh source.FileHandle) source.M file: fh, cfg: cfg, } - return s.modHandles[uri] + return s.modHandles[uri], nil } func goModWhy(ctx context.Context, cfg *packages.Config, folder string, data *modData) error { diff --git a/internal/lsp/link.go b/internal/lsp/link.go index 73865bdb6a..ea09d76da8 100644 --- a/internal/lsp/link.go +++ b/internal/lsp/link.go @@ -46,7 +46,11 @@ func (s *Server) documentLink(ctx context.Context, params *protocol.DocumentLink func modLinks(ctx context.Context, snapshot source.Snapshot, fh source.FileHandle) ([]protocol.DocumentLink, error) { view := snapshot.View() - file, m, err := snapshot.ModHandle(ctx, fh).Parse(ctx) + mh, err := snapshot.ModHandle(ctx, fh) + if err != nil { + return nil, err + } + file, m, err := mh.Parse(ctx) if err != nil { return nil, err } diff --git a/internal/lsp/mod/code_lens.go b/internal/lsp/mod/code_lens.go index 8f117dccff..9948882064 100644 --- a/internal/lsp/mod/code_lens.go +++ b/internal/lsp/mod/code_lens.go @@ -33,7 +33,11 @@ func CodeLens(ctx context.Context, snapshot source.Snapshot, uri span.URI) ([]pr if err != nil { return nil, err } - f, m, upgrades, err := snapshot.ModHandle(ctx, fh).Upgrades(ctx) + mh, err := snapshot.ModHandle(ctx, fh) + if err != nil { + return nil, err + } + f, m, upgrades, err := mh.Upgrades(ctx) if err != nil { return nil, err } diff --git a/internal/lsp/mod/format.go b/internal/lsp/mod/format.go index a635cb8318..4aaf8aef06 100644 --- a/internal/lsp/mod/format.go +++ b/internal/lsp/mod/format.go @@ -12,7 +12,11 @@ func Format(ctx context.Context, snapshot source.Snapshot, fh source.FileHandle) ctx, done := event.Start(ctx, "mod.Format") defer done() - file, m, err := snapshot.ModHandle(ctx, fh).Parse(ctx) + mh, err := snapshot.ModHandle(ctx, fh) + if err != nil { + return nil, err + } + file, m, err := mh.Parse(ctx) if err != nil { return nil, err } diff --git a/internal/lsp/mod/hover.go b/internal/lsp/mod/hover.go index 97d3dd3808..f437c7c758 100644 --- a/internal/lsp/mod/hover.go +++ b/internal/lsp/mod/hover.go @@ -23,7 +23,11 @@ func Hover(ctx context.Context, snapshot source.Snapshot, fh source.FileHandle, ctx, done := event.Start(ctx, "mod.Hover") defer done() - file, m, why, err := snapshot.ModHandle(ctx, fh).Why(ctx) + mh, err := snapshot.ModHandle(ctx, fh) + if err != nil { + return nil, err + } + file, m, why, err := mh.Why(ctx) if err != nil { return nil, err } diff --git a/internal/lsp/source/fill_struct.go b/internal/lsp/source/fill_struct.go index e44c53b376..f7fa00108f 100644 --- a/internal/lsp/source/fill_struct.go +++ b/internal/lsp/source/fill_struct.go @@ -17,7 +17,6 @@ import ( // FillStruct completes all of targeted struct's fields with their default values. func FillStruct(ctx context.Context, snapshot Snapshot, fh FileHandle, protoRng protocol.Range) ([]protocol.CodeAction, error) { - pkg, pgh, err := getParsedFile(ctx, snapshot, fh, NarrowestPackageHandle) if err != nil { return nil, fmt.Errorf("getting file for struct fill code action: %v", err) diff --git a/internal/lsp/source/view.go b/internal/lsp/source/view.go index d60e37c377..f1649e5070 100644 --- a/internal/lsp/source/view.go +++ b/internal/lsp/source/view.go @@ -54,7 +54,7 @@ type Snapshot interface { // ModHandle returns a ModHandle for the passed in go.mod file handle. // This function can have no data if there is no modfile detected. - ModHandle(ctx context.Context, fh FileHandle) ModHandle + ModHandle(ctx context.Context, fh FileHandle) (ModHandle, error) // PackageHandles returns the PackageHandles for the packages that this file // belongs to.