Skip to content

Commit e66c3a5

Browse files
committed
gopls/internal/lsp/cache: use correct metadata in resolveImportGraph
Following CL 510095, we no longer await loading in resolveImportGraph. However, we were still acquiring the snapshot metadata once, before requesting MetadataForFile. This means that the metadata used to build package handles had more information than the metadata used to invalidate volatile package handles, causing some deps not to be present in the handles map. While at it, change cache.Package to hold only Metadata, not packageHandle. Going forward, packageHandles should be scoped more narrowly to the typecheckBatch logic. Fixes golang/go#61512 Change-Id: I32fc248b3b1329e071caf0cb304f55a6a29f8c49 Reviewed-on: https://go-review.googlesource.com/c/tools/+/513095 Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com> TryBot-Result: Gopher Robot <gobot@golang.org> gopls-CI: kokoro <noreply+kokoro@google.com> Run-TryBot: Robert Findley <rfindley@google.com>
1 parent 364bdd0 commit e66c3a5

File tree

3 files changed

+19
-23
lines changed

3 files changed

+19
-23
lines changed

gopls/internal/lsp/cache/check.go

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,6 @@ func (s *snapshot) resolveImportGraph() (*importGraph, error) {
169169
defer done()
170170

171171
s.mu.Lock()
172-
meta := s.meta
173172
lastImportGraph := s.importGraph
174173
s.mu.Unlock()
175174

@@ -211,28 +210,25 @@ func (s *snapshot) resolveImportGraph() (*importGraph, error) {
211210
//
212211
// TODO(rfindley): this logic could use a unit test.
213212
volatileDeps := make(map[PackageID]bool)
214-
var isVolatile func(PackageID) bool
215-
isVolatile = func(id PackageID) (volatile bool) {
216-
if v, ok := volatileDeps[id]; ok {
213+
var isVolatile func(*packageHandle) bool
214+
isVolatile = func(ph *packageHandle) (volatile bool) {
215+
if v, ok := volatileDeps[ph.m.ID]; ok {
217216
return v
218217
}
219218
defer func() {
220-
volatileDeps[id] = volatile
219+
volatileDeps[ph.m.ID] = volatile
221220
}()
222-
if openPackages[id] {
221+
if openPackages[ph.m.ID] {
223222
return true
224223
}
225-
m := meta.metadata[id]
226-
if m != nil {
227-
for _, dep := range m.DepsByPkgPath {
228-
if isVolatile(dep) {
229-
return true
230-
}
224+
for _, dep := range ph.m.DepsByPkgPath {
225+
if isVolatile(handles[dep]) {
226+
return true
231227
}
232228
}
233229
return false
234230
}
235-
for dep := range handles {
231+
for _, dep := range handles {
236232
isVolatile(dep)
237233
}
238234
for id, volatile := range volatileDeps {
@@ -687,7 +683,7 @@ func (b *typeCheckBatch) checkPackage(ctx context.Context, ph *packageHandle) (*
687683
}()
688684
}
689685

690-
return &Package{ph, pkg}, err
686+
return &Package{ph.m, pkg}, err
691687
}
692688

693689
// awaitPredecessors awaits all packages for m.DepsByPkgPath, returning an
@@ -894,6 +890,7 @@ func (s *snapshot) getPackageHandles(ctx context.Context, ids []PackageID) (map[
894890
// Copy handles into the result map.
895891
handles := make(map[PackageID]*packageHandle, len(b.nodes))
896892
for _, v := range b.nodes {
893+
assert(v.ph != nil, "nil handle")
897894
handles[v.m.ID] = v.ph
898895
}
899896

gopls/internal/lsp/cache/pkg.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,13 @@ type (
2727
ImportPath = source.ImportPath
2828
)
2929

30-
// A Package is the union of type-checking inputs (packageHandle) and results
31-
// (a syntaxPackage).
30+
// A Package is the union of package metadata and type checking results.
3231
//
3332
// TODO(rfindley): for now, we do not persist the post-processing of
34-
// loadDiagnostics, because the value of the snapshot.packages map is just the
33+
// loadDiagnostics, because the value of the snapshot.packages map is just the
3534
// package handle. Fix this.
3635
type Package struct {
37-
ph *packageHandle
36+
m *source.Metadata
3837
pkg *syntaxPackage
3938
}
4039

@@ -76,9 +75,9 @@ func (p *syntaxPackage) methodsets() *methodsets.Index {
7675
return p._methodsets
7776
}
7877

79-
func (p *Package) String() string { return string(p.ph.m.ID) }
78+
func (p *Package) String() string { return string(p.m.ID) }
8079

81-
func (p *Package) Metadata() *source.Metadata { return p.ph.m }
80+
func (p *Package) Metadata() *source.Metadata { return p.m }
8281

8382
// A loadScope defines a package loading scope for use with go/packages.
8483
//
@@ -163,7 +162,7 @@ func (p *Package) GetTypeErrors() []types.Error {
163162

164163
func (p *Package) DiagnosticsForFile(ctx context.Context, s source.Snapshot, uri span.URI) ([]*source.Diagnostic, error) {
165164
var diags []*source.Diagnostic
166-
for _, diag := range p.ph.m.Diagnostics {
165+
for _, diag := range p.m.Diagnostics {
167166
if diag.URI == uri {
168167
diags = append(diags, diag)
169168
}

gopls/internal/lsp/cache/snapshot.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -647,7 +647,7 @@ func (s *snapshot) PackageDiagnostics(ctx context.Context, ids ...PackageID) (ma
647647
return true
648648
}
649649
post := func(_ int, pkg *Package) {
650-
collect(pkg.ph.m.Diagnostics)
650+
collect(pkg.m.Diagnostics)
651651
collect(pkg.pkg.diagnostics)
652652
}
653653
return perFile, s.forEachPackage(ctx, ids, pre, post)
@@ -669,7 +669,7 @@ func (s *snapshot) References(ctx context.Context, ids ...PackageID) ([]source.X
669669
return true
670670
}
671671
post := func(i int, pkg *Package) {
672-
indexes[i] = XrefIndex{m: pkg.ph.m, data: pkg.pkg.xrefs()}
672+
indexes[i] = XrefIndex{m: pkg.m, data: pkg.pkg.xrefs()}
673673
}
674674
return indexes, s.forEachPackage(ctx, ids, pre, post)
675675
}

0 commit comments

Comments
 (0)