2019-05-10 14:35:43 -06:00
|
|
|
// Copyright 2019 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2019-03-04 15:01:39 -07:00
|
|
|
package cache
|
|
|
|
|
|
|
|
import (
|
2019-03-05 15:30:44 -07:00
|
|
|
"context"
|
2019-03-04 15:01:39 -07:00
|
|
|
"fmt"
|
|
|
|
"go/ast"
|
|
|
|
"go/scanner"
|
2019-05-17 08:51:19 -06:00
|
|
|
"go/token"
|
2019-03-04 15:01:39 -07:00
|
|
|
"go/types"
|
|
|
|
|
2019-03-06 14:33:47 -07:00
|
|
|
"golang.org/x/tools/go/analysis"
|
2019-03-04 15:01:39 -07:00
|
|
|
"golang.org/x/tools/go/packages"
|
2019-02-19 19:11:15 -07:00
|
|
|
"golang.org/x/tools/internal/span"
|
2019-03-04 15:01:39 -07:00
|
|
|
)
|
|
|
|
|
2019-03-11 15:14:55 -06:00
|
|
|
type importer struct {
|
2019-05-14 21:04:23 -06:00
|
|
|
view *view
|
2019-03-11 15:14:55 -06:00
|
|
|
|
2019-04-22 14:04:44 -06:00
|
|
|
// seen maintains the set of previously imported packages.
|
2019-03-11 15:14:55 -06:00
|
|
|
// If we have seen a package that is already in this map, we have a circular import.
|
2019-06-11 16:06:27 -06:00
|
|
|
seen map[packageID]struct{}
|
2019-04-01 08:55:08 -06:00
|
|
|
|
2019-06-05 15:44:09 -06:00
|
|
|
// topLevelPkgID is the ID of the package from which type-checking began.
|
2019-06-11 15:09:26 -06:00
|
|
|
topLevelPkgID packageID
|
2019-05-23 11:51:56 -06:00
|
|
|
|
2019-05-17 08:51:19 -06:00
|
|
|
ctx context.Context
|
|
|
|
fset *token.FileSet
|
2019-03-11 15:14:55 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (imp *importer) Import(pkgPath string) (*types.Package, error) {
|
2019-06-11 16:06:27 -06:00
|
|
|
id, ok := imp.view.mcache.ids[packagePath(pkgPath)]
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("no known ID for %s", pkgPath)
|
|
|
|
}
|
|
|
|
pkg, err := imp.getPkg(id)
|
2019-05-24 14:03:39 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return pkg.types, nil
|
|
|
|
}
|
|
|
|
|
2019-06-11 16:06:27 -06:00
|
|
|
func (imp *importer) getPkg(id packageID) (*pkg, error) {
|
|
|
|
if _, ok := imp.seen[id]; ok {
|
2019-03-11 15:14:55 -06:00
|
|
|
return nil, fmt.Errorf("circular import detected")
|
|
|
|
}
|
|
|
|
imp.view.pcache.mu.Lock()
|
2019-06-11 16:06:27 -06:00
|
|
|
e, ok := imp.view.pcache.packages[id]
|
2019-06-11 09:32:51 -06:00
|
|
|
|
2019-03-04 15:01:39 -07:00
|
|
|
if ok {
|
|
|
|
// cache hit
|
2019-03-11 15:14:55 -06:00
|
|
|
imp.view.pcache.mu.Unlock()
|
2019-03-04 15:01:39 -07:00
|
|
|
// wait for entry to become ready
|
|
|
|
<-e.ready
|
|
|
|
} else {
|
|
|
|
// cache miss
|
|
|
|
e = &entry{ready: make(chan struct{})}
|
2019-06-11 16:06:27 -06:00
|
|
|
imp.view.pcache.packages[id] = e
|
2019-03-11 15:14:55 -06:00
|
|
|
imp.view.pcache.mu.Unlock()
|
2019-03-04 15:01:39 -07:00
|
|
|
|
|
|
|
// This goroutine becomes responsible for populating
|
|
|
|
// the entry and broadcasting its readiness.
|
2019-06-11 16:06:27 -06:00
|
|
|
e.pkg, e.err = imp.typeCheck(id)
|
2019-03-04 15:01:39 -07:00
|
|
|
close(e.ready)
|
|
|
|
}
|
2019-06-11 09:32:51 -06:00
|
|
|
|
2019-03-04 15:45:01 -07:00
|
|
|
if e.err != nil {
|
2019-06-11 09:32:51 -06:00
|
|
|
// If the import had been previously canceled, and that error cached, try again.
|
|
|
|
if e.err == context.Canceled && imp.ctx.Err() == nil {
|
|
|
|
imp.view.pcache.mu.Lock()
|
|
|
|
// Clear out canceled cache entry if it is still there.
|
2019-06-11 16:06:27 -06:00
|
|
|
if imp.view.pcache.packages[id] == e {
|
|
|
|
delete(imp.view.pcache.packages, id)
|
2019-06-11 09:32:51 -06:00
|
|
|
}
|
|
|
|
imp.view.pcache.mu.Unlock()
|
2019-06-11 16:06:27 -06:00
|
|
|
return imp.getPkg(id)
|
2019-06-11 09:32:51 -06:00
|
|
|
}
|
2019-03-04 15:45:01 -07:00
|
|
|
return nil, e.err
|
|
|
|
}
|
2019-06-11 09:32:51 -06:00
|
|
|
|
2019-05-24 14:03:39 -06:00
|
|
|
return e.pkg, nil
|
2019-03-04 15:01:39 -07:00
|
|
|
}
|
|
|
|
|
2019-06-11 16:06:27 -06:00
|
|
|
func (imp *importer) typeCheck(id packageID) (*pkg, error) {
|
|
|
|
meta, ok := imp.view.mcache.packages[id]
|
2019-03-04 15:01:39 -07:00
|
|
|
if !ok {
|
2019-06-11 16:06:27 -06:00
|
|
|
return nil, fmt.Errorf("no metadata for %v", id)
|
2019-03-04 15:01:39 -07:00
|
|
|
}
|
2019-05-14 21:04:23 -06:00
|
|
|
pkg := &pkg{
|
2019-03-29 14:39:22 -06:00
|
|
|
id: meta.id,
|
|
|
|
pkgPath: meta.pkgPath,
|
|
|
|
files: meta.files,
|
2019-06-11 15:09:26 -06:00
|
|
|
imports: make(map[packagePath]*pkg),
|
2019-03-29 14:39:22 -06:00
|
|
|
typesSizes: meta.typesSizes,
|
2019-03-06 14:33:47 -07:00
|
|
|
typesInfo: &types.Info{
|
2019-03-04 16:01:51 -07:00
|
|
|
Types: make(map[ast.Expr]types.TypeAndValue),
|
|
|
|
Defs: make(map[*ast.Ident]types.Object),
|
|
|
|
Uses: make(map[*ast.Ident]types.Object),
|
|
|
|
Implicits: make(map[ast.Node]types.Object),
|
|
|
|
Selections: make(map[*ast.SelectorExpr]*types.Selection),
|
|
|
|
Scopes: make(map[ast.Node]*types.Scope),
|
|
|
|
},
|
2019-03-06 14:33:47 -07:00
|
|
|
analyses: make(map[*analysis.Analyzer]*analysisEntry),
|
2019-03-04 16:01:51 -07:00
|
|
|
}
|
2019-06-05 15:44:09 -06:00
|
|
|
// Ignore function bodies for any dependency packages.
|
2019-06-07 19:57:42 -06:00
|
|
|
ignoreFuncBodies := imp.topLevelPkgID != pkg.id
|
2019-06-11 09:32:51 -06:00
|
|
|
files, parseErrs, err := imp.parseFiles(meta.files, ignoreFuncBodies)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for _, err := range parseErrs {
|
2019-06-11 12:50:43 -06:00
|
|
|
imp.view.appendPkgError(pkg, err)
|
2019-03-04 15:01:39 -07:00
|
|
|
}
|
2019-06-11 09:32:51 -06:00
|
|
|
|
2019-06-11 12:50:43 -06:00
|
|
|
// Use the default type information for the unsafe package.
|
|
|
|
if meta.pkgPath == "unsafe" {
|
|
|
|
pkg.types = types.Unsafe
|
|
|
|
} else if len(files) == 0 { // not the unsafe package, no parsed files
|
2019-06-11 09:32:51 -06:00
|
|
|
return nil, fmt.Errorf("no parsed files for package %s", pkg.pkgPath)
|
2019-06-11 12:50:43 -06:00
|
|
|
} else {
|
2019-06-11 15:09:26 -06:00
|
|
|
pkg.types = types.NewPackage(string(meta.pkgPath), meta.name)
|
2019-06-11 09:32:51 -06:00
|
|
|
}
|
|
|
|
|
2019-03-06 14:33:47 -07:00
|
|
|
pkg.syntax = files
|
2019-03-11 15:14:55 -06:00
|
|
|
|
|
|
|
// Handle circular imports by copying previously seen imports.
|
2019-06-11 16:06:27 -06:00
|
|
|
seen := make(map[packageID]struct{})
|
2019-04-22 14:04:44 -06:00
|
|
|
for k, v := range imp.seen {
|
|
|
|
seen[k] = v
|
|
|
|
}
|
2019-06-11 16:06:27 -06:00
|
|
|
seen[id] = struct{}{}
|
2019-03-11 15:14:55 -06:00
|
|
|
|
2019-03-04 15:01:39 -07:00
|
|
|
cfg := &types.Config{
|
2019-06-11 12:50:43 -06:00
|
|
|
Error: func(err error) {
|
|
|
|
imp.view.appendPkgError(pkg, err)
|
|
|
|
},
|
2019-06-05 15:44:09 -06:00
|
|
|
IgnoreFuncBodies: ignoreFuncBodies,
|
2019-03-11 15:14:55 -06:00
|
|
|
Importer: &importer{
|
2019-06-05 15:44:09 -06:00
|
|
|
view: imp.view,
|
|
|
|
ctx: imp.ctx,
|
|
|
|
fset: imp.fset,
|
|
|
|
topLevelPkgID: imp.topLevelPkgID,
|
|
|
|
seen: seen,
|
2019-03-11 15:14:55 -06:00
|
|
|
},
|
2019-03-04 15:01:39 -07:00
|
|
|
}
|
2019-05-17 08:51:19 -06:00
|
|
|
check := types.NewChecker(cfg, imp.fset, pkg.types, pkg.typesInfo)
|
2019-06-05 15:44:09 -06:00
|
|
|
check.Files(pkg.GetSyntax())
|
2019-03-04 15:01:39 -07:00
|
|
|
|
2019-04-01 08:55:08 -06:00
|
|
|
// Add every file in this package to our cache.
|
2019-05-24 14:03:39 -06:00
|
|
|
imp.cachePackage(imp.ctx, pkg, meta)
|
2019-05-01 20:46:07 -06:00
|
|
|
|
|
|
|
return pkg, nil
|
|
|
|
}
|
|
|
|
|
2019-05-24 14:03:39 -06:00
|
|
|
func (imp *importer) cachePackage(ctx context.Context, pkg *pkg, meta *metadata) {
|
2019-06-11 16:07:34 -06:00
|
|
|
for _, filename := range pkg.files {
|
2019-06-13 13:55:53 -06:00
|
|
|
f, err := imp.view.getFile(span.FileURI(filename))
|
2019-05-01 20:46:07 -06:00
|
|
|
if err != nil {
|
2019-06-13 13:55:53 -06:00
|
|
|
imp.view.session.log.Errorf(ctx, "no file: %v", err)
|
2019-05-01 20:46:07 -06:00
|
|
|
continue
|
|
|
|
}
|
2019-05-03 22:04:18 -06:00
|
|
|
gof, ok := f.(*goFile)
|
|
|
|
if !ok {
|
2019-06-13 13:55:53 -06:00
|
|
|
imp.view.session.log.Errorf(ctx, "%v is not a Go file", f.URI())
|
2019-05-03 22:04:18 -06:00
|
|
|
continue
|
|
|
|
}
|
2019-06-13 13:55:53 -06:00
|
|
|
// Set the package even if we failed to parse the file.
|
2019-06-11 16:07:34 -06:00
|
|
|
gof.pkg = pkg
|
|
|
|
|
2019-06-13 13:55:53 -06:00
|
|
|
// Get the *token.File directly from the AST.
|
|
|
|
gof.ast = pkg.syntax[filename]
|
|
|
|
if gof.ast == nil {
|
|
|
|
imp.view.session.log.Errorf(ctx, "no AST information for %s", filename)
|
2019-06-11 16:07:34 -06:00
|
|
|
continue
|
|
|
|
}
|
2019-06-13 13:55:53 -06:00
|
|
|
if gof.ast.file == nil {
|
|
|
|
imp.view.session.log.Errorf(ctx, "no AST for %s", filename)
|
|
|
|
}
|
|
|
|
pos := gof.ast.file.Pos()
|
|
|
|
if !pos.IsValid() {
|
|
|
|
imp.view.session.log.Errorf(ctx, "AST for %s has an invalid position", filename)
|
2019-06-11 16:07:34 -06:00
|
|
|
continue
|
|
|
|
}
|
2019-06-13 13:55:53 -06:00
|
|
|
tok := imp.view.session.cache.FileSet().File(pos)
|
2019-06-11 16:07:34 -06:00
|
|
|
if tok == nil {
|
2019-06-13 13:55:53 -06:00
|
|
|
imp.view.session.log.Errorf(ctx, "no *token.File for %s", filename)
|
2019-06-11 16:07:34 -06:00
|
|
|
continue
|
|
|
|
}
|
2019-05-03 22:04:18 -06:00
|
|
|
gof.token = tok
|
2019-06-13 13:55:53 -06:00
|
|
|
gof.imports = gof.ast.file.Imports
|
2019-05-01 20:46:07 -06:00
|
|
|
}
|
|
|
|
|
2019-03-06 14:33:47 -07:00
|
|
|
// Set imports of package to correspond to cached packages.
|
|
|
|
// We lock the package cache, but we shouldn't get any inconsistencies
|
|
|
|
// because we are still holding the lock on the view.
|
2019-03-05 15:30:44 -07:00
|
|
|
for importPath := range meta.children {
|
2019-05-24 14:03:39 -06:00
|
|
|
importPkg, err := imp.getPkg(importPath)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
2019-03-05 15:30:44 -07:00
|
|
|
}
|
2019-06-11 16:06:27 -06:00
|
|
|
pkg.imports[importPkg.pkgPath] = importPkg
|
2019-03-05 15:30:44 -07:00
|
|
|
}
|
2019-03-04 15:01:39 -07:00
|
|
|
}
|
|
|
|
|
2019-05-14 21:04:23 -06:00
|
|
|
func (v *view) appendPkgError(pkg *pkg, err error) {
|
2019-03-04 15:01:39 -07:00
|
|
|
if err == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var errs []packages.Error
|
|
|
|
switch err := err.(type) {
|
|
|
|
case *scanner.Error:
|
|
|
|
errs = append(errs, packages.Error{
|
|
|
|
Pos: err.Pos.String(),
|
|
|
|
Msg: err.Msg,
|
|
|
|
Kind: packages.ParseError,
|
|
|
|
})
|
|
|
|
case scanner.ErrorList:
|
|
|
|
// The first parser error is likely the root cause of the problem.
|
|
|
|
if err.Len() > 0 {
|
|
|
|
errs = append(errs, packages.Error{
|
|
|
|
Pos: err[0].Pos.String(),
|
|
|
|
Msg: err[0].Msg,
|
|
|
|
Kind: packages.ParseError,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
case types.Error:
|
|
|
|
errs = append(errs, packages.Error{
|
2019-05-17 08:51:19 -06:00
|
|
|
Pos: v.Session().Cache().FileSet().Position(err.Pos).String(),
|
2019-03-04 15:01:39 -07:00
|
|
|
Msg: err.Msg,
|
|
|
|
Kind: packages.TypeError,
|
|
|
|
})
|
|
|
|
}
|
2019-03-06 14:33:47 -07:00
|
|
|
pkg.errors = append(pkg.errors, errs...)
|
2019-03-04 15:01:39 -07:00
|
|
|
}
|