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"
|
2019-09-27 11:17:59 -06:00
|
|
|
"go/types"
|
2019-05-29 12:59:35 -06:00
|
|
|
|
|
|
|
"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-27 11:17:59 -06:00
|
|
|
type packageKey struct {
|
|
|
|
mode source.ParseMode
|
|
|
|
id packageID
|
2019-09-23 16:18:01 -06:00
|
|
|
}
|
|
|
|
|
2019-09-27 11:17:59 -06:00
|
|
|
type metadata struct {
|
|
|
|
id packageID
|
|
|
|
pkgPath packagePath
|
|
|
|
name string
|
|
|
|
files []span.URI
|
|
|
|
typesSizes types.Sizes
|
|
|
|
errors []packages.Error
|
|
|
|
deps []packageID
|
|
|
|
missingDeps map[packagePath]struct{}
|
2019-10-01 13:21:06 -06:00
|
|
|
|
|
|
|
// config is the *packages.Config associated with the loaded package.
|
|
|
|
config *packages.Config
|
2019-06-04 16:06:55 -06:00
|
|
|
}
|
|
|
|
|
2019-10-01 13:21:06 -06:00
|
|
|
func (s *snapshot) load(ctx context.Context, uri span.URI) ([]*metadata, error) {
|
2019-09-27 11:17:59 -06:00
|
|
|
ctx, done := trace.StartSpan(ctx, "cache.view.load", telemetry.URI.Of(uri))
|
2019-07-09 18:16:21 -06:00
|
|
|
defer done()
|
2019-09-17 09:19:11 -06:00
|
|
|
|
2019-10-01 13:21:06 -06:00
|
|
|
cfg := s.view.Config(ctx)
|
2019-09-27 11:17:59 -06:00
|
|
|
pkgs, err := packages.Load(cfg, fmt.Sprintf("file=%s", uri.Filename()))
|
2019-09-23 18:06:15 -06:00
|
|
|
log.Print(ctx, "go/packages.Load", tag.Of("packages", len(pkgs)))
|
|
|
|
|
2019-05-23 11:51:56 -06:00
|
|
|
if len(pkgs) == 0 {
|
|
|
|
if err == nil {
|
2019-09-27 11:17:59 -06:00
|
|
|
err = errors.Errorf("go/packages.Load: no packages found for %s", uri)
|
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-10-01 13:21:06 -06:00
|
|
|
m, prevMissingImports, err := s.updateMetadata(ctx, uri, pkgs, cfg)
|
2019-09-23 18:19:50 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
meta, err := validateMetadata(ctx, m, prevMissingImports)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return meta, nil
|
2019-07-09 15:52:23 -06:00
|
|
|
}
|
|
|
|
|
2019-09-23 18:19:50 -06:00
|
|
|
func validateMetadata(ctx context.Context, metadata []*metadata, prevMissingImports map[packageID]map[packagePath]struct{}) ([]*metadata, error) {
|
|
|
|
// If we saw incorrect metadata for this package previously, don't both rechecking it.
|
|
|
|
for _, m := range metadata {
|
|
|
|
if len(m.missingDeps) > 0 {
|
|
|
|
prev, ok := prevMissingImports[m.id]
|
|
|
|
// There are missing imports that we previously hadn't seen before.
|
|
|
|
if !ok {
|
|
|
|
return metadata, nil
|
|
|
|
}
|
|
|
|
// The set of missing imports has changed.
|
|
|
|
if !sameSet(prev, m.missingDeps) {
|
|
|
|
return metadata, nil
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// There are no missing imports.
|
|
|
|
return metadata, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
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-27 11:17:59 -06:00
|
|
|
|
|
|
|
// shouldLoad reparses a file's package and import declarations to
|
|
|
|
// determine if they have changed.
|
|
|
|
func (c *cache) shouldLoad(ctx context.Context, s *snapshot, originalFH, currentFH source.FileHandle) bool {
|
|
|
|
if originalFH == nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the original parsed file in order to check package name and imports.
|
|
|
|
original, _, _, err := c.ParseGoHandle(originalFH, source.ParseHeader).Parse(ctx)
|
|
|
|
if err != nil {
|
2019-10-01 13:21:06 -06:00
|
|
|
log.Error(ctx, "no ParseGoHandle for original FileHandle", err, telemetry.URI.Of(originalFH.Identity().URI))
|
2019-09-27 11:17:59 -06:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the current parsed file in order to check package name and imports.
|
|
|
|
current, _, _, err := c.ParseGoHandle(currentFH, source.ParseHeader).Parse(ctx)
|
|
|
|
if err != nil {
|
2019-10-01 13:21:06 -06:00
|
|
|
log.Error(ctx, "no ParseGoHandle for original FileHandle", err, telemetry.URI.Of(currentFH.Identity().URI))
|
2019-09-27 11:17:59 -06:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the package's metadata has changed. The cases handled are:
|
|
|
|
//
|
|
|
|
// 1. A package's name has changed
|
|
|
|
// 2. A file's imports have changed
|
|
|
|
//
|
|
|
|
if original.Name.Name != current.Name.Name {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
// If the package's imports have changed, re-run `go list`.
|
|
|
|
if len(original.Imports) != len(current.Imports) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
for i, importSpec := range original.Imports {
|
|
|
|
// TODO: Handle the case where the imports have just been re-ordered.
|
|
|
|
if importSpec.Path.Value != current.Imports[i].Path.Value {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-10-01 13:21:06 -06:00
|
|
|
func (s *snapshot) updateMetadata(ctx context.Context, uri span.URI, pkgs []*packages.Package, cfg *packages.Config) ([]*metadata, map[packageID]map[packagePath]struct{}, error) {
|
2019-09-27 11:17:59 -06:00
|
|
|
// Clear metadata since we are re-running go/packages.
|
|
|
|
prevMissingImports := make(map[packageID]map[packagePath]struct{})
|
|
|
|
m := s.getMetadataForURI(uri)
|
|
|
|
|
|
|
|
for _, m := range m {
|
|
|
|
if len(m.missingDeps) > 0 {
|
|
|
|
prevMissingImports[m.id] = m.missingDeps
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var results []*metadata
|
|
|
|
for _, pkg := range pkgs {
|
|
|
|
log.Print(ctx, "go/packages.Load", tag.Of("package", pkg.PkgPath), tag.Of("files", pkg.CompiledGoFiles))
|
|
|
|
|
|
|
|
// Set the metadata for this package.
|
2019-10-01 13:21:06 -06:00
|
|
|
if err := s.updateImports(ctx, packagePath(pkg.PkgPath), pkg, cfg); err != nil {
|
2019-09-27 11:17:59 -06:00
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
m := s.getMetadata(packageID(pkg.ID))
|
|
|
|
if m != nil {
|
|
|
|
results = append(results, m)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Rebuild the import graph when the metadata is updated.
|
|
|
|
s.clearAndRebuildImportGraph()
|
|
|
|
|
|
|
|
if len(results) == 0 {
|
|
|
|
return nil, nil, errors.Errorf("no metadata for %s", uri)
|
|
|
|
}
|
|
|
|
return results, prevMissingImports, nil
|
|
|
|
}
|
|
|
|
|
2019-10-01 13:21:06 -06:00
|
|
|
func (s *snapshot) updateImports(ctx context.Context, pkgPath packagePath, pkg *packages.Package, cfg *packages.Config) error {
|
2019-09-27 11:17:59 -06:00
|
|
|
// Recreate the metadata rather than reusing it to avoid locking.
|
|
|
|
m := &metadata{
|
|
|
|
id: packageID(pkg.ID),
|
|
|
|
pkgPath: pkgPath,
|
|
|
|
name: pkg.Name,
|
|
|
|
typesSizes: pkg.TypesSizes,
|
|
|
|
errors: pkg.Errors,
|
2019-10-01 13:21:06 -06:00
|
|
|
config: cfg,
|
2019-09-27 11:17:59 -06:00
|
|
|
}
|
|
|
|
for _, filename := range pkg.CompiledGoFiles {
|
|
|
|
uri := span.FileURI(filename)
|
|
|
|
m.files = append(m.files, uri)
|
|
|
|
|
|
|
|
s.addID(uri, m.id)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the metadata to the cache.
|
|
|
|
s.setMetadata(m)
|
|
|
|
|
|
|
|
for importPath, importPkg := range pkg.Imports {
|
|
|
|
importPkgPath := packagePath(importPath)
|
|
|
|
importID := packageID(importPkg.ID)
|
|
|
|
|
|
|
|
if importPkgPath == pkgPath {
|
|
|
|
return errors.Errorf("cycle detected in %s", importPath)
|
|
|
|
}
|
|
|
|
m.deps = append(m.deps, importID)
|
|
|
|
|
|
|
|
// Don't remember any imports with significant errors.
|
|
|
|
if importPkgPath != "unsafe" && len(importPkg.CompiledGoFiles) == 0 {
|
|
|
|
if m.missingDeps == nil {
|
|
|
|
m.missingDeps = make(map[packagePath]struct{})
|
|
|
|
}
|
|
|
|
m.missingDeps[importPkgPath] = struct{}{}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
dep := s.getMetadata(importID)
|
|
|
|
if dep == nil {
|
2019-10-01 13:21:06 -06:00
|
|
|
if err := s.updateImports(ctx, importPkgPath, importPkg, cfg); err != nil {
|
2019-09-27 11:17:59 -06:00
|
|
|
log.Error(ctx, "error in dependency", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|