mirror of
https://github.com/golang/go
synced 2024-11-19 04:54:41 -07:00
57610eddc9
This change does not complete the work to handle snapshots correctly, but it does implement the behavior of re-building the snapshot on each file invalidation. It also moves to the approach of caching the FileHandles on the snapshot, rather than in the goFile object, which is now not necessary. Finally, this change shifts the logic of metadata invalidation into the content invalidation step, so there is less logic to decide if we should re-load a package or not. Change-Id: I18387c385fb070da4db1302bf97035ce6328b5c3 Reviewed-on: https://go-review.googlesource.com/c/tools/+/197799 Run-TryBot: Rebecca Stambler <rstambler@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Ian Cottrell <iancottrell@google.com>
166 lines
4.3 KiB
Go
166 lines
4.3 KiB
Go
// 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.
|
|
|
|
package cache
|
|
|
|
import (
|
|
"context"
|
|
|
|
"golang.org/x/tools/internal/lsp/source"
|
|
"golang.org/x/tools/internal/lsp/telemetry"
|
|
"golang.org/x/tools/internal/span"
|
|
errors "golang.org/x/xerrors"
|
|
)
|
|
|
|
func (v *view) CheckPackageHandles(ctx context.Context, f source.File) (source.Snapshot, []source.CheckPackageHandle, error) {
|
|
// Get the snapshot that will be used for type-checking.
|
|
s := v.getSnapshot()
|
|
|
|
cphs, err := s.checkPackageHandles(ctx, f)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
return s, cphs, nil
|
|
}
|
|
|
|
func (s *snapshot) checkPackageHandles(ctx context.Context, f source.File) ([]source.CheckPackageHandle, error) {
|
|
ctx = telemetry.File.With(ctx, f.URI())
|
|
|
|
fh := s.Handle(ctx, f)
|
|
|
|
// Determine if we need to type-check the package.
|
|
m, cphs, load, check := s.shouldCheck(fh)
|
|
cfg := s.view.Config(ctx)
|
|
|
|
// We may need to re-load package metadata.
|
|
// We only need to this if it has been invalidated, and is therefore unvailable.
|
|
if load {
|
|
var err error
|
|
m, err = s.load(ctx, f.URI(), cfg)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
// If load has explicitly returned nil metadata and no error,
|
|
// it means that we should not re-type-check the packages.
|
|
if m == nil {
|
|
return cphs, nil
|
|
}
|
|
}
|
|
if check {
|
|
var results []source.CheckPackageHandle
|
|
for _, m := range m {
|
|
imp := &importer{
|
|
config: cfg,
|
|
seen: make(map[packageID]struct{}),
|
|
topLevelPackageID: m.id,
|
|
snapshot: s,
|
|
}
|
|
cph, err := imp.checkPackageHandle(ctx, m.id, s)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
results = append(results, cph)
|
|
}
|
|
cphs = results
|
|
}
|
|
if len(cphs) == 0 {
|
|
return nil, errors.Errorf("no CheckPackageHandles for %s", f.URI())
|
|
}
|
|
return cphs, nil
|
|
}
|
|
|
|
func (s *snapshot) shouldCheck(fh source.FileHandle) (m []*metadata, cphs []source.CheckPackageHandle, load, check bool) {
|
|
// Get the metadata for the given file.
|
|
m = s.getMetadataForURI(fh.Identity().URI)
|
|
|
|
// If there is no metadata for the package, we definitely need to type-check again.
|
|
if len(m) == 0 {
|
|
return nil, nil, true, true
|
|
}
|
|
|
|
// 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.
|
|
for _, m := range m {
|
|
if len(m.missingDeps) != 0 {
|
|
load = true
|
|
check = true
|
|
}
|
|
}
|
|
// We expect to see a checked package for each package ID,
|
|
// and it should be parsed in full mode.
|
|
var (
|
|
expected = len(m)
|
|
cachedCPHs = s.getPackages(fh.Identity().URI)
|
|
)
|
|
if len(cachedCPHs) < expected {
|
|
return m, nil, load, true
|
|
}
|
|
for _, cph := range cachedCPHs {
|
|
// The package may have been checked in the exported mode.
|
|
if cph.Mode() != source.ParseFull {
|
|
continue
|
|
}
|
|
// Confirm that the file belongs to this package.
|
|
for _, file := range cph.Files() {
|
|
if file.File().Identity() == fh.Identity() {
|
|
cphs = append(cphs, cph)
|
|
}
|
|
}
|
|
}
|
|
if len(cphs) < expected {
|
|
return m, nil, load, true
|
|
}
|
|
return m, cphs, load, check
|
|
}
|
|
|
|
func (v *view) GetActiveReverseDeps(ctx context.Context, f source.File) (results []source.CheckPackageHandle) {
|
|
var (
|
|
s = v.getSnapshot()
|
|
rdeps = transitiveReverseDependencies(ctx, f.URI(), s)
|
|
files = v.openFiles(ctx, rdeps)
|
|
seen = make(map[span.URI]struct{})
|
|
)
|
|
for _, f := range files {
|
|
if _, ok := seen[f.URI()]; ok {
|
|
continue
|
|
}
|
|
cphs, err := s.checkPackageHandles(ctx, f)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
cph := source.WidestCheckPackageHandle(cphs)
|
|
for _, ph := range cph.Files() {
|
|
seen[ph.File().Identity().URI] = struct{}{}
|
|
}
|
|
results = append(results, cph)
|
|
}
|
|
return results
|
|
}
|
|
|
|
func transitiveReverseDependencies(ctx context.Context, uri span.URI, s *snapshot) (result []span.URI) {
|
|
var (
|
|
seen = make(map[packageID]struct{})
|
|
uris = make(map[span.URI]struct{})
|
|
topLevelURIs = make(map[span.URI]struct{})
|
|
)
|
|
|
|
metadata := s.getMetadataForURI(uri)
|
|
|
|
for _, m := range metadata {
|
|
for _, uri := range m.files {
|
|
topLevelURIs[uri] = struct{}{}
|
|
}
|
|
s.reverseDependencies(m.id, uris, seen)
|
|
}
|
|
// Filter out the URIs that belong to the original package.
|
|
for uri := range uris {
|
|
if _, ok := topLevelURIs[uri]; ok {
|
|
continue
|
|
}
|
|
result = append(result, uri)
|
|
}
|
|
return result
|
|
}
|