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"
|
2020-01-07 19:37:41 -07:00
|
|
|
"strings"
|
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"
|
2020-01-16 12:32:14 -07:00
|
|
|
"golang.org/x/tools/internal/packagesinternal"
|
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 metadata struct {
|
2019-11-20 14:15:00 -07:00
|
|
|
id packageID
|
|
|
|
pkgPath packagePath
|
|
|
|
name string
|
2019-11-21 12:54:31 -07:00
|
|
|
goFiles []span.URI
|
2019-11-20 14:15:00 -07:00
|
|
|
compiledGoFiles []span.URI
|
2020-01-16 12:32:14 -07:00
|
|
|
forTest packagePath
|
2019-11-20 14:15:00 -07:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-01-14 22:22:42 -07:00
|
|
|
func (s *snapshot) load(ctx context.Context, scopes ...interface{}) ([]*metadata, error) {
|
2020-01-14 11:59:17 -07:00
|
|
|
var query []string
|
2020-01-14 22:22:42 -07:00
|
|
|
for _, scope := range scopes {
|
|
|
|
switch scope := scope.(type) {
|
|
|
|
case []packagePath:
|
2020-01-22 17:23:30 -07:00
|
|
|
// The only time we pass package paths is when we're doing a
|
|
|
|
// partial workspace load. In those cases, the paths came back from
|
|
|
|
// go list and should already be GOPATH-vendorized when appropriate.
|
2020-01-14 22:22:42 -07:00
|
|
|
for _, p := range scope {
|
|
|
|
query = append(query, string(p))
|
|
|
|
}
|
|
|
|
case packagePath:
|
|
|
|
query = append(query, string(scope))
|
|
|
|
case fileURI:
|
|
|
|
query = append(query, fmt.Sprintf("file=%s", span.URI(scope).Filename()))
|
|
|
|
case directoryURI:
|
|
|
|
filename := span.URI(scope).Filename()
|
|
|
|
q := fmt.Sprintf("%s/...", filename)
|
|
|
|
// Simplify the query if it will be run in the requested directory.
|
|
|
|
// This ensures compatibility with Go 1.12 that doesn't allow
|
|
|
|
// <directory>/... in GOPATH mode.
|
|
|
|
if s.view.folder.Filename() == filename {
|
|
|
|
q = "./..."
|
|
|
|
}
|
|
|
|
query = append(query, q)
|
2020-01-22 17:23:30 -07:00
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("unknown scope type %T", scope))
|
2020-01-10 15:18:59 -07:00
|
|
|
}
|
2019-11-01 14:45:32 -06:00
|
|
|
}
|
2019-12-27 14:44:33 -07:00
|
|
|
ctx, done := trace.StartSpan(ctx, "cache.view.load", telemetry.Query.Of(query))
|
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)
|
2020-01-14 11:59:17 -07:00
|
|
|
pkgs, err := packages.Load(cfg, query...)
|
2019-09-23 18:06:15 -06:00
|
|
|
|
2020-01-13 15:05:54 -07:00
|
|
|
// If the context was canceled, return early. Otherwise, we might be
|
|
|
|
// type-checking an incomplete result. Check the context directly,
|
|
|
|
// because go/packages adds extra information to the error.
|
|
|
|
if ctx.Err() != nil {
|
|
|
|
return nil, ctx.Err()
|
2019-10-16 14:31:27 -06:00
|
|
|
}
|
2019-12-27 14:44:33 -07:00
|
|
|
|
2020-01-17 13:20:54 -07:00
|
|
|
log.Print(ctx, "go/packages.Load", tag.Of("snapshot", s.ID()), tag.Of("query", query), tag.Of("packages", len(pkgs)))
|
2019-11-11 10:56:51 -07:00
|
|
|
if len(pkgs) == 0 {
|
2019-05-23 11:51:56 -06:00
|
|
|
if err == nil {
|
2019-11-30 00:01:00 -07:00
|
|
|
err = errors.Errorf("no packages found for query %s", query)
|
2019-05-29 12:59:35 -06:00
|
|
|
}
|
2019-07-09 15:52:23 -06:00
|
|
|
return nil, err
|
2019-05-23 11:51:56 -06:00
|
|
|
}
|
2020-01-14 22:22:42 -07:00
|
|
|
return s.updateMetadata(ctx, scopes, pkgs, cfg)
|
2019-09-23 18:19:50 -06:00
|
|
|
}
|
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 {
|
2020-01-11 21:59:57 -07:00
|
|
|
// TODO(rstambler): go.mod files should be tracked in the snapshot.
|
2019-09-27 11:17:59 -06:00
|
|
|
if originalFH == nil {
|
2020-01-11 21:59:57 -07:00
|
|
|
return currentFH.Identity().Kind == source.Go
|
2019-09-27 11:17:59 -06:00
|
|
|
}
|
2020-01-09 17:22:08 -07:00
|
|
|
// If the file hasn't changed, there's no need to reload.
|
|
|
|
if originalFH.Identity().String() == currentFH.Identity().String() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
// If a go.mod file's contents have changed, always invalidate metadata.
|
|
|
|
if kind := originalFH.Identity().Kind; kind == source.Mod {
|
2019-12-05 14:03:59 -07:00
|
|
|
return true
|
|
|
|
}
|
2019-10-15 16:07:52 -06:00
|
|
|
// Get the original and current parsed files in order to check package name and imports.
|
|
|
|
original, _, _, originalErr := c.ParseGoHandle(originalFH, source.ParseHeader).Parse(ctx)
|
|
|
|
current, _, _, currentErr := c.ParseGoHandle(currentFH, source.ParseHeader).Parse(ctx)
|
|
|
|
if originalErr != nil || currentErr != nil {
|
|
|
|
return (originalErr == nil) != (currentErr == nil)
|
2019-09-27 11:17:59 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
2019-11-26 15:47:09 -07:00
|
|
|
// If the package's imports have increased, definitely re-run `go list`.
|
|
|
|
if len(original.Imports) < len(current.Imports) {
|
2019-09-27 11:17:59 -06:00
|
|
|
return true
|
|
|
|
}
|
2019-11-26 15:47:09 -07:00
|
|
|
importSet := make(map[string]struct{})
|
|
|
|
for _, importSpec := range original.Imports {
|
|
|
|
importSet[importSpec.Path.Value] = struct{}{}
|
|
|
|
}
|
|
|
|
// If any of the current imports were not in the original imports.
|
|
|
|
for _, importSpec := range current.Imports {
|
|
|
|
if _, ok := importSet[importSpec.Path.Value]; !ok {
|
2019-09-27 11:17:59 -06:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-01-14 22:22:42 -07:00
|
|
|
func (s *snapshot) updateMetadata(ctx context.Context, scopes []interface{}, pkgs []*packages.Package, cfg *packages.Config) ([]*metadata, error) {
|
2019-09-27 11:17:59 -06:00
|
|
|
var results []*metadata
|
|
|
|
for _, pkg := range pkgs {
|
2020-01-14 22:22:42 -07:00
|
|
|
// Don't log output for full workspace packages.Loads.
|
|
|
|
var containsDir bool
|
|
|
|
for _, scope := range scopes {
|
|
|
|
if _, ok := scope.(directoryURI); ok {
|
|
|
|
containsDir = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !containsDir || s.view.Options().VerboseOutput {
|
2019-12-10 12:29:58 -07:00
|
|
|
log.Print(ctx, "go/packages.Load", tag.Of("package", pkg.PkgPath), tag.Of("files", pkg.CompiledGoFiles))
|
|
|
|
}
|
2020-01-07 19:37:41 -07:00
|
|
|
// golang/go#36292: Ignore packages with no sources and no errors.
|
2019-12-27 14:44:33 -07:00
|
|
|
if len(pkg.GoFiles) == 0 && len(pkg.CompiledGoFiles) == 0 && len(pkg.Errors) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
2020-01-07 19:37:41 -07:00
|
|
|
// Skip test main packages.
|
|
|
|
if s.view.isTestMain(ctx, pkg) {
|
|
|
|
continue
|
|
|
|
}
|
2019-09-27 11:17:59 -06:00
|
|
|
// Set the metadata for this package.
|
2019-10-31 16:18:00 -06:00
|
|
|
if err := s.updateImports(ctx, packagePath(pkg.PkgPath), pkg, cfg, map[packageID]struct{}{}); err != nil {
|
2019-12-03 17:56:44 -07:00
|
|
|
return nil, err
|
2019-09-27 11:17:59 -06:00
|
|
|
}
|
2020-01-07 19:37:41 -07:00
|
|
|
if m := s.getMetadata(packageID(pkg.ID)); m != nil {
|
2019-09-27 11:17:59 -06:00
|
|
|
results = append(results, m)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Rebuild the import graph when the metadata is updated.
|
|
|
|
s.clearAndRebuildImportGraph()
|
|
|
|
|
|
|
|
if len(results) == 0 {
|
2020-01-14 22:22:42 -07:00
|
|
|
return nil, errors.Errorf("no metadata for %s", scopes)
|
2019-09-27 11:17:59 -06:00
|
|
|
}
|
2019-12-03 17:56:44 -07:00
|
|
|
return results, nil
|
2019-09-27 11:17:59 -06:00
|
|
|
}
|
|
|
|
|
2019-10-31 16:18:00 -06:00
|
|
|
func (s *snapshot) updateImports(ctx context.Context, pkgPath packagePath, pkg *packages.Package, cfg *packages.Config, seen map[packageID]struct{}) error {
|
|
|
|
id := packageID(pkg.ID)
|
|
|
|
if _, ok := seen[id]; ok {
|
|
|
|
return errors.Errorf("import cycle detected: %q", id)
|
|
|
|
}
|
2019-09-27 11:17:59 -06:00
|
|
|
// Recreate the metadata rather than reusing it to avoid locking.
|
|
|
|
m := &metadata{
|
2019-10-31 16:18:00 -06:00
|
|
|
id: id,
|
2019-09-27 11:17:59 -06:00
|
|
|
pkgPath: pkgPath,
|
|
|
|
name: pkg.Name,
|
2020-01-16 12:32:14 -07:00
|
|
|
forTest: packagePath(packagesinternal.GetForTest(pkg)),
|
2019-09-27 11:17:59 -06:00
|
|
|
typesSizes: pkg.TypesSizes,
|
|
|
|
errors: pkg.Errors,
|
2019-10-01 13:21:06 -06:00
|
|
|
config: cfg,
|
2019-09-27 11:17:59 -06:00
|
|
|
}
|
2019-11-21 12:54:31 -07:00
|
|
|
|
2019-09-27 11:17:59 -06:00
|
|
|
for _, filename := range pkg.CompiledGoFiles {
|
|
|
|
uri := span.FileURI(filename)
|
2019-11-20 14:15:00 -07:00
|
|
|
m.compiledGoFiles = append(m.compiledGoFiles, uri)
|
2019-11-21 12:54:31 -07:00
|
|
|
s.addID(uri, m.id)
|
|
|
|
}
|
|
|
|
for _, filename := range pkg.GoFiles {
|
|
|
|
uri := span.FileURI(filename)
|
|
|
|
m.goFiles = append(m.goFiles, uri)
|
2019-09-27 11:17:59 -06:00
|
|
|
s.addID(uri, m.id)
|
|
|
|
}
|
|
|
|
|
2019-11-21 12:54:31 -07:00
|
|
|
seen[id] = struct{}{}
|
2019-10-31 16:18:00 -06:00
|
|
|
copied := make(map[packageID]struct{})
|
|
|
|
for k, v := range seen {
|
|
|
|
copied[k] = v
|
|
|
|
}
|
2019-09-27 11:17:59 -06:00
|
|
|
for importPath, importPkg := range pkg.Imports {
|
|
|
|
importPkgPath := packagePath(importPath)
|
|
|
|
importID := packageID(importPkg.ID)
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2020-01-07 19:37:41 -07:00
|
|
|
if s.getMetadata(importID) == nil {
|
2019-10-31 16:18:00 -06:00
|
|
|
if err := s.updateImports(ctx, importPkgPath, importPkg, cfg, copied); err != nil {
|
2019-09-27 11:17:59 -06:00
|
|
|
log.Error(ctx, "error in dependency", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-10-31 16:18:00 -06:00
|
|
|
// Add the metadata to the cache.
|
|
|
|
s.setMetadata(m)
|
2019-09-27 11:17:59 -06:00
|
|
|
return nil
|
|
|
|
}
|
2020-01-07 19:37:41 -07:00
|
|
|
|
|
|
|
func (v *view) isTestMain(ctx context.Context, pkg *packages.Package) bool {
|
|
|
|
// Test mains must have an import path that ends with ".test".
|
|
|
|
if !strings.HasSuffix(pkg.PkgPath, ".test") {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
// Test main packages are always named "main".
|
|
|
|
if pkg.Name != "main" {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
// Test mains always have exactly one GoFile that is in the build cache.
|
|
|
|
if len(pkg.GoFiles) > 1 {
|
|
|
|
return false
|
|
|
|
}
|
2020-01-11 18:29:13 -07:00
|
|
|
if !strings.HasPrefix(pkg.GoFiles[0], v.gocache) {
|
2020-01-07 19:37:41 -07:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|