Skip to content

Commit 807dcd8

Browse files
committed
internal/lsp: invalidate package IDs along with metadata
We had previously only added to a list of package IDs, rather than invalidating package IDs along with metadata. This is because we loaded packages by file, rather than by package path or workspace. Now that we load by workspace, we can invalidate package IDs. This will avoid the issue of lingering "command-line-arguments" IDs. Fixes golang/go#37213 Change-Id: I21830219efaf0df9531e9d811ccbc3f141c0cbcb Reviewed-on: https://go-review.googlesource.com/c/tools/+/220197 Run-TryBot: Rebecca Stambler <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Heschi Kreinick <[email protected]>
1 parent c099ead commit 807dcd8

File tree

1 file changed

+21
-8
lines changed

1 file changed

+21
-8
lines changed

internal/lsp/cache/snapshot.go

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -432,11 +432,17 @@ func (s *snapshot) addID(uri span.URI, id packageID) {
432432
s.mu.Lock()
433433
defer s.mu.Unlock()
434434

435-
for _, existingID := range s.ids[uri] {
435+
for i, existingID := range s.ids[uri] {
436+
// TODO: We should make sure not to set duplicate IDs,
437+
// and instead panic here. This can be done by making sure not to
438+
// reset metadata information for packages we've already seen.
436439
if existingID == id {
437-
// TODO: We should make sure not to set duplicate IDs,
438-
// and instead panic here. This can be done by making sure not to
439-
// reset metadata information for packages we've already seen.
440+
return
441+
}
442+
// If we are setting a real ID, when the package had only previously
443+
// had a command-line-arguments ID, we should just replace it.
444+
if existingID == "command-line-arguments" {
445+
s.ids[uri][i] = id
440446
return
441447
}
442448
}
@@ -690,10 +696,6 @@ func (s *snapshot) clone(ctx context.Context, withoutURIs map[span.URI]source.Fi
690696
delete(result.unloadableFiles, withoutURI)
691697
}
692698

693-
// Collect the IDs for the packages associated with the excluded URIs.
694-
for k, ids := range s.ids {
695-
result.ids[k] = ids
696-
}
697699
// Copy the set of initally loaded packages.
698700
for k, v := range s.workspacePackages {
699701
result.workspacePackages[k] = v
@@ -720,6 +722,17 @@ func (s *snapshot) clone(ctx context.Context, withoutURIs map[span.URI]source.Fi
720722
}
721723
result.metadata[k] = v
722724
}
725+
// Copy the URI to package ID mappings, skipping only those URIs whose
726+
// metadata will be reloaded in future calls to load.
727+
outer:
728+
for k, ids := range s.ids {
729+
for _, id := range ids {
730+
if invalidateMetadata, ok := transitiveIDs[id]; invalidateMetadata && ok {
731+
break outer
732+
}
733+
}
734+
result.ids[k] = ids
735+
}
723736
// Don't bother copying the importedBy graph,
724737
// as it changes each time we update metadata.
725738

0 commit comments

Comments
 (0)