Skip to content

Commit 2527e7e

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]> (cherry picked from commit 2ecf2a5) Reviewed-on: https://go-review.googlesource.com/c/tools/+/275436 Run-TryBot: Robert Findley <[email protected]>
1 parent 30bfc3f commit 2527e7e

File tree

3 files changed

+35
-9
lines changed

3 files changed

+35
-9
lines changed

gopls/go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,5 @@ require (
1010
mvdan.cc/gofumpt v0.0.0-20200927160801-5bfeb2e70dd6
1111
mvdan.cc/xurls/v2 v2.2.0
1212
)
13+
14+
replace golang.org/x/tools => ../

gopls/internal/regtest/diagnostics_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1515,3 +1515,22 @@ func main() {
15151515
)
15161516
})
15171517
}
1518+
1519+
func TestAdHocPackagesReloading(t *testing.T) {
1520+
const nomod = `
1521+
-- main.go --
1522+
package main
1523+
1524+
func main() {}
1525+
`
1526+
run(t, nomod, func(t *testing.T, env *Env) {
1527+
env.OpenFile("main.go")
1528+
env.RegexpReplace("main.go", "{}", "{ var x int; }") // simulate typing
1529+
env.Await(
1530+
OnceMet(
1531+
CompletedWork(lsp.DiagnosticWorkTitle(lsp.FromDidChange), 1),
1532+
NoLogMatching(protocol.Info, "packages=1"),
1533+
),
1534+
)
1535+
})
1536+
}

internal/lsp/cache/snapshot.go

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

938938
// reloadWorkspace reloads the metadata for all invalidated workspace packages.
939939
func (s *snapshot) reloadWorkspace(ctx context.Context) error {
940-
// If the view's build configuration is invalid, we cannot reload by
941-
// package path. Just reload the directory instead.
942-
if !s.ValidBuildConfiguration() {
943-
return s.load(ctx, viewLoadScope("LOAD_INVALID_VIEW"))
944-
}
945-
946940
// See which of the workspace packages are missing metadata.
947941
s.mu.Lock()
942+
missingMetadata := len(s.workspacePackages) == 0 || len(s.metadata) == 0
948943
pkgPathSet := map[packagePath]struct{}{}
949944
for id, pkgPath := range s.workspacePackages {
945+
if s.metadata[id] != nil {
946+
continue
947+
}
948+
missingMetadata = true
949+
950950
// Don't try to reload "command-line-arguments" directly.
951951
if pkgPath == "command-line-arguments" {
952952
continue
953953
}
954-
if s.metadata[id] == nil {
955-
pkgPathSet[pkgPath] = struct{}{}
956-
}
954+
pkgPathSet[pkgPath] = struct{}{}
957955
}
958956
s.mu.Unlock()
959957

958+
// If the view's build configuration is invalid, we cannot reload by
959+
// package path. Just reload the directory instead.
960+
if missingMetadata && !s.ValidBuildConfiguration() {
961+
return s.load(ctx, viewLoadScope("LOAD_INVALID_VIEW"))
962+
}
963+
960964
if len(pkgPathSet) == 0 {
961965
return nil
962966
}
967+
963968
var pkgPaths []interface{}
964969
for pkgPath := range pkgPathSet {
965970
pkgPaths = append(pkgPaths, pkgPath)

0 commit comments

Comments
 (0)