mirror of
https://github.com/golang/go
synced 2024-10-01 03:28:32 -06:00
internal/lsp: refactor to separate pieces of type-checking
Change-Id: Idab49286e59803e4ae0f749eb9f2990b611ea689 Reviewed-on: https://go-review.googlesource.com/c/tools/+/179439 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:
parent
b97706b7f6
commit
12d7342421
152
internal/lsp/cache/check.go
vendored
152
internal/lsp/cache/check.go
vendored
@ -8,7 +8,6 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"go/ast"
|
"go/ast"
|
||||||
"go/parser"
|
|
||||||
"go/scanner"
|
"go/scanner"
|
||||||
"go/token"
|
"go/token"
|
||||||
"go/types"
|
"go/types"
|
||||||
@ -18,157 +17,6 @@ import (
|
|||||||
"golang.org/x/tools/internal/span"
|
"golang.org/x/tools/internal/span"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (v *view) parse(ctx context.Context, f *goFile) ([]packages.Error, error) {
|
|
||||||
v.mcache.mu.Lock()
|
|
||||||
defer v.mcache.mu.Unlock()
|
|
||||||
|
|
||||||
// Apply any queued-up content changes.
|
|
||||||
if err := v.applyContentChanges(ctx); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// If the package for the file has not been invalidated by the application
|
|
||||||
// of the pending changes, there is no need to continue.
|
|
||||||
if f.isPopulated() {
|
|
||||||
return nil, nil
|
|
||||||
}
|
|
||||||
// Check if the file's imports have changed. If they have, update the
|
|
||||||
// metadata by calling packages.Load.
|
|
||||||
if errs, err := v.checkMetadata(ctx, f); err != nil {
|
|
||||||
return errs, err
|
|
||||||
}
|
|
||||||
if f.meta == nil {
|
|
||||||
return nil, fmt.Errorf("no metadata found for %v", f.filename())
|
|
||||||
}
|
|
||||||
imp := &importer{
|
|
||||||
view: v,
|
|
||||||
seen: make(map[string]struct{}),
|
|
||||||
ctx: ctx,
|
|
||||||
fset: f.FileSet(),
|
|
||||||
}
|
|
||||||
// Start prefetching direct imports.
|
|
||||||
for importPath := range f.meta.children {
|
|
||||||
go imp.Import(importPath)
|
|
||||||
}
|
|
||||||
// Type-check package.
|
|
||||||
pkg, err := imp.getPkg(f.meta.pkgPath)
|
|
||||||
if pkg == nil || pkg.GetTypes() == nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// If we still have not found the package for the file, something is wrong.
|
|
||||||
if f.pkg == nil {
|
|
||||||
return nil, fmt.Errorf("parse: no package found for %v", f.filename())
|
|
||||||
}
|
|
||||||
return nil, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (v *view) checkMetadata(ctx context.Context, f *goFile) ([]packages.Error, error) {
|
|
||||||
if v.reparseImports(ctx, f, f.filename()) {
|
|
||||||
cfg := v.buildConfig()
|
|
||||||
pkgs, err := packages.Load(cfg, fmt.Sprintf("file=%s", f.filename()))
|
|
||||||
if len(pkgs) == 0 {
|
|
||||||
if err == nil {
|
|
||||||
err = fmt.Errorf("%s: no packages found", f.filename())
|
|
||||||
}
|
|
||||||
// Return this error as a diagnostic to the user.
|
|
||||||
return []packages.Error{
|
|
||||||
{
|
|
||||||
Msg: err.Error(),
|
|
||||||
Kind: packages.ListError,
|
|
||||||
},
|
|
||||||
}, err
|
|
||||||
}
|
|
||||||
for _, pkg := range pkgs {
|
|
||||||
// If the package comes back with errors from `go list`, don't bother
|
|
||||||
// type-checking it.
|
|
||||||
if len(pkg.Errors) > 0 {
|
|
||||||
return pkg.Errors, fmt.Errorf("package %s has errors, skipping type-checking", pkg.PkgPath)
|
|
||||||
}
|
|
||||||
v.link(ctx, pkg.PkgPath, pkg, nil)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// reparseImports reparses a file's import declarations to determine if they
|
|
||||||
// have changed.
|
|
||||||
func (v *view) reparseImports(ctx context.Context, f *goFile, filename string) bool {
|
|
||||||
if f.meta == nil {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
// Get file content in case we don't already have it.
|
|
||||||
f.read(ctx)
|
|
||||||
if f.fc.Error != nil {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
parsed, _ := parser.ParseFile(f.FileSet(), filename, f.fc.Data, parser.ImportsOnly)
|
|
||||||
if parsed == nil {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
// If the package name has changed, re-run `go list`.
|
|
||||||
if f.meta.name != parsed.Name.Name {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
// If the package's imports have changed, re-run `go list`.
|
|
||||||
if len(f.imports) != len(parsed.Imports) {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
for i, importSpec := range f.imports {
|
|
||||||
if importSpec.Path.Value != f.imports[i].Path.Value {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
func (v *view) link(ctx context.Context, pkgPath string, pkg *packages.Package, parent *metadata) *metadata {
|
|
||||||
m, ok := v.mcache.packages[pkgPath]
|
|
||||||
if !ok {
|
|
||||||
m = &metadata{
|
|
||||||
pkgPath: pkgPath,
|
|
||||||
id: pkg.ID,
|
|
||||||
typesSizes: pkg.TypesSizes,
|
|
||||||
parents: make(map[string]bool),
|
|
||||||
children: make(map[string]bool),
|
|
||||||
}
|
|
||||||
v.mcache.packages[pkgPath] = m
|
|
||||||
}
|
|
||||||
// Reset any field that could have changed across calls to packages.Load.
|
|
||||||
m.name = pkg.Name
|
|
||||||
m.files = pkg.CompiledGoFiles
|
|
||||||
for _, filename := range m.files {
|
|
||||||
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())
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
gof.meta = m
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// Connect the import graph.
|
|
||||||
if parent != nil {
|
|
||||||
m.parents[parent.pkgPath] = true
|
|
||||||
parent.children[pkgPath] = true
|
|
||||||
}
|
|
||||||
for importPath, importPkg := range pkg.Imports {
|
|
||||||
if _, ok := m.children[importPath]; !ok {
|
|
||||||
v.link(ctx, importPath, importPkg, m)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// Clear out any imports that have been removed.
|
|
||||||
for importPath := range m.children {
|
|
||||||
if _, ok := pkg.Imports[importPath]; !ok {
|
|
||||||
delete(m.children, importPath)
|
|
||||||
if child, ok := v.mcache.packages[importPath]; ok {
|
|
||||||
delete(child.parents, pkgPath)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return m
|
|
||||||
}
|
|
||||||
|
|
||||||
type importer struct {
|
type importer struct {
|
||||||
view *view
|
view *view
|
||||||
|
|
||||||
|
5
internal/lsp/cache/file.go
vendored
5
internal/lsp/cache/file.go
vendored
@ -87,8 +87,3 @@ func (f *fileBase) read(ctx context.Context) {
|
|||||||
// We don't know the content yet, so read it.
|
// We don't know the content yet, so read it.
|
||||||
f.fc = f.view.Session().ReadFile(f.URI())
|
f.fc = f.view.Session().ReadFile(f.URI())
|
||||||
}
|
}
|
||||||
|
|
||||||
// isPopulated returns true if all of the computed fields of the file are set.
|
|
||||||
func (f *goFile) isPopulated() bool {
|
|
||||||
return f.ast != nil && f.token != nil && f.pkg != nil && f.meta != nil && f.imports != nil
|
|
||||||
}
|
|
||||||
|
18
internal/lsp/cache/gofile.go
vendored
18
internal/lsp/cache/gofile.go
vendored
@ -22,8 +22,8 @@ type goFile struct {
|
|||||||
func (f *goFile) GetToken(ctx context.Context) *token.File {
|
func (f *goFile) GetToken(ctx context.Context) *token.File {
|
||||||
f.view.mu.Lock()
|
f.view.mu.Lock()
|
||||||
defer f.view.mu.Unlock()
|
defer f.view.mu.Unlock()
|
||||||
if f.token == nil || len(f.view.contentChanges) > 0 {
|
if f.isDirty() {
|
||||||
if _, err := f.view.parse(ctx, f); err != nil {
|
if _, err := f.view.loadParseTypecheck(ctx, f); err != nil {
|
||||||
f.View().Session().Logger().Errorf(ctx, "unable to check package for %s: %v", f.URI(), err)
|
f.View().Session().Logger().Errorf(ctx, "unable to check package for %s: %v", f.URI(), err)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -35,8 +35,8 @@ func (f *goFile) GetAST(ctx context.Context) *ast.File {
|
|||||||
f.view.mu.Lock()
|
f.view.mu.Lock()
|
||||||
defer f.view.mu.Unlock()
|
defer f.view.mu.Unlock()
|
||||||
|
|
||||||
if f.ast == nil || len(f.view.contentChanges) > 0 {
|
if f.isDirty() {
|
||||||
if _, err := f.view.parse(ctx, f); err != nil {
|
if _, err := f.view.loadParseTypecheck(ctx, f); err != nil {
|
||||||
f.View().Session().Logger().Errorf(ctx, "unable to check package for %s: %v", f.URI(), err)
|
f.View().Session().Logger().Errorf(ctx, "unable to check package for %s: %v", f.URI(), err)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -48,8 +48,8 @@ func (f *goFile) GetPackage(ctx context.Context) source.Package {
|
|||||||
f.view.mu.Lock()
|
f.view.mu.Lock()
|
||||||
defer f.view.mu.Unlock()
|
defer f.view.mu.Unlock()
|
||||||
|
|
||||||
if f.pkg == nil || len(f.view.contentChanges) > 0 {
|
if f.isDirty() {
|
||||||
if errs, err := f.view.parse(ctx, f); err != nil {
|
if errs, err := f.view.loadParseTypecheck(ctx, f); err != nil {
|
||||||
f.View().Session().Logger().Errorf(ctx, "unable to check package for %s: %v", f.URI(), err)
|
f.View().Session().Logger().Errorf(ctx, "unable to check package for %s: %v", f.URI(), err)
|
||||||
|
|
||||||
// Create diagnostics for errors if we are able to.
|
// Create diagnostics for errors if we are able to.
|
||||||
@ -62,6 +62,12 @@ func (f *goFile) GetPackage(ctx context.Context) source.Package {
|
|||||||
return f.pkg
|
return f.pkg
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// isDirty is true if the file needs to be type-checked.
|
||||||
|
// It assumes that the file's view's mutex is held by the caller.
|
||||||
|
func (f *goFile) isDirty() bool {
|
||||||
|
return f.meta == nil || f.imports == nil || f.token == nil || f.ast == nil || f.pkg == nil || len(f.view.contentChanges) > 0
|
||||||
|
}
|
||||||
|
|
||||||
func (f *goFile) GetActiveReverseDeps(ctx context.Context) []source.GoFile {
|
func (f *goFile) GetActiveReverseDeps(ctx context.Context) []source.GoFile {
|
||||||
pkg := f.GetPackage(ctx)
|
pkg := f.GetPackage(ctx)
|
||||||
if pkg == nil {
|
if pkg == nil {
|
||||||
|
160
internal/lsp/cache/load.go
vendored
Normal file
160
internal/lsp/cache/load.go
vendored
Normal file
@ -0,0 +1,160 @@
|
|||||||
|
package cache
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"go/parser"
|
||||||
|
|
||||||
|
"golang.org/x/tools/go/packages"
|
||||||
|
"golang.org/x/tools/internal/span"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (v *view) loadParseTypecheck(ctx context.Context, f *goFile) ([]packages.Error, error) {
|
||||||
|
v.mcache.mu.Lock()
|
||||||
|
defer v.mcache.mu.Unlock()
|
||||||
|
|
||||||
|
// Apply any queued-up content changes.
|
||||||
|
if err := v.applyContentChanges(ctx); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// If the package for the file has not been invalidated by the application
|
||||||
|
// of the pending changes, there is no need to continue.
|
||||||
|
if !f.isDirty() {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
// Check if the file's imports have changed. If they have, update the
|
||||||
|
// metadata by calling packages.Load.
|
||||||
|
if errs, err := v.checkMetadata(ctx, f); err != nil {
|
||||||
|
return errs, err
|
||||||
|
}
|
||||||
|
if f.meta == nil {
|
||||||
|
return nil, fmt.Errorf("no metadata found for %v", f.filename())
|
||||||
|
}
|
||||||
|
imp := &importer{
|
||||||
|
view: v,
|
||||||
|
seen: make(map[string]struct{}),
|
||||||
|
ctx: ctx,
|
||||||
|
fset: f.FileSet(),
|
||||||
|
}
|
||||||
|
// Start prefetching direct imports.
|
||||||
|
for importPath := range f.meta.children {
|
||||||
|
go imp.Import(importPath)
|
||||||
|
}
|
||||||
|
// Type-check package.
|
||||||
|
pkg, err := imp.getPkg(f.meta.pkgPath)
|
||||||
|
if pkg == nil || pkg.IsIllTyped() {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
// If we still have not found the package for the file, something is wrong.
|
||||||
|
if f.pkg == nil {
|
||||||
|
return nil, fmt.Errorf("parse: no package found for %v", f.filename())
|
||||||
|
}
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (v *view) checkMetadata(ctx context.Context, f *goFile) ([]packages.Error, error) {
|
||||||
|
if v.reparseImports(ctx, f, f.filename()) {
|
||||||
|
cfg := v.buildConfig()
|
||||||
|
pkgs, err := packages.Load(cfg, fmt.Sprintf("file=%s", f.filename()))
|
||||||
|
if len(pkgs) == 0 {
|
||||||
|
if err == nil {
|
||||||
|
err = fmt.Errorf("%s: no packages found", f.filename())
|
||||||
|
}
|
||||||
|
// Return this error as a diagnostic to the user.
|
||||||
|
return []packages.Error{
|
||||||
|
{
|
||||||
|
Msg: err.Error(),
|
||||||
|
Kind: packages.ListError,
|
||||||
|
},
|
||||||
|
}, err
|
||||||
|
}
|
||||||
|
for _, pkg := range pkgs {
|
||||||
|
// If the package comes back with errors from `go list`, don't bother
|
||||||
|
// type-checking it.
|
||||||
|
if len(pkg.Errors) > 0 {
|
||||||
|
return pkg.Errors, fmt.Errorf("package %s has errors, skipping type-checking", pkg.PkgPath)
|
||||||
|
}
|
||||||
|
v.link(ctx, pkg.PkgPath, pkg, nil)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// reparseImports reparses a file's import declarations to determine if they
|
||||||
|
// have changed.
|
||||||
|
func (v *view) reparseImports(ctx context.Context, f *goFile, filename string) bool {
|
||||||
|
if f.meta == nil {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
// Get file content in case we don't already have it.
|
||||||
|
f.read(ctx)
|
||||||
|
if f.fc.Error != nil {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
parsed, _ := parser.ParseFile(f.FileSet(), filename, f.fc.Data, parser.ImportsOnly)
|
||||||
|
if parsed == nil {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
// If the package name has changed, re-run `go list`.
|
||||||
|
if f.meta.name != parsed.Name.Name {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
// If the package's imports have changed, re-run `go list`.
|
||||||
|
if len(f.imports) != len(parsed.Imports) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
for i, importSpec := range f.imports {
|
||||||
|
if importSpec.Path.Value != f.imports[i].Path.Value {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func (v *view) link(ctx context.Context, pkgPath string, pkg *packages.Package, parent *metadata) *metadata {
|
||||||
|
m, ok := v.mcache.packages[pkgPath]
|
||||||
|
if !ok {
|
||||||
|
m = &metadata{
|
||||||
|
pkgPath: pkgPath,
|
||||||
|
id: pkg.ID,
|
||||||
|
typesSizes: pkg.TypesSizes,
|
||||||
|
parents: make(map[string]bool),
|
||||||
|
children: make(map[string]bool),
|
||||||
|
}
|
||||||
|
v.mcache.packages[pkgPath] = m
|
||||||
|
}
|
||||||
|
// Reset any field that could have changed across calls to packages.Load.
|
||||||
|
m.name = pkg.Name
|
||||||
|
m.files = pkg.CompiledGoFiles
|
||||||
|
for _, filename := range m.files {
|
||||||
|
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())
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
gof.meta = m
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Connect the import graph.
|
||||||
|
if parent != nil {
|
||||||
|
m.parents[parent.pkgPath] = true
|
||||||
|
parent.children[pkgPath] = true
|
||||||
|
}
|
||||||
|
for importPath, importPkg := range pkg.Imports {
|
||||||
|
if _, ok := m.children[importPath]; !ok {
|
||||||
|
v.link(ctx, importPath, importPkg, m)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Clear out any imports that have been removed.
|
||||||
|
for importPath := range m.children {
|
||||||
|
if _, ok := pkg.Imports[importPath]; !ok {
|
||||||
|
delete(m.children, importPath)
|
||||||
|
if child, ok := v.mcache.packages[importPath]; ok {
|
||||||
|
delete(child.parents, pkgPath)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return m
|
||||||
|
}
|
8
internal/lsp/cache/pkg.go
vendored
8
internal/lsp/cache/pkg.go
vendored
@ -161,11 +161,5 @@ func (pkg *pkg) IsIllTyped() bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (pkg *pkg) GetImport(pkgPath string) source.Package {
|
func (pkg *pkg) GetImport(pkgPath string) source.Package {
|
||||||
imported := pkg.imports[pkgPath]
|
return pkg.imports[pkgPath]
|
||||||
// Be careful not to return a nil pointer because that still satisfies the
|
|
||||||
// interface.
|
|
||||||
if imported != nil {
|
|
||||||
return imported
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
@ -56,7 +56,7 @@ func Diagnostics(ctx context.Context, v View, f GoFile) (map[span.URI][]Diagnost
|
|||||||
if pkg == nil {
|
if pkg == nil {
|
||||||
return singleDiagnostic(f.URI(), "%s is not part of a package", f.URI()), nil
|
return singleDiagnostic(f.URI(), "%s is not part of a package", f.URI()), nil
|
||||||
}
|
}
|
||||||
// Prepare the reports we will send for 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)
|
uri := span.FileURI(filename)
|
||||||
@ -66,7 +66,7 @@ func Diagnostics(ctx context.Context, v View, f GoFile) (map[span.URI][]Diagnost
|
|||||||
reports[uri] = []Diagnostic{}
|
reports[uri] = []Diagnostic{}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Prepare reports for package errors
|
// 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{}
|
reports[packageErrorSpan(pkgErr).URI()] = []Diagnostic{}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user