Skip to content

Commit 622cf7b

Browse files
committed
internal/lsp/cache: copy workFile when invalidating workspace
to avoid losing workFile information when that happens. This fixes an issue where diagnostics, hover, etc didn't show up after the initial load when some changes were made to go.work files. Change-Id: I42e2dcfd94a5b4726856ab0a4d8dfc9c1efc48b1 Reviewed-on: https://go-review.googlesource.com/c/tools/+/391257 Trust: Michael Matloob <[email protected]> Run-TryBot: Michael Matloob <[email protected]> gopls-CI: kokoro <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Robert Findley <[email protected]>
1 parent e7a12a3 commit 622cf7b

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

gopls/internal/regtest/workspace/workspace_test.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -802,13 +802,28 @@ func TestUseGoWorkDiagnosticMissingModule(t *testing.T) {
802802
go 1.18
803803
804804
use ./foo
805+
-- bar/go.mod --
806+
module example.com/bar
805807
`
806808
Run(t, files, func(t *testing.T, env *Env) {
807809
env.OpenFile("go.work")
808810
env.Await(
809811
env.DiagnosticAtRegexpWithMessage("go.work", "use", "directory ./foo does not contain a module"),
810812
)
811-
t.Log("bar")
813+
// The following tests is a regression test against an issue where we weren't
814+
// copying the workFile struct field on workspace when a new one was created in
815+
// (*workspace).invalidate. Set the buffer content to a working file so that
816+
// invalidate recognizes the workspace to be change and copies over the workspace
817+
// struct, and then set the content back to the old contents to make sure
818+
// the diagnostic still shows up.
819+
env.SetBufferContent("go.work", "go 1.18 \n\n use ./bar\n")
820+
env.Await(
821+
env.NoDiagnosticAtRegexp("go.work", "use"),
822+
)
823+
env.SetBufferContent("go.work", "go 1.18 \n\n use ./foo\n")
824+
env.Await(
825+
env.DiagnosticAtRegexpWithMessage("go.work", "use", "directory ./foo does not contain a module"),
826+
)
812827
})
813828
}
814829

internal/lsp/cache/workspace.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,7 @@ func (w *workspace) invalidate(ctx context.Context, changes map[span.URI]*fileCh
282282
moduleSource: w.moduleSource,
283283
knownModFiles: make(map[span.URI]struct{}),
284284
activeModFiles: make(map[span.URI]struct{}),
285+
workFile: w.workFile,
285286
mod: w.mod,
286287
sum: w.sum,
287288
wsDirs: w.wsDirs,

0 commit comments

Comments
 (0)