From 96d05aa12061649f10c7fc8b9d385e6ee297fc9a Mon Sep 17 00:00:00 2001 From: Robert Findley Date: Mon, 8 Aug 2022 16:36:42 -0400 Subject: [PATCH] gopls/internal/regtest: fix TestFailingDiagnosticClearingOnEdit This test was asserting immediately on the non-presence of published diagnostics, and therefore was racing with the diagnostics calculation pass. Following https://go.dev/cl/420539 this became a flake, because once gopls has calculated diagnostics for the open event, it will actually publish empty diagnostics for the opened file. Update the test to use a OnceMet so that we only evaluate the expectation once we've finished the diagnostics pass. As a result, the test deterministically failed, which was fixed by using the EmptyOrNoDiagnostics expectation. Fixes golang/go#54271 Change-Id: Id3f220ce44c7996132699a724b6c627f034e367f Reviewed-on: https://go-review.googlesource.com/c/tools/+/422136 gopls-CI: kokoro Run-TryBot: Robert Findley Reviewed-by: Peter Weinberger TryBot-Result: Gopher Robot --- gopls/internal/regtest/misc/failures_test.go | 19 ++++++++++++++----- internal/lsp/regtest/env.go | 1 + 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/gopls/internal/regtest/misc/failures_test.go b/gopls/internal/regtest/misc/failures_test.go index 86c9b223c94..185a68cfc8b 100644 --- a/gopls/internal/regtest/misc/failures_test.go +++ b/gopls/internal/regtest/misc/failures_test.go @@ -39,9 +39,12 @@ func main() { }) } -// badPackageDup contains a duplicate definition of the 'a' const. -// this is from diagnostics_test.go, -const badPackageDup = ` +// This test demonstrates a case where gopls is confused by line directives, +// and fails to surface type checking errors. +func TestFailingDiagnosticClearingOnEdit(t *testing.T) { + // badPackageDup contains a duplicate definition of the 'a' const. + // this is from diagnostics_test.go, + const badPackageDup = ` -- go.mod -- module mod.com @@ -56,11 +59,17 @@ package consts const a = 2 ` -func TestFailingDiagnosticClearingOnEdit(t *testing.T) { Run(t, badPackageDup, func(t *testing.T, env *Env) { env.OpenFile("b.go") + // no diagnostics for any files, but there should be - env.Await(NoDiagnostics("a.go"), NoDiagnostics("b.go")) + env.Await( + OnceMet( + env.DoneWithOpen(), + EmptyOrNoDiagnostics("a.go"), + EmptyOrNoDiagnostics("b.go"), + ), + ) // Fix the error by editing the const name in b.go to `b`. env.RegexpReplace("b.go", "(a) = 2", "b") diff --git a/internal/lsp/regtest/env.go b/internal/lsp/regtest/env.go index f8a68b34190..77744ae614a 100644 --- a/internal/lsp/regtest/env.go +++ b/internal/lsp/regtest/env.go @@ -282,6 +282,7 @@ func (a *Awaiter) DiagnosticsFor(name string) *protocol.PublishDiagnosticsParams } func (e *Env) Await(expectations ...Expectation) { + e.T.Helper() if err := e.Awaiter.Await(e.Ctx, expectations...); err != nil { e.T.Fatal(err) }