Skip to content

Commit

Permalink
gopls/internal/regtest: fix regtest failures from "undefined" errors
Browse files Browse the repository at this point in the history
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 <gri@google.com>
Run-TryBot: Robert Findley <rfindley@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
gopls-CI: kokoro <noreply+kokoro@google.com>
  • Loading branch information
findleyr committed Sep 26, 2022
1 parent 1bfc469 commit eb45e98
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
6 changes: 6 additions & 0 deletions gopls/internal/lsp/code_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -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: ")
Expand Down
12 changes: 6 additions & 6 deletions gopls/internal/regtest/diagnostics/diagnostics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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`)))
}
})
}
Expand Down Expand Up @@ -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"))
})
Expand Down Expand Up @@ -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"),
),
)
})
Expand Down Expand Up @@ -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),
),
)
Expand Down
4 changes: 2 additions & 2 deletions gopls/internal/regtest/workspace/workspace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit eb45e98

Please sign in to comment.