Skip to content

Commit 84f0c7c

Browse files
committed
internal/lsp/cache: don't forget files just because they change
The situation in golang/go#35638 was as follows: didOpen main.go creates a snapshot that knows main.go is in package "mod.com". didChange main.go creates a snapshot. When a file changes, we discard its contents by leaving the file handle out of the "files" map. didOpen const.go creates a snapshot, and attempts to invalidate the metadata for packages in the same directory. The way we detect packages in the same directory is by iterating through the files in the snapshot. But we threw away the only file in "mod.com" in step 2 when its contents changed. If a diagnostics run happened to get in between the two steps, it would re-load main.go and the bug would go away. If not, step 3 would find no files and fail to invalidate "mod.com". The best way to fix this is to insert the new file handle eagerly during cloning. That way there's no confusion. Fixes golang/go#35638. Change-Id: I340bd28a96ad7b4cc912032065f3c2732c380bb2 Reviewed-on: https://go-review.googlesource.com/c/tools/+/211578 Run-TryBot: Heschi Kreinick <heschi@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Rebecca Stambler <rstambler@golang.org>
1 parent ac28154 commit 84f0c7c

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

internal/lsp/cache/snapshot.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -536,13 +536,19 @@ func (s *snapshot) clone(ctx context.Context, withoutFile source.File) *snapshot
536536
files: make(map[span.URI]source.FileHandle),
537537
workspacePackages: make(map[packageID]bool),
538538
}
539-
// Copy all of the FileHandles except for the one that was invalidated.
539+
540+
// Copy all of the FileHandles.
540541
for k, v := range s.files {
541-
if k == withoutFile.URI() {
542-
continue
543-
}
544542
result.files[k] = v
545543
}
544+
// Handle the invalidated file; it may have new contents or not exist.
545+
currentFH := s.view.session.GetFile(withoutFile.URI(), withoutFile.Kind())
546+
if _, _, err := currentFH.Read(ctx); os.IsNotExist(err) {
547+
delete(result.files, withoutFile.URI())
548+
} else {
549+
result.files[withoutFile.URI()] = currentFH
550+
}
551+
546552
// Collect the IDs for the packages associated with the excluded URIs.
547553
for k, ids := range s.ids {
548554
result.ids[k] = ids
@@ -566,9 +572,6 @@ func (s *snapshot) clone(ctx context.Context, withoutFile source.File) *snapshot
566572
result.workspacePackages[k] = v
567573
}
568574

569-
// Get the current FileHandle for the URI.
570-
currentFH := s.view.session.GetFile(withoutFile.URI(), withoutFile.Kind())
571-
572575
// Check if the file's package name or imports have changed,
573576
// and if so, invalidate this file's packages' metadata.
574577
invalidateMetadata := s.view.session.cache.shouldLoad(ctx, s, originalFH, currentFH)

0 commit comments

Comments
 (0)