1
0
mirror of https://github.com/golang/go synced 2024-10-01 03:38:32 -06:00

internal/lsp: initialize the view on creation

This change moves the initialization of the view into the view's
creation, instead of forcing the tests to call WorkspacePackageIDs to
initialize.

Change-Id: Iccea820ec268b1851d58821481d92c7a3d4772c3
Reviewed-on: https://go-review.googlesource.com/c/tools/+/214279
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Heschi Kreinick <heschi@google.com>
This commit is contained in:
Rebecca Stambler 2020-01-10 01:45:57 -05:00
parent 3e6f5d44f4
commit a7a6caa82a
4 changed files with 16 additions and 18 deletions

View File

@ -115,7 +115,13 @@ func (s *session) createView(ctx context.Context, name string, folder span.URI,
// Preemptively build the builtin package,
// so we immediately add builtin.go to the list of ignored files.
v.buildBuiltinPackage(ctx)
// TODO(rstambler): This could be part of the initialization process.
if err := v.buildBuiltinPackage(ctx); err != nil {
return nil, nil, err
}
// Initialize the view without blocking.
go v.initialize(xcontext.Detach(ctx), v.snapshot)
debug.AddView(debugView{v})
return v, v.snapshot, nil

View File

@ -470,11 +470,18 @@ func (v *view) getSnapshot() *snapshot {
func (v *view) WorkspacePackageIDs(ctx context.Context) ([]string, error) {
s := v.getSnapshot()
if err := s.awaitInitialized(ctx); err != nil {
return nil, err
}
return s.workspacePackageIDs(), nil
}
func (v *view) initialize(ctx context.Context, s *snapshot) {
v.initializeOnce.Do(func() {
defer close(v.initialized)
// Do not cancel the call to go/packages.Load for the entire workspace.
meta, err := s.load(xcontext.Detach(ctx), directoryURI(v.folder))
meta, err := s.load(ctx, directoryURI(v.folder))
if err != nil {
v.initializationError = err
}
@ -484,10 +491,6 @@ func (v *view) WorkspacePackageIDs(ctx context.Context) ([]string, error) {
s.setWorkspacePackage(m.id, m.pkgPath)
}
})
if v.initializationError != nil {
return nil, v.initializationError
}
return s.workspacePackageIDs(), nil
}
func (s *snapshot) awaitInitialized(ctx context.Context) error {

View File

@ -53,13 +53,7 @@ func testLSP(t *testing.T, exporter packagestest.Exporter) {
options := tests.DefaultOptions()
session.SetOptions(options)
options.Env = data.Config.Env
view, _, err := session.NewView(ctx, viewName, span.FileURI(data.Config.Dir), options)
if err != nil {
t.Fatal(err)
}
// Load the workspace packages, since this would normally happen when a view is initialized.
// Otherwise, tests that need to look at all workspace packages will fail.
if _, err := view.WorkspacePackageIDs(ctx); err != nil {
if _, _, err := session.NewView(ctx, viewName, span.FileURI(data.Config.Dir), options); err != nil {
t.Fatal(err)
}
for filename, content := range data.Config.Overlay {

View File

@ -60,11 +60,6 @@ func testSource(t *testing.T, exporter packagestest.Exporter) {
data: data,
ctx: ctx,
}
// Load the workspace packages, since this would normally happen when a view is initialized.
// Otherwise, tests that need to look at all workspace packages will fail.
if _, err := view.WorkspacePackageIDs(ctx); err != nil {
t.Fatal(err)
}
for filename, content := range data.Config.Overlay {
kind := source.DetectLanguage("", filename)
if kind != source.Go {