2019-12-17 13:43:36 -07: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-09-23 18:06:15 -06:00
|
|
|
package cache
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-10-15 16:07:52 -06:00
|
|
|
"os"
|
2020-01-06 16:08:39 -07:00
|
|
|
"path/filepath"
|
2019-09-27 11:17:59 -06:00
|
|
|
"sync"
|
2019-09-23 18:06:15 -06:00
|
|
|
|
2019-10-15 11:27:09 -06:00
|
|
|
"golang.org/x/tools/go/analysis"
|
2019-09-27 11:17:59 -06:00
|
|
|
"golang.org/x/tools/internal/lsp/source"
|
2019-11-15 10:48:50 -07:00
|
|
|
"golang.org/x/tools/internal/lsp/telemetry"
|
2019-09-23 18:06:15 -06:00
|
|
|
"golang.org/x/tools/internal/span"
|
2019-11-20 15:57:05 -07:00
|
|
|
errors "golang.org/x/xerrors"
|
2019-09-23 18:06:15 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
type snapshot struct {
|
2019-09-27 11:17:59 -06:00
|
|
|
id uint64
|
|
|
|
view *view
|
2019-09-23 18:06:15 -06:00
|
|
|
|
2019-11-21 16:55:49 -07:00
|
|
|
// mu guards all of the maps in the snapshot.
|
2019-09-27 11:17:59 -06:00
|
|
|
mu sync.Mutex
|
|
|
|
|
|
|
|
// ids maps file URIs to package IDs.
|
|
|
|
// It may be invalidated on calls to go/packages.
|
|
|
|
ids map[span.URI][]packageID
|
|
|
|
|
|
|
|
// metadata maps file IDs to their associated metadata.
|
|
|
|
// It may invalidated on calls to go/packages.
|
2019-09-23 18:06:15 -06:00
|
|
|
metadata map[packageID]*metadata
|
|
|
|
|
2019-09-27 11:17:59 -06:00
|
|
|
// importedBy maps package IDs to the list of packages that import them.
|
|
|
|
importedBy map[packageID][]packageID
|
2019-09-23 18:06:15 -06:00
|
|
|
|
2019-09-27 11:17:59 -06:00
|
|
|
// files maps file URIs to their corresponding FileHandles.
|
|
|
|
// It may invalidated when a file's content changes.
|
|
|
|
files map[span.URI]source.FileHandle
|
2019-09-23 18:06:15 -06:00
|
|
|
|
2020-01-14 16:29:21 -07:00
|
|
|
// packages maps a packageKey to a set of packageHandles to which that file belongs.
|
2019-09-27 11:17:59 -06:00
|
|
|
// It may be invalidated when a file's content changes.
|
2019-11-29 23:17:57 -07:00
|
|
|
packages map[packageKey]*packageHandle
|
2019-10-14 14:13:06 -06:00
|
|
|
|
|
|
|
// actions maps an actionkey to its actionHandle.
|
|
|
|
actions map[actionKey]*actionHandle
|
2019-11-12 18:16:00 -07:00
|
|
|
|
|
|
|
// workspacePackages contains the workspace's packages, which are loaded
|
|
|
|
// when the view is created.
|
2020-01-07 19:37:41 -07:00
|
|
|
workspacePackages map[packageID]packagePath
|
2019-09-23 18:06:15 -06:00
|
|
|
}
|
|
|
|
|
2019-10-15 11:27:09 -06:00
|
|
|
type packageKey struct {
|
|
|
|
mode source.ParseMode
|
|
|
|
id packageID
|
|
|
|
}
|
|
|
|
|
|
|
|
type actionKey struct {
|
|
|
|
pkg packageKey
|
|
|
|
analyzer *analysis.Analyzer
|
|
|
|
}
|
|
|
|
|
2019-10-04 15:18:43 -06:00
|
|
|
func (s *snapshot) View() source.View {
|
|
|
|
return s.view
|
|
|
|
}
|
|
|
|
|
2019-11-29 23:17:57 -07:00
|
|
|
func (s *snapshot) PackageHandles(ctx context.Context, fh source.FileHandle) ([]source.PackageHandle, error) {
|
2019-12-10 09:51:34 -07:00
|
|
|
// If the file is a go.mod file, go.Packages.Load will always return 0 packages.
|
|
|
|
if fh.Identity().Kind == source.Mod {
|
|
|
|
return nil, errors.Errorf("attempting to get PackageHandles of .mod file %s", fh.Identity().URI)
|
|
|
|
}
|
|
|
|
|
2019-11-20 12:26:02 -07:00
|
|
|
ctx = telemetry.File.With(ctx, fh.Identity().URI)
|
2019-12-03 17:56:44 -07:00
|
|
|
meta := s.getMetadataForURI(fh.Identity().URI)
|
2019-11-20 15:57:05 -07:00
|
|
|
|
2019-12-27 14:44:33 -07:00
|
|
|
phs, err := s.packageHandles(ctx, fileURI(fh.Identity().URI), meta)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var results []source.PackageHandle
|
|
|
|
for _, ph := range phs {
|
|
|
|
results = append(results, ph)
|
|
|
|
}
|
|
|
|
return results, nil
|
|
|
|
}
|
|
|
|
|
2020-01-10 15:18:59 -07:00
|
|
|
func (s *snapshot) packageHandle(ctx context.Context, id packageID) (*packageHandle, error) {
|
|
|
|
m := s.getMetadata(id)
|
|
|
|
|
|
|
|
// Don't reload metadata in this function.
|
|
|
|
// Callers of this function must reload metadata themselves.
|
|
|
|
if m == nil {
|
|
|
|
return nil, errors.Errorf("%s has no metadata", id)
|
|
|
|
}
|
|
|
|
phs, load, check := s.shouldCheck([]*metadata{m})
|
|
|
|
if load {
|
|
|
|
return nil, errors.Errorf("%s needs loading", id)
|
|
|
|
}
|
|
|
|
if check {
|
|
|
|
return s.buildPackageHandle(ctx, m.id, source.ParseFull)
|
2019-12-27 14:44:33 -07:00
|
|
|
}
|
2020-01-06 11:14:29 -07:00
|
|
|
var result *packageHandle
|
|
|
|
for _, ph := range phs {
|
|
|
|
if ph.m.id == id {
|
|
|
|
if result != nil {
|
|
|
|
return nil, errors.Errorf("multiple package handles for the same ID: %s", id)
|
|
|
|
}
|
|
|
|
result = ph
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if result == nil {
|
|
|
|
return nil, errors.Errorf("no PackageHandle for %s", id)
|
2019-12-27 14:44:33 -07:00
|
|
|
}
|
2020-01-06 11:14:29 -07:00
|
|
|
return result, nil
|
2019-12-27 14:44:33 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *snapshot) packageHandles(ctx context.Context, scope interface{}, meta []*metadata) ([]*packageHandle, error) {
|
|
|
|
// First, determine if we need to reload or recheck the package.
|
|
|
|
phs, load, check := s.shouldCheck(meta)
|
2019-11-20 15:57:05 -07:00
|
|
|
if load {
|
2019-12-27 14:44:33 -07:00
|
|
|
newMeta, err := s.load(ctx, scope)
|
2019-11-20 15:57:05 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-12-03 17:56:44 -07:00
|
|
|
newMissing := missingImports(newMeta)
|
|
|
|
if len(newMissing) != 0 {
|
|
|
|
// Type checking a package with the same missing imports over and over
|
|
|
|
// is futile. Don't re-check unless something has changed.
|
|
|
|
check = check && !sameSet(missingImports(meta), newMissing)
|
2019-11-20 15:57:05 -07:00
|
|
|
}
|
2019-12-03 17:56:44 -07:00
|
|
|
meta = newMeta
|
2019-11-20 15:57:05 -07:00
|
|
|
}
|
2019-12-27 14:44:33 -07:00
|
|
|
var results []*packageHandle
|
2019-11-20 15:57:05 -07:00
|
|
|
if check {
|
2019-12-03 17:56:44 -07:00
|
|
|
for _, m := range meta {
|
2019-12-27 14:44:33 -07:00
|
|
|
ph, err := s.buildPackageHandle(ctx, m.id, source.ParseFull)
|
2019-11-20 15:57:05 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-11-29 23:17:57 -07:00
|
|
|
results = append(results, ph)
|
2019-11-20 15:57:05 -07:00
|
|
|
}
|
2019-12-27 14:44:33 -07:00
|
|
|
} else {
|
|
|
|
results = phs
|
2019-11-20 15:57:05 -07:00
|
|
|
}
|
2019-12-27 14:44:33 -07:00
|
|
|
if len(results) == 0 {
|
|
|
|
return nil, errors.Errorf("packageHandles: no package handles for %v", scope)
|
2019-11-20 15:57:05 -07:00
|
|
|
}
|
2019-12-27 14:44:33 -07:00
|
|
|
return results, nil
|
2019-11-20 15:57:05 -07:00
|
|
|
}
|
|
|
|
|
2019-12-03 17:56:44 -07:00
|
|
|
func missingImports(metadata []*metadata) map[packagePath]struct{} {
|
|
|
|
result := map[packagePath]struct{}{}
|
|
|
|
for _, m := range metadata {
|
|
|
|
for path := range m.missingDeps {
|
|
|
|
result[path] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-11-20 15:57:05 -07:00
|
|
|
// shouldCheck determines if the packages provided by the metadata
|
|
|
|
// need to be re-loaded or re-type-checked.
|
2019-12-27 14:44:33 -07:00
|
|
|
func (s *snapshot) shouldCheck(m []*metadata) (phs []*packageHandle, load, check bool) {
|
2019-11-20 15:57:05 -07:00
|
|
|
// No metadata. Re-load and re-check.
|
|
|
|
if len(m) == 0 {
|
|
|
|
return nil, true, true
|
|
|
|
}
|
|
|
|
// We expect to see a checked package for each package ID,
|
|
|
|
// and it should be parsed in full mode.
|
2020-01-14 16:29:21 -07:00
|
|
|
// If a single PackageHandle is missing, re-check all of them.
|
2019-11-20 15:57:05 -07:00
|
|
|
// TODO: Optimize this by only checking the necessary packages.
|
|
|
|
for _, metadata := range m {
|
2019-11-29 23:17:57 -07:00
|
|
|
ph := s.getPackage(metadata.id, source.ParseFull)
|
|
|
|
if ph == nil {
|
2019-11-20 15:57:05 -07:00
|
|
|
return nil, false, true
|
|
|
|
}
|
2019-11-29 23:17:57 -07:00
|
|
|
phs = append(phs, ph)
|
2019-11-20 15:57:05 -07:00
|
|
|
}
|
|
|
|
// If the metadata for the package had missing dependencies,
|
|
|
|
// we _may_ need to re-check. If the missing dependencies haven't changed
|
|
|
|
// since previous load, we will not check again.
|
2019-11-29 23:17:57 -07:00
|
|
|
if len(phs) < len(m) {
|
2019-11-20 15:57:05 -07:00
|
|
|
for _, m := range m {
|
|
|
|
if len(m.missingDeps) != 0 {
|
|
|
|
return nil, true, true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-11-29 23:17:57 -07:00
|
|
|
return phs, false, false
|
2019-11-20 15:57:05 -07:00
|
|
|
}
|
|
|
|
|
2020-01-10 15:18:59 -07:00
|
|
|
func (s *snapshot) GetReverseDependencies(ctx context.Context, id string) ([]source.PackageHandle, error) {
|
|
|
|
if err := s.awaitLoaded(ctx); err != nil {
|
2019-12-19 12:31:39 -07:00
|
|
|
return nil, err
|
|
|
|
}
|
2019-11-20 15:57:05 -07:00
|
|
|
ids := make(map[packageID]struct{})
|
|
|
|
s.transitiveReverseDependencies(packageID(id), ids)
|
|
|
|
|
|
|
|
// Make sure to delete the original package ID from the map.
|
|
|
|
delete(ids, packageID(id))
|
|
|
|
|
2020-01-10 15:18:59 -07:00
|
|
|
var results []source.PackageHandle
|
2019-11-20 15:57:05 -07:00
|
|
|
for id := range ids {
|
2020-01-10 15:18:59 -07:00
|
|
|
ph, err := s.packageHandle(ctx, id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
results = append(results, ph)
|
2019-11-20 15:57:05 -07:00
|
|
|
}
|
2019-12-19 12:31:39 -07:00
|
|
|
return results, nil
|
2019-11-20 15:57:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// transitiveReverseDependencies populates the uris map with file URIs
|
|
|
|
// belonging to the provided package and its transitive reverse dependencies.
|
|
|
|
func (s *snapshot) transitiveReverseDependencies(id packageID, ids map[packageID]struct{}) {
|
|
|
|
if _, ok := ids[id]; ok {
|
|
|
|
return
|
|
|
|
}
|
2019-12-19 12:31:39 -07:00
|
|
|
if s.getMetadata(id) == nil {
|
2019-11-20 15:57:05 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
ids[id] = struct{}{}
|
|
|
|
importedBy := s.getImportedBy(id)
|
|
|
|
for _, parentID := range importedBy {
|
|
|
|
s.transitiveReverseDependencies(parentID, ids)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-27 11:17:59 -06:00
|
|
|
func (s *snapshot) getImportedBy(id packageID) []packageID {
|
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
2019-12-13 15:12:11 -07:00
|
|
|
return s.getImportedByLocked(id)
|
|
|
|
}
|
2019-09-23 18:06:15 -06:00
|
|
|
|
2019-12-13 15:12:11 -07:00
|
|
|
func (s *snapshot) getImportedByLocked(id packageID) []packageID {
|
2019-09-27 11:17:59 -06:00
|
|
|
// If we haven't rebuilt the import graph since creating the snapshot.
|
|
|
|
if len(s.importedBy) == 0 {
|
|
|
|
s.rebuildImportGraph()
|
2019-09-23 18:06:15 -06:00
|
|
|
}
|
2019-09-27 11:17:59 -06:00
|
|
|
return s.importedBy[id]
|
2019-09-23 18:06:15 -06:00
|
|
|
}
|
|
|
|
|
2019-12-19 12:31:39 -07:00
|
|
|
func (s *snapshot) clearAndRebuildImportGraph() {
|
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
|
|
|
|
// Completely invalidate the original map.
|
|
|
|
s.importedBy = make(map[packageID][]packageID)
|
|
|
|
s.rebuildImportGraph()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *snapshot) rebuildImportGraph() {
|
|
|
|
for id, m := range s.metadata {
|
|
|
|
for _, importID := range m.deps {
|
|
|
|
s.importedBy[importID] = append(s.importedBy[importID], id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-29 23:17:57 -07:00
|
|
|
func (s *snapshot) addPackage(ph *packageHandle) {
|
2019-09-27 11:17:59 -06:00
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
2019-09-23 18:06:15 -06:00
|
|
|
|
2020-01-14 16:29:21 -07:00
|
|
|
// TODO: We should make sure not to compute duplicate packageHandles,
|
2019-10-01 13:21:06 -06:00
|
|
|
// and instead panic here. This will be hard to do because we may encounter
|
|
|
|
// the same package multiple times in the dependency tree.
|
2019-11-29 23:17:57 -07:00
|
|
|
if _, ok := s.packages[ph.packageKey()]; ok {
|
2019-10-01 13:21:06 -06:00
|
|
|
return
|
2019-09-27 11:17:59 -06:00
|
|
|
}
|
2019-11-29 23:17:57 -07:00
|
|
|
s.packages[ph.packageKey()] = ph
|
2019-09-23 18:06:15 -06:00
|
|
|
}
|
|
|
|
|
2020-01-10 15:18:59 -07:00
|
|
|
func (s *snapshot) workspacePackageIDs() (ids []packageID) {
|
2019-11-29 21:51:14 -07:00
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
|
|
|
|
for id := range s.workspacePackages {
|
2020-01-10 15:18:59 -07:00
|
|
|
ids = append(ids, id)
|
2019-11-29 21:51:14 -07:00
|
|
|
}
|
|
|
|
return ids
|
|
|
|
}
|
|
|
|
|
2020-01-10 15:18:59 -07:00
|
|
|
func (s *snapshot) WorkspacePackages(ctx context.Context) ([]source.PackageHandle, error) {
|
|
|
|
if err := s.awaitLoaded(ctx); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var results []source.PackageHandle
|
|
|
|
for _, pkgID := range s.workspacePackageIDs() {
|
|
|
|
ph, err := s.packageHandle(ctx, pkgID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
results = append(results, ph)
|
|
|
|
}
|
|
|
|
return results, nil
|
|
|
|
}
|
|
|
|
|
2019-12-19 12:31:39 -07:00
|
|
|
func (s *snapshot) KnownPackages(ctx context.Context) ([]source.PackageHandle, error) {
|
2020-01-10 15:18:59 -07:00
|
|
|
if err := s.awaitLoaded(ctx); err != nil {
|
2019-12-19 12:31:39 -07:00
|
|
|
return nil, err
|
|
|
|
}
|
2019-12-27 14:44:33 -07:00
|
|
|
// Collect PackageHandles for all of the workspace packages first.
|
|
|
|
// They may need to be reloaded if their metadata has been invalidated.
|
|
|
|
wsPackages := make(map[packageID]bool)
|
2019-11-11 14:51:47 -07:00
|
|
|
s.mu.Lock()
|
2019-11-12 18:16:00 -07:00
|
|
|
for id := range s.workspacePackages {
|
2019-12-27 14:44:33 -07:00
|
|
|
wsPackages[id] = true
|
2019-11-12 18:16:00 -07:00
|
|
|
}
|
|
|
|
s.mu.Unlock()
|
2019-11-11 14:51:47 -07:00
|
|
|
|
2019-12-27 14:44:33 -07:00
|
|
|
var results []source.PackageHandle
|
|
|
|
for pkgID := range wsPackages {
|
2020-01-10 15:18:59 -07:00
|
|
|
ph, err := s.packageHandle(ctx, pkgID)
|
2019-11-12 18:16:00 -07:00
|
|
|
if err != nil {
|
2020-01-07 19:37:41 -07:00
|
|
|
return nil, err
|
2019-11-12 18:16:00 -07:00
|
|
|
}
|
2019-12-27 14:44:33 -07:00
|
|
|
results = append(results, ph)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Once all workspace packages have been checked, the metadata will be up-to-date.
|
|
|
|
// Add all packages known in the workspace (that haven't already been added).
|
|
|
|
pkgIDs := make(map[packageID]bool)
|
|
|
|
s.mu.Lock()
|
|
|
|
for id := range s.metadata {
|
|
|
|
if !wsPackages[id] {
|
|
|
|
pkgIDs[id] = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s.mu.Unlock()
|
|
|
|
|
|
|
|
for pkgID := range pkgIDs {
|
|
|
|
// Metadata for these packages should already be up-to-date,
|
|
|
|
// so just build the package handle directly (without a reload).
|
|
|
|
ph, err := s.buildPackageHandle(ctx, pkgID, source.ParseExported)
|
2019-11-11 14:51:47 -07:00
|
|
|
if err != nil {
|
2020-01-14 11:59:17 -07:00
|
|
|
return nil, err
|
2019-11-11 14:51:47 -07:00
|
|
|
}
|
2019-12-27 14:44:33 -07:00
|
|
|
results = append(results, ph)
|
2019-11-11 14:51:47 -07:00
|
|
|
}
|
2019-12-19 12:31:39 -07:00
|
|
|
return results, nil
|
2019-11-11 14:51:47 -07:00
|
|
|
}
|
|
|
|
|
2020-01-10 15:18:59 -07:00
|
|
|
func (s *snapshot) CachedImportPaths(ctx context.Context) (map[string]source.Package, error) {
|
|
|
|
// Don't reload workspace package metadata.
|
|
|
|
// This function is meant to only return currently cached information.
|
|
|
|
if err := s.view.awaitInitialized(ctx); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-11-01 15:59:28 -06:00
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
|
|
|
|
results := map[string]source.Package{}
|
2019-11-29 23:17:57 -07:00
|
|
|
for _, ph := range s.packages {
|
|
|
|
cachedPkg, err := ph.cached()
|
2019-11-01 15:59:28 -06:00
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
for importPath, newPkg := range cachedPkg.imports {
|
|
|
|
if oldPkg, ok := results[string(importPath)]; ok {
|
|
|
|
// Using the same trick as NarrowestPackageHandle, prefer non-variants.
|
2019-11-20 14:15:00 -07:00
|
|
|
if len(newPkg.compiledGoFiles) < len(oldPkg.(*pkg).compiledGoFiles) {
|
2019-11-01 15:59:28 -06:00
|
|
|
results[string(importPath)] = newPkg
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
results[string(importPath)] = newPkg
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-01-10 15:18:59 -07:00
|
|
|
return results, nil
|
2019-11-01 15:59:28 -06:00
|
|
|
}
|
|
|
|
|
2019-11-29 23:17:57 -07:00
|
|
|
func (s *snapshot) getPackage(id packageID, m source.ParseMode) *packageHandle {
|
2019-10-01 13:21:06 -06:00
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
|
|
|
|
key := packageKey{
|
|
|
|
id: id,
|
|
|
|
mode: m,
|
|
|
|
}
|
|
|
|
return s.packages[key]
|
|
|
|
}
|
|
|
|
|
2019-11-21 16:55:49 -07:00
|
|
|
func (s *snapshot) getActionHandle(id packageID, m source.ParseMode, a *analysis.Analyzer) *actionHandle {
|
2019-10-14 14:13:06 -06:00
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
|
|
|
|
key := actionKey{
|
|
|
|
pkg: packageKey{
|
|
|
|
id: id,
|
|
|
|
mode: m,
|
|
|
|
},
|
2019-10-15 11:27:09 -06:00
|
|
|
analyzer: a,
|
2019-10-14 14:13:06 -06:00
|
|
|
}
|
|
|
|
return s.actions[key]
|
|
|
|
}
|
|
|
|
|
2019-11-21 16:55:49 -07:00
|
|
|
func (s *snapshot) addActionHandle(ah *actionHandle) {
|
2019-10-14 14:13:06 -06:00
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
|
|
|
|
key := actionKey{
|
2019-10-15 11:27:09 -06:00
|
|
|
analyzer: ah.analyzer,
|
2019-10-14 14:13:06 -06:00
|
|
|
pkg: packageKey{
|
|
|
|
id: ah.pkg.id,
|
|
|
|
mode: ah.pkg.mode,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if _, ok := s.actions[key]; ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
s.actions[key] = ah
|
|
|
|
}
|
|
|
|
|
2019-09-27 11:17:59 -06:00
|
|
|
func (s *snapshot) getMetadataForURI(uri span.URI) (metadata []*metadata) {
|
2019-11-01 14:45:32 -06:00
|
|
|
// TODO(matloob): uri can be a file or directory. Should we update the mappings
|
|
|
|
// to map directories to their contained packages?
|
2019-09-27 11:17:59 -06:00
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
2019-09-23 18:06:15 -06:00
|
|
|
|
2019-09-27 11:17:59 -06:00
|
|
|
for _, id := range s.ids[uri] {
|
|
|
|
if m, ok := s.metadata[id]; ok {
|
|
|
|
metadata = append(metadata, m)
|
2019-09-23 18:06:15 -06:00
|
|
|
}
|
|
|
|
}
|
2019-09-27 11:17:59 -06:00
|
|
|
return metadata
|
2019-09-23 18:06:15 -06:00
|
|
|
}
|
|
|
|
|
2019-09-27 11:17:59 -06:00
|
|
|
func (s *snapshot) setMetadata(m *metadata) {
|
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
2019-09-23 18:06:15 -06:00
|
|
|
|
2019-10-01 13:21:06 -06:00
|
|
|
// TODO: We should make sure not to set duplicate metadata,
|
|
|
|
// and instead panic here. This can be done by making sure not to
|
|
|
|
// reset metadata information for packages we've already seen.
|
|
|
|
if _, ok := s.metadata[m.id]; ok {
|
|
|
|
return
|
|
|
|
}
|
2019-09-27 11:17:59 -06:00
|
|
|
s.metadata[m.id] = m
|
2019-09-23 18:06:15 -06:00
|
|
|
}
|
|
|
|
|
2019-09-27 11:17:59 -06:00
|
|
|
func (s *snapshot) getMetadata(id packageID) *metadata {
|
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
2019-09-23 18:06:15 -06:00
|
|
|
|
2019-09-27 11:17:59 -06:00
|
|
|
return s.metadata[id]
|
|
|
|
}
|
2019-09-23 18:06:15 -06:00
|
|
|
|
2019-09-27 11:17:59 -06:00
|
|
|
func (s *snapshot) addID(uri span.URI, id packageID) {
|
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
2019-09-23 18:06:15 -06:00
|
|
|
|
2019-10-01 13:21:06 -06:00
|
|
|
for _, existingID := range s.ids[uri] {
|
|
|
|
if existingID == id {
|
|
|
|
// TODO: We should make sure not to set duplicate IDs,
|
|
|
|
// and instead panic here. This can be done by making sure not to
|
|
|
|
// reset metadata information for packages we've already seen.
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2019-09-27 11:17:59 -06:00
|
|
|
s.ids[uri] = append(s.ids[uri], id)
|
2019-09-23 18:06:15 -06:00
|
|
|
}
|
|
|
|
|
2019-09-27 11:17:59 -06:00
|
|
|
func (s *snapshot) getIDs(uri span.URI) []packageID {
|
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
|
|
|
|
return s.ids[uri]
|
|
|
|
}
|
2019-09-23 18:06:15 -06:00
|
|
|
|
2020-01-07 19:37:41 -07:00
|
|
|
func (s *snapshot) isWorkspacePackage(id packageID) (packagePath, bool) {
|
2019-12-19 12:31:39 -07:00
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
|
2020-01-07 19:37:41 -07:00
|
|
|
scope, ok := s.workspacePackages[id]
|
|
|
|
return scope, ok
|
2019-12-19 12:31:39 -07:00
|
|
|
}
|
|
|
|
|
2020-01-07 19:37:41 -07:00
|
|
|
func (s *snapshot) setWorkspacePackage(id packageID, pkgPath packagePath) {
|
2019-12-19 12:31:39 -07:00
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
|
2020-01-07 19:37:41 -07:00
|
|
|
s.workspacePackages[id] = pkgPath
|
2019-12-19 12:31:39 -07:00
|
|
|
}
|
|
|
|
|
2019-12-05 17:17:56 -07:00
|
|
|
func (s *snapshot) getFileURIs() []span.URI {
|
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
|
|
|
|
var uris []span.URI
|
|
|
|
for uri := range s.files {
|
|
|
|
uris = append(uris, uri)
|
|
|
|
}
|
|
|
|
return uris
|
|
|
|
}
|
|
|
|
|
2019-12-17 16:57:54 -07:00
|
|
|
// GetFile returns a File for the given URI. It will always succeed because it
|
|
|
|
// adds the file to the managed set if needed.
|
2020-01-10 15:37:29 -07:00
|
|
|
func (s *snapshot) GetFile(uri span.URI) (source.FileHandle, error) {
|
2020-01-21 13:36:50 -07:00
|
|
|
f, err := s.view.getFile(uri)
|
2019-12-17 16:57:54 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-01-10 15:37:29 -07:00
|
|
|
return s.getFileHandle(f), nil
|
2019-09-23 18:06:15 -06:00
|
|
|
}
|
|
|
|
|
2020-01-10 15:37:29 -07:00
|
|
|
func (s *snapshot) getFileHandle(f *fileBase) source.FileHandle {
|
2019-09-27 11:17:59 -06:00
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
|
|
|
|
if _, ok := s.files[f.URI()]; !ok {
|
2020-01-06 16:08:39 -07:00
|
|
|
s.files[f.URI()] = s.view.session.GetFile(f.URI())
|
2019-09-23 18:06:15 -06:00
|
|
|
}
|
2019-09-27 11:17:59 -06:00
|
|
|
return s.files[f.URI()]
|
2019-09-23 18:06:15 -06:00
|
|
|
}
|
|
|
|
|
2020-01-10 15:18:59 -07:00
|
|
|
func (s *snapshot) findFileHandle(f *fileBase) source.FileHandle {
|
2020-01-09 20:45:06 -07:00
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
|
|
|
|
return s.files[f.URI()]
|
|
|
|
}
|
|
|
|
|
2020-01-10 15:18:59 -07:00
|
|
|
func (s *snapshot) awaitLoaded(ctx context.Context) error {
|
|
|
|
// Do not return results until the snapshot's view has been initialized.
|
|
|
|
if err := s.view.awaitInitialized(ctx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Make sure that the workspace is in a valid state.
|
|
|
|
return s.reloadWorkspace(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
// reloadWorkspace reloads the metadata for all invalidated workspace packages.
|
|
|
|
func (s *snapshot) reloadWorkspace(ctx context.Context) error {
|
2020-01-11 21:59:57 -07:00
|
|
|
scope := s.workspaceScope(ctx)
|
|
|
|
if scope == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
_, err := s.load(ctx, scope)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *snapshot) workspaceScope(ctx context.Context) interface{} {
|
2020-01-10 15:18:59 -07:00
|
|
|
s.mu.Lock()
|
2020-01-11 21:59:57 -07:00
|
|
|
defer s.mu.Unlock()
|
|
|
|
|
|
|
|
var pkgPaths []packagePath
|
2020-01-10 15:18:59 -07:00
|
|
|
for id, pkgPath := range s.workspacePackages {
|
|
|
|
if s.metadata[id] == nil {
|
2020-01-11 21:59:57 -07:00
|
|
|
pkgPaths = append(pkgPaths, pkgPath)
|
2020-01-10 15:18:59 -07:00
|
|
|
}
|
|
|
|
}
|
2020-01-11 21:59:57 -07:00
|
|
|
switch len(pkgPaths) {
|
|
|
|
case 0:
|
2020-01-10 15:18:59 -07:00
|
|
|
return nil
|
2020-01-11 21:59:57 -07:00
|
|
|
case len(s.workspacePackages):
|
|
|
|
return directoryURI(s.view.folder)
|
|
|
|
default:
|
|
|
|
return pkgPaths
|
2020-01-10 15:18:59 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-22 17:56:26 -07:00
|
|
|
func (s *snapshot) clone(ctx context.Context, withoutURIs []span.URI) *snapshot {
|
2019-09-27 11:17:59 -06:00
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
|
2019-09-23 18:06:15 -06:00
|
|
|
result := &snapshot{
|
2019-11-12 18:16:00 -07:00
|
|
|
id: s.id + 1,
|
|
|
|
view: s.view,
|
|
|
|
ids: make(map[span.URI][]packageID),
|
|
|
|
importedBy: make(map[packageID][]packageID),
|
|
|
|
metadata: make(map[packageID]*metadata),
|
2019-11-29 23:17:57 -07:00
|
|
|
packages: make(map[packageKey]*packageHandle),
|
2019-11-12 18:16:00 -07:00
|
|
|
actions: make(map[actionKey]*actionHandle),
|
|
|
|
files: make(map[span.URI]source.FileHandle),
|
2020-01-07 19:37:41 -07:00
|
|
|
workspacePackages: make(map[packageID]packagePath),
|
2019-09-27 11:17:59 -06:00
|
|
|
}
|
2019-12-16 14:34:44 -07:00
|
|
|
|
|
|
|
// Copy all of the FileHandles.
|
2019-09-27 11:17:59 -06:00
|
|
|
for k, v := range s.files {
|
|
|
|
result.files[k] = v
|
2019-09-23 18:06:15 -06:00
|
|
|
}
|
2020-01-09 17:22:08 -07:00
|
|
|
|
2020-01-22 17:56:26 -07:00
|
|
|
// transitiveIDs keeps track of transitive reverse dependencies.
|
|
|
|
// If an ID is present in the map, invalidate its types.
|
|
|
|
// If an ID's value is true, invalidate its metadata too.
|
|
|
|
transitiveIDs := make(map[packageID]bool)
|
|
|
|
|
|
|
|
for _, withoutURI := range withoutURIs {
|
|
|
|
directIDs := map[packageID]struct{}{}
|
|
|
|
|
|
|
|
// Collect all of the package IDs that correspond to the given file.
|
|
|
|
// TODO: if the file has moved into a new package, we should invalidate that too.
|
|
|
|
for _, id := range s.ids[withoutURI] {
|
|
|
|
directIDs[id] = struct{}{}
|
|
|
|
}
|
|
|
|
// Get the current and original FileHandles for this URI.
|
|
|
|
currentFH := s.view.session.GetFile(withoutURI)
|
|
|
|
originalFH := s.files[withoutURI]
|
|
|
|
|
|
|
|
// Check if the file's package name or imports have changed,
|
|
|
|
// and if so, invalidate this file's packages' metadata.
|
|
|
|
invalidateMetadata := s.view.session.cache.shouldLoad(ctx, s, originalFH, currentFH)
|
|
|
|
|
|
|
|
// If a go.mod file's contents have changed, invalidate the metadata
|
|
|
|
// for all of the packages in the workspace.
|
|
|
|
if invalidateMetadata && currentFH.Identity().Kind == source.Mod {
|
|
|
|
for id := range s.workspacePackages {
|
|
|
|
directIDs[id] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If this is a file we don't yet know about,
|
|
|
|
// then we do not yet know what packages it should belong to.
|
|
|
|
// Make a rough estimate of what metadata to invalidate by finding the package IDs
|
|
|
|
// of all of the files in the same directory as this one.
|
|
|
|
// TODO(rstambler): Speed this up by mapping directories to filenames.
|
|
|
|
if len(directIDs) == 0 {
|
|
|
|
if dirStat, err := os.Stat(filepath.Dir(withoutURI.Filename())); err == nil {
|
|
|
|
for uri := range s.files {
|
|
|
|
if fdirStat, err := os.Stat(filepath.Dir(uri.Filename())); err == nil {
|
|
|
|
if os.SameFile(dirStat, fdirStat) {
|
|
|
|
for _, id := range s.ids[uri] {
|
|
|
|
directIDs[id] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Invalidate reverse dependencies too.
|
|
|
|
// TODO(heschi): figure out the locking model and use transitiveReverseDeps?
|
|
|
|
var addRevDeps func(packageID)
|
|
|
|
addRevDeps = func(id packageID) {
|
|
|
|
if _, seen := transitiveIDs[id]; seen {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
transitiveIDs[id] = invalidateMetadata
|
|
|
|
for _, rid := range s.getImportedByLocked(id) {
|
|
|
|
addRevDeps(rid)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for id := range directIDs {
|
|
|
|
addRevDeps(id)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle the invalidated file; it may have new contents or not exist.
|
|
|
|
if _, _, err := currentFH.Read(ctx); os.IsNotExist(err) {
|
|
|
|
delete(result.files, withoutURI)
|
|
|
|
} else {
|
|
|
|
result.files[withoutURI] = currentFH
|
|
|
|
}
|
2019-12-16 14:34:44 -07:00
|
|
|
}
|
|
|
|
|
2019-10-01 13:21:06 -06:00
|
|
|
// Collect the IDs for the packages associated with the excluded URIs.
|
|
|
|
for k, ids := range s.ids {
|
2019-09-23 18:06:15 -06:00
|
|
|
result.ids[k] = ids
|
|
|
|
}
|
2020-01-22 17:56:26 -07:00
|
|
|
// Copy the set of initally loaded packages.
|
|
|
|
for k, v := range s.workspacePackages {
|
|
|
|
result.workspacePackages[k] = v
|
|
|
|
}
|
2019-10-01 13:21:06 -06:00
|
|
|
// Copy the package type information.
|
|
|
|
for k, v := range s.packages {
|
2019-12-17 17:18:48 -07:00
|
|
|
if _, ok := transitiveIDs[k.id]; ok {
|
2019-10-01 13:21:06 -06:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
result.packages[k] = v
|
|
|
|
}
|
2019-10-14 14:13:06 -06:00
|
|
|
// Copy the package analysis information.
|
|
|
|
for k, v := range s.actions {
|
2019-12-17 17:18:48 -07:00
|
|
|
if _, ok := transitiveIDs[k.pkg.id]; ok {
|
2019-10-14 14:13:06 -06:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
result.actions[k] = v
|
|
|
|
}
|
2019-12-17 17:18:48 -07:00
|
|
|
// Copy the package metadata. We only need to invalidate packages directly
|
|
|
|
// containing the affected file, and only if it changed in a relevant way.
|
2019-09-23 18:06:15 -06:00
|
|
|
for k, v := range s.metadata {
|
2020-01-22 17:56:26 -07:00
|
|
|
if invalidateMetadata, ok := transitiveIDs[k]; invalidateMetadata && ok {
|
2019-09-23 18:06:15 -06:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
result.metadata[k] = v
|
|
|
|
}
|
2019-10-01 13:21:06 -06:00
|
|
|
// Don't bother copying the importedBy graph,
|
|
|
|
// as it changes each time we update metadata.
|
2020-01-22 17:56:26 -07:00
|
|
|
|
2019-09-23 18:06:15 -06:00
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2019-11-15 12:47:29 -07:00
|
|
|
func (s *snapshot) ID() uint64 {
|
|
|
|
return s.id
|
|
|
|
}
|