Skip to content

Commit b3b5c13

Browse files
committed
internal/lsp/cache: invalidate packages with missing deps when files are
added Add logic to invalidate any packages with missing dependencies when a file is added. Also fix a latent bug overwriting 'anyFileOpenenedOrClosed' for each loop iteration. Fixes golang/go#54073 Change-Id: I000ceb354885bd4863a1dfdda09e4d5f0e5481f3 Reviewed-on: https://go-review.googlesource.com/c/tools/+/419501 Run-TryBot: Robert Findley <[email protected]> Reviewed-by: Suzy Mueller <[email protected]> gopls-CI: kokoro <[email protected]> TryBot-Result: Gopher Robot <[email protected]>
1 parent 39a4e36 commit b3b5c13

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

gopls/internal/regtest/watch/watch_test.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -199,14 +199,12 @@ func _() {
199199
}
200200
`
201201
Run(t, missing, func(t *testing.T, env *Env) {
202-
t.Skip("the initial workspace load fails and never retries")
203-
204202
env.Await(
205203
env.DiagnosticAtRegexp("a/a.go", "\"mod.com/c\""),
206204
)
207205
env.WriteWorkspaceFile("c/c.go", `package c; func C() {};`)
208206
env.Await(
209-
EmptyDiagnostics("c/c.go"),
207+
EmptyDiagnostics("a/a.go"),
210208
)
211209
})
212210
}

internal/lsp/cache/snapshot.go

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1695,8 +1695,10 @@ func (s *snapshot) clone(ctx, bgCtx context.Context, changes map[span.URI]*fileC
16951695

16961696
// Compute invalidations based on file changes.
16971697
changedPkgFiles := map[PackageID]bool{} // packages whose file set may have changed
1698-
anyImportDeleted := false
1699-
anyFileOpenedOrClosed := false
1698+
anyImportDeleted := false // import deletions can resolve cycles
1699+
anyFileOpenedOrClosed := false // opened files affect workspace packages
1700+
anyFileAdded := false // adding a file can resolve missing dependencies
1701+
17001702
for uri, change := range changes {
17011703
// Maybe reinitialize the view if we see a change in the vendor
17021704
// directory.
@@ -1709,7 +1711,8 @@ func (s *snapshot) clone(ctx, bgCtx context.Context, changes map[span.URI]*fileC
17091711
var originalOpen, newOpen bool
17101712
_, originalOpen = originalFH.(*overlay)
17111713
_, newOpen = change.fileHandle.(*overlay)
1712-
anyFileOpenedOrClosed = originalOpen != newOpen
1714+
anyFileOpenedOrClosed = anyFileOpenedOrClosed || (originalOpen != newOpen)
1715+
anyFileAdded = anyFileAdded || (originalFH == nil && change.fileHandle != nil)
17131716

17141717
// If uri is a Go file, check if it has changed in a way that would
17151718
// invalidate metadata. Note that we can't use s.view.FileKind here,
@@ -1779,6 +1782,19 @@ func (s *snapshot) clone(ctx, bgCtx context.Context, changes map[span.URI]*fileC
17791782
}
17801783
}
17811784

1785+
// Adding a file can resolve missing dependencies from existing packages.
1786+
//
1787+
// We could be smart here and try to guess which packages may have been
1788+
// fixed, but until that proves necessary, just invalidate metadata for any
1789+
// package with missing dependencies.
1790+
if anyFileAdded {
1791+
for id, metadata := range s.meta.metadata {
1792+
if len(metadata.MissingDeps) > 0 {
1793+
directIDs[id] = true
1794+
}
1795+
}
1796+
}
1797+
17821798
// Invalidate reverse dependencies too.
17831799
// idsToInvalidate keeps track of transitive reverse dependencies.
17841800
// If an ID is present in the map, invalidate its types.

0 commit comments

Comments
 (0)