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-07-09 15:52:23 -06:00
|
|
|
"bytes"
|
2019-03-05 15:30:44 -07:00
|
|
|
"context"
|
2019-10-01 13:21:06 -06:00
|
|
|
"fmt"
|
2019-03-04 15:01:39 -07:00
|
|
|
"go/ast"
|
|
|
|
"go/types"
|
2019-10-01 13:21:06 -06:00
|
|
|
"sort"
|
2019-06-21 15:00:02 -06:00
|
|
|
"sync"
|
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-07-14 21:08:10 -06:00
|
|
|
"golang.org/x/tools/internal/lsp/telemetry"
|
2019-07-09 15:52:23 -06:00
|
|
|
"golang.org/x/tools/internal/memoize"
|
2019-10-16 17:09:37 -06:00
|
|
|
"golang.org/x/tools/internal/telemetry/log"
|
2019-08-13 13:07:39 -06:00
|
|
|
"golang.org/x/tools/internal/telemetry/trace"
|
2019-08-06 13:13:11 -06:00
|
|
|
errors "golang.org/x/xerrors"
|
2019-03-04 15:01:39 -07:00
|
|
|
)
|
|
|
|
|
2019-07-09 15:52:23 -06:00
|
|
|
// checkPackageHandle implements source.CheckPackageHandle.
|
|
|
|
type checkPackageHandle struct {
|
|
|
|
handle *memoize.Handle
|
|
|
|
|
2019-10-01 13:21:06 -06:00
|
|
|
// files are the ParseGoHandles that compose the package.
|
|
|
|
files []source.ParseGoHandle
|
|
|
|
|
|
|
|
// mode is the mode the the files were parsed in.
|
|
|
|
mode source.ParseMode
|
2019-07-09 15:52:23 -06:00
|
|
|
|
2019-10-01 13:21:06 -06:00
|
|
|
// imports is the map of the package's imports.
|
|
|
|
imports map[packagePath]packageID
|
|
|
|
|
|
|
|
// m is the metadata associated with the package.
|
|
|
|
m *metadata
|
|
|
|
|
|
|
|
// key is the hashed key for the package.
|
|
|
|
key []byte
|
2019-07-09 15:52:23 -06:00
|
|
|
}
|
|
|
|
|
2019-10-01 13:21:06 -06:00
|
|
|
func (cph *checkPackageHandle) packageKey() packageKey {
|
|
|
|
return packageKey{
|
|
|
|
id: cph.m.id,
|
|
|
|
mode: cph.mode,
|
2019-09-19 14:53:53 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-09 15:52:23 -06:00
|
|
|
// checkPackageData contains the data produced by type-checking a package.
|
|
|
|
type checkPackageData struct {
|
|
|
|
memoize.NoCopy
|
|
|
|
|
|
|
|
pkg *pkg
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
|
|
|
// checkPackageHandle returns a source.CheckPackageHandle for a given package and config.
|
2019-10-20 22:00:20 -06:00
|
|
|
func (s *snapshot) checkPackageHandle(ctx context.Context, id packageID, mode source.ParseMode) (*checkPackageHandle, error) {
|
2019-10-01 13:21:06 -06:00
|
|
|
// Check if we already have this CheckPackageHandle cached.
|
2019-10-20 22:00:20 -06:00
|
|
|
if cph := s.getPackage(id, mode); cph != nil {
|
2019-10-01 13:21:06 -06:00
|
|
|
return cph, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build the CheckPackageHandle for this ID and its dependencies.
|
2019-10-20 22:00:20 -06:00
|
|
|
cph, err := s.buildKey(ctx, id, mode)
|
2019-10-01 13:21:06 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-10-20 22:00:20 -06:00
|
|
|
h := s.view.session.cache.store.Bind(string(cph.key), func(ctx context.Context) interface{} {
|
|
|
|
// Begin loading the direct dependencies, in parallel.
|
|
|
|
for _, impID := range cph.imports {
|
|
|
|
dep := s.getPackage(impID, source.ParseExported)
|
|
|
|
if dep == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
go func(dep *checkPackageHandle) {
|
|
|
|
dep.check(ctx)
|
|
|
|
}(dep)
|
|
|
|
}
|
2019-10-01 13:21:06 -06:00
|
|
|
data := &checkPackageData{}
|
2019-10-20 22:00:20 -06:00
|
|
|
data.pkg, data.err = s.typeCheck(ctx, cph)
|
2019-10-01 13:21:06 -06:00
|
|
|
return data
|
|
|
|
})
|
|
|
|
cph.handle = h
|
|
|
|
|
2019-10-18 13:14:59 -06:00
|
|
|
// Cache the CheckPackageHandle in the snapshot.
|
2019-10-20 22:00:20 -06:00
|
|
|
s.addPackage(cph)
|
2019-10-18 13:14:59 -06:00
|
|
|
|
2019-10-01 13:21:06 -06:00
|
|
|
return cph, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// buildKey computes the checkPackageKey for a given checkPackageHandle.
|
2019-10-20 22:00:20 -06:00
|
|
|
func (s *snapshot) buildKey(ctx context.Context, id packageID, mode source.ParseMode) (*checkPackageHandle, error) {
|
|
|
|
m := s.getMetadata(id)
|
2019-09-27 11:17:59 -06:00
|
|
|
if m == nil {
|
|
|
|
return nil, errors.Errorf("no metadata for %s", id)
|
|
|
|
}
|
2019-10-20 22:00:20 -06:00
|
|
|
phs, err := s.parseGoHandles(ctx, m, mode)
|
2019-05-24 14:03:39 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-07-09 15:52:23 -06:00
|
|
|
cph := &checkPackageHandle{
|
|
|
|
m: m,
|
|
|
|
files: phs,
|
2019-10-01 13:21:06 -06:00
|
|
|
imports: make(map[packagePath]packageID),
|
|
|
|
mode: mode,
|
2019-07-09 15:52:23 -06:00
|
|
|
}
|
2019-10-01 13:21:06 -06:00
|
|
|
|
|
|
|
// Make sure all of the deps are sorted.
|
|
|
|
deps := append([]packageID{}, m.deps...)
|
|
|
|
sort.Slice(deps, func(i, j int) bool {
|
|
|
|
return deps[i] < deps[j]
|
2019-07-09 15:52:23 -06:00
|
|
|
})
|
2019-09-27 11:17:59 -06:00
|
|
|
|
2019-10-01 13:21:06 -06:00
|
|
|
// Begin computing the key by getting the depKeys for all dependencies.
|
|
|
|
var depKeys [][]byte
|
2019-10-20 22:00:20 -06:00
|
|
|
for _, depID := range deps {
|
|
|
|
depHandle, err := s.checkPackageHandle(ctx, depID, source.ParseExported)
|
2019-10-01 13:21:06 -06:00
|
|
|
if err != nil {
|
2019-10-20 22:00:20 -06:00
|
|
|
log.Error(ctx, "no dep handle", err, telemetry.Package.Of(depID))
|
2019-10-16 17:09:37 -06:00
|
|
|
|
|
|
|
// One bad dependency should not prevent us from checking the entire package.
|
|
|
|
// Add a special key to mark a bad dependency.
|
|
|
|
depKeys = append(depKeys, []byte(fmt.Sprintf("%s import not found", id)))
|
|
|
|
continue
|
2019-10-01 13:21:06 -06:00
|
|
|
}
|
|
|
|
cph.imports[depHandle.m.pkgPath] = depHandle.m.id
|
|
|
|
depKeys = append(depKeys, depHandle.key)
|
|
|
|
}
|
|
|
|
cph.key = checkPackageKey(cph.m.id, cph.files, m.config, depKeys)
|
2019-09-27 11:17:59 -06:00
|
|
|
|
2019-07-09 15:52:23 -06:00
|
|
|
return cph, nil
|
2019-05-24 14:03:39 -06:00
|
|
|
}
|
|
|
|
|
2019-10-01 13:21:06 -06:00
|
|
|
func checkPackageKey(id packageID, phs []source.ParseGoHandle, cfg *packages.Config, deps [][]byte) []byte {
|
|
|
|
return []byte(hashContents([]byte(fmt.Sprintf("%s%s%s%s", id, hashParseKeys(phs), hashConfig(cfg), hashContents(bytes.Join(deps, nil))))))
|
|
|
|
}
|
|
|
|
|
2019-07-09 15:52:23 -06:00
|
|
|
// hashConfig returns the hash for the *packages.Config.
|
|
|
|
func hashConfig(config *packages.Config) string {
|
|
|
|
b := bytes.NewBuffer(nil)
|
|
|
|
|
|
|
|
// Dir, Mode, Env, BuildFlags are the parts of the config that can change.
|
|
|
|
b.WriteString(config.Dir)
|
|
|
|
b.WriteString(string(config.Mode))
|
|
|
|
|
|
|
|
for _, e := range config.Env {
|
|
|
|
b.WriteString(e)
|
2019-03-11 15:14:55 -06:00
|
|
|
}
|
2019-07-09 15:52:23 -06:00
|
|
|
for _, f := range config.BuildFlags {
|
|
|
|
b.WriteString(f)
|
|
|
|
}
|
|
|
|
return hashContents(b.Bytes())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cph *checkPackageHandle) Check(ctx context.Context) (source.Package, error) {
|
|
|
|
return cph.check(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cph *checkPackageHandle) check(ctx context.Context) (*pkg, error) {
|
|
|
|
v := cph.handle.Get(ctx)
|
|
|
|
if v == nil {
|
2019-10-01 13:21:06 -06:00
|
|
|
return nil, errors.Errorf("no package for %s", cph.m.id)
|
2019-03-04 15:01:39 -07:00
|
|
|
}
|
2019-07-09 15:52:23 -06:00
|
|
|
data := v.(*checkPackageData)
|
|
|
|
return data.pkg, data.err
|
|
|
|
}
|
2019-06-11 09:32:51 -06:00
|
|
|
|
2019-07-09 15:52:23 -06:00
|
|
|
func (cph *checkPackageHandle) Files() []source.ParseGoHandle {
|
|
|
|
return cph.files
|
|
|
|
}
|
|
|
|
|
2019-09-10 15:33:35 -06:00
|
|
|
func (cph *checkPackageHandle) ID() string {
|
|
|
|
return string(cph.m.id)
|
|
|
|
}
|
|
|
|
|
2019-09-23 18:19:50 -06:00
|
|
|
func (cph *checkPackageHandle) MissingDependencies() []string {
|
|
|
|
var md []string
|
|
|
|
for i := range cph.m.missingDeps {
|
|
|
|
md = append(md, string(i))
|
|
|
|
}
|
|
|
|
return md
|
|
|
|
}
|
|
|
|
|
2019-10-24 13:44:41 -06:00
|
|
|
func (cph *checkPackageHandle) Cached() (source.Package, error) {
|
|
|
|
return cph.cached()
|
2019-09-19 10:39:03 -06:00
|
|
|
}
|
|
|
|
|
2019-10-24 13:44:41 -06:00
|
|
|
func (cph *checkPackageHandle) cached() (*pkg, error) {
|
2019-07-09 15:52:23 -06:00
|
|
|
v := cph.handle.Cached()
|
|
|
|
if v == nil {
|
2019-09-09 17:26:26 -06:00
|
|
|
return nil, errors.Errorf("no cached type information for %s", cph.m.pkgPath)
|
2019-03-04 15:45:01 -07:00
|
|
|
}
|
2019-07-09 15:52:23 -06:00
|
|
|
data := v.(*checkPackageData)
|
|
|
|
return data.pkg, data.err
|
|
|
|
}
|
2019-06-11 09:32:51 -06:00
|
|
|
|
2019-10-20 22:00:20 -06:00
|
|
|
func (s *snapshot) parseGoHandles(ctx context.Context, m *metadata, mode source.ParseMode) ([]source.ParseGoHandle, error) {
|
2019-07-09 15:52:23 -06:00
|
|
|
phs := make([]source.ParseGoHandle, 0, len(m.files))
|
|
|
|
for _, uri := range m.files {
|
2019-10-20 22:00:20 -06:00
|
|
|
f, err := s.view.GetFile(ctx, uri)
|
2019-07-09 15:52:23 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-10-20 22:00:20 -06:00
|
|
|
fh := s.Handle(ctx, f)
|
|
|
|
phs = append(phs, s.view.session.cache.ParseGoHandle(fh, mode))
|
2019-07-09 15:52:23 -06:00
|
|
|
}
|
|
|
|
return phs, nil
|
2019-03-04 15:01:39 -07:00
|
|
|
}
|
|
|
|
|
2019-10-20 22:00:20 -06:00
|
|
|
func (s *snapshot) typeCheck(ctx context.Context, cph *checkPackageHandle) (*pkg, error) {
|
2019-10-01 13:21:06 -06:00
|
|
|
ctx, done := trace.StartSpan(ctx, "cache.importer.typeCheck", telemetry.Package.Of(cph.m.id))
|
2019-07-09 15:52:23 -06:00
|
|
|
defer done()
|
|
|
|
|
2019-10-20 17:57:03 -06:00
|
|
|
var rawErrors []error
|
|
|
|
for _, err := range cph.m.errors {
|
|
|
|
rawErrors = append(rawErrors, err)
|
|
|
|
}
|
|
|
|
|
2019-05-14 21:04:23 -06:00
|
|
|
pkg := &pkg{
|
2019-10-20 22:00:20 -06:00
|
|
|
view: s.view,
|
2019-10-01 13:21:06 -06:00
|
|
|
id: cph.m.id,
|
2019-10-14 14:13:06 -06:00
|
|
|
mode: cph.mode,
|
2019-10-01 13:21:06 -06:00
|
|
|
pkgPath: cph.m.pkgPath,
|
2019-07-09 15:52:23 -06:00
|
|
|
files: cph.Files(),
|
2019-06-11 15:09:26 -06:00
|
|
|
imports: make(map[packagePath]*pkg),
|
2019-10-01 13:21:06 -06:00
|
|
|
typesSizes: cph.m.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-07-09 15:52:23 -06:00
|
|
|
var (
|
|
|
|
files = make([]*ast.File, len(pkg.files))
|
|
|
|
parseErrors = make([]error, len(pkg.files))
|
|
|
|
wg sync.WaitGroup
|
|
|
|
)
|
2019-07-11 19:05:55 -06:00
|
|
|
for i, ph := range pkg.files {
|
2019-06-21 15:00:02 -06:00
|
|
|
wg.Add(1)
|
|
|
|
go func(i int, ph source.ParseGoHandle) {
|
|
|
|
defer wg.Done()
|
|
|
|
|
2019-09-17 09:19:11 -06:00
|
|
|
files[i], _, parseErrors[i], _ = ph.Parse(ctx)
|
2019-06-21 15:00:02 -06:00
|
|
|
}(i, ph)
|
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
|
2019-10-20 17:57:03 -06:00
|
|
|
for _, e := range parseErrors {
|
|
|
|
if e != nil {
|
|
|
|
rawErrors = append(rawErrors, e)
|
2019-06-21 15:00:02 -06:00
|
|
|
}
|
2019-03-04 15:01:39 -07:00
|
|
|
}
|
2019-06-11 09:32:51 -06:00
|
|
|
|
2019-08-07 15:49:42 -06:00
|
|
|
var i int
|
|
|
|
for _, f := range files {
|
|
|
|
if f != nil {
|
|
|
|
files[i] = f
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
files = files[:i]
|
|
|
|
|
2019-06-11 12:50:43 -06:00
|
|
|
// Use the default type information for the unsafe package.
|
2019-10-20 17:57:03 -06:00
|
|
|
if pkg.pkgPath == "unsafe" {
|
2019-06-11 12:50:43 -06:00
|
|
|
pkg.types = types.Unsafe
|
|
|
|
} else if len(files) == 0 { // not the unsafe package, no parsed files
|
2019-08-06 13:13:11 -06:00
|
|
|
return nil, errors.Errorf("no parsed files for package %s", pkg.pkgPath)
|
2019-06-11 12:50:43 -06:00
|
|
|
} else {
|
2019-10-01 13:21:06 -06:00
|
|
|
pkg.types = types.NewPackage(string(cph.m.pkgPath), cph.m.name)
|
2019-04-22 14:04:44 -06:00
|
|
|
}
|
2019-03-11 15:14:55 -06:00
|
|
|
|
2019-03-04 15:01:39 -07:00
|
|
|
cfg := &types.Config{
|
2019-10-20 17:57:03 -06:00
|
|
|
Error: func(e error) {
|
|
|
|
rawErrors = append(rawErrors, e)
|
2019-06-11 12:50:43 -06:00
|
|
|
},
|
2019-10-20 22:00:20 -06:00
|
|
|
Importer: importerFunc(func(pkgPath string) (*types.Package, error) {
|
|
|
|
impID, ok := cph.imports[packagePath(pkgPath)]
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.Errorf("no package data for import %s", pkgPath)
|
|
|
|
}
|
|
|
|
dep := s.getPackage(impID, source.ParseExported)
|
|
|
|
if dep == nil {
|
|
|
|
return nil, errors.Errorf("no package for import %s", impID)
|
|
|
|
}
|
|
|
|
depPkg, err := dep.check(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
pkg.imports[depPkg.pkgPath] = depPkg
|
|
|
|
return depPkg.types, nil
|
|
|
|
}),
|
2019-03-04 15:01:39 -07:00
|
|
|
}
|
2019-10-20 22:00:20 -06:00
|
|
|
check := types.NewChecker(cfg, s.view.session.cache.FileSet(), pkg.types, pkg.typesInfo)
|
2019-06-21 15:00:02 -06:00
|
|
|
|
2019-09-06 14:32:09 -06:00
|
|
|
// Type checking errors are handled via the config, so ignore them here.
|
|
|
|
_ = check.Files(files)
|
2019-03-04 15:01:39 -07:00
|
|
|
|
2019-10-22 15:27:17 -06:00
|
|
|
// We don't care about a package's errors unless we have parsed it in full.
|
|
|
|
if cph.mode == source.ParseFull {
|
|
|
|
for _, e := range rawErrors {
|
|
|
|
srcErr, err := sourceError(ctx, pkg, e)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
pkg.errors = append(pkg.errors, srcErr)
|
2019-10-20 17:57:03 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-01 20:46:07 -06:00
|
|
|
return pkg, nil
|
|
|
|
}
|
|
|
|
|
2019-10-20 22:00:20 -06:00
|
|
|
// An importFunc is an implementation of the single-method
|
|
|
|
// types.Importer interface based on a function value.
|
|
|
|
type importerFunc func(path string) (*types.Package, error)
|
|
|
|
|
|
|
|
func (f importerFunc) Import(path string) (*types.Package, error) { return f(path) }
|