From 622cf7b3386c687d58046ab8ed33fa6e70260dc6 Mon Sep 17 00:00:00 2001 From: Michael Matloob Date: Wed, 9 Mar 2022 18:50:14 -0500 Subject: [PATCH] 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 Run-TryBot: Michael Matloob gopls-CI: kokoro TryBot-Result: Gopher Robot Reviewed-by: Robert Findley --- .../regtest/workspace/workspace_test.go | 17 ++++++++++++++++- internal/lsp/cache/workspace.go | 1 + 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/gopls/internal/regtest/workspace/workspace_test.go b/gopls/internal/regtest/workspace/workspace_test.go index a241348cae6..ed2c9effdaf 100644 --- a/gopls/internal/regtest/workspace/workspace_test.go +++ b/gopls/internal/regtest/workspace/workspace_test.go @@ -802,13 +802,28 @@ func TestUseGoWorkDiagnosticMissingModule(t *testing.T) { go 1.18 use ./foo +-- bar/go.mod -- +module example.com/bar ` Run(t, files, func(t *testing.T, env *Env) { env.OpenFile("go.work") env.Await( env.DiagnosticAtRegexpWithMessage("go.work", "use", "directory ./foo does not contain a module"), ) - t.Log("bar") + // The following tests is a regression test against an issue where we weren't + // copying the workFile struct field on workspace when a new one was created in + // (*workspace).invalidate. Set the buffer content to a working file so that + // invalidate recognizes the workspace to be change and copies over the workspace + // struct, and then set the content back to the old contents to make sure + // the diagnostic still shows up. + env.SetBufferContent("go.work", "go 1.18 \n\n use ./bar\n") + env.Await( + env.NoDiagnosticAtRegexp("go.work", "use"), + ) + env.SetBufferContent("go.work", "go 1.18 \n\n use ./foo\n") + env.Await( + env.DiagnosticAtRegexpWithMessage("go.work", "use", "directory ./foo does not contain a module"), + ) }) } diff --git a/internal/lsp/cache/workspace.go b/internal/lsp/cache/workspace.go index d10f5bc217c..16f97dbe635 100644 --- a/internal/lsp/cache/workspace.go +++ b/internal/lsp/cache/workspace.go @@ -282,6 +282,7 @@ func (w *workspace) invalidate(ctx context.Context, changes map[span.URI]*fileCh moduleSource: w.moduleSource, knownModFiles: make(map[span.URI]struct{}), activeModFiles: make(map[span.URI]struct{}), + workFile: w.workFile, mod: w.mod, sum: w.sum, wsDirs: w.wsDirs,