Skip to content

Commit 2ecf2a5

Browse files
committed
internal/lsp: don't reload invalid build configurations unconditionally
Previously, we would always reload views with invalid build configurations on every call to reloadWorkspace, even if the metadata had no reason to be treated as invalid. Fixes golang/go#42813 Change-Id: I9e0e493228916262908b81bc1b1ab1eb4e4eca9e Reviewed-on: https://go-review.googlesource.com/c/tools/+/274443 Trust: Rebecca Stambler <[email protected]> Run-TryBot: Rebecca Stambler <[email protected]> gopls-CI: kokoro <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Robert Findley <[email protected]>
1 parent bef1c47 commit 2ecf2a5

File tree

2 files changed

+33
-9
lines changed

2 files changed

+33
-9
lines changed

gopls/internal/regtest/diagnostics_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1627,3 +1627,22 @@ package b
16271627
)
16281628
})
16291629
}
1630+
1631+
func TestAdHocPackagesReloading(t *testing.T) {
1632+
const nomod = `
1633+
-- main.go --
1634+
package main
1635+
1636+
func main() {}
1637+
`
1638+
run(t, nomod, func(t *testing.T, env *Env) {
1639+
env.OpenFile("main.go")
1640+
env.RegexpReplace("main.go", "{}", "{ var x int; }") // simulate typing
1641+
env.Await(
1642+
OnceMet(
1643+
CompletedWork(lsp.DiagnosticWorkTitle(lsp.FromDidChange), 1),
1644+
NoLogMatching(protocol.Info, "packages=1"),
1645+
),
1646+
)
1647+
})
1648+
}

internal/lsp/cache/snapshot.go

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -966,29 +966,34 @@ func (s *snapshot) AwaitInitialized(ctx context.Context) {
966966

967967
// reloadWorkspace reloads the metadata for all invalidated workspace packages.
968968
func (s *snapshot) reloadWorkspace(ctx context.Context) error {
969-
// If the view's build configuration is invalid, we cannot reload by
970-
// package path. Just reload the directory instead.
971-
if !s.ValidBuildConfiguration() {
972-
return s.load(ctx, viewLoadScope("LOAD_INVALID_VIEW"))
973-
}
974-
975969
// See which of the workspace packages are missing metadata.
976970
s.mu.Lock()
971+
missingMetadata := len(s.workspacePackages) == 0 || len(s.metadata) == 0
977972
pkgPathSet := map[packagePath]struct{}{}
978973
for id, pkgPath := range s.workspacePackages {
974+
if s.metadata[id] != nil {
975+
continue
976+
}
977+
missingMetadata = true
978+
979979
// Don't try to reload "command-line-arguments" directly.
980980
if pkgPath == "command-line-arguments" {
981981
continue
982982
}
983-
if s.metadata[id] == nil {
984-
pkgPathSet[pkgPath] = struct{}{}
985-
}
983+
pkgPathSet[pkgPath] = struct{}{}
986984
}
987985
s.mu.Unlock()
988986

987+
// If the view's build configuration is invalid, we cannot reload by
988+
// package path. Just reload the directory instead.
989+
if missingMetadata && !s.ValidBuildConfiguration() {
990+
return s.load(ctx, viewLoadScope("LOAD_INVALID_VIEW"))
991+
}
992+
989993
if len(pkgPathSet) == 0 {
990994
return nil
991995
}
996+
992997
var pkgPaths []interface{}
993998
for pkgPath := range pkgPathSet {
994999
pkgPaths = append(pkgPaths, pkgPath)

0 commit comments

Comments
 (0)