2019-06-21 15:00:02 -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-05-29 12:59:35 -06:00
|
|
|
package cache
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"golang.org/x/tools/go/packages"
|
2019-06-04 20:14:37 -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-05-29 12:59:35 -06:00
|
|
|
"golang.org/x/tools/internal/span"
|
2019-08-13 13:07:39 -06:00
|
|
|
"golang.org/x/tools/internal/telemetry/log"
|
|
|
|
"golang.org/x/tools/internal/telemetry/tag"
|
|
|
|
"golang.org/x/tools/internal/telemetry/trace"
|
2019-08-06 13:13:11 -06:00
|
|
|
errors "golang.org/x/xerrors"
|
2019-05-29 12:59:35 -06:00
|
|
|
)
|
|
|
|
|
2019-09-05 15:34:00 -06:00
|
|
|
func (view *view) loadParseTypecheck(ctx context.Context, f *goFile, fh source.FileHandle) error {
|
2019-09-05 12:09:42 -06:00
|
|
|
ctx, done := trace.StartSpan(ctx, "cache.view.loadParseTypeCheck", telemetry.URI.Of(f.URI()))
|
|
|
|
defer done()
|
|
|
|
|
2019-09-05 15:34:00 -06:00
|
|
|
meta, err := view.load(ctx, f, fh)
|
2019-06-26 12:34:36 -06:00
|
|
|
if err != nil {
|
2019-07-09 15:52:23 -06:00
|
|
|
return err
|
2019-05-29 12:59:35 -06:00
|
|
|
}
|
2019-09-05 12:09:42 -06:00
|
|
|
for _, m := range meta {
|
2019-06-24 14:34:21 -06:00
|
|
|
imp := &importer{
|
2019-07-09 15:52:23 -06:00
|
|
|
view: view,
|
|
|
|
config: view.Config(ctx),
|
|
|
|
seen: make(map[packageID]struct{}),
|
|
|
|
topLevelPackageID: m.id,
|
2019-06-24 14:34:21 -06:00
|
|
|
}
|
2019-09-05 09:27:06 -06:00
|
|
|
cph, err := imp.checkPackageHandle(ctx, m)
|
2019-06-24 14:34:21 -06:00
|
|
|
if err != nil {
|
2019-09-05 12:09:42 -06:00
|
|
|
log.Error(ctx, "loadParseTypeCheck: failed to get CheckPackageHandle", err, telemetry.Package.Of(m.id))
|
2019-07-09 15:52:23 -06:00
|
|
|
continue
|
2019-06-24 14:34:21 -06:00
|
|
|
}
|
2019-09-05 12:09:42 -06:00
|
|
|
if _, err := cph.check(ctx); err != nil {
|
|
|
|
log.Error(ctx, "loadParseTypeCheck: failed to check package", err, telemetry.Package.Of(m.id))
|
2019-07-09 15:52:23 -06:00
|
|
|
continue
|
2019-06-24 14:34:21 -06:00
|
|
|
}
|
2019-07-09 15:52:23 -06:00
|
|
|
// Cache this package on the file object, since all dependencies are cached in the Import function.
|
2019-09-05 12:09:42 -06:00
|
|
|
if err := imp.cachePackage(ctx, cph); err != nil {
|
|
|
|
log.Error(ctx, "loadParseTypeCheck: failed to cache package", err, telemetry.Package.Of(m.id))
|
|
|
|
continue
|
|
|
|
}
|
2019-06-11 09:32:51 -06:00
|
|
|
}
|
2019-07-09 15:52:23 -06:00
|
|
|
return nil
|
2019-05-29 12:59:35 -06:00
|
|
|
}
|
|
|
|
|
2019-09-05 15:34:00 -06:00
|
|
|
func (view *view) load(ctx context.Context, f *goFile, fh source.FileHandle) ([]*metadata, error) {
|
2019-09-05 12:09:42 -06:00
|
|
|
ctx, done := trace.StartSpan(ctx, "cache.view.load", telemetry.URI.Of(f.URI()))
|
|
|
|
defer done()
|
|
|
|
|
2019-07-09 15:52:23 -06:00
|
|
|
view.mu.Lock()
|
|
|
|
defer view.mu.Unlock()
|
|
|
|
|
|
|
|
view.mcache.mu.Lock()
|
|
|
|
defer view.mcache.mu.Unlock()
|
|
|
|
|
2019-09-04 13:30:42 -06:00
|
|
|
var toDelete []packageID
|
|
|
|
f.mu.Lock()
|
|
|
|
for id, cph := range f.pkgs {
|
|
|
|
if cph != nil {
|
|
|
|
toDelete = append(toDelete, id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
f.mu.Unlock()
|
|
|
|
|
2019-07-09 15:52:23 -06:00
|
|
|
// If the AST for this file is trimmed, and we are explicitly type-checking it,
|
|
|
|
// don't ignore function bodies.
|
2019-09-05 15:34:00 -06:00
|
|
|
if f.wrongParseMode(ctx, fh, source.ParseFull) {
|
2019-09-04 13:30:42 -06:00
|
|
|
// Remove the package and all of its reverse dependencies from the cache.
|
|
|
|
for _, id := range toDelete {
|
|
|
|
f.view.remove(ctx, id, map[packageID]struct{}{})
|
|
|
|
}
|
2019-06-04 16:06:55 -06:00
|
|
|
}
|
2019-07-09 15:52:23 -06:00
|
|
|
|
|
|
|
// Get the metadata for the file.
|
2019-09-05 15:34:00 -06:00
|
|
|
meta, err := view.checkMetadata(ctx, f, fh)
|
2019-07-09 15:52:23 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2019-06-04 16:06:55 -06:00
|
|
|
}
|
2019-09-05 12:09:42 -06:00
|
|
|
if len(meta) == 0 {
|
2019-07-09 15:52:23 -06:00
|
|
|
return nil, fmt.Errorf("no package metadata found for %s", f.URI())
|
|
|
|
}
|
|
|
|
return meta, nil
|
2019-06-04 16:06:55 -06:00
|
|
|
}
|
|
|
|
|
2019-05-23 11:51:56 -06:00
|
|
|
// checkMetadata determines if we should run go/packages.Load for this file.
|
|
|
|
// If yes, update the metadata for the file and its package.
|
2019-09-05 15:34:00 -06:00
|
|
|
func (v *view) checkMetadata(ctx context.Context, f *goFile, fh source.FileHandle) ([]*metadata, error) {
|
2019-09-05 14:29:59 -06:00
|
|
|
// Check if we need to re-run go/packages before loading the package.
|
|
|
|
f.mu.Lock()
|
2019-09-05 15:34:00 -06:00
|
|
|
runGopackages := v.shouldRunGopackages(ctx, f, fh)
|
2019-09-05 14:29:59 -06:00
|
|
|
metadata := f.metadata()
|
|
|
|
f.mu.Unlock()
|
|
|
|
|
|
|
|
// The package metadata is correct as-is, so just return it.
|
|
|
|
if !runGopackages {
|
|
|
|
return metadata, nil
|
2019-05-23 11:51:56 -06:00
|
|
|
}
|
2019-06-24 14:34:21 -06:00
|
|
|
|
2019-06-28 14:37:54 -06:00
|
|
|
// Check if the context has been canceled before calling packages.Load.
|
|
|
|
if ctx.Err() != nil {
|
2019-08-16 15:29:19 -06:00
|
|
|
return nil, ctx.Err()
|
2019-06-24 14:34:21 -06:00
|
|
|
}
|
|
|
|
|
2019-07-09 18:16:21 -06:00
|
|
|
ctx, done := trace.StartSpan(ctx, "packages.Load", telemetry.File.Of(f.filename()))
|
|
|
|
defer done()
|
2019-07-14 11:59:24 -06:00
|
|
|
pkgs, err := packages.Load(v.Config(ctx), fmt.Sprintf("file=%s", f.filename()))
|
2019-05-23 11:51:56 -06:00
|
|
|
if len(pkgs) == 0 {
|
|
|
|
if err == nil {
|
2019-08-06 13:13:11 -06:00
|
|
|
err = errors.Errorf("go/packages.Load: no packages found for %s", f.filename())
|
2019-05-29 12:59:35 -06:00
|
|
|
}
|
2019-05-23 11:51:56 -06:00
|
|
|
// Return this error as a diagnostic to the user.
|
2019-07-09 15:52:23 -06:00
|
|
|
return nil, err
|
2019-05-23 11:51:56 -06:00
|
|
|
}
|
2019-06-27 20:07:33 -06:00
|
|
|
// Track missing imports as we look at the package's errors.
|
|
|
|
missingImports := make(map[packagePath]struct{})
|
2019-07-14 12:54:41 -06:00
|
|
|
|
2019-09-05 13:56:05 -06:00
|
|
|
// Clear metadata since we are re-running go/packages.
|
|
|
|
// Reset the file's metadata and type information if we are re-running `go list`.
|
|
|
|
f.mu.Lock()
|
|
|
|
for k := range f.meta {
|
|
|
|
delete(f.meta, k)
|
|
|
|
}
|
|
|
|
for k := range f.pkgs {
|
|
|
|
delete(f.pkgs, k)
|
|
|
|
}
|
|
|
|
f.mu.Unlock()
|
|
|
|
|
2019-07-14 21:08:10 -06:00
|
|
|
log.Print(ctx, "go/packages.Load", tag.Of("packages", len(pkgs)))
|
2019-05-23 11:51:56 -06:00
|
|
|
for _, pkg := range pkgs {
|
2019-07-14 21:08:10 -06:00
|
|
|
log.Print(ctx, "go/packages.Load", tag.Of("package", pkg.PkgPath), tag.Of("files", pkg.CompiledGoFiles))
|
2019-05-23 11:51:56 -06:00
|
|
|
// Build the import graph for this package.
|
2019-07-09 15:52:23 -06:00
|
|
|
if err := v.link(ctx, &importGraph{
|
|
|
|
pkgPath: packagePath(pkg.PkgPath),
|
|
|
|
pkg: pkg,
|
|
|
|
parent: nil,
|
|
|
|
missingImports: make(map[packagePath]struct{}),
|
|
|
|
}); err != nil {
|
|
|
|
return nil, err
|
2019-06-28 14:37:54 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
m, err := validateMetadata(ctx, missingImports, f)
|
|
|
|
if err != nil {
|
2019-07-09 15:52:23 -06:00
|
|
|
return nil, err
|
2019-05-29 12:59:35 -06:00
|
|
|
}
|
2019-07-09 15:52:23 -06:00
|
|
|
return m, nil
|
2019-06-28 14:37:54 -06:00
|
|
|
}
|
|
|
|
|
2019-09-05 14:29:59 -06:00
|
|
|
func validateMetadata(ctx context.Context, missingImports map[packagePath]struct{}, f *goFile) ([]*metadata, error) {
|
2019-06-28 14:37:54 -06:00
|
|
|
f.mu.Lock()
|
|
|
|
defer f.mu.Unlock()
|
2019-06-26 12:34:36 -06:00
|
|
|
|
|
|
|
// If `go list` failed to get data for the file in question (this should never happen).
|
|
|
|
if len(f.meta) == 0 {
|
2019-08-06 13:13:11 -06:00
|
|
|
return nil, errors.Errorf("loadParseTypecheck: no metadata found for %v", f.filename())
|
2019-06-26 12:34:36 -06:00
|
|
|
}
|
|
|
|
|
2019-06-27 20:07:33 -06:00
|
|
|
// If we have already seen these missing imports before, and we have type information,
|
2019-06-26 12:34:36 -06:00
|
|
|
// there is no need to continue.
|
2019-06-27 20:07:33 -06:00
|
|
|
if sameSet(missingImports, f.missingImports) && len(f.pkgs) != 0 {
|
2019-06-28 14:37:54 -06:00
|
|
|
return nil, nil
|
2019-06-26 12:34:36 -06:00
|
|
|
}
|
2019-09-05 14:29:59 -06:00
|
|
|
|
2019-06-27 20:07:33 -06:00
|
|
|
// Otherwise, update the missing imports map.
|
|
|
|
f.missingImports = missingImports
|
2019-09-05 14:29:59 -06:00
|
|
|
|
|
|
|
return f.metadata(), nil
|
2019-05-29 12:59:35 -06:00
|
|
|
}
|
|
|
|
|
2019-07-09 15:52:23 -06:00
|
|
|
func sameSet(x, y map[packagePath]struct{}) bool {
|
|
|
|
if len(x) != len(y) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for k := range x {
|
|
|
|
if _, ok := y[k]; !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2019-09-05 14:29:59 -06:00
|
|
|
// shouldRunGopackages reparses a file's package and import declarations to
|
2019-05-23 11:51:56 -06:00
|
|
|
// determine if they have changed.
|
2019-09-05 14:29:59 -06:00
|
|
|
// It assumes that the caller holds the lock on the f.mu lock.
|
2019-09-05 15:34:00 -06:00
|
|
|
func (v *view) shouldRunGopackages(ctx context.Context, f *goFile, fh source.FileHandle) (result bool) {
|
2019-06-24 14:34:21 -06:00
|
|
|
if len(f.meta) == 0 || len(f.missingImports) > 0 {
|
2019-06-28 17:59:25 -06:00
|
|
|
return true
|
2019-05-29 12:59:35 -06:00
|
|
|
}
|
|
|
|
// Get file content in case we don't already have it.
|
2019-09-05 15:34:00 -06:00
|
|
|
parsed, err := v.session.cache.ParseGoHandle(fh, source.ParseHeader).Parse(ctx)
|
2019-07-09 11:09:04 -06:00
|
|
|
if err == context.Canceled {
|
2019-08-16 15:29:19 -06:00
|
|
|
log.Error(ctx, "parsing file header", err, tag.Of("file", f.URI()))
|
2019-07-09 11:09:04 -06:00
|
|
|
return false
|
|
|
|
}
|
2019-05-29 12:59:35 -06:00
|
|
|
if parsed == nil {
|
2019-06-28 17:59:25 -06:00
|
|
|
return true
|
2019-06-28 14:37:54 -06:00
|
|
|
}
|
|
|
|
// Check if the package's name has changed, by checking if this is a filename
|
|
|
|
// we already know about, and if so, check if its package name has changed.
|
|
|
|
for _, m := range f.meta {
|
2019-07-09 15:52:23 -06:00
|
|
|
for _, uri := range m.files {
|
|
|
|
if span.CompareURI(uri, f.URI()) == 0 {
|
2019-06-28 14:37:54 -06:00
|
|
|
if m.name != parsed.Name.Name {
|
2019-06-28 17:59:25 -06:00
|
|
|
return true
|
2019-06-28 14:37:54 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-05-29 12:59:35 -06:00
|
|
|
}
|
|
|
|
// If the package's imports have changed, re-run `go list`.
|
|
|
|
if len(f.imports) != len(parsed.Imports) {
|
2019-06-28 17:59:25 -06:00
|
|
|
return true
|
2019-05-29 12:59:35 -06:00
|
|
|
}
|
|
|
|
for i, importSpec := range f.imports {
|
2019-06-10 22:52:54 -06:00
|
|
|
if importSpec.Path.Value != parsed.Imports[i].Path.Value {
|
2019-06-28 17:59:25 -06:00
|
|
|
return true
|
2019-05-29 12:59:35 -06:00
|
|
|
}
|
|
|
|
}
|
2019-06-28 17:59:25 -06:00
|
|
|
return false
|
2019-05-29 12:59:35 -06:00
|
|
|
}
|
|
|
|
|
2019-07-09 15:52:23 -06:00
|
|
|
type importGraph struct {
|
|
|
|
pkgPath packagePath
|
|
|
|
pkg *packages.Package
|
|
|
|
parent *metadata
|
|
|
|
missingImports map[packagePath]struct{}
|
|
|
|
}
|
2019-06-11 16:07:34 -06:00
|
|
|
|
2019-07-09 15:52:23 -06:00
|
|
|
func (v *view) link(ctx context.Context, g *importGraph) error {
|
|
|
|
// Recreate the metadata rather than reusing it to avoid locking.
|
|
|
|
m := &metadata{
|
|
|
|
id: packageID(g.pkg.ID),
|
|
|
|
pkgPath: g.pkgPath,
|
|
|
|
name: g.pkg.Name,
|
|
|
|
typesSizes: g.pkg.TypesSizes,
|
|
|
|
errors: g.pkg.Errors,
|
2019-06-11 16:07:34 -06:00
|
|
|
}
|
2019-07-09 15:52:23 -06:00
|
|
|
for _, filename := range g.pkg.CompiledGoFiles {
|
|
|
|
m.files = append(m.files, span.FileURI(filename))
|
2019-06-24 14:34:21 -06:00
|
|
|
|
2019-07-09 15:52:23 -06:00
|
|
|
// Call the unlocked version of getFile since we are holding the view's mutex.
|
2019-06-28 14:37:54 -06:00
|
|
|
f, err := v.getFile(ctx, span.FileURI(filename))
|
|
|
|
if err != nil {
|
2019-07-14 21:08:10 -06:00
|
|
|
log.Error(ctx, "no file", err, telemetry.File.Of(filename))
|
2019-07-14 12:54:41 -06:00
|
|
|
continue
|
2019-06-28 14:37:54 -06:00
|
|
|
}
|
|
|
|
gof, ok := f.(*goFile)
|
|
|
|
if !ok {
|
2019-07-14 21:08:10 -06:00
|
|
|
log.Error(ctx, "not a Go file", nil, telemetry.File.Of(filename))
|
2019-07-14 12:54:41 -06:00
|
|
|
continue
|
2019-06-28 14:37:54 -06:00
|
|
|
}
|
2019-08-14 08:57:47 -06:00
|
|
|
// Cache the metadata for this file.
|
|
|
|
gof.mu.Lock()
|
2019-06-28 14:37:54 -06:00
|
|
|
if gof.meta == nil {
|
|
|
|
gof.meta = make(map[packageID]*metadata)
|
2019-05-29 12:59:35 -06:00
|
|
|
}
|
2019-06-28 14:37:54 -06:00
|
|
|
gof.meta[m.id] = m
|
2019-08-14 08:57:47 -06:00
|
|
|
gof.mu.Unlock()
|
2019-05-29 12:59:35 -06:00
|
|
|
}
|
2019-07-09 15:52:23 -06:00
|
|
|
|
|
|
|
// Preserve the import graph.
|
|
|
|
if original, ok := v.mcache.packages[m.id]; ok {
|
|
|
|
m.children = original.children
|
|
|
|
m.parents = original.parents
|
|
|
|
}
|
|
|
|
if m.children == nil {
|
|
|
|
m.children = make(map[packageID]*metadata)
|
|
|
|
}
|
|
|
|
if m.parents == nil {
|
|
|
|
m.parents = make(map[packageID]bool)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the metadata to the cache.
|
|
|
|
v.mcache.packages[m.id] = m
|
|
|
|
v.mcache.ids[g.pkgPath] = m.id
|
|
|
|
|
2019-05-29 12:59:35 -06:00
|
|
|
// Connect the import graph.
|
2019-07-09 15:52:23 -06:00
|
|
|
if g.parent != nil {
|
|
|
|
m.parents[g.parent.id] = true
|
|
|
|
g.parent.children[m.id] = m
|
2019-05-29 12:59:35 -06:00
|
|
|
}
|
2019-07-09 15:52:23 -06:00
|
|
|
for importPath, importPkg := range g.pkg.Imports {
|
2019-06-28 14:37:54 -06:00
|
|
|
importPkgPath := packagePath(importPath)
|
2019-07-09 15:52:23 -06:00
|
|
|
if importPkgPath == g.pkgPath {
|
|
|
|
return fmt.Errorf("cycle detected in %s", importPath)
|
2019-06-28 17:59:25 -06:00
|
|
|
}
|
|
|
|
// Don't remember any imports with significant errors.
|
2019-07-09 15:52:23 -06:00
|
|
|
if importPkgPath != "unsafe" && len(importPkg.CompiledGoFiles) == 0 {
|
|
|
|
g.missingImports[importPkgPath] = struct{}{}
|
2019-06-28 17:59:25 -06:00
|
|
|
continue
|
2019-06-28 14:37:54 -06:00
|
|
|
}
|
2019-06-11 16:06:27 -06:00
|
|
|
if _, ok := m.children[packageID(importPkg.ID)]; !ok {
|
2019-07-09 15:52:23 -06:00
|
|
|
if err := v.link(ctx, &importGraph{
|
|
|
|
pkgPath: importPkgPath,
|
|
|
|
pkg: importPkg,
|
|
|
|
parent: m,
|
|
|
|
missingImports: g.missingImports,
|
|
|
|
}); err != nil {
|
|
|
|
log.Error(ctx, "error in dependecny", err)
|
2019-06-28 14:37:54 -06:00
|
|
|
}
|
2019-05-29 12:59:35 -06:00
|
|
|
}
|
|
|
|
}
|
2019-07-09 15:52:23 -06:00
|
|
|
// Clear out any imports that have been removed since the package was last loaded.
|
2019-06-11 16:06:27 -06:00
|
|
|
for importID := range m.children {
|
|
|
|
child, ok := v.mcache.packages[importID]
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
importPath := string(child.pkgPath)
|
2019-07-09 15:52:23 -06:00
|
|
|
if _, ok := g.pkg.Imports[importPath]; ok {
|
2019-06-11 16:06:27 -06:00
|
|
|
continue
|
2019-05-29 12:59:35 -06:00
|
|
|
}
|
2019-06-11 16:06:27 -06:00
|
|
|
delete(m.children, importID)
|
2019-07-09 15:52:23 -06:00
|
|
|
delete(child.parents, m.id)
|
2019-05-29 12:59:35 -06:00
|
|
|
}
|
2019-06-28 14:37:54 -06:00
|
|
|
return nil
|
2019-05-29 12:59:35 -06:00
|
|
|
}
|
2019-06-11 16:07:34 -06:00
|
|
|
|
2019-07-09 15:52:23 -06:00
|
|
|
func identicalFileHandles(old, new []source.ParseGoHandle) bool {
|
|
|
|
if len(old) != len(new) {
|
2019-06-11 16:07:34 -06:00
|
|
|
return false
|
|
|
|
}
|
2019-07-09 15:52:23 -06:00
|
|
|
oldByIdentity := make(map[string]struct{}, len(old))
|
|
|
|
for _, ph := range old {
|
|
|
|
oldByIdentity[hashParseKey(ph)] = struct{}{}
|
2019-06-11 16:07:34 -06:00
|
|
|
}
|
2019-07-09 15:52:23 -06:00
|
|
|
for _, ph := range new {
|
|
|
|
if _, found := oldByIdentity[hashParseKey(ph)]; !found {
|
2019-06-11 16:07:34 -06:00
|
|
|
return false
|
|
|
|
}
|
2019-07-09 15:52:23 -06:00
|
|
|
delete(oldByIdentity, hashParseKey(ph))
|
2019-06-11 16:07:34 -06:00
|
|
|
}
|
2019-07-09 15:52:23 -06:00
|
|
|
return len(oldByIdentity) == 0
|
2019-06-11 16:07:34 -06:00
|
|
|
}
|