aboutsummaryrefslogtreecommitdiff
path: root/internal/lsp/cache/parse.go
diff options
context:
space:
mode:
authorRobert Findley <rfindley@google.com>2022-05-20 11:35:51 -0400
committerRobert Findley <rfindley@google.com>2022-05-23 18:14:40 +0000
commitccb10502d1a5c404de12649bc783a8f25bac5c5d (patch)
treea59ece597b9ae21843ab2d5b1dd946f60998a410 /internal/lsp/cache/parse.go
parent904e24e9fcf9e04175d563e5112b8264262acbb0 (diff)
downloadgolang-x-tools-ccb10502d1a5c404de12649bc783a8f25bac5c5d.tar.gz
internal/lsp/cache: invalidate metadata when parsing issues resolve
Package loading (at least via go list) fails when the import header doesn't parse, so we need to invalidate metadata upon a state change from non parsing->parsing. Refactor metadata invalidation to implement this feature, add additional documentation, and avoid unnecessary work. This change revealed a latent bug (via TestDeleteDirectory): when statting a deleted directory failed, we could fail to invalidate any package IDs, even those we already knew about. This bug was masked by the somewhat complicated semantics of pkgNameChanged. The semantics of pkgNameChanged are simplified to encapsulate any change to a package->file association. Also refactor the parsing API somewhat, and add a bug report if we don't get a ParseGoFile while inspecting for metadata changes. Update most regtests to panic upon receiving bug reports. Fixes golang/go#52981 Change-Id: I1838963ecc9c01e316f887aa9d8f1260662281ab Reviewed-on: https://go-review.googlesource.com/c/tools/+/407501 Reviewed-by: Alan Donovan <adonovan@google.com> gopls-CI: kokoro <noreply+kokoro@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Run-TryBot: Robert Findley <rfindley@google.com>
Diffstat (limited to 'internal/lsp/cache/parse.go')
-rw-r--r--internal/lsp/cache/parse.go34
1 files changed, 21 insertions, 13 deletions
diff --git a/internal/lsp/cache/parse.go b/internal/lsp/cache/parse.go
index 5d751ebe5..1ab1fa9e5 100644
--- a/internal/lsp/cache/parse.go
+++ b/internal/lsp/cache/parse.go
@@ -72,27 +72,19 @@ func (s *snapshot) parseGoHandle(ctx context.Context, fh source.FileHandle, mode
}
func (pgh *parseGoHandle) String() string {
- return pgh.File().URI().Filename()
-}
-
-func (pgh *parseGoHandle) File() source.FileHandle {
- return pgh.file
-}
-
-func (pgh *parseGoHandle) Mode() source.ParseMode {
- return pgh.mode
+ return pgh.file.URI().Filename()
}
func (s *snapshot) ParseGo(ctx context.Context, fh source.FileHandle, mode source.ParseMode) (*source.ParsedGoFile, error) {
- pgh := s.parseGoHandle(ctx, fh, mode)
- pgf, _, err := s.parseGo(ctx, pgh)
+ pgf, _, err := s.parseGo(ctx, fh, mode)
return pgf, err
}
-func (s *snapshot) parseGo(ctx context.Context, pgh *parseGoHandle) (*source.ParsedGoFile, bool, error) {
- if pgh.mode == source.ParseExported {
+func (s *snapshot) parseGo(ctx context.Context, fh source.FileHandle, mode source.ParseMode) (*source.ParsedGoFile, bool, error) {
+ if mode == source.ParseExported {
panic("only type checking should use Exported")
}
+ pgh := s.parseGoHandle(ctx, fh, mode)
d, err := pgh.handle.Get(ctx, s.generation, s)
if err != nil {
return nil, false, err
@@ -101,6 +93,22 @@ func (s *snapshot) parseGo(ctx context.Context, pgh *parseGoHandle) (*source.Par
return data.parsed, data.fixed, data.err
}
+// cachedPGF returns the cached ParsedGoFile for the given ParseMode, if it
+// has already been computed. Otherwise, it returns nil.
+func (s *snapshot) cachedPGF(fh source.FileHandle, mode source.ParseMode) *source.ParsedGoFile {
+ key := parseKey{file: fh.FileIdentity(), mode: mode}
+ if pgh := s.getGoFile(key); pgh != nil {
+ cached := pgh.handle.Cached(s.generation)
+ if cached != nil {
+ cached := cached.(*parseGoData)
+ if cached.parsed != nil {
+ return cached.parsed
+ }
+ }
+ }
+ return nil
+}
+
type astCacheKey struct {
pkg packageHandleKey
uri span.URI