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,