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-23 13:03:11 -06:00
|
|
|
package cache
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"go/ast"
|
2019-06-26 12:34:36 -06:00
|
|
|
"sync"
|
2019-05-23 13:03:11 -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-08-06 13:13:11 -06:00
|
|
|
errors "golang.org/x/xerrors"
|
2019-05-23 13:03:11 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
// goFile holds all of the information we know about a Go file.
|
|
|
|
type goFile struct {
|
|
|
|
fileBase
|
|
|
|
|
2019-06-26 12:34:36 -06:00
|
|
|
// mu protects all mutable state of the Go file,
|
|
|
|
// which can be modified during type-checking.
|
|
|
|
mu sync.Mutex
|
2019-05-23 11:51:56 -06:00
|
|
|
|
2019-06-24 14:34:21 -06:00
|
|
|
// missingImports is the set of unresolved imports for this package.
|
|
|
|
// It contains any packages with `go list` errors.
|
|
|
|
missingImports map[packagePath]struct{}
|
|
|
|
|
2019-05-23 13:03:11 -06:00
|
|
|
imports []*ast.ImportSpec
|
2019-06-26 12:34:36 -06:00
|
|
|
|
2019-09-19 14:53:53 -06:00
|
|
|
cphs map[packageKey]*checkPackageHandle
|
2019-06-26 12:34:36 -06:00
|
|
|
meta map[packageID]*metadata
|
2019-05-23 13:03:11 -06:00
|
|
|
}
|
|
|
|
|
2019-09-19 14:53:53 -06:00
|
|
|
type packageKey struct {
|
|
|
|
id packageID
|
|
|
|
mode source.ParseMode
|
|
|
|
}
|
|
|
|
|
2019-09-09 17:26:26 -06:00
|
|
|
func (f *goFile) CheckPackageHandles(ctx context.Context) ([]source.CheckPackageHandle, error) {
|
2019-07-14 21:08:10 -06:00
|
|
|
ctx = telemetry.File.With(ctx, f.URI())
|
2019-09-05 15:34:00 -06:00
|
|
|
fh := f.Handle(ctx)
|
2019-05-23 13:03:11 -06:00
|
|
|
|
2019-09-19 14:53:53 -06:00
|
|
|
if f.isDirty(ctx, fh) {
|
2019-09-05 15:34:00 -06:00
|
|
|
if err := f.view.loadParseTypecheck(ctx, f, fh); err != nil {
|
2019-07-09 15:52:23 -06:00
|
|
|
return nil, err
|
2019-05-23 13:03:11 -06:00
|
|
|
}
|
|
|
|
}
|
2019-06-28 17:59:25 -06:00
|
|
|
|
|
|
|
f.mu.Lock()
|
|
|
|
defer f.mu.Unlock()
|
|
|
|
|
2019-09-19 14:53:53 -06:00
|
|
|
var results []source.CheckPackageHandle
|
|
|
|
seenIDs := make(map[string]bool)
|
2019-09-09 17:26:26 -06:00
|
|
|
for _, cph := range f.cphs {
|
2019-09-19 14:53:53 -06:00
|
|
|
if seenIDs[cph.ID()] {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if cph.mode() < source.ParseFull {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
results = append(results, cph)
|
|
|
|
seenIDs[cph.ID()] = true
|
|
|
|
}
|
|
|
|
if len(results) == 0 {
|
|
|
|
return nil, errors.Errorf("no CheckPackageHandles for %s", f.URI())
|
2019-06-24 14:34:21 -06:00
|
|
|
}
|
2019-09-19 14:53:53 -06:00
|
|
|
return results, nil
|
2019-06-24 14:34:21 -06:00
|
|
|
}
|
|
|
|
|
2019-09-09 17:26:26 -06:00
|
|
|
func (f *goFile) GetActiveReverseDeps(ctx context.Context) (files []source.GoFile) {
|
|
|
|
seen := make(map[packageID]struct{}) // visited packages
|
|
|
|
results := make(map[*goFile]struct{})
|
2019-05-23 13:03:11 -06:00
|
|
|
|
2019-09-09 17:26:26 -06:00
|
|
|
f.view.mu.Lock()
|
|
|
|
defer f.view.mu.Unlock()
|
2019-08-06 16:51:17 -06:00
|
|
|
|
2019-09-09 17:26:26 -06:00
|
|
|
f.view.mcache.mu.Lock()
|
|
|
|
defer f.view.mcache.mu.Unlock()
|
|
|
|
|
|
|
|
for _, m := range f.metadata() {
|
|
|
|
f.view.reverseDeps(ctx, seen, results, m.id)
|
|
|
|
for f := range results {
|
|
|
|
if f == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// Don't return any of the active files in this package.
|
|
|
|
f.mu.Lock()
|
|
|
|
_, ok := f.meta[m.id]
|
|
|
|
f.mu.Unlock()
|
|
|
|
if ok {
|
|
|
|
continue
|
|
|
|
}
|
2019-08-26 22:26:45 -06:00
|
|
|
|
2019-09-09 17:26:26 -06:00
|
|
|
files = append(files, f)
|
|
|
|
}
|
2019-07-09 15:52:23 -06:00
|
|
|
}
|
2019-09-09 17:26:26 -06:00
|
|
|
return files
|
2019-07-09 15:52:23 -06:00
|
|
|
}
|
|
|
|
|
2019-09-09 17:26:26 -06:00
|
|
|
func (v *view) reverseDeps(ctx context.Context, seen map[packageID]struct{}, results map[*goFile]struct{}, id packageID) {
|
|
|
|
if _, ok := seen[id]; ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
seen[id] = struct{}{}
|
|
|
|
m, ok := v.mcache.packages[id]
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, uri := range m.files {
|
|
|
|
// Call unlocked version of getFile since we hold the lock on the view.
|
2019-09-18 23:21:54 -06:00
|
|
|
if f, err := v.getFile(ctx, uri, source.Go); err == nil && v.session.IsOpen(uri) {
|
2019-09-09 17:26:26 -06:00
|
|
|
results[f.(*goFile)] = struct{}{}
|
2019-09-06 21:58:07 -06:00
|
|
|
}
|
|
|
|
}
|
2019-09-09 17:26:26 -06:00
|
|
|
for parentID := range m.parents {
|
|
|
|
v.reverseDeps(ctx, seen, results, parentID)
|
2019-09-06 21:58:07 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-09 17:26:26 -06:00
|
|
|
// metadata assumes that the caller holds the f.mu lock.
|
|
|
|
func (f *goFile) metadata() []*metadata {
|
|
|
|
result := make([]*metadata, 0, len(f.meta))
|
|
|
|
for _, m := range f.meta {
|
|
|
|
result = append(result, m)
|
2019-08-06 16:51:17 -06:00
|
|
|
}
|
2019-09-09 17:26:26 -06:00
|
|
|
return result
|
2019-08-06 16:51:17 -06:00
|
|
|
}
|
|
|
|
|
2019-05-29 12:59:35 -06:00
|
|
|
// isDirty is true if the file needs to be type-checked.
|
|
|
|
// It assumes that the file's view's mutex is held by the caller.
|
2019-09-05 15:34:00 -06:00
|
|
|
func (f *goFile) isDirty(ctx context.Context, fh source.FileHandle) bool {
|
2019-06-26 12:34:36 -06:00
|
|
|
f.mu.Lock()
|
|
|
|
defer f.mu.Unlock()
|
|
|
|
|
2019-09-09 17:26:26 -06:00
|
|
|
if len(f.meta) == 0 || len(f.cphs) == 0 {
|
2019-06-24 14:34:21 -06:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
if len(f.missingImports) > 0 {
|
|
|
|
return true
|
|
|
|
}
|
2019-09-19 14:53:53 -06:00
|
|
|
for key, cph := range f.cphs {
|
|
|
|
if key.mode != source.ParseFull {
|
|
|
|
continue
|
|
|
|
}
|
2019-07-09 15:52:23 -06:00
|
|
|
for _, file := range cph.Files() {
|
2019-07-11 19:05:55 -06:00
|
|
|
if file.File().Identity() == fh.Identity() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
2019-05-23 11:51:56 -06:00
|
|
|
}
|