From eb45e986a7f1b6c4699cac573516739609bffd8a Mon Sep 17 00:00:00 2001 From: Robert Findley Date: Mon, 26 Sep 2022 18:01:05 -0400 Subject: [PATCH] gopls/internal/regtest: fix regtest failures from "undefined" errors Following-up on CL 434636, also update gopls regtests to handle the new "undefined: ..." errors replacing "undeclared name: ..." errors in go/types. Change-Id: I53a05623b63851e8165ab3685aff2cdf494fa5b6 Reviewed-on: https://go-review.googlesource.com/c/tools/+/434639 Reviewed-by: Robert Griesemer Run-TryBot: Robert Findley TryBot-Result: Gopher Robot gopls-CI: kokoro --- gopls/internal/lsp/code_action.go | 6 ++++++ .../internal/regtest/diagnostics/diagnostics_test.go | 12 ++++++------ gopls/internal/regtest/workspace/workspace_test.go | 4 ++-- 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/gopls/internal/lsp/code_action.go b/gopls/internal/lsp/code_action.go index 1d062d717e..dea83f5bdd 100644 --- a/gopls/internal/lsp/code_action.go +++ b/gopls/internal/lsp/code_action.go @@ -261,6 +261,12 @@ func importDiagnostics(fix *imports.ImportFix, diagnostics []protocol.Diagnostic if ident == fix.IdentName { results = append(results, diagnostic) } + // "undefined: X" may be an unresolved import at Go 1.20+. + case strings.HasPrefix(diagnostic.Message, "undefined: "): + ident := strings.TrimPrefix(diagnostic.Message, "undefined: ") + if ident == fix.IdentName { + results = append(results, diagnostic) + } // "could not import: X" may be an invalid import. case strings.HasPrefix(diagnostic.Message, "could not import: "): ident := strings.TrimPrefix(diagnostic.Message, "could not import: ") diff --git a/gopls/internal/regtest/diagnostics/diagnostics_test.go b/gopls/internal/regtest/diagnostics/diagnostics_test.go index 473d9b7417..f12bdbae75 100644 --- a/gopls/internal/regtest/diagnostics/diagnostics_test.go +++ b/gopls/internal/regtest/diagnostics/diagnostics_test.go @@ -618,7 +618,7 @@ func main() { env.RegexpReplace("x/x.go", `package x`, `package main`) env.Await(OnceMet( env.DoneWithChange(), - env.DiagnosticAtRegexpWithMessage("x/main.go", `fmt`, "undeclared name"))) + env.DiagnosticAtRegexp("x/main.go", `fmt`))) } }) } @@ -1800,7 +1800,7 @@ var Bar = Foo Run(t, files, func(t *testing.T, env *Env) { env.OpenFile("foo.go") - env.Await(env.DiagnosticAtRegexpWithMessage("bar.go", `Foo`, "undeclared name")) + env.Await(env.DiagnosticAtRegexp("bar.go", `Foo`)) env.RegexpReplace("foo.go", `\+build`, "") env.Await(EmptyDiagnostics("bar.go")) }) @@ -1831,15 +1831,15 @@ package main env.OpenFile("main.go") env.OpenFile("other.go") env.Await( - env.DiagnosticAtRegexpWithMessage("main.go", "asdf", "undeclared name"), - env.DiagnosticAtRegexpWithMessage("main.go", "fdas", "undeclared name"), + env.DiagnosticAtRegexp("main.go", "asdf"), + env.DiagnosticAtRegexp("main.go", "fdas"), ) env.SetBufferContent("other.go", "package main\n\nasdf") // The new diagnostic in other.go should not suppress diagnostics in main.go. env.Await( OnceMet( env.DiagnosticAtRegexpWithMessage("other.go", "asdf", "expected declaration"), - env.DiagnosticAtRegexpWithMessage("main.go", "asdf", "undeclared name"), + env.DiagnosticAtRegexp("main.go", "asdf"), ), ) }) @@ -2082,7 +2082,7 @@ func F[T C](_ T) { var d protocol.PublishDiagnosticsParams env.Await( OnceMet( - env.DiagnosticAtRegexpWithMessage("main.go", `C`, "undeclared name"), + env.DiagnosticAtRegexp("main.go", `C`), ReadDiagnostics("main.go", &d), ), ) diff --git a/gopls/internal/regtest/workspace/workspace_test.go b/gopls/internal/regtest/workspace/workspace_test.go index cf24886cea..92cd174c2e 100644 --- a/gopls/internal/regtest/workspace/workspace_test.go +++ b/gopls/internal/regtest/workspace/workspace_test.go @@ -1157,8 +1157,8 @@ func (Server) Foo() {} // as invalid. So we need to wait for the metadata of main_test.go to be // updated before moving other_test.go back to the main_test package. env.Await( - env.DiagnosticAtRegexpWithMessage("other_test.go", "Server", "undeclared"), - env.DiagnosticAtRegexpWithMessage("main_test.go", "otherConst", "undeclared"), + env.DiagnosticAtRegexp("other_test.go", "Server"), + env.DiagnosticAtRegexp("main_test.go", "otherConst"), ) env.RegexpReplace("other_test.go", "main", "main_test") env.Await(