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-06-21 15:00:02 -06:00
|
|
|
"sync"
|
2019-03-04 15:01:39 -07:00
|
|
|
|
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-06-21 15:00:02 -06:00
|
|
|
"golang.org/x/tools/internal/lsp/source"
|
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,
|
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-21 15:00:02 -06:00
|
|
|
|
2019-06-05 15:44:09 -06:00
|
|
|
// Ignore function bodies for any dependency packages.
|
2019-06-21 15:00:02 -06:00
|
|
|
mode := source.ParseFull
|
|
|
|
if imp.topLevelPkgID != pkg.id {
|
|
|
|
mode = source.ParseExported
|
|
|
|
}
|
|
|
|
var (
|
|
|
|
files []*astFile
|
|
|
|
phs []source.ParseGoHandle
|
|
|
|
wg sync.WaitGroup
|
|
|
|
)
|
|
|
|
for _, filename := range meta.files {
|
|
|
|
uri := span.FileURI(filename)
|
|
|
|
f, err := imp.view.getFile(uri)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
2019-06-24 14:34:21 -06:00
|
|
|
ph := imp.view.session.cache.ParseGoHandle(f.Handle(imp.ctx), mode)
|
|
|
|
phs = append(phs, ph)
|
2019-06-21 15:00:02 -06:00
|
|
|
files = append(files, &astFile{
|
2019-06-24 14:34:21 -06:00
|
|
|
uri: ph.File().Identity().URI,
|
2019-06-21 15:00:02 -06:00
|
|
|
isTrimmed: mode == source.ParseExported,
|
2019-06-24 14:34:21 -06:00
|
|
|
ph: ph,
|
2019-06-21 15:00:02 -06:00
|
|
|
})
|
2019-06-11 09:32:51 -06:00
|
|
|
}
|
2019-06-21 15:00:02 -06:00
|
|
|
for i, ph := range phs {
|
|
|
|
wg.Add(1)
|
|
|
|
go func(i int, ph source.ParseGoHandle) {
|
|
|
|
defer wg.Done()
|
|
|
|
|
|
|
|
files[i].file, files[i].err = ph.Parse(imp.ctx)
|
|
|
|
}(i, ph)
|
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
for _, f := range files {
|
2019-06-26 17:28:06 -06:00
|
|
|
pkg.files = append(pkg.files, f)
|
|
|
|
|
2019-06-21 15:00:02 -06:00
|
|
|
if f.err != nil {
|
2019-06-26 17:28:06 -06:00
|
|
|
if f.err == context.Canceled {
|
|
|
|
return nil, f.err
|
|
|
|
}
|
2019-06-21 15:00:02 -06:00
|
|
|
imp.view.session.cache.appendPkgError(pkg, f.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-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) {
|
2019-06-21 15:00:02 -06:00
|
|
|
imp.view.session.cache.appendPkgError(pkg, err)
|
2019-06-11 12:50:43 -06:00
|
|
|
},
|
2019-06-21 15:00:02 -06:00
|
|
|
IgnoreFuncBodies: mode == source.ParseExported,
|
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-21 15:00:02 -06:00
|
|
|
|
|
|
|
// Ignore type-checking errors.
|
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-06-21 15:00:02 -06:00
|
|
|
imp.cachePackage(imp.ctx, pkg, meta, mode)
|
2019-05-01 20:46:07 -06:00
|
|
|
|
|
|
|
return pkg, nil
|
|
|
|
}
|
|
|
|
|
2019-06-26 12:34:36 -06:00
|
|
|
func (imp *importer) cachePackage(ctx context.Context, pkg *pkg, meta *metadata, mode source.ParseMode) {
|
|
|
|
for _, file := range pkg.files {
|
2019-06-21 15:00:02 -06:00
|
|
|
f, err := imp.view.getFile(file.uri)
|
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-21 15:00:02 -06:00
|
|
|
imp.view.session.log.Errorf(ctx, "%v is not a Go file", file.uri)
|
2019-05-03 22:04:18 -06:00
|
|
|
continue
|
|
|
|
}
|
2019-06-26 12:34:36 -06:00
|
|
|
if err := imp.cachePerFile(gof, file, pkg); err != nil {
|
|
|
|
imp.view.session.log.Errorf(ctx, "failed to cache file %s: %v", gof.URI(), err)
|
2019-06-24 14:34:21 -06:00
|
|
|
}
|
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-26 12:34:36 -06:00
|
|
|
pkg.imports[importPkg.pkgPath] = importPkg
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (imp *importer) cachePerFile(gof *goFile, file *astFile, p *pkg) error {
|
|
|
|
gof.mu.Lock()
|
|
|
|
defer gof.mu.Unlock()
|
|
|
|
|
|
|
|
// Set the package even if we failed to parse the file.
|
|
|
|
if gof.pkgs == nil {
|
|
|
|
gof.pkgs = make(map[packageID]*pkg)
|
|
|
|
}
|
|
|
|
gof.pkgs[p.id] = p
|
|
|
|
|
|
|
|
// Get the AST for the file.
|
|
|
|
gof.ast = file
|
|
|
|
if gof.ast == nil {
|
|
|
|
return fmt.Errorf("no AST information for %s", file.uri)
|
|
|
|
}
|
|
|
|
if gof.ast.file == nil {
|
|
|
|
return fmt.Errorf("no AST for %s", file.uri)
|
|
|
|
}
|
|
|
|
// Get the *token.File directly from the AST.
|
|
|
|
pos := gof.ast.file.Pos()
|
|
|
|
if !pos.IsValid() {
|
|
|
|
return fmt.Errorf("AST for %s has an invalid position", file.uri)
|
|
|
|
}
|
|
|
|
tok := imp.view.session.cache.FileSet().File(pos)
|
|
|
|
if tok == nil {
|
|
|
|
return fmt.Errorf("no *token.File for %s", file.uri)
|
2019-03-05 15:30:44 -07:00
|
|
|
}
|
2019-06-26 12:34:36 -06:00
|
|
|
gof.token = tok
|
|
|
|
gof.imports = gof.ast.file.Imports
|
|
|
|
return nil
|
2019-03-04 15:01:39 -07:00
|
|
|
}
|
|
|
|
|
2019-06-21 15:00:02 -06:00
|
|
|
func (c *cache) 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-06-21 15:00:02 -06:00
|
|
|
Pos: c.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
|
|
|
}
|