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"
|
2020-01-30 18:52:23 -07:00
|
|
|
"fmt"
|
2020-02-06 15:49:19 -07:00
|
|
|
"go/ast"
|
|
|
|
"go/token"
|
2019-10-15 16:07:52 -06:00
|
|
|
"os"
|
2020-01-06 16:08:39 -07:00
|
|
|
"path/filepath"
|
2020-02-14 09:33:11 -07:00
|
|
|
"sort"
|
|
|
|
"strings"
|
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"
|
2020-02-06 15:49:19 -07:00
|
|
|
"golang.org/x/tools/go/packages"
|
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"
|
2020-02-06 15:49:19 -07:00
|
|
|
"golang.org/x/tools/internal/telemetry/log"
|
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
|
2020-01-23 17:24:51 -07:00
|
|
|
|
|
|
|
// unloadableFiles keeps track of files that we've failed to load.
|
|
|
|
unloadableFiles map[span.URI]struct{}
|
2020-02-18 13:47:38 -07:00
|
|
|
|
|
|
|
// modHandles keeps track of any ParseModHandles for this snapshot.
|
|
|
|
modHandles map[span.URI]*modHandle
|
2020-02-14 09:33:11 -07:00
|
|
|
|
|
|
|
// modTidyHandle is the saved modTidyHandle for this snapshot, it is attached to the
|
|
|
|
// snapshot so we can reuse it without having to call "go mod tidy" everytime.
|
|
|
|
modTidyHandle *modHandle
|
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
|
|
|
|
}
|
|
|
|
|
2020-01-28 22:12:28 -07:00
|
|
|
func (s *snapshot) ID() uint64 {
|
|
|
|
return s.id
|
|
|
|
}
|
|
|
|
|
2019-10-04 15:18:43 -06:00
|
|
|
func (s *snapshot) View() source.View {
|
|
|
|
return s.view
|
|
|
|
}
|
|
|
|
|
2020-02-06 15:49:19 -07:00
|
|
|
// Config returns the configuration used for the snapshot's interaction with the
|
|
|
|
// go/packages API.
|
|
|
|
func (s *snapshot) Config(ctx context.Context) *packages.Config {
|
|
|
|
env, buildFlags := s.view.env()
|
|
|
|
cfg := &packages.Config{
|
|
|
|
Env: env,
|
|
|
|
Dir: s.view.folder.Filename(),
|
|
|
|
Context: ctx,
|
|
|
|
BuildFlags: buildFlags,
|
|
|
|
Mode: packages.NeedName |
|
|
|
|
packages.NeedFiles |
|
|
|
|
packages.NeedCompiledGoFiles |
|
|
|
|
packages.NeedImports |
|
|
|
|
packages.NeedDeps |
|
|
|
|
packages.NeedTypesSizes,
|
|
|
|
Fset: s.view.session.cache.fset,
|
|
|
|
Overlay: s.buildOverlay(),
|
|
|
|
ParseFile: func(*token.FileSet, string, []byte) (*ast.File, error) {
|
|
|
|
panic("go/packages must not be used to parse files")
|
|
|
|
},
|
|
|
|
Logf: func(format string, args ...interface{}) {
|
|
|
|
if s.view.options.VerboseOutput {
|
|
|
|
log.Print(ctx, fmt.Sprintf(format, args...))
|
|
|
|
}
|
|
|
|
},
|
|
|
|
Tests: true,
|
|
|
|
}
|
|
|
|
return cfg
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *snapshot) buildOverlay() map[string][]byte {
|
2020-02-06 14:20:50 -07:00
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
|
2020-02-06 15:49:19 -07:00
|
|
|
overlays := make(map[string][]byte)
|
|
|
|
for uri, fh := range s.files {
|
|
|
|
overlay, ok := fh.(*overlay)
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if overlay.saved {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// TODO(rstambler): Make sure not to send overlays outside of the current view.
|
|
|
|
overlays[uri.Filename()] = overlay.text
|
|
|
|
}
|
|
|
|
return overlays
|
|
|
|
}
|
|
|
|
|
2020-02-14 09:33:11 -07:00
|
|
|
func hashUnsavedOverlays(files map[span.URI]source.FileHandle) string {
|
|
|
|
var unsaved []string
|
|
|
|
for uri, fh := range files {
|
|
|
|
if overlay, ok := fh.(*overlay); ok && !overlay.saved {
|
|
|
|
unsaved = append(unsaved, uri.Filename())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sort.Strings(unsaved)
|
|
|
|
return hashContents([]byte(strings.Join(unsaved, "")))
|
|
|
|
}
|
|
|
|
|
2019-11-29 23:17:57 -07:00
|
|
|
func (s *snapshot) PackageHandles(ctx context.Context, fh source.FileHandle) ([]source.PackageHandle, error) {
|
2020-01-30 19:29:41 -07:00
|
|
|
if fh.Identity().Kind != source.Go {
|
2020-02-26 10:10:06 -07:00
|
|
|
panic("called PackageHandles on a non-Go FileHandle")
|
2019-12-10 09:51:34 -07:00
|
|
|
}
|
|
|
|
|
2019-11-20 12:26:02 -07:00
|
|
|
ctx = telemetry.File.With(ctx, fh.Identity().URI)
|
2019-11-20 15:57:05 -07:00
|
|
|
|
2020-01-30 19:29:41 -07:00
|
|
|
// Check if we should reload metadata for the file. We don't invalidate IDs
|
|
|
|
// (though we should), so the IDs will be a better source of truth than the
|
|
|
|
// metadata. If there are no IDs for the file, then we should also reload.
|
|
|
|
ids := s.getIDsForURI(fh.Identity().URI)
|
|
|
|
reload := len(ids) == 0
|
|
|
|
for _, id := range ids {
|
|
|
|
// Reload package metadata if any of the metadata has missing
|
|
|
|
// dependencies, in case something has changed since the last time we
|
|
|
|
// reloaded it.
|
2020-02-26 13:27:56 -07:00
|
|
|
if m := s.getMetadata(id); m == nil {
|
2020-01-30 19:29:41 -07:00
|
|
|
reload = true
|
|
|
|
break
|
|
|
|
}
|
2020-02-26 13:27:56 -07:00
|
|
|
// TODO(golang/go#36918): Previously, we would reload any package with
|
|
|
|
// missing dependencies. This is expensive and results in too many
|
|
|
|
// calls to packages.Load. Determine what we should do instead.
|
2020-01-30 19:29:41 -07:00
|
|
|
}
|
|
|
|
if reload {
|
|
|
|
if err := s.load(ctx, fileURI(fh.Identity().URI)); err != nil {
|
2019-11-20 15:57:05 -07:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2020-01-30 19:29:41 -07:00
|
|
|
// Get the list of IDs from the snapshot again, in case it has changed.
|
|
|
|
var phs []source.PackageHandle
|
|
|
|
for _, id := range s.getIDsForURI(fh.Identity().URI) {
|
|
|
|
ph, err := s.packageHandle(ctx, id, source.ParseFull)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2019-12-03 17:56:44 -07:00
|
|
|
}
|
2020-01-30 19:29:41 -07:00
|
|
|
phs = append(phs, ph)
|
2019-12-03 17:56:44 -07:00
|
|
|
}
|
2020-01-30 19:29:41 -07:00
|
|
|
return phs, nil
|
2019-12-03 17:56:44 -07:00
|
|
|
}
|
|
|
|
|
2020-01-30 19:29:41 -07:00
|
|
|
// packageHandle returns a PackageHandle for the given ID. It assumes that
|
|
|
|
// the metadata for the given ID has already been loaded, but if the
|
|
|
|
// PackageHandle has not been constructed, it will rebuild it.
|
|
|
|
func (s *snapshot) packageHandle(ctx context.Context, id packageID, mode source.ParseMode) (*packageHandle, error) {
|
|
|
|
ph := s.getPackage(id, mode)
|
|
|
|
if ph != nil {
|
|
|
|
return ph, nil
|
2019-11-20 15:57:05 -07:00
|
|
|
}
|
2020-01-30 19:29:41 -07:00
|
|
|
// Don't reload metadata in this function.
|
|
|
|
// Callers of this function must reload metadata themselves.
|
|
|
|
m := s.getMetadata(id)
|
|
|
|
if m == nil {
|
|
|
|
return nil, errors.Errorf("%s has no metadata", id)
|
2019-11-20 15:57:05 -07:00
|
|
|
}
|
2020-01-30 19:29:41 -07:00
|
|
|
return s.buildPackageHandle(ctx, m.id, mode)
|
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-30 19:29:41 -07:00
|
|
|
ph, err := s.packageHandle(ctx, id, source.ParseFull)
|
2020-01-10 15:18:59 -07:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-18 13:47:38 -07:00
|
|
|
func (s *snapshot) getModHandle(uri span.URI) *modHandle {
|
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
return s.modHandles[uri]
|
|
|
|
}
|
|
|
|
|
2020-02-14 09:33:11 -07:00
|
|
|
func (s *snapshot) getModTidyHandle() *modHandle {
|
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
return s.modTidyHandle
|
|
|
|
}
|
|
|
|
|
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() {
|
2020-01-30 19:29:41 -07:00
|
|
|
ph, err := s.packageHandle(ctx, pkgID, source.ParseFull)
|
2020-01-10 15:18:59 -07:00
|
|
|
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-30 19:29:41 -07:00
|
|
|
ph, err := s.packageHandle(ctx, pkgID, source.ParseFull)
|
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).
|
2020-02-07 12:38:36 -07:00
|
|
|
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.
|
2020-01-27 18:25:48 -07:00
|
|
|
s.view.awaitInitialized(ctx)
|
2020-01-10 15:18:59 -07:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-02-07 12:38:36 -07:00
|
|
|
func (s *snapshot) getPackage(id packageID, mode source.ParseMode) *packageHandle {
|
2019-10-01 13:21:06 -06:00
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
|
|
|
|
key := packageKey{
|
|
|
|
id: id,
|
2020-02-07 12:38:36 -07:00
|
|
|
mode: mode,
|
2019-10-01 13:21:06 -06:00
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-01-30 19:29:41 -07:00
|
|
|
func (s *snapshot) getIDsForURI(uri span.URI) []packageID {
|
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-30 19:29:41 -07:00
|
|
|
return s.ids[uri]
|
2020-01-23 17:24:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *snapshot) getMetadataForURILocked(uri span.URI) (metadata []*metadata) {
|
|
|
|
// 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
|
|
|
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) 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
|
|
|
|
2020-02-19 15:14:33 -07:00
|
|
|
for i, existingID := range s.ids[uri] {
|
|
|
|
// 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.
|
2019-10-01 13:21:06 -06:00
|
|
|
if existingID == id {
|
2020-02-19 15:14:33 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
// If we are setting a real ID, when the package had only previously
|
|
|
|
// had a command-line-arguments ID, we should just replace it.
|
|
|
|
if existingID == "command-line-arguments" {
|
|
|
|
s.ids[uri][i] = id
|
2019-10-01 13:21:06 -06:00
|
|
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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-02-06 14:20:50 -07:00
|
|
|
|
2019-09-27 11:17:59 -06:00
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
|
|
|
|
if _, ok := s.files[f.URI()]; !ok {
|
2020-02-06 14:20:50 -07:00
|
|
|
s.files[f.URI()] = s.view.session.cache.GetFile(uri)
|
2019-09-23 18:06:15 -06:00
|
|
|
}
|
2020-01-28 18:38:33 -07:00
|
|
|
return s.files[f.URI()], nil
|
2020-01-09 20:45:06 -07:00
|
|
|
}
|
|
|
|
|
2020-02-06 14:20:50 -07:00
|
|
|
func (s *snapshot) IsOpen(uri span.URI) bool {
|
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
|
|
|
|
_, open := s.files[uri].(*overlay)
|
|
|
|
return open
|
|
|
|
}
|
|
|
|
|
2020-02-14 09:52:17 -07:00
|
|
|
func (s *snapshot) IsSaved(uri span.URI) bool {
|
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
|
|
|
|
ovl, open := s.files[uri].(*overlay)
|
|
|
|
return !open || ovl.saved
|
|
|
|
}
|
|
|
|
|
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.
|
2020-01-27 18:25:48 -07:00
|
|
|
s.view.awaitInitialized(ctx)
|
|
|
|
|
2020-01-28 22:08:22 -07:00
|
|
|
if err := s.reloadWorkspace(ctx); err != nil {
|
2020-01-25 14:41:35 -07:00
|
|
|
return err
|
|
|
|
}
|
2020-01-28 22:08:22 -07:00
|
|
|
return s.reloadOrphanedFiles(ctx)
|
2020-01-10 15:18:59 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// reloadWorkspace reloads the metadata for all invalidated workspace packages.
|
2020-01-28 22:08:22 -07:00
|
|
|
func (s *snapshot) reloadWorkspace(ctx context.Context) error {
|
2020-01-25 14:41:35 -07:00
|
|
|
// If the view's build configuration is invalid, we cannot reload by package path.
|
|
|
|
// Just reload the directory instead.
|
|
|
|
if !s.view.hasValidBuildConfiguration {
|
2020-01-30 18:44:00 -07:00
|
|
|
return s.load(ctx, viewLoadScope("LOAD_INVALID_VIEW"))
|
2020-01-25 14:41:35 -07:00
|
|
|
}
|
|
|
|
|
2020-01-23 17:24:51 -07:00
|
|
|
// See which of the workspace packages are missing metadata.
|
|
|
|
s.mu.Lock()
|
|
|
|
var pkgPaths []interface{}
|
|
|
|
for id, pkgPath := range s.workspacePackages {
|
|
|
|
if s.metadata[id] == nil {
|
|
|
|
pkgPaths = append(pkgPaths, pkgPath)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s.mu.Unlock()
|
|
|
|
|
2020-01-25 14:41:35 -07:00
|
|
|
if len(pkgPaths) == 0 {
|
2020-01-28 22:08:22 -07:00
|
|
|
return nil
|
2020-01-23 17:24:51 -07:00
|
|
|
}
|
2020-01-30 18:44:00 -07:00
|
|
|
return s.load(ctx, pkgPaths...)
|
2020-01-25 14:41:35 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *snapshot) reloadOrphanedFiles(ctx context.Context) error {
|
2020-01-23 17:24:51 -07:00
|
|
|
// When we load ./... or a package path directly, we may not get packages
|
|
|
|
// that exist only in overlays. As a workaround, we search all of the files
|
|
|
|
// available in the snapshot and reload their metadata individually using a
|
|
|
|
// file= query if the metadata is unavailable.
|
2020-01-25 14:41:35 -07:00
|
|
|
scopes := s.orphanedFileScopes()
|
|
|
|
if len(scopes) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-01-30 18:44:00 -07:00
|
|
|
err := s.load(ctx, scopes...)
|
2020-01-25 14:41:35 -07:00
|
|
|
|
|
|
|
// If we failed to load some files, i.e. they have no metadata,
|
|
|
|
// mark the failures so we don't bother retrying until the file's
|
|
|
|
// content changes.
|
|
|
|
//
|
|
|
|
// TODO(rstambler): This may be an overestimate if the load stopped
|
|
|
|
// early for an unrelated errors. Add a fallback?
|
|
|
|
//
|
|
|
|
// Check for context cancellation so that we don't incorrectly mark files
|
|
|
|
// as unloadable, but don't return before setting all workspace packages.
|
|
|
|
if ctx.Err() == nil && err != nil {
|
|
|
|
s.mu.Lock()
|
|
|
|
for _, scope := range scopes {
|
|
|
|
uri := span.URI(scope.(fileURI))
|
|
|
|
if s.getMetadataForURILocked(uri) == nil {
|
|
|
|
s.unloadableFiles[uri] = struct{}{}
|
2020-01-23 17:24:51 -07:00
|
|
|
}
|
|
|
|
}
|
2020-01-25 14:41:35 -07:00
|
|
|
s.mu.Unlock()
|
|
|
|
}
|
2020-01-23 17:24:51 -07:00
|
|
|
return nil
|
2020-01-11 21:59:57 -07:00
|
|
|
}
|
|
|
|
|
2020-01-23 17:24:51 -07:00
|
|
|
func (s *snapshot) orphanedFileScopes() []interface{} {
|
2020-01-10 15:18:59 -07:00
|
|
|
s.mu.Lock()
|
2020-01-11 21:59:57 -07:00
|
|
|
defer s.mu.Unlock()
|
|
|
|
|
2020-01-23 17:24:51 -07:00
|
|
|
scopeSet := make(map[span.URI]struct{})
|
|
|
|
for uri, fh := range s.files {
|
|
|
|
// Don't try to reload metadata for go.mod files.
|
|
|
|
if fh.Identity().Kind != source.Go {
|
|
|
|
continue
|
|
|
|
}
|
2020-01-27 14:39:28 -07:00
|
|
|
// If the URI doesn't belong to this view, then it's not in a workspace
|
|
|
|
// package and should not be reloaded directly.
|
|
|
|
if !contains(s.view.session.viewsOf(uri), s.view) {
|
|
|
|
continue
|
|
|
|
}
|
2020-01-23 17:24:51 -07:00
|
|
|
// Don't reload metadata for files we've already deemed unloadable.
|
|
|
|
if _, ok := s.unloadableFiles[uri]; ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if s.getMetadataForURILocked(uri) == nil {
|
|
|
|
scopeSet[uri] = struct{}{}
|
2020-01-10 15:18:59 -07:00
|
|
|
}
|
|
|
|
}
|
2020-01-23 17:24:51 -07:00
|
|
|
var scopes []interface{}
|
|
|
|
for uri := range scopeSet {
|
|
|
|
scopes = append(scopes, fileURI(uri))
|
2020-01-10 15:18:59 -07:00
|
|
|
}
|
2020-01-23 17:24:51 -07:00
|
|
|
return scopes
|
|
|
|
}
|
|
|
|
|
|
|
|
func contains(views []*view, view *view) bool {
|
|
|
|
for _, v := range views {
|
|
|
|
if v == view {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-02-06 14:20:50 -07:00
|
|
|
func (s *snapshot) clone(ctx context.Context, withoutURIs map[span.URI]source.FileHandle) *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),
|
2020-01-23 17:24:51 -07:00
|
|
|
unloadableFiles: make(map[span.URI]struct{}),
|
2020-02-18 13:47:38 -07:00
|
|
|
modHandles: make(map[span.URI]*modHandle),
|
2020-02-14 09:33:11 -07:00
|
|
|
modTidyHandle: s.modTidyHandle,
|
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-23 17:24:51 -07:00
|
|
|
// Copy the set of unloadable files.
|
|
|
|
for k, v := range s.unloadableFiles {
|
|
|
|
result.unloadableFiles[k] = v
|
|
|
|
}
|
2020-02-18 13:47:38 -07:00
|
|
|
// Copy all of the modHandles.
|
|
|
|
for k, v := range s.modHandles {
|
|
|
|
result.modHandles[k] = v
|
|
|
|
}
|
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)
|
|
|
|
|
2020-02-06 14:20:50 -07:00
|
|
|
for withoutURI, currentFH := range withoutURIs {
|
2020-01-22 17:56:26 -07:00
|
|
|
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{}{}
|
|
|
|
}
|
2020-02-06 14:20:50 -07:00
|
|
|
// The original FileHandle for this URI is cached on the snapshot.
|
2020-01-22 17:56:26 -07:00
|
|
|
originalFH := s.files[withoutURI]
|
|
|
|
|
|
|
|
// Check if the file's package name or imports have changed,
|
|
|
|
// and if so, invalidate this file's packages' metadata.
|
2020-01-30 19:29:41 -07:00
|
|
|
invalidateMetadata := s.shouldInvalidateMetadata(ctx, originalFH, currentFH)
|
2020-01-22 17:56:26 -07:00
|
|
|
|
2020-02-14 09:33:11 -07:00
|
|
|
// Invalidate the previous modTidyHandle if any of the files have been
|
|
|
|
// saved or if any of the metadata has been invalidated.
|
|
|
|
if invalidateMetadata || fileWasSaved(originalFH, currentFH) {
|
|
|
|
result.modTidyHandle = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if currentFH.Identity().Kind == source.Mod {
|
|
|
|
// If the view's go.mod file's contents have changed, invalidate the metadata
|
|
|
|
// for all of the packages in the workspace.
|
|
|
|
if invalidateMetadata {
|
|
|
|
for id := range s.workspacePackages {
|
|
|
|
directIDs[id] = struct{}{}
|
|
|
|
}
|
2020-01-22 17:56:26 -07:00
|
|
|
}
|
2020-02-18 13:47:38 -07:00
|
|
|
delete(result.modHandles, withoutURI)
|
2020-01-22 17:56:26 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
2020-01-23 17:24:51 -07:00
|
|
|
// Make sure to remove the changed file from the unloadable set.
|
|
|
|
delete(result.unloadableFiles, withoutURI)
|
2019-12-16 14:34:44 -07:00
|
|
|
}
|
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
|
|
|
|
}
|
2020-02-19 15:14:33 -07:00
|
|
|
// Copy the URI to package ID mappings, skipping only those URIs whose
|
|
|
|
// metadata will be reloaded in future calls to load.
|
|
|
|
outer:
|
|
|
|
for k, ids := range s.ids {
|
|
|
|
for _, id := range ids {
|
|
|
|
if invalidateMetadata, ok := transitiveIDs[id]; invalidateMetadata && ok {
|
2020-02-26 10:10:06 -07:00
|
|
|
continue outer
|
2020-02-19 15:14:33 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
result.ids[k] = ids
|
|
|
|
}
|
2019-10-01 13:21:06 -06:00
|
|
|
// Don't bother copying the importedBy graph,
|
|
|
|
// as it changes each time we update metadata.
|
2019-09-23 18:06:15 -06:00
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2020-03-05 10:43:50 -07:00
|
|
|
// fileWasSaved reports whether the FileHandle passed in has been saved. It
|
|
|
|
// accomplishes this by checking to see if the original and current FileHandles
|
|
|
|
// are both overlays, and if the current FileHandle is saved while the original
|
|
|
|
// FileHandle was not saved.
|
2020-02-14 09:33:11 -07:00
|
|
|
func fileWasSaved(originalFH, currentFH source.FileHandle) bool {
|
|
|
|
c, ok := currentFH.(*overlay)
|
2020-03-05 10:43:50 -07:00
|
|
|
if !ok || c == nil {
|
2020-02-14 09:33:11 -07:00
|
|
|
return true
|
|
|
|
}
|
2020-03-05 10:43:50 -07:00
|
|
|
o, ok := originalFH.(*overlay)
|
|
|
|
if !ok || o == nil {
|
2020-02-14 09:33:11 -07:00
|
|
|
return c.saved
|
|
|
|
}
|
2020-03-05 10:43:50 -07:00
|
|
|
return !o.saved && c.saved
|
2020-02-14 09:33:11 -07:00
|
|
|
}
|
|
|
|
|
2020-01-30 19:29:41 -07:00
|
|
|
// shouldInvalidateMetadata reparses a file's package and import declarations to
|
2020-01-28 22:12:28 -07:00
|
|
|
// determine if the file requires a metadata reload.
|
2020-01-30 19:29:41 -07:00
|
|
|
func (s *snapshot) shouldInvalidateMetadata(ctx context.Context, originalFH, currentFH source.FileHandle) bool {
|
2020-01-28 22:12:28 -07:00
|
|
|
if originalFH == nil {
|
|
|
|
return currentFH.Identity().Kind == source.Go
|
|
|
|
}
|
|
|
|
// 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 {
|
2020-01-31 11:59:11 -07:00
|
|
|
modfile, _ := s.view.ModFiles()
|
|
|
|
return originalFH.Identity().URI == modfile
|
2020-01-28 22:12:28 -07:00
|
|
|
}
|
|
|
|
// Get the original and current parsed files in order to check package name and imports.
|
2020-02-10 21:10:59 -07:00
|
|
|
original, _, _, _, originalErr := s.view.session.cache.ParseGoHandle(originalFH, source.ParseHeader).Parse(ctx)
|
|
|
|
current, _, _, _, currentErr := s.view.session.cache.ParseGoHandle(currentFH, source.ParseHeader).Parse(ctx)
|
2020-01-28 22:12:28 -07:00
|
|
|
if originalErr != nil || currentErr != nil {
|
|
|
|
return (originalErr == nil) != (currentErr == nil)
|
|
|
|
}
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
// If the package's imports have increased, definitely re-run `go list`.
|
|
|
|
if len(original.Imports) < len(current.Imports) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
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 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
2019-11-15 12:47:29 -07:00
|
|
|
}
|