Skip to content

Commit 7cfd249

Browse files
committed
internal/lsp/cache: hardcode parse modes instead of guessing them
This change largely reverts CL 217139, which attempted to guess a package's parse mode based on whether or not it was in the user's workspace. This ignored the fact that a user may jump to the definition of a file outside of their workspace. Fixes golang/go#37045 Change-Id: Icb6b9d055bd1f260013227db1a6a34873c45b680 Reviewed-on: https://go-review.googlesource.com/c/tools/+/218499 Run-TryBot: Rebecca Stambler <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Robert Findley <[email protected]>
1 parent 112b901 commit 7cfd249

File tree

5 files changed

+20
-46
lines changed

5 files changed

+20
-46
lines changed

internal/lsp/cache/analysis.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,10 @@ type packageFactKey struct {
8282
}
8383

8484
func (s *snapshot) actionHandle(ctx context.Context, id packageID, a *analysis.Analyzer) (*actionHandle, error) {
85-
ph := s.getPackage(id)
85+
ph := s.getPackage(id, source.ParseFull)
8686
if ph == nil {
8787
return nil, errors.Errorf("no PackageHandle for %s", id)
8888
}
89-
expectMode(ph, source.ParseFull)
9089
act := s.getActionHandle(id, ph.mode, a)
9190
if act != nil {
9291
return act, nil

internal/lsp/cache/check.go

+9-7
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,13 @@ type packageData struct {
6363
}
6464

6565
// buildPackageHandle returns a source.PackageHandle for a given package and config.
66-
func (s *snapshot) buildPackageHandle(ctx context.Context, id packageID) (*packageHandle, error) {
67-
if ph := s.getPackage(id); ph != nil {
66+
func (s *snapshot) buildPackageHandle(ctx context.Context, id packageID, mode source.ParseMode) (*packageHandle, error) {
67+
if ph := s.getPackage(id, mode); ph != nil {
6868
return ph, nil
6969
}
7070

7171
// Build the PackageHandle for this ID and its dependencies.
72-
ph, deps, err := s.buildKey(ctx, id)
72+
ph, deps, err := s.buildKey(ctx, id, mode)
7373
if err != nil {
7474
return nil, err
7575
}
@@ -83,7 +83,6 @@ func (s *snapshot) buildPackageHandle(ctx context.Context, id packageID) (*packa
8383
//
8484

8585
m := ph.m
86-
mode := ph.mode
8786
goFiles := ph.goFiles
8887
compiledGoFiles := ph.compiledGoFiles
8988
key := ph.key
@@ -109,12 +108,11 @@ func (s *snapshot) buildPackageHandle(ctx context.Context, id packageID) (*packa
109108
}
110109

111110
// buildKey computes the key for a given packageHandle.
112-
func (s *snapshot) buildKey(ctx context.Context, id packageID) (*packageHandle, map[packagePath]*packageHandle, error) {
111+
func (s *snapshot) buildKey(ctx context.Context, id packageID, mode source.ParseMode) (*packageHandle, map[packagePath]*packageHandle, error) {
113112
m := s.getMetadata(id)
114113
if m == nil {
115114
return nil, nil, errors.Errorf("no metadata for %s", id)
116115
}
117-
mode := s.packageMode(id)
118116
goFiles, err := s.parseGoHandles(ctx, m.goFiles, mode)
119117
if err != nil {
120118
return nil, nil, err
@@ -140,7 +138,11 @@ func (s *snapshot) buildKey(ctx context.Context, id packageID) (*packageHandle,
140138
// Begin computing the key by getting the depKeys for all dependencies.
141139
var depKeys []packageHandleKey
142140
for _, depID := range depList {
143-
depHandle, err := s.buildPackageHandle(ctx, depID)
141+
mode := source.ParseExported
142+
if _, ok := s.isWorkspacePackage(depID); ok {
143+
mode = source.ParseFull
144+
}
145+
depHandle, err := s.buildPackageHandle(ctx, depID, mode)
144146
if err != nil {
145147
log.Error(ctx, "no dep handle", err, telemetry.Package.Of(depID))
146148

internal/lsp/cache/load.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"strings"
1313

1414
"golang.org/x/tools/go/packages"
15+
"golang.org/x/tools/internal/lsp/source"
1516
"golang.org/x/tools/internal/lsp/telemetry"
1617
"golang.org/x/tools/internal/packagesinternal"
1718
"golang.org/x/tools/internal/span"
@@ -119,7 +120,7 @@ func (s *snapshot) load(ctx context.Context, scopes ...interface{}) error {
119120
if err != nil {
120121
return err
121122
}
122-
if _, err := s.buildPackageHandle(ctx, m.id); err != nil {
123+
if _, err := s.buildPackageHandle(ctx, m.id, source.ParseFull); err != nil {
123124
return err
124125
}
125126
}

internal/lsp/cache/snapshot.go

+6-36
Original file line numberDiff line numberDiff line change
@@ -159,12 +159,7 @@ func (s *snapshot) packageHandle(ctx context.Context, id packageID) (*packageHan
159159
return nil, errors.Errorf("%s needs loading", id)
160160
}
161161
if check {
162-
ph, err := s.buildPackageHandle(ctx, m.id)
163-
if err != nil {
164-
return nil, err
165-
}
166-
expectMode(ph, source.ParseFull)
167-
return ph, nil
162+
return s.buildPackageHandle(ctx, m.id, source.ParseFull)
168163
}
169164
var result *packageHandle
170165
for _, ph := range phs {
@@ -202,11 +197,10 @@ func (s *snapshot) packageHandles(ctx context.Context, uri span.URI, meta []*met
202197
var results []*packageHandle
203198
if check {
204199
for _, m := range meta {
205-
ph, err := s.buildPackageHandle(ctx, m.id)
200+
ph, err := s.buildPackageHandle(ctx, m.id, source.ParseFull)
206201
if err != nil {
207202
return nil, err
208203
}
209-
expectMode(ph, source.ParseFull)
210204
results = append(results, ph)
211205
}
212206
} else {
@@ -252,11 +246,10 @@ func (s *snapshot) shouldCheck(m []*metadata) (phs []*packageHandle, load, check
252246
// If a single PackageHandle is missing, re-check all of them.
253247
// TODO: Optimize this by only checking the necessary packages.
254248
for _, m := range m {
255-
ph := s.getPackage(m.id)
249+
ph := s.getPackage(m.id, source.ParseFull)
256250
if ph == nil {
257251
return nil, false, true
258252
}
259-
expectMode(ph, source.ParseFull)
260253
phs = append(phs, ph)
261254
}
262255
// If the metadata for the package had missing dependencies,
@@ -414,24 +407,15 @@ func (s *snapshot) KnownPackages(ctx context.Context) ([]source.PackageHandle, e
414407
for pkgID := range pkgIDs {
415408
// Metadata for these packages should already be up-to-date,
416409
// so just build the package handle directly (without a reload).
417-
ph, err := s.buildPackageHandle(ctx, pkgID)
410+
ph, err := s.buildPackageHandle(ctx, pkgID, source.ParseExported)
418411
if err != nil {
419412
return nil, err
420413
}
421-
expectMode(ph, source.ParseExported)
422414
results = append(results, ph)
423415
}
424416
return results, nil
425417
}
426418

427-
// expectMode is a defensive check to make sure that we mark workspace packages
428-
// correctly. TODO(rstambler): Remove this once we're confident this works.
429-
func expectMode(ph *packageHandle, mode source.ParseMode) {
430-
if ph.mode != mode {
431-
panic(fmt.Sprintf("unexpected parse mode for %s", ph.m.id))
432-
}
433-
}
434-
435419
func (s *snapshot) CachedImportPaths(ctx context.Context) (map[string]source.Package, error) {
436420
// Don't reload workspace package metadata.
437421
// This function is meant to only return currently cached information.
@@ -460,31 +444,17 @@ func (s *snapshot) CachedImportPaths(ctx context.Context) (map[string]source.Pac
460444
return results, nil
461445
}
462446

463-
func (s *snapshot) getPackage(id packageID) *packageHandle {
447+
func (s *snapshot) getPackage(id packageID, mode source.ParseMode) *packageHandle {
464448
s.mu.Lock()
465449
defer s.mu.Unlock()
466450

467451
key := packageKey{
468452
id: id,
469-
mode: s.packageModeLocked(id),
453+
mode: mode,
470454
}
471455
return s.packages[key]
472456
}
473457

474-
func (s *snapshot) packageMode(id packageID) source.ParseMode {
475-
s.mu.Lock()
476-
defer s.mu.Unlock()
477-
478-
return s.packageModeLocked(id)
479-
}
480-
481-
func (s *snapshot) packageModeLocked(id packageID) source.ParseMode {
482-
if _, ok := s.workspacePackages[id]; ok {
483-
return source.ParseFull
484-
}
485-
return source.ParseExported
486-
}
487-
488458
func (s *snapshot) getActionHandle(id packageID, m source.ParseMode, a *analysis.Analyzer) *actionHandle {
489459
s.mu.Lock()
490460
defer s.mu.Unlock()

internal/lsp/source/view.go

+2
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ type Snapshot interface {
6161
CachedImportPaths(ctx context.Context) (map[string]Package, error)
6262

6363
// KnownPackages returns all the packages loaded in this snapshot.
64+
// Workspace packages may be parsed in ParseFull mode, whereas transitive
65+
// dependencies will be in ParseExported mode.
6466
KnownPackages(ctx context.Context) ([]PackageHandle, error)
6567

6668
// WorkspacePackages returns the PackageHandles for the snapshot's

0 commit comments

Comments
 (0)