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"
|
|
|
|
"golang.org/x/tools/internal/lsp/telemetry/log"
|
|
|
|
"golang.org/x/tools/internal/lsp/telemetry/tag"
|
2019-07-09 18:16:21 -06:00
|
|
|
"golang.org/x/tools/internal/lsp/telemetry/trace"
|
2019-05-29 12:59:35 -06:00
|
|
|
"golang.org/x/tools/internal/span"
|
2019-08-06 13:13:11 -06:00
|
|
|
errors "golang.org/x/xerrors"
|
2019-05-29 12:59:35 -06:00
|
|
|
)
|
|
|
|
|
2019-07-09 15:52:23 -06:00
|
|
|
func (view *view) loadParseTypecheck(ctx context.Context, f *goFile) error {
|
|
|
|
pkgs, err := view.load(ctx, f)
|
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-07-09 15:52:23 -06:00
|
|
|
for _, m := range pkgs {
|
2019-06-24 14:34:21 -06:00
|
|
|
imp := &importer{
|
2019-07-09 15:52:23 -06:00
|
|
|
view: view,
|
|
|
|
ctx: ctx,
|
|
|
|
config: view.Config(ctx),
|
|
|
|
seen: make(map[packageID]struct{}),
|
|
|
|
topLevelPackageID: m.id,
|
2019-06-24 14:34:21 -06:00
|
|
|
}
|
2019-07-09 15:52:23 -06:00
|
|
|
cph, err := imp.checkPackageHandle(m)
|
2019-06-24 14:34:21 -06:00
|
|
|
if err != nil {
|
2019-07-09 15:52:23 -06:00
|
|
|
log.Error(ctx, "failed to get CheckPackgeHandle", err)
|
|
|
|
continue
|
2019-06-24 14:34:21 -06:00
|
|
|
}
|
2019-07-09 15:52:23 -06:00
|
|
|
pkg, err := cph.check(ctx)
|
|
|
|
if err != nil {
|
|
|
|
log.Error(ctx, "failed to check package", err)
|
|
|
|
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.
|
|
|
|
imp.cachePackage(cph, pkg, m)
|
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-07-09 15:52:23 -06:00
|
|
|
func (view *view) load(ctx context.Context, f *goFile) (map[packageID]*metadata, error) {
|
|
|
|
view.mu.Lock()
|
|
|
|
defer view.mu.Unlock()
|
|
|
|
|
|
|
|
view.mcache.mu.Lock()
|
|
|
|
defer view.mcache.mu.Unlock()
|
|
|
|
|
|
|
|
// If the AST for this file is trimmed, and we are explicitly type-checking it,
|
|
|
|
// don't ignore function bodies.
|
|
|
|
if f.wrongParseMode(ctx, source.ParseFull) {
|
|
|
|
f.invalidateAST(ctx)
|
2019-06-04 16:06:55 -06:00
|
|
|
}
|
2019-07-09 15:52:23 -06:00
|
|
|
|
|
|
|
// Get the metadata for the file.
|
|
|
|
meta, err := view.checkMetadata(ctx, f)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2019-06-04 16:06:55 -06:00
|
|
|
}
|
2019-07-09 15:52:23 -06:00
|
|
|
if len(f.meta) == 0 {
|
|
|
|
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-07-09 15:52:23 -06:00
|
|
|
func (v *view) checkMetadata(ctx context.Context, f *goFile) (map[packageID]*metadata, error) {
|
|
|
|
if !v.shouldRunGopackages(ctx, f) {
|
|
|
|
return f.meta, 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-07-09 15:52:23 -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-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
|
|
|
}
|
|
|
|
|
|
|
|
func validateMetadata(ctx context.Context, missingImports map[packagePath]struct{}, f *goFile) (map[packageID]*metadata, error) {
|
|
|
|
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-06-27 20:07:33 -06:00
|
|
|
// Otherwise, update the missing imports map.
|
|
|
|
f.missingImports = missingImports
|
2019-06-28 14:37:54 -06:00
|
|
|
return f.meta, 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-05-23 11:51:56 -06:00
|
|
|
// reparseImports reparses a file's package and import declarations to
|
|
|
|
// determine if they have changed.
|
2019-07-09 15:52:23 -06:00
|
|
|
func (v *view) shouldRunGopackages(ctx context.Context, f *goFile) (result bool) {
|
2019-06-28 14:37:54 -06:00
|
|
|
f.mu.Lock()
|
|
|
|
defer func() {
|
|
|
|
// Clear metadata if we are intending to re-run go/packages.
|
|
|
|
if result {
|
|
|
|
// Reset the file's metadata and type information if we are re-running `go list`.
|
|
|
|
for k := range f.meta {
|
|
|
|
delete(f.meta, k)
|
|
|
|
}
|
|
|
|
for k := range f.pkgs {
|
|
|
|
delete(f.pkgs, k)
|
|
|
|
}
|
|
|
|
}
|
2019-07-09 11:09:04 -06:00
|
|
|
f.mu.Unlock()
|
2019-06-28 14:37:54 -06:00
|
|
|
}()
|
|
|
|
|
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-07-09 11:09:04 -06:00
|
|
|
parsed, err := v.session.cache.ParseGoHandle(f.Handle(ctx), source.ParseHeader).Parse(ctx)
|
|
|
|
if err == context.Canceled {
|
|
|
|
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
|
|
|
}
|