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"
|
2019-10-29 16:13:19 -06:00
|
|
|
"go/token"
|
2019-03-04 15:01:39 -07:00
|
|
|
"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
|
|
|
// 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-29 10:16:15 -06:00
|
|
|
cph, deps, err := s.buildKey(ctx, id, mode)
|
2019-10-01 13:21:06 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-10-29 10:16:15 -06:00
|
|
|
|
|
|
|
// Do not close over the checkPackageHandle or the snapshot in the Bind function.
|
|
|
|
// This creates a cycle, which causes the finalizers to never run on the handles.
|
|
|
|
// The possible cycles are:
|
|
|
|
//
|
|
|
|
// checkPackageHandle.h.function -> checkPackageHandle
|
|
|
|
// checkPackageHandle.h.function -> snapshot -> checkPackageHandle
|
|
|
|
//
|
|
|
|
|
|
|
|
m := cph.m
|
|
|
|
files := cph.files
|
|
|
|
key := cph.key
|
|
|
|
fset := s.view.session.cache.fset
|
|
|
|
|
|
|
|
h := s.view.session.cache.store.Bind(string(key), func(ctx context.Context) interface{} {
|
2019-10-20 22:00:20 -06:00
|
|
|
// Begin loading the direct dependencies, in parallel.
|
2019-10-29 10:16:15 -06:00
|
|
|
for _, dep := range deps {
|
2019-10-20 22:00:20 -06:00
|
|
|
go func(dep *checkPackageHandle) {
|
|
|
|
dep.check(ctx)
|
|
|
|
}(dep)
|
|
|
|
}
|
2019-10-01 13:21:06 -06:00
|
|
|
data := &checkPackageData{}
|
2019-10-29 10:16:15 -06:00
|
|
|
data.pkg, data.err = typeCheck(ctx, fset, m, mode, files, deps)
|
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-29 10:16:15 -06:00
|
|
|
func (s *snapshot) buildKey(ctx context.Context, id packageID, mode source.ParseMode) (*checkPackageHandle, map[packagePath]*checkPackageHandle, error) {
|
2019-10-20 22:00:20 -06:00
|
|
|
m := s.getMetadata(id)
|
2019-09-27 11:17:59 -06:00
|
|
|
if m == nil {
|
2019-10-29 10:16:15 -06:00
|
|
|
return nil, nil, errors.Errorf("no metadata for %s", id)
|
2019-09-27 11:17:59 -06:00
|
|
|
}
|
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 {
|
2019-10-29 10:16:15 -06:00
|
|
|
return nil, nil, err
|
2019-05-24 14:03:39 -06:00
|
|
|
}
|
2019-07-09 15:52:23 -06:00
|
|
|
cph := &checkPackageHandle{
|
2019-10-29 10:16:15 -06:00
|
|
|
m: m,
|
|
|
|
files: phs,
|
|
|
|
mode: mode,
|
2019-07-09 15:52:23 -06:00
|
|
|
}
|
2019-10-01 13:21:06 -06:00
|
|
|
|
2019-10-29 10:16:15 -06:00
|
|
|
// Make sure all of the depList are sorted.
|
2019-10-31 16:06:25 -06:00
|
|
|
depList := append([]packageID{}, m.deps...)
|
2019-10-29 10:16:15 -06:00
|
|
|
sort.Slice(depList, func(i, j int) bool {
|
|
|
|
return depList[i] < depList[j]
|
2019-07-09 15:52:23 -06:00
|
|
|
})
|
2019-09-27 11:17:59 -06:00
|
|
|
|
2019-10-29 10:16:15 -06:00
|
|
|
deps := make(map[packagePath]*checkPackageHandle)
|
|
|
|
|
2019-10-01 13:21:06 -06:00
|
|
|
// Begin computing the key by getting the depKeys for all dependencies.
|
|
|
|
var depKeys [][]byte
|
2019-10-29 10:16:15 -06:00
|
|
|
for _, depID := range depList {
|
2019-10-20 22:00:20 -06:00
|
|
|
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
|
|
|
}
|
2019-10-29 10:16:15 -06:00
|
|
|
deps[depHandle.m.pkgPath] = depHandle
|
2019-10-01 13:21:06 -06:00
|
|
|
depKeys = append(depKeys, depHandle.key)
|
|
|
|
}
|
|
|
|
cph.key = checkPackageKey(cph.m.id, cph.files, m.config, depKeys)
|
2019-10-29 10:16:15 -06:00
|
|
|
return cph, deps, 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-29 10:16:15 -06:00
|
|
|
func typeCheck(ctx context.Context, fset *token.FileSet, m *metadata, mode source.ParseMode, phs []source.ParseGoHandle, deps map[packagePath]*checkPackageHandle) (*pkg, error) {
|
|
|
|
ctx, done := trace.StartSpan(ctx, "cache.importer.typeCheck", telemetry.Package.Of(m.id))
|
2019-07-09 15:52:23 -06:00
|
|
|
defer done()
|
|
|
|
|
2019-10-20 17:57:03 -06:00
|
|
|
var rawErrors []error
|
2019-10-29 10:16:15 -06:00
|
|
|
for _, err := range m.errors {
|
2019-10-20 17:57:03 -06:00
|
|
|
rawErrors = append(rawErrors, err)
|
|
|
|
}
|
|
|
|
|
2019-05-14 21:04:23 -06:00
|
|
|
pkg := &pkg{
|
2019-10-29 10:16:15 -06:00
|
|
|
id: m.id,
|
|
|
|
pkgPath: m.pkgPath,
|
|
|
|
mode: mode,
|
|
|
|
files: phs,
|
2019-06-11 15:09:26 -06:00
|
|
|
imports: make(map[packagePath]*pkg),
|
2019-10-29 10:16:15 -06:00
|
|
|
typesSizes: 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-29 10:16:15 -06:00
|
|
|
pkg.types = types.NewPackage(string(m.pkgPath), 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) {
|
2019-10-29 10:16:15 -06:00
|
|
|
dep := deps[packagePath(pkgPath)]
|
2019-10-20 22:00:20 -06:00
|
|
|
if dep == nil {
|
2019-10-29 10:16:15 -06:00
|
|
|
return nil, errors.Errorf("no package for import %s", pkgPath)
|
2019-10-20 22:00:20 -06:00
|
|
|
}
|
|
|
|
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-29 10:16:15 -06:00
|
|
|
check := types.NewChecker(cfg, fset, 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.
|
2019-10-29 10:16:15 -06:00
|
|
|
if mode == source.ParseFull {
|
2019-10-22 15:27:17 -06:00
|
|
|
for _, e := range rawErrors {
|
2019-10-29 16:13:19 -06:00
|
|
|
srcErr, err := sourceError(ctx, fset, pkg, e)
|
2019-10-22 15:27:17 -06:00
|
|
|
if err != nil {
|
2019-11-11 16:27:04 -07:00
|
|
|
log.Error(ctx, "unable to compute error positions", err, telemetry.Package.Of(pkg.ID()))
|
|
|
|
continue
|
2019-10-22 15:27:17 -06:00
|
|
|
}
|
|
|
|
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) }
|