Skip to content

Commit 4f69bf3

Browse files
findleyrgopherbot
authored andcommitted
gopls/internal/lsp/cache: narrow reloadOrphanedFiles to open files
This CL implements a narrower fix for #57053 than completely removing orphaned file reloading, as done in CL 454116. One side-effect of orphaned file reloading that we still depend on is to ensure we at least try to load each open file. Elsewhere in the code (for example, in GetCriticalError) we assume that we've tried loading all open files. For golang/go#57053 Change-Id: I33b77859b3c2bf1437558b64b8262985730a7215 Reviewed-on: https://go-review.googlesource.com/c/tools/+/454559 Reviewed-by: Alan Donovan <[email protected]> Run-TryBot: Robert Findley <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Auto-Submit: Robert Findley <[email protected]> gopls-CI: kokoro <[email protected]>
1 parent 6002d6e commit 4f69bf3

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

gopls/internal/lsp/cache/snapshot.go

+12-9
Original file line numberDiff line numberDiff line change
@@ -1390,6 +1390,10 @@ func (s *snapshot) GetCriticalError(ctx context.Context) *source.CriticalError {
13901390
// ID "command-line-arguments" are usually a symptom of a bad workspace
13911391
// configuration.
13921392
//
1393+
// This heuristic is path-dependent: we only get command-line-arguments
1394+
// packages when we've loaded using file scopes, which only occurs
1395+
// on-demand or via orphaned file reloading.
1396+
//
13931397
// TODO(rfindley): re-evaluate this heuristic.
13941398
if containsCommandLineArguments(wsPkgs) {
13951399
return s.workspaceLayoutError(ctx)
@@ -1492,7 +1496,7 @@ func (s *snapshot) awaitLoadedAllErrors(ctx context.Context) *source.CriticalErr
14921496
}
14931497
}
14941498

1495-
if err := s.reloadOrphanedFiles(ctx); err != nil {
1499+
if err := s.reloadOrphanedOpenFiles(ctx); err != nil {
14961500
diags := s.extractGoCommandErrors(ctx, err)
14971501
return &source.CriticalError{
14981502
MainError: err,
@@ -1560,12 +1564,12 @@ func (s *snapshot) reloadWorkspace(ctx context.Context) error {
15601564
return err
15611565
}
15621566

1563-
func (s *snapshot) reloadOrphanedFiles(ctx context.Context) error {
1567+
func (s *snapshot) reloadOrphanedOpenFiles(ctx context.Context) error {
15641568
// When we load ./... or a package path directly, we may not get packages
15651569
// that exist only in overlays. As a workaround, we search all of the files
15661570
// available in the snapshot and reload their metadata individually using a
15671571
// file= query if the metadata is unavailable.
1568-
files := s.orphanedFiles()
1572+
files := s.orphanedOpenFiles()
15691573

15701574
// Files without a valid package declaration can't be loaded. Don't try.
15711575
var scopes []loadScope
@@ -1612,12 +1616,16 @@ func (s *snapshot) reloadOrphanedFiles(ctx context.Context) error {
16121616
return nil
16131617
}
16141618

1615-
func (s *snapshot) orphanedFiles() []source.VersionedFileHandle {
1619+
func (s *snapshot) orphanedOpenFiles() []source.VersionedFileHandle {
16161620
s.mu.Lock()
16171621
defer s.mu.Unlock()
16181622

16191623
var files []source.VersionedFileHandle
16201624
s.files.Range(func(uri span.URI, fh source.VersionedFileHandle) {
1625+
// Only consider open files, which will be represented as overlays.
1626+
if _, isOverlay := fh.(*overlay); !isOverlay {
1627+
return
1628+
}
16211629
// Don't try to reload metadata for go.mod files.
16221630
if s.view.FileKind(fh) != source.Go {
16231631
return
@@ -1627,11 +1635,6 @@ func (s *snapshot) orphanedFiles() []source.VersionedFileHandle {
16271635
if !source.InDir(s.view.folder.Filename(), uri.Filename()) {
16281636
return
16291637
}
1630-
// If the file is not open and is in a vendor directory, don't treat it
1631-
// like a workspace package.
1632-
if _, ok := fh.(*overlay); !ok && inVendor(uri) {
1633-
return
1634-
}
16351638
// Don't reload metadata for files we've already deemed unloadable.
16361639
if _, ok := s.unloadableFiles[uri]; ok {
16371640
return

0 commit comments

Comments
 (0)