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-05-29 12:59:35 -06:00
|
|
|
"golang.org/x/tools/internal/span"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (v *view) loadParseTypecheck(ctx context.Context, f *goFile) ([]packages.Error, error) {
|
|
|
|
v.mcache.mu.Lock()
|
|
|
|
defer v.mcache.mu.Unlock()
|
|
|
|
|
2019-06-05 15:44:09 -06:00
|
|
|
// If the AST for this file is trimmed, and we are explicitly type-checking it,
|
|
|
|
// don't ignore function bodies.
|
|
|
|
if f.astIsTrimmed() {
|
2019-06-26 12:34:36 -06:00
|
|
|
v.pcache.mu.Lock()
|
2019-06-05 15:44:09 -06:00
|
|
|
f.invalidateAST()
|
2019-06-26 12:34:36 -06:00
|
|
|
v.pcache.mu.Unlock()
|
2019-05-29 12:59:35 -06:00
|
|
|
}
|
2019-06-24 14:34:21 -06:00
|
|
|
|
2019-06-26 12:34:36 -06:00
|
|
|
// Get the metadata for the file.
|
|
|
|
meta, errs, err := v.checkMetadata(ctx, f)
|
|
|
|
if err != nil {
|
2019-05-29 12:59:35 -06:00
|
|
|
return errs, err
|
|
|
|
}
|
2019-06-26 12:34:36 -06:00
|
|
|
if meta == nil {
|
2019-06-04 16:06:55 -06:00
|
|
|
return nil, nil
|
|
|
|
}
|
2019-06-26 12:34:36 -06:00
|
|
|
for id, m := range meta {
|
2019-06-24 14:34:21 -06:00
|
|
|
imp := &importer{
|
|
|
|
view: v,
|
|
|
|
seen: make(map[packageID]struct{}),
|
|
|
|
ctx: ctx,
|
2019-06-26 12:34:36 -06:00
|
|
|
fset: v.session.cache.FileSet(),
|
|
|
|
topLevelPkgID: id,
|
2019-06-24 14:34:21 -06:00
|
|
|
}
|
|
|
|
// Start prefetching direct imports.
|
2019-06-26 12:34:36 -06:00
|
|
|
for importID := range m.children {
|
2019-06-24 14:34:21 -06:00
|
|
|
go imp.getPkg(importID)
|
|
|
|
}
|
|
|
|
// Type-check package.
|
2019-06-26 12:34:36 -06:00
|
|
|
pkg, err := imp.getPkg(imp.topLevelPkgID)
|
2019-06-24 14:34:21 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if pkg == nil || pkg.IsIllTyped() {
|
2019-06-26 12:34:36 -06:00
|
|
|
return nil, fmt.Errorf("loadParseTypecheck: %s is ill typed", m.pkgPath)
|
2019-06-24 14:34:21 -06:00
|
|
|
}
|
2019-06-11 09:32:51 -06:00
|
|
|
}
|
2019-06-24 14:34:21 -06:00
|
|
|
if len(f.pkgs) == 0 {
|
2019-06-26 12:34:36 -06:00
|
|
|
return nil, fmt.Errorf("no packages found for %s", f.URI())
|
2019-05-29 12:59:35 -06:00
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2019-06-11 15:09:26 -06:00
|
|
|
func sameSet(x, y map[packagePath]struct{}) bool {
|
2019-06-04 16:06:55 -06:00
|
|
|
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
|
|
|
// checkMetadata determines if we should run go/packages.Load for this file.
|
|
|
|
// If yes, update the metadata for the file and its package.
|
2019-06-26 12:34:36 -06:00
|
|
|
func (v *view) checkMetadata(ctx context.Context, f *goFile) (map[packageID]*metadata, []packages.Error, error) {
|
|
|
|
f.mu.Lock()
|
|
|
|
defer f.mu.Unlock()
|
|
|
|
|
|
|
|
// Save the metadata's current missing imports, if any.
|
|
|
|
originalMissingImports := f.missingImports
|
|
|
|
|
2019-06-04 16:06:55 -06:00
|
|
|
if !v.parseImports(ctx, f) {
|
2019-06-26 12:34:36 -06:00
|
|
|
return f.meta, nil, nil
|
2019-05-23 11:51:56 -06:00
|
|
|
}
|
2019-06-24 14:34:21 -06:00
|
|
|
|
|
|
|
// 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-05-23 11:51:56 -06:00
|
|
|
pkgs, err := packages.Load(v.buildConfig(), fmt.Sprintf("file=%s", f.filename()))
|
|
|
|
if len(pkgs) == 0 {
|
|
|
|
if err == nil {
|
2019-06-14 12:23:47 -06:00
|
|
|
err = fmt.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-06-26 12:34:36 -06:00
|
|
|
return nil, []packages.Error{
|
2019-05-23 11:51:56 -06:00
|
|
|
{
|
|
|
|
Msg: err.Error(),
|
2019-06-14 12:23:47 -06:00
|
|
|
Kind: packages.UnknownError,
|
2019-05-23 11:51:56 -06:00
|
|
|
},
|
|
|
|
}, err
|
|
|
|
}
|
2019-06-24 14:34:21 -06:00
|
|
|
|
|
|
|
// Clear missing imports.
|
|
|
|
for k := range f.missingImports {
|
|
|
|
delete(f.missingImports, k)
|
|
|
|
}
|
2019-05-23 11:51:56 -06:00
|
|
|
for _, pkg := range pkgs {
|
|
|
|
// If the package comes back with errors from `go list`,
|
|
|
|
// don't bother type-checking it.
|
|
|
|
if len(pkg.Errors) > 0 {
|
2019-06-26 12:34:36 -06:00
|
|
|
return nil, pkg.Errors, fmt.Errorf("package %s has errors, skipping type-checking", pkg.PkgPath)
|
2019-05-29 12:59:35 -06:00
|
|
|
}
|
2019-06-24 14:34:21 -06:00
|
|
|
for importPath, importPkg := range pkg.Imports {
|
|
|
|
if len(importPkg.Errors) > 0 {
|
|
|
|
if f.missingImports == nil {
|
|
|
|
f.missingImports = make(map[packagePath]struct{})
|
|
|
|
}
|
|
|
|
f.missingImports[packagePath(importPath)] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
2019-05-23 11:51:56 -06:00
|
|
|
// Build the import graph for this package.
|
2019-06-11 15:09:26 -06:00
|
|
|
v.link(ctx, packagePath(pkg.PkgPath), pkg, nil)
|
2019-05-29 12:59:35 -06:00
|
|
|
}
|
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 {
|
|
|
|
return nil, nil, fmt.Errorf("loadParseTypecheck: no metadata found for %v", f.filename())
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we have already seen these missing imports before, and we still have type information,
|
|
|
|
// there is no need to continue.
|
|
|
|
if sameSet(originalMissingImports, f.missingImports) && len(f.pkgs) != 0 {
|
|
|
|
return nil, nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return f.meta, nil, nil
|
2019-05-29 12:59:35 -06:00
|
|
|
}
|
|
|
|
|
2019-05-23 11:51:56 -06:00
|
|
|
// reparseImports reparses a file's package and import declarations to
|
|
|
|
// determine if they have changed.
|
2019-06-04 16:06:55 -06:00
|
|
|
func (v *view) parseImports(ctx context.Context, f *goFile) bool {
|
2019-06-24 14:34:21 -06:00
|
|
|
if len(f.meta) == 0 || len(f.missingImports) > 0 {
|
2019-05-29 12:59:35 -06:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
// Get file content in case we don't already have it.
|
2019-06-13 13:55:53 -06:00
|
|
|
parsed, _ := v.session.cache.ParseGoHandle(f.Handle(ctx), source.ParseHeader).Parse(ctx)
|
2019-05-29 12:59:35 -06:00
|
|
|
if parsed == nil {
|
|
|
|
return true
|
|
|
|
}
|
2019-06-24 14:34:21 -06:00
|
|
|
// TODO: Add support for re-running `go list` when the package name changes.
|
2019-06-04 16:06:55 -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) {
|
|
|
|
return true
|
|
|
|
}
|
2019-06-26 12:34:36 -06:00
|
|
|
|
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-05-29 12:59:35 -06:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-06-11 15:09:26 -06:00
|
|
|
func (v *view) link(ctx context.Context, pkgPath packagePath, pkg *packages.Package, parent *metadata) *metadata {
|
2019-06-11 16:06:27 -06:00
|
|
|
id := packageID(pkg.ID)
|
|
|
|
m, ok := v.mcache.packages[id]
|
2019-06-11 16:07:34 -06:00
|
|
|
|
|
|
|
// If a file was added or deleted we need to invalidate the package cache
|
2019-06-24 14:34:21 -06:00
|
|
|
// so relevant packages get parsed and type-checked again.
|
2019-06-11 16:07:34 -06:00
|
|
|
if ok && !filenamesIdentical(m.files, pkg.CompiledGoFiles) {
|
2019-06-26 12:34:36 -06:00
|
|
|
v.pcache.mu.Lock()
|
|
|
|
v.remove(id, make(map[packageID]struct{}))
|
|
|
|
v.pcache.mu.Unlock()
|
2019-06-11 16:07:34 -06:00
|
|
|
}
|
2019-06-24 14:34:21 -06:00
|
|
|
|
2019-06-11 16:06:27 -06:00
|
|
|
// If we haven't seen this package before.
|
2019-05-29 12:59:35 -06:00
|
|
|
if !ok {
|
|
|
|
m = &metadata{
|
2019-06-14 12:10:39 -06:00
|
|
|
pkgPath: pkgPath,
|
|
|
|
id: id,
|
|
|
|
typesSizes: pkg.TypesSizes,
|
|
|
|
parents: make(map[packageID]bool),
|
|
|
|
children: make(map[packageID]bool),
|
2019-05-29 12:59:35 -06:00
|
|
|
}
|
2019-06-11 16:06:27 -06:00
|
|
|
v.mcache.packages[id] = m
|
|
|
|
v.mcache.ids[pkgPath] = id
|
2019-05-29 12:59:35 -06:00
|
|
|
}
|
|
|
|
// Reset any field that could have changed across calls to packages.Load.
|
|
|
|
m.name = pkg.Name
|
|
|
|
m.files = pkg.CompiledGoFiles
|
2019-06-24 14:34:21 -06:00
|
|
|
for _, filename := range m.files {
|
2019-05-29 12:59:35 -06:00
|
|
|
if f, _ := v.getFile(span.FileURI(filename)); f != nil {
|
2019-05-23 11:51:56 -06:00
|
|
|
if gof, ok := f.(*goFile); ok {
|
2019-06-24 14:34:21 -06:00
|
|
|
if gof.meta == nil {
|
|
|
|
gof.meta = make(map[packageID]*metadata)
|
|
|
|
}
|
|
|
|
gof.meta[m.id] = m
|
2019-05-23 11:51:56 -06:00
|
|
|
} else {
|
|
|
|
v.Session().Logger().Errorf(ctx, "not a Go file: %s", f.URI())
|
2019-05-29 12:59:35 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Connect the import graph.
|
|
|
|
if parent != nil {
|
2019-06-11 16:06:27 -06:00
|
|
|
m.parents[parent.id] = true
|
|
|
|
parent.children[id] = true
|
2019-05-29 12:59:35 -06:00
|
|
|
}
|
|
|
|
for importPath, importPkg := range pkg.Imports {
|
2019-06-11 16:06:27 -06:00
|
|
|
if _, ok := m.children[packageID(importPkg.ID)]; !ok {
|
|
|
|
v.link(ctx, packagePath(importPath), importPkg, m)
|
2019-05-29 12:59:35 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Clear out any imports that have been removed.
|
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)
|
|
|
|
if _, ok := pkg.Imports[importPath]; ok {
|
|
|
|
continue
|
2019-05-29 12:59:35 -06:00
|
|
|
}
|
2019-06-11 16:06:27 -06:00
|
|
|
delete(m.children, importID)
|
|
|
|
delete(child.parents, id)
|
2019-05-29 12:59:35 -06:00
|
|
|
}
|
|
|
|
return m
|
|
|
|
}
|
2019-06-11 16:07:34 -06:00
|
|
|
|
|
|
|
// filenamesIdentical reports whether two sets of file names are identical.
|
|
|
|
func filenamesIdentical(oldFiles, newFiles []string) bool {
|
|
|
|
if len(oldFiles) != len(newFiles) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
oldByName := make(map[string]struct{}, len(oldFiles))
|
|
|
|
for _, filename := range oldFiles {
|
|
|
|
oldByName[filename] = struct{}{}
|
|
|
|
}
|
|
|
|
for _, newFilename := range newFiles {
|
|
|
|
if _, found := oldByName[newFilename]; !found {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
delete(oldByName, newFilename)
|
|
|
|
}
|
|
|
|
return len(oldByName) == 0
|
|
|
|
}
|