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"
|
2019-07-11 19:05:55 -06:00
|
|
|
"fmt"
|
2019-05-23 13:03:11 -06:00
|
|
|
"go/ast"
|
|
|
|
"go/token"
|
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"
|
|
|
|
"golang.org/x/tools/internal/lsp/telemetry/log"
|
2019-05-23 13:03:11 -06:00
|
|
|
"golang.org/x/tools/internal/span"
|
|
|
|
)
|
|
|
|
|
|
|
|
// 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{}
|
|
|
|
|
|
|
|
// justOpened indicates that the file has just been opened.
|
|
|
|
// We re-run go/packages.Load on just opened files to make sure
|
|
|
|
// that we know about all of their packages.
|
|
|
|
justOpened bool
|
|
|
|
|
2019-05-23 13:03:11 -06:00
|
|
|
imports []*ast.ImportSpec
|
2019-06-26 12:34:36 -06:00
|
|
|
|
|
|
|
pkgs map[packageID]*pkg
|
|
|
|
meta map[packageID]*metadata
|
2019-05-23 13:03:11 -06:00
|
|
|
}
|
|
|
|
|
2019-05-23 11:51:56 -06:00
|
|
|
type astFile struct {
|
2019-06-21 15:00:02 -06:00
|
|
|
uri span.URI
|
2019-05-23 11:51:56 -06:00
|
|
|
file *ast.File
|
2019-06-07 19:57:42 -06:00
|
|
|
err error // parse errors
|
2019-06-24 14:34:21 -06:00
|
|
|
ph source.ParseGoHandle
|
2019-05-23 11:51:56 -06:00
|
|
|
isTrimmed bool
|
|
|
|
}
|
|
|
|
|
2019-07-11 19:05:55 -06:00
|
|
|
func (f *goFile) GetToken(ctx context.Context) (*token.File, error) {
|
|
|
|
file, err := f.GetAST(ctx, source.ParseFull)
|
|
|
|
if file == nil {
|
|
|
|
return nil, err
|
2019-05-23 13:03:11 -06:00
|
|
|
}
|
2019-08-02 08:46:19 -06:00
|
|
|
tok := f.view.session.cache.fset.File(file.Pos())
|
|
|
|
if tok == nil {
|
|
|
|
return nil, fmt.Errorf("no token.File for %s", f.URI())
|
|
|
|
}
|
|
|
|
return tok, nil
|
2019-05-23 13:03:11 -06:00
|
|
|
}
|
|
|
|
|
2019-07-11 19:05:55 -06:00
|
|
|
func (f *goFile) GetAST(ctx context.Context, mode source.ParseMode) (*ast.File, error) {
|
2019-05-23 13:03:11 -06:00
|
|
|
f.view.mu.Lock()
|
|
|
|
defer f.view.mu.Unlock()
|
2019-07-14 21:08:10 -06:00
|
|
|
ctx = telemetry.File.With(ctx, f.URI())
|
2019-05-23 13:03:11 -06:00
|
|
|
|
2019-07-11 19:05:55 -06:00
|
|
|
if f.isDirty(ctx) || f.wrongParseMode(ctx, mode) {
|
2019-05-29 12:59:35 -06:00
|
|
|
if _, err := f.view.loadParseTypecheck(ctx, f); err != nil {
|
2019-07-11 19:05:55 -06:00
|
|
|
return nil, fmt.Errorf("GetAST: unable to check package for %s: %v", f.URI(), err)
|
2019-05-23 13:03:11 -06:00
|
|
|
}
|
|
|
|
}
|
2019-07-11 19:05:55 -06:00
|
|
|
fh := f.Handle(ctx)
|
|
|
|
// Check for a cached AST first, in case getting a trimmed version would actually cause a re-parse.
|
|
|
|
for _, m := range []source.ParseMode{
|
|
|
|
source.ParseHeader,
|
|
|
|
source.ParseExported,
|
|
|
|
source.ParseFull,
|
|
|
|
} {
|
|
|
|
if m < mode {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if v, ok := f.view.session.cache.store.Cached(parseKey{
|
|
|
|
file: fh.Identity(),
|
|
|
|
mode: m,
|
|
|
|
}).(*parseGoData); ok {
|
|
|
|
return v.ast, v.err
|
2019-05-23 11:51:56 -06:00
|
|
|
}
|
|
|
|
}
|
2019-07-11 19:05:55 -06:00
|
|
|
ph := f.view.session.cache.ParseGoHandle(fh, mode)
|
|
|
|
return ph.Parse(ctx)
|
2019-05-23 13:03:11 -06:00
|
|
|
}
|
|
|
|
|
2019-06-24 14:34:21 -06:00
|
|
|
func (f *goFile) GetPackages(ctx context.Context) []source.Package {
|
2019-05-23 13:03:11 -06:00
|
|
|
f.view.mu.Lock()
|
|
|
|
defer f.view.mu.Unlock()
|
2019-07-14 21:08:10 -06:00
|
|
|
ctx = telemetry.File.With(ctx, f.URI())
|
2019-05-23 13:03:11 -06:00
|
|
|
|
2019-07-11 19:05:55 -06:00
|
|
|
if f.isDirty(ctx) || f.wrongParseMode(ctx, source.ParseFull) {
|
2019-05-29 12:59:35 -06:00
|
|
|
if errs, err := f.view.loadParseTypecheck(ctx, f); err != nil {
|
2019-07-14 21:08:10 -06:00
|
|
|
log.Error(ctx, "unable to check package", err, telemetry.File)
|
2019-05-23 13:03:11 -06:00
|
|
|
|
|
|
|
// Create diagnostics for errors if we are able to.
|
|
|
|
if len(errs) > 0 {
|
2019-06-24 14:34:21 -06:00
|
|
|
return []source.Package{&pkg{errors: errs}}
|
2019-05-23 13:03:11 -06:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2019-06-28 17:59:25 -06:00
|
|
|
|
|
|
|
f.mu.Lock()
|
|
|
|
defer f.mu.Unlock()
|
|
|
|
|
2019-06-24 14:34:21 -06:00
|
|
|
var pkgs []source.Package
|
|
|
|
for _, pkg := range f.pkgs {
|
|
|
|
pkgs = append(pkgs, pkg)
|
|
|
|
}
|
|
|
|
return pkgs
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *goFile) GetPackage(ctx context.Context) source.Package {
|
|
|
|
pkgs := f.GetPackages(ctx)
|
|
|
|
var result source.Package
|
|
|
|
|
|
|
|
// Pick the "narrowest" package, i.e. the package with the fewest number of files.
|
|
|
|
// This solves the problem of test variants,
|
|
|
|
// as the test will have more files than the non-test package.
|
|
|
|
for _, pkg := range pkgs {
|
|
|
|
if result == nil || len(pkg.GetFilenames()) < len(result.GetFilenames()) {
|
|
|
|
result = pkg
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result
|
2019-05-23 13:03:11 -06:00
|
|
|
}
|
|
|
|
|
2019-07-11 19:05:55 -06:00
|
|
|
func (f *goFile) wrongParseMode(ctx context.Context, mode source.ParseMode) bool {
|
|
|
|
f.mu.Lock()
|
|
|
|
defer f.mu.Unlock()
|
|
|
|
|
|
|
|
fh := f.Handle(ctx)
|
|
|
|
for _, pkg := range f.pkgs {
|
|
|
|
for _, ph := range pkg.files {
|
|
|
|
if fh.Identity() == ph.File().Identity() {
|
|
|
|
return ph.Mode() < mode
|
|
|
|
}
|
|
|
|
}
|
2019-06-05 15:44:09 -06:00
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
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-07-11 19:05:55 -06:00
|
|
|
func (f *goFile) isDirty(ctx context.Context) bool {
|
2019-06-26 12:34:36 -06:00
|
|
|
f.mu.Lock()
|
|
|
|
defer f.mu.Unlock()
|
|
|
|
|
2019-06-24 14:34:21 -06:00
|
|
|
// If the the file has just been opened,
|
|
|
|
// it may be part of more packages than we are aware of.
|
|
|
|
//
|
|
|
|
// Note: This must be the first case, otherwise we may not reset the value of f.justOpened.
|
|
|
|
if f.justOpened {
|
|
|
|
f.meta = make(map[packageID]*metadata)
|
|
|
|
f.pkgs = make(map[packageID]*pkg)
|
|
|
|
f.justOpened = false
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if len(f.meta) == 0 || len(f.pkgs) == 0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if len(f.missingImports) > 0 {
|
|
|
|
return true
|
|
|
|
}
|
2019-07-11 19:05:55 -06:00
|
|
|
fh := f.Handle(ctx)
|
|
|
|
for _, pkg := range f.pkgs {
|
|
|
|
for _, file := range pkg.files {
|
|
|
|
// There is a type-checked package for the current file handle.
|
|
|
|
if file.File().Identity() == fh.Identity() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
2019-05-23 11:51:56 -06:00
|
|
|
}
|
|
|
|
|
2019-05-23 13:03:11 -06:00
|
|
|
func (f *goFile) GetActiveReverseDeps(ctx context.Context) []source.GoFile {
|
|
|
|
pkg := f.GetPackage(ctx)
|
|
|
|
if pkg == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
f.view.mu.Lock()
|
|
|
|
defer f.view.mu.Unlock()
|
|
|
|
|
|
|
|
f.view.mcache.mu.Lock()
|
|
|
|
defer f.view.mcache.mu.Unlock()
|
|
|
|
|
2019-06-24 14:34:21 -06:00
|
|
|
id := packageID(pkg.ID())
|
|
|
|
|
2019-06-11 16:06:27 -06:00
|
|
|
seen := make(map[packageID]struct{}) // visited packages
|
2019-05-23 13:03:11 -06:00
|
|
|
results := make(map[*goFile]struct{})
|
2019-06-24 14:34:21 -06:00
|
|
|
f.view.reverseDeps(ctx, seen, results, id)
|
2019-05-23 13:03:11 -06:00
|
|
|
|
|
|
|
var files []source.GoFile
|
|
|
|
for rd := range results {
|
|
|
|
if rd == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// Don't return any of the active files in this package.
|
2019-06-24 14:34:21 -06:00
|
|
|
if _, ok := rd.pkgs[id]; ok {
|
2019-05-23 13:03:11 -06:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
files = append(files, rd)
|
|
|
|
}
|
|
|
|
return files
|
|
|
|
}
|
|
|
|
|
2019-06-11 16:06:27 -06:00
|
|
|
func (v *view) reverseDeps(ctx context.Context, seen map[packageID]struct{}, results map[*goFile]struct{}, id packageID) {
|
|
|
|
if _, ok := seen[id]; ok {
|
2019-05-23 13:03:11 -06:00
|
|
|
return
|
|
|
|
}
|
2019-06-11 16:06:27 -06:00
|
|
|
seen[id] = struct{}{}
|
|
|
|
m, ok := v.mcache.packages[id]
|
2019-05-23 13:03:11 -06:00
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, filename := range m.files {
|
|
|
|
uri := span.FileURI(filename)
|
2019-06-28 14:37:54 -06:00
|
|
|
if f, err := v.getFile(ctx, uri); err == nil && v.session.IsOpen(uri) {
|
2019-05-23 13:03:11 -06:00
|
|
|
results[f.(*goFile)] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
2019-06-11 16:06:27 -06:00
|
|
|
for parentID := range m.parents {
|
|
|
|
v.reverseDeps(ctx, seen, results, parentID)
|
2019-05-23 13:03:11 -06:00
|
|
|
}
|
|
|
|
}
|