Skip to content

Commit

Permalink
internal/lsp/cache: copy workFile when invalidating workspace
Browse files Browse the repository at this point in the history
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 <matloob@golang.org>
Run-TryBot: Michael Matloob <matloob@golang.org>
gopls-CI: kokoro <noreply+kokoro@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
  • Loading branch information
matloob committed Mar 10, 2022
1 parent e7a12a3 commit 622cf7b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
17 changes: 16 additions & 1 deletion gopls/internal/regtest/workspace/workspace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
)
})
}

Expand Down
1 change: 1 addition & 0 deletions internal/lsp/cache/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 622cf7b

Please sign in to comment.