1
0
mirror of https://github.com/golang/go synced 2024-11-18 16:54:43 -07:00

internal/lsp: fix setting overlays in tests

This change fixes the test failure that has appeared in a few TryBot
runs.

Change-Id: If583555250d63b7f446ec7d8eb09810b842633ae
Reviewed-on: https://go-review.googlesource.com/c/tools/+/179437
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Cottrell <iancottrell@google.com>
This commit is contained in:
Rebecca Stambler 2019-05-29 14:37:11 -04:00
parent f98590f1bf
commit d850aa06e8
5 changed files with 22 additions and 13 deletions

View File

@ -141,7 +141,7 @@ func (v *view) link(ctx context.Context, pkgPath string, pkg *packages.Package,
if f, _ := v.getFile(span.FileURI(filename)); f != nil {
gof, ok := f.(*goFile)
if !ok {
v.Session().Logger().Errorf(ctx, "not a go file: %v", f.URI())
v.Session().Logger().Errorf(ctx, "not a Go file: %v", f.URI())
continue
}
gof.meta = m

View File

@ -54,6 +54,7 @@ func (f *fileBase) View() source.View {
func (f *fileBase) Content(ctx context.Context) *source.FileContent {
f.view.mu.Lock()
defer f.view.mu.Unlock()
f.read(ctx)
return f.fc
}

View File

@ -94,12 +94,11 @@ func (s *session) ViewOf(uri span.URI) source.View {
s.viewMu.Lock()
defer s.viewMu.Unlock()
// check if we already know this file
// Check if we already know this file.
if v, found := s.viewMap[uri]; found {
return v
}
// pick the best view for this file and memoize the result
// Pick the best view for this file and memoize the result.
v := s.bestView(uri)
s.viewMap[uri] = v
return v
@ -131,7 +130,7 @@ func (s *session) bestView(uri span.URI) source.View {
if longest != nil {
return longest
}
//TODO: are there any more heuristics we can use?
// TODO: are there any more heuristics we can use?
return s.views[0]
}
@ -175,13 +174,10 @@ func (s *session) IsOpen(uri span.URI) bool {
}
func (s *session) ReadFile(uri span.URI) *source.FileContent {
s.overlayMu.Lock()
defer s.overlayMu.Unlock()
// We might have the content saved in an overlay.
if overlay, ok := s.overlays[uri]; ok {
if overlay := s.readOverlay(uri); overlay != nil {
return overlay
}
// fall back to the cache level file system
// Fall back to the cache-level file system.
return s.Cache().ReadFile(uri)
}
@ -191,6 +187,7 @@ func (s *session) SetOverlay(uri span.URI, data []byte) {
s.overlayMu.Unlock()
s.filesWatchMap.Notify(uri)
}()
if data == nil {
delete(s.overlays, uri)
return
@ -202,9 +199,21 @@ func (s *session) SetOverlay(uri span.URI, data []byte) {
}
}
func (s *session) readOverlay(uri span.URI) *source.FileContent {
s.overlayMu.Lock()
defer s.overlayMu.Unlock()
// We might have the content saved in an overlay.
if overlay, ok := s.overlays[uri]; ok {
return overlay
}
return nil
}
func (s *session) buildOverlay() map[string][]byte {
s.overlayMu.Lock()
defer s.overlayMu.Unlock()
overlays := make(map[string][]byte)
for uri, overlay := range s.overlays {
if overlay.Error != nil {

View File

@ -36,7 +36,6 @@ type runner struct {
const viewName = "lsp_test"
func testLSP(t *testing.T, exporter packagestest.Exporter) {
ctx := context.Background()
data := tests.Load(t, exporter, "testdata")
defer data.Exported.Cleanup()
@ -46,7 +45,7 @@ func testLSP(t *testing.T, exporter packagestest.Exporter) {
view := session.NewView(viewName, span.FileURI(data.Config.Dir))
view.SetEnv(data.Config.Env)
for filename, content := range data.Config.Overlay {
view.SetContent(ctx, span.FileURI(filename), content)
session.SetOverlay(span.FileURI(filename), content)
}
r := &runner{
server: &Server{

View File

@ -71,7 +71,7 @@ func getGoFile(ctx context.Context, v source.View, uri span.URI) (source.GoFile,
}
gof, ok := f.(source.GoFile)
if !ok {
return nil, nil, fmt.Errorf("not a go file %v", f.URI())
return nil, nil, fmt.Errorf("not a Go file %v", f.URI())
}
return gof, m, nil
}