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-23 16:18:01 -06:00
|
|
|
"go/ast"
|
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-23 18:06:15 -06:00
|
|
|
func (v *view) loadParseTypecheck(ctx context.Context, f *goFile, fh source.FileHandle) ([]source.CheckPackageHandle, 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-23 18:06:15 -06:00
|
|
|
meta, err := v.load(ctx, f, fh)
|
2019-06-26 12:34:36 -06:00
|
|
|
if err != nil {
|
2019-09-23 18:06:15 -06:00
|
|
|
return nil, err
|
2019-05-29 12:59:35 -06:00
|
|
|
}
|
2019-09-23 18:19:50 -06:00
|
|
|
// If load has explicitly returns nil metadata and no error,
|
|
|
|
// it means that we should not re-typecheck the packages.
|
|
|
|
if meta == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2019-09-23 18:06:15 -06:00
|
|
|
var (
|
|
|
|
cphs []*checkPackageHandle
|
|
|
|
results []source.CheckPackageHandle
|
|
|
|
)
|
2019-09-05 12:09:42 -06:00
|
|
|
for _, m := range meta {
|
2019-06-24 14:34:21 -06:00
|
|
|
imp := &importer{
|
2019-09-23 18:06:15 -06:00
|
|
|
view: v,
|
|
|
|
config: v.Config(ctx),
|
2019-07-09 15:52:23 -06:00
|
|
|
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-23 18:06:15 -06:00
|
|
|
return nil, err
|
2019-06-24 14:34:21 -06:00
|
|
|
}
|
2019-09-23 16:18:01 -06:00
|
|
|
for _, ph := range cph.files {
|
2019-09-23 18:06:15 -06:00
|
|
|
if err := v.cachePerFile(ctx, ph); err != nil {
|
|
|
|
return nil, err
|
2019-09-23 16:18:01 -06:00
|
|
|
}
|
2019-09-05 12:09:42 -06:00
|
|
|
}
|
2019-09-23 18:06:15 -06:00
|
|
|
cphs = append(cphs, cph)
|
|
|
|
results = append(results, cph)
|
2019-06-11 09:32:51 -06:00
|
|
|
}
|
2019-09-23 18:06:15 -06:00
|
|
|
// Cache the package type information for the top-level package.
|
|
|
|
v.updatePackages(cphs)
|
|
|
|
return results, nil
|
2019-05-29 12:59:35 -06:00
|
|
|
}
|
|
|
|
|
2019-09-23 18:06:15 -06:00
|
|
|
func (v *view) cachePerFile(ctx context.Context, ph source.ParseGoHandle) error {
|
|
|
|
file, _, _, err := ph.Parse(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-09-23 16:18:01 -06:00
|
|
|
}
|
2019-09-23 18:06:15 -06:00
|
|
|
f, err := v.GetFile(ctx, ph.File().Identity().URI)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
gof, ok := f.(*goFile)
|
|
|
|
if !ok {
|
|
|
|
return errors.Errorf("%s is not a Go file", ph.File().Identity().URI)
|
|
|
|
}
|
|
|
|
gof.mu.Lock()
|
|
|
|
gof.imports = file.Imports
|
|
|
|
gof.mu.Unlock()
|
2019-09-23 16:18:01 -06:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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
|
|
|
// 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-23 18:06:15 -06:00
|
|
|
func (v *view) checkMetadata(ctx context.Context, f *goFile, fh source.FileHandle) ([]*metadata, error) {
|
|
|
|
var shouldRunGopackages bool
|
2019-09-17 09:19:11 -06:00
|
|
|
|
2019-09-23 18:06:15 -06:00
|
|
|
m := v.getMetadata(fh.Identity().URI)
|
|
|
|
if len(m) == 0 {
|
|
|
|
shouldRunGopackages = true
|
|
|
|
}
|
|
|
|
// Get file content in case we don't already have it.
|
|
|
|
parsed, _, _, err := v.session.cache.ParseGoHandle(fh, source.ParseHeader).Parse(ctx)
|
2019-09-17 09:19:11 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-09-23 18:06:15 -06:00
|
|
|
// Check if we need to re-run go/packages before loading the package.
|
|
|
|
shouldRunGopackages = shouldRunGopackages || v.shouldRunGopackages(ctx, f, parsed, m)
|
2019-09-05 14:29:59 -06:00
|
|
|
|
|
|
|
// The package metadata is correct as-is, so just return it.
|
2019-09-23 18:06:15 -06:00
|
|
|
if !shouldRunGopackages {
|
|
|
|
return m, nil
|
2019-05-23 11:51:56 -06:00
|
|
|
}
|
2019-06-24 14:34:21 -06:00
|
|
|
|
2019-09-17 09:19:11 -06:00
|
|
|
// Don't bother running go/packages if the context has been canceled.
|
2019-06-28 14:37:54 -06:00
|
|
|
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-09-17 09:19:11 -06:00
|
|
|
|
2019-07-14 11:59:24 -06:00
|
|
|
pkgs, err := packages.Load(v.Config(ctx), fmt.Sprintf("file=%s", f.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-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-09-23 18:19:50 -06:00
|
|
|
m, prevMissingImports, err := v.updateMetadata(ctx, f.URI(), pkgs)
|
|
|
|
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-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-23 18:06:15 -06:00
|
|
|
func (v *view) shouldRunGopackages(ctx context.Context, f *goFile, file *ast.File, metadata []*metadata) bool {
|
2019-09-25 10:19:15 -06:00
|
|
|
f.mu.Lock()
|
|
|
|
defer f.mu.Unlock()
|
|
|
|
|
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.
|
2019-09-23 18:06:15 -06:00
|
|
|
for _, m := range metadata {
|
2019-07-09 15:52:23 -06:00
|
|
|
for _, uri := range m.files {
|
|
|
|
if span.CompareURI(uri, f.URI()) == 0 {
|
2019-09-23 18:06:15 -06:00
|
|
|
if m.name != file.Name.Name {
|
|
|
|
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`.
|
2019-09-23 18:06:15 -06:00
|
|
|
if len(f.imports) != len(file.Imports) {
|
|
|
|
return true
|
2019-05-29 12:59:35 -06:00
|
|
|
}
|
|
|
|
for i, importSpec := range f.imports {
|
2019-09-23 18:06:15 -06:00
|
|
|
if importSpec.Path.Value != file.Imports[i].Path.Value {
|
|
|
|
return true
|
2019-05-29 12:59:35 -06:00
|
|
|
}
|
|
|
|
}
|
2019-09-23 18:06:15 -06:00
|
|
|
return false
|
2019-05-29 12:59:35 -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
|
|
|
|
}
|