internal/lsp: add correct handling for circular imports
This change brings back handling for circular imports, which was removed because I originally thought that go/packages would handle that. However, since we are type-checking from source, we still end up having to deal with that. Additionally, we propagate the errors of type-checking to the diagnostics so that the user can actually see some of the problems. Change-Id: I0139bcaae461f1bcaf95706532bc5026f2430101 Reviewed-on: https://go-review.googlesource.com/c/tools/+/166882 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
8b67d361bb
commit
8110780cfa
|
@ -19,51 +19,55 @@ import (
|
||||||
"golang.org/x/tools/internal/span"
|
"golang.org/x/tools/internal/span"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (v *View) parse(ctx context.Context, uri span.URI) error {
|
func (v *View) parse(ctx context.Context, uri span.URI) ([]packages.Error, error) {
|
||||||
v.mcache.mu.Lock()
|
v.mcache.mu.Lock()
|
||||||
defer v.mcache.mu.Unlock()
|
defer v.mcache.mu.Unlock()
|
||||||
|
|
||||||
// Apply any queued-up content changes.
|
// Apply any queued-up content changes.
|
||||||
if err := v.applyContentChanges(ctx); err != nil {
|
if err := v.applyContentChanges(ctx); err != nil {
|
||||||
return err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
f := v.files[uri]
|
f := v.files[uri]
|
||||||
|
|
||||||
// This should never happen.
|
// This should never happen.
|
||||||
if f == nil {
|
if f == nil {
|
||||||
return fmt.Errorf("no file for %v", uri)
|
return nil, fmt.Errorf("no file for %v", uri)
|
||||||
}
|
}
|
||||||
// If the package for the file has not been invalidated by the application
|
// If the package for the file has not been invalidated by the application
|
||||||
// of the pending changes, there is no need to continue.
|
// of the pending changes, there is no need to continue.
|
||||||
if f.isPopulated() {
|
if f.isPopulated() {
|
||||||
return nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
// Check if the file's imports have changed. If they have, update the
|
// Check if the file's imports have changed. If they have, update the
|
||||||
// metadata by calling packages.Load.
|
// metadata by calling packages.Load.
|
||||||
if err := v.checkMetadata(ctx, f); err != nil {
|
if errs, err := v.checkMetadata(ctx, f); err != nil {
|
||||||
return err
|
return errs, err
|
||||||
}
|
}
|
||||||
if f.meta == nil {
|
if f.meta == nil {
|
||||||
return fmt.Errorf("no metadata found for %v", uri)
|
return nil, fmt.Errorf("no metadata found for %v", uri)
|
||||||
|
}
|
||||||
|
imp := &importer{
|
||||||
|
view: v,
|
||||||
|
circular: make(map[string]struct{}),
|
||||||
}
|
}
|
||||||
// Start prefetching direct imports.
|
// Start prefetching direct imports.
|
||||||
for importPath := range f.meta.children {
|
for importPath := range f.meta.children {
|
||||||
go v.Import(importPath)
|
go imp.Import(importPath)
|
||||||
}
|
}
|
||||||
// Type-check package.
|
// Type-check package.
|
||||||
pkg, err := v.typeCheck(f.meta.pkgPath)
|
pkg, err := imp.typeCheck(f.meta.pkgPath)
|
||||||
if pkg == nil || pkg.GetTypes() == nil {
|
if pkg == nil || pkg.GetTypes() == nil {
|
||||||
return err
|
return nil, err
|
||||||
}
|
}
|
||||||
// Add every file in this package to our cache.
|
// Add every file in this package to our cache.
|
||||||
v.cachePackage(pkg)
|
v.cachePackage(pkg)
|
||||||
|
|
||||||
// If we still have not found the package for the file, something is wrong.
|
// If we still have not found the package for the file, something is wrong.
|
||||||
if f.pkg == nil {
|
if f.pkg == nil {
|
||||||
return fmt.Errorf("no package found for %v", uri)
|
return nil, fmt.Errorf("no package found for %v", uri)
|
||||||
}
|
}
|
||||||
return nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v *View) cachePackage(pkg *Package) {
|
func (v *View) cachePackage(pkg *Package) {
|
||||||
|
@ -87,10 +91,10 @@ func (v *View) cachePackage(pkg *Package) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v *View) checkMetadata(ctx context.Context, f *File) error {
|
func (v *View) checkMetadata(ctx context.Context, f *File) ([]packages.Error, error) {
|
||||||
filename, err := f.uri.Filename()
|
filename, err := f.uri.Filename()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return nil, err
|
||||||
}
|
}
|
||||||
if v.reparseImports(ctx, f, filename) {
|
if v.reparseImports(ctx, f, filename) {
|
||||||
cfg := v.Config
|
cfg := v.Config
|
||||||
|
@ -100,21 +104,18 @@ func (v *View) checkMetadata(ctx context.Context, f *File) error {
|
||||||
if err == nil {
|
if err == nil {
|
||||||
err = fmt.Errorf("no packages found for %s", filename)
|
err = fmt.Errorf("no packages found for %s", filename)
|
||||||
}
|
}
|
||||||
return err
|
return nil, err
|
||||||
}
|
}
|
||||||
for _, pkg := range pkgs {
|
for _, pkg := range pkgs {
|
||||||
// If the package comes back with errors from `go list`, don't bother
|
// If the package comes back with errors from `go list`, don't bother
|
||||||
// type-checking it.
|
// type-checking it.
|
||||||
for _, err := range pkg.Errors {
|
if len(pkg.Errors) > 0 {
|
||||||
switch err.Kind {
|
return pkg.Errors, fmt.Errorf("package %s has errors, skipping type-checking", pkg.PkgPath)
|
||||||
case packages.UnknownError, packages.ListError:
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
v.link(pkg.PkgPath, pkg, nil)
|
v.link(pkg.PkgPath, pkg, nil)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// reparseImports reparses a file's import declarations to determine if they
|
// reparseImports reparses a file's import declarations to determine if they
|
||||||
|
@ -181,23 +182,34 @@ func (v *View) link(pkgPath string, pkg *packages.Package, parent *metadata) *me
|
||||||
return m
|
return m
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v *View) Import(pkgPath string) (*types.Package, error) {
|
type importer struct {
|
||||||
v.pcache.mu.Lock()
|
view *View
|
||||||
e, ok := v.pcache.packages[pkgPath]
|
|
||||||
|
// circular maintains the set of previously imported packages.
|
||||||
|
// If we have seen a package that is already in this map, we have a circular import.
|
||||||
|
circular map[string]struct{}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (imp *importer) Import(pkgPath string) (*types.Package, error) {
|
||||||
|
if _, ok := imp.circular[pkgPath]; ok {
|
||||||
|
return nil, fmt.Errorf("circular import detected")
|
||||||
|
}
|
||||||
|
imp.view.pcache.mu.Lock()
|
||||||
|
e, ok := imp.view.pcache.packages[pkgPath]
|
||||||
if ok {
|
if ok {
|
||||||
// cache hit
|
// cache hit
|
||||||
v.pcache.mu.Unlock()
|
imp.view.pcache.mu.Unlock()
|
||||||
// wait for entry to become ready
|
// wait for entry to become ready
|
||||||
<-e.ready
|
<-e.ready
|
||||||
} else {
|
} else {
|
||||||
// cache miss
|
// cache miss
|
||||||
e = &entry{ready: make(chan struct{})}
|
e = &entry{ready: make(chan struct{})}
|
||||||
v.pcache.packages[pkgPath] = e
|
imp.view.pcache.packages[pkgPath] = e
|
||||||
v.pcache.mu.Unlock()
|
imp.view.pcache.mu.Unlock()
|
||||||
|
|
||||||
// This goroutine becomes responsible for populating
|
// This goroutine becomes responsible for populating
|
||||||
// the entry and broadcasting its readiness.
|
// the entry and broadcasting its readiness.
|
||||||
e.pkg, e.err = v.typeCheck(pkgPath)
|
e.pkg, e.err = imp.typeCheck(pkgPath)
|
||||||
close(e.ready)
|
close(e.ready)
|
||||||
}
|
}
|
||||||
if e.err != nil {
|
if e.err != nil {
|
||||||
|
@ -206,8 +218,8 @@ func (v *View) Import(pkgPath string) (*types.Package, error) {
|
||||||
return e.pkg.types, nil
|
return e.pkg.types, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v *View) typeCheck(pkgPath string) (*Package, error) {
|
func (imp *importer) typeCheck(pkgPath string) (*Package, error) {
|
||||||
meta, ok := v.mcache.packages[pkgPath]
|
meta, ok := imp.view.mcache.packages[pkgPath]
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("no metadata for %v", pkgPath)
|
return nil, fmt.Errorf("no metadata for %v", pkgPath)
|
||||||
}
|
}
|
||||||
|
@ -235,28 +247,36 @@ func (v *View) typeCheck(pkgPath string) (*Package, error) {
|
||||||
analyses: make(map[*analysis.Analyzer]*analysisEntry),
|
analyses: make(map[*analysis.Analyzer]*analysisEntry),
|
||||||
}
|
}
|
||||||
appendError := func(err error) {
|
appendError := func(err error) {
|
||||||
v.appendPkgError(pkg, err)
|
imp.view.appendPkgError(pkg, err)
|
||||||
}
|
}
|
||||||
files, errs := v.parseFiles(meta.files)
|
files, errs := imp.view.parseFiles(meta.files)
|
||||||
for _, err := range errs {
|
for _, err := range errs {
|
||||||
appendError(err)
|
appendError(err)
|
||||||
}
|
}
|
||||||
pkg.syntax = files
|
pkg.syntax = files
|
||||||
|
|
||||||
|
// Handle circular imports by copying previously seen imports.
|
||||||
|
newCircular := copySet(imp.circular)
|
||||||
|
newCircular[pkgPath] = struct{}{}
|
||||||
|
|
||||||
cfg := &types.Config{
|
cfg := &types.Config{
|
||||||
Error: appendError,
|
Error: appendError,
|
||||||
Importer: v,
|
Importer: &importer{
|
||||||
|
view: imp.view,
|
||||||
|
circular: newCircular,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
check := types.NewChecker(cfg, v.Config.Fset, pkg.types, pkg.typesInfo)
|
check := types.NewChecker(cfg, imp.view.Config.Fset, pkg.types, pkg.typesInfo)
|
||||||
check.Files(pkg.syntax)
|
check.Files(pkg.syntax)
|
||||||
|
|
||||||
// Set imports of package to correspond to cached packages.
|
// Set imports of package to correspond to cached packages.
|
||||||
// We lock the package cache, but we shouldn't get any inconsistencies
|
// We lock the package cache, but we shouldn't get any inconsistencies
|
||||||
// because we are still holding the lock on the view.
|
// because we are still holding the lock on the view.
|
||||||
v.pcache.mu.Lock()
|
imp.view.pcache.mu.Lock()
|
||||||
defer v.pcache.mu.Unlock()
|
defer imp.view.pcache.mu.Unlock()
|
||||||
|
|
||||||
for importPath := range meta.children {
|
for importPath := range meta.children {
|
||||||
if importEntry, ok := v.pcache.packages[importPath]; ok {
|
if importEntry, ok := imp.view.pcache.packages[importPath]; ok {
|
||||||
pkg.imports[importPath] = importEntry.pkg
|
pkg.imports[importPath] = importEntry.pkg
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -264,6 +284,14 @@ func (v *View) typeCheck(pkgPath string) (*Package, error) {
|
||||||
return pkg, nil
|
return pkg, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func copySet(m map[string]struct{}) map[string]struct{} {
|
||||||
|
result := make(map[string]struct{})
|
||||||
|
for k, v := range m {
|
||||||
|
result[k] = v
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
func (v *View) appendPkgError(pkg *Package, err error) {
|
func (v *View) appendPkgError(pkg *Package, err error) {
|
||||||
if err == nil {
|
if err == nil {
|
||||||
return
|
return
|
||||||
|
|
|
@ -52,7 +52,7 @@ func (f *File) GetToken(ctx context.Context) *token.File {
|
||||||
defer f.view.mu.Unlock()
|
defer f.view.mu.Unlock()
|
||||||
|
|
||||||
if f.token == nil || len(f.view.contentChanges) > 0 {
|
if f.token == nil || len(f.view.contentChanges) > 0 {
|
||||||
if err := f.view.parse(ctx, f.uri); err != nil {
|
if _, err := f.view.parse(ctx, f.uri); err != nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -64,7 +64,7 @@ func (f *File) GetAST(ctx context.Context) *ast.File {
|
||||||
defer f.view.mu.Unlock()
|
defer f.view.mu.Unlock()
|
||||||
|
|
||||||
if f.ast == nil || len(f.view.contentChanges) > 0 {
|
if f.ast == nil || len(f.view.contentChanges) > 0 {
|
||||||
if err := f.view.parse(ctx, f.uri); err != nil {
|
if _, err := f.view.parse(ctx, f.uri); err != nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -76,7 +76,12 @@ func (f *File) GetPackage(ctx context.Context) source.Package {
|
||||||
defer f.view.mu.Unlock()
|
defer f.view.mu.Unlock()
|
||||||
|
|
||||||
if f.pkg == nil || len(f.view.contentChanges) > 0 {
|
if f.pkg == nil || len(f.view.contentChanges) > 0 {
|
||||||
if err := f.view.parse(ctx, f.uri); err != nil {
|
errs, err := f.view.parse(ctx, f.uri)
|
||||||
|
if err != nil {
|
||||||
|
// Create diagnostics for errors if we are able to.
|
||||||
|
if len(errs) > 0 {
|
||||||
|
return &Package{errors: errs}
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -117,3 +117,7 @@ func (pkg *Package) GetTypes() *types.Package {
|
||||||
func (pkg *Package) GetTypesInfo() *types.Info {
|
func (pkg *Package) GetTypesInfo() *types.Info {
|
||||||
return pkg.typesInfo
|
return pkg.typesInfo
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (pkg *Package) IsIllTyped() bool {
|
||||||
|
return pkg.types == nil && pkg.typesInfo == nil
|
||||||
|
}
|
||||||
|
|
|
@ -43,6 +43,7 @@ type View struct {
|
||||||
// mcache caches metadata for the packages of the opened files in a view.
|
// mcache caches metadata for the packages of the opened files in a view.
|
||||||
mcache *metadataCache
|
mcache *metadataCache
|
||||||
|
|
||||||
|
// pcache caches type information for the packages of the opened files in a view.
|
||||||
pcache *packageCache
|
pcache *packageCache
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -49,6 +49,9 @@ type finder func(types.Object, float64, []CompletionItem) []CompletionItem
|
||||||
func Completion(ctx context.Context, f File, pos token.Pos) (items []CompletionItem, prefix string, err error) {
|
func Completion(ctx context.Context, f File, pos token.Pos) (items []CompletionItem, prefix string, err error) {
|
||||||
file := f.GetAST(ctx)
|
file := f.GetAST(ctx)
|
||||||
pkg := f.GetPackage(ctx)
|
pkg := f.GetPackage(ctx)
|
||||||
|
if pkg.IsIllTyped() {
|
||||||
|
return nil, "", fmt.Errorf("package for %s is ill typed", f.URI())
|
||||||
|
}
|
||||||
path, _ := astutil.PathEnclosingInterval(file, pos, pos)
|
path, _ := astutil.PathEnclosingInterval(file, pos, pos)
|
||||||
if path == nil {
|
if path == nil {
|
||||||
return nil, "", fmt.Errorf("cannot find node enclosing position")
|
return nil, "", fmt.Errorf("cannot find node enclosing position")
|
||||||
|
|
|
@ -62,6 +62,9 @@ func (i *IdentifierInfo) Hover(ctx context.Context, q types.Qualifier) (string,
|
||||||
func identifier(ctx context.Context, v View, f File, pos token.Pos) (*IdentifierInfo, error) {
|
func identifier(ctx context.Context, v View, f File, pos token.Pos) (*IdentifierInfo, error) {
|
||||||
fAST := f.GetAST(ctx)
|
fAST := f.GetAST(ctx)
|
||||||
pkg := f.GetPackage(ctx)
|
pkg := f.GetPackage(ctx)
|
||||||
|
if pkg.IsIllTyped() {
|
||||||
|
return nil, fmt.Errorf("package for %s is ill typed", f.URI())
|
||||||
|
}
|
||||||
path, _ := astutil.PathEnclosingInterval(fAST, pos, pos)
|
path, _ := astutil.PathEnclosingInterval(fAST, pos, pos)
|
||||||
result := &IdentifierInfo{
|
result := &IdentifierInfo{
|
||||||
File: f,
|
File: f,
|
||||||
|
|
|
@ -64,7 +64,7 @@ func Diagnostics(ctx context.Context, v View, uri span.URI) (map[span.URI][]Diag
|
||||||
for _, filename := range pkg.GetFilenames() {
|
for _, filename := range pkg.GetFilenames() {
|
||||||
reports[span.FileURI(filename)] = []Diagnostic{}
|
reports[span.FileURI(filename)] = []Diagnostic{}
|
||||||
}
|
}
|
||||||
var parseErrors, typeErrors []packages.Error
|
var listErrors, parseErrors, typeErrors []packages.Error
|
||||||
for _, err := range pkg.GetErrors() {
|
for _, err := range pkg.GetErrors() {
|
||||||
switch err.Kind {
|
switch err.Kind {
|
||||||
case packages.ParseError:
|
case packages.ParseError:
|
||||||
|
@ -72,14 +72,15 @@ func Diagnostics(ctx context.Context, v View, uri span.URI) (map[span.URI][]Diag
|
||||||
case packages.TypeError:
|
case packages.TypeError:
|
||||||
typeErrors = append(typeErrors, err)
|
typeErrors = append(typeErrors, err)
|
||||||
default:
|
default:
|
||||||
// ignore other types of errors
|
listErrors = append(listErrors, err)
|
||||||
continue
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Don't report type errors if there are parse errors.
|
// Don't report type errors if there are parse errors or list errors.
|
||||||
diags := typeErrors
|
diags := typeErrors
|
||||||
if len(parseErrors) > 0 {
|
if len(parseErrors) > 0 {
|
||||||
diags = parseErrors
|
diags = parseErrors
|
||||||
|
} else if len(listErrors) > 0 {
|
||||||
|
diags = listErrors
|
||||||
}
|
}
|
||||||
for _, diag := range diags {
|
for _, diag := range diags {
|
||||||
spn := span.Parse(diag.Pos)
|
spn := span.Parse(diag.Pos)
|
||||||
|
|
|
@ -27,6 +27,9 @@ type ParameterInformation struct {
|
||||||
func SignatureHelp(ctx context.Context, f File, pos token.Pos) (*SignatureInformation, error) {
|
func SignatureHelp(ctx context.Context, f File, pos token.Pos) (*SignatureInformation, error) {
|
||||||
fAST := f.GetAST(ctx)
|
fAST := f.GetAST(ctx)
|
||||||
pkg := f.GetPackage(ctx)
|
pkg := f.GetPackage(ctx)
|
||||||
|
if pkg.IsIllTyped() {
|
||||||
|
return nil, fmt.Errorf("package for %s is ill typed", f.URI())
|
||||||
|
}
|
||||||
|
|
||||||
// Find a call expression surrounding the query position.
|
// Find a call expression surrounding the query position.
|
||||||
var callExpr *ast.CallExpr
|
var callExpr *ast.CallExpr
|
||||||
|
|
|
@ -45,6 +45,7 @@ type Package interface {
|
||||||
GetErrors() []packages.Error
|
GetErrors() []packages.Error
|
||||||
GetTypes() *types.Package
|
GetTypes() *types.Package
|
||||||
GetTypesInfo() *types.Info
|
GetTypesInfo() *types.Info
|
||||||
|
IsIllTyped() bool
|
||||||
GetActionGraph(ctx context.Context, a *analysis.Analyzer) (*Action, error)
|
GetActionGraph(ctx context.Context, a *analysis.Analyzer) (*Action, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue