internal/lsp: build the builtin package preemptively

This change fixes a regression introduced by the building the builtin
package on demand. Although this change increases the startup tasks of
gopls, it is necessary to ensure that we ignore diagnostics from
builtin.go.

Change-Id: I897e747a273056d70cecba486a74c75a736d8f80
Reviewed-on: https://go-review.googlesource.com/c/tools/+/179921
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-06-01 00:08:57 -04:00
parent 359a0754b7
commit 178e83bc9d
3 changed files with 22 additions and 13 deletions

View File

@ -78,6 +78,10 @@ func (s *session) NewView(name string, folder span.URI) source.View {
}, },
ignoredURIs: make(map[span.URI]struct{}), ignoredURIs: make(map[span.URI]struct{}),
} }
// Preemptively build the builtin package,
// so we immediately add builtin.go to the list of ignored files.
v.buildBuiltinPkg()
s.views = append(s.views, v) s.views = append(s.views, v)
// we always need to drop the view map // we always need to drop the view map
s.viewMap = make(map[span.URI]source.View) s.viewMap = make(map[span.URI]source.View)

View File

@ -177,15 +177,13 @@ func (v *view) BackgroundContext() context.Context {
} }
func (v *view) BuiltinPackage() *ast.Package { func (v *view) BuiltinPackage() *ast.Package {
if v.builtinPkg == nil {
v.buildBuiltinPkg()
}
return v.builtinPkg return v.builtinPkg
} }
// buildBuiltinPkg builds the view's builtin package.
// It assumes that the view is not active yet,
// i.e. it has not been added to the session's list of views.
func (v *view) buildBuiltinPkg() { func (v *view) buildBuiltinPkg() {
v.mu.Lock()
defer v.mu.Unlock()
cfg := *v.buildConfig() cfg := *v.buildConfig()
pkgs, _ := packages.Load(&cfg, "builtin") pkgs, _ := packages.Load(&cfg, "builtin")
if len(pkgs) != 1 { if len(pkgs) != 1 {

View File

@ -59,16 +59,12 @@ func Diagnostics(ctx context.Context, v View, f GoFile) (map[span.URI][]Diagnost
// Prepare the reports we will send for the files in this package. // Prepare the reports we will send for the files in this package.
reports := make(map[span.URI][]Diagnostic) reports := make(map[span.URI][]Diagnostic)
for _, filename := range pkg.GetFilenames() { for _, filename := range pkg.GetFilenames() {
uri := span.FileURI(filename) addReport(v, reports, span.FileURI(filename), nil)
if v.Ignore(uri) {
continue
}
reports[uri] = []Diagnostic{}
} }
// Prepare any additional reports for the errors in this package. // Prepare any additional reports for the errors in this package.
for _, pkgErr := range pkg.GetErrors() { for _, pkgErr := range pkg.GetErrors() {
reports[packageErrorSpan(pkgErr).URI()] = []Diagnostic{} addReport(v, reports, packageErrorSpan(pkgErr).URI(), nil)
} }
// Run diagnostics for the package that this URI belongs to. // Run diagnostics for the package that this URI belongs to.
@ -85,7 +81,7 @@ func Diagnostics(ctx context.Context, v View, f GoFile) (map[span.URI][]Diagnost
continue continue
} }
for _, filename := range pkg.GetFilenames() { for _, filename := range pkg.GetFilenames() {
reports[span.FileURI(filename)] = []Diagnostic{} addReport(v, reports, span.FileURI(filename), nil)
} }
diagnostics(ctx, v, pkg, reports) diagnostics(ctx, v, pkg, reports)
} }
@ -143,7 +139,7 @@ func analyses(ctx context.Context, v View, pkg Package, reports map[span.URI][]D
if diag.Category != "" { if diag.Category != "" {
category += "." + category category += "." + category
} }
reports[s.URI()] = append(reports[s.URI()], Diagnostic{ addReport(v, reports, s.URI(), &Diagnostic{
Source: category, Source: category,
Span: s, Span: s,
Message: diag.Message, Message: diag.Message,
@ -156,6 +152,17 @@ func analyses(ctx context.Context, v View, pkg Package, reports map[span.URI][]D
return nil return nil
} }
func addReport(v View, reports map[span.URI][]Diagnostic, uri span.URI, diagnostic *Diagnostic) {
if v.Ignore(uri) {
return
}
if diagnostic == nil {
reports[uri] = []Diagnostic{}
} else {
reports[uri] = append(reports[uri], *diagnostic)
}
}
// parseDiagnosticMessage attempts to parse a standard error message by stripping off the trailing error message. // parseDiagnosticMessage attempts to parse a standard error message by stripping off the trailing error message.
// Works only on errors where the message is prefixed by ": ". // Works only on errors where the message is prefixed by ": ".
// e.g.: // e.g.: