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(