Skip to content

Commit 23056f6

Browse files
committed
internal/lsp/cache: clean up getOrLoadIDsForURI
Address some TODOs in getOrLoadIDsForURI. In particular, after this change files will be marked as unloadable if getOrLoadIDsForURI returns fails to load valid IDs. Change-Id: I1f09d1c7edd776e46bf8178157bcf9e439b9f293 Reviewed-on: https://go-review.googlesource.com/c/tools/+/447742 Run-TryBot: Robert Findley <[email protected]> Reviewed-by: Alan Donovan <[email protected]> TryBot-Result: Gopher Robot <[email protected]> gopls-CI: kokoro <[email protected]>
1 parent 5a4eba5 commit 23056f6

File tree

1 file changed

+7
-17
lines changed

1 file changed

+7
-17
lines changed

gopls/internal/lsp/cache/snapshot.go

+7-17
Original file line numberDiff line numberDiff line change
@@ -734,23 +734,14 @@ func (s *snapshot) getOrLoadIDsForURI(ctx context.Context, uri span.URI) ([]Pack
734734
// Start with the set of package associations derived from the last load.
735735
ids := s.meta.ids[uri]
736736

737-
hasValidID := false // whether we have any valid package metadata containing uri
738737
shouldLoad := false // whether any packages containing uri are marked 'shouldLoad'
739738
for _, id := range ids {
740-
// TODO(rfindley): remove the defensiveness here. s.meta.metadata[id] must
741-
// exist.
742-
if _, ok := s.meta.metadata[id]; ok {
743-
hasValidID = true
744-
}
745739
if len(s.shouldLoad[id]) > 0 {
746740
shouldLoad = true
747741
}
748742
}
749743

750744
// Check if uri is known to be unloadable.
751-
//
752-
// TODO(rfindley): shouldn't we also mark uri as unloadable if the load below
753-
// fails? Otherwise we endlessly load files with no packages.
754745
_, unloadable := s.unloadableFiles[uri]
755746

756747
s.mu.Unlock()
@@ -759,7 +750,7 @@ func (s *snapshot) getOrLoadIDsForURI(ctx context.Context, uri span.URI) ([]Pack
759750
// - uri is not contained in any valid packages
760751
// - ...or one of the packages containing uri is marked 'shouldLoad'
761752
// - ...but uri is not unloadable
762-
if (shouldLoad || !hasValidID) && !unloadable {
753+
if (shouldLoad || len(ids) == 0) && !unloadable {
763754
scope := fileLoadScope(uri)
764755
err := s.load(ctx, false, scope)
765756

@@ -788,14 +779,11 @@ func (s *snapshot) getOrLoadIDsForURI(ctx context.Context, uri span.URI) ([]Pack
788779

789780
s.mu.Lock()
790781
ids = s.meta.ids[uri]
791-
var validIDs []PackageID
792-
for _, id := range ids {
793-
// TODO(rfindley): remove the defensiveness here as well.
794-
if _, ok := s.meta.metadata[id]; ok {
795-
validIDs = append(validIDs, id)
796-
}
782+
// metadata is only ever added by loading, so if we get here and still have
783+
// no ids, uri is unloadable.
784+
if !unloadable && len(ids) == 0 {
785+
s.unloadableFiles[uri] = struct{}{}
797786
}
798-
ids = validIDs
799787
s.mu.Unlock()
800788

801789
return ids, nil
@@ -1745,6 +1733,8 @@ func (s *snapshot) clone(ctx, bgCtx context.Context, changes map[span.URI]*fileC
17451733
// TODO(rfindley): this looks wrong. Shouldn't we clear unloadableFiles on
17461734
// changes to environment or workspace layout, or more generally on any
17471735
// metadata change?
1736+
//
1737+
// Maybe not, as major configuration changes cause a new view.
17481738
for k, v := range s.unloadableFiles {
17491739
result.unloadableFiles[k] = v
17501740
}

0 commit comments

Comments
 (0)