internal/lsp/cache: don't skip x_tests

We were marking normal tests as workspace packages, but pruning x_tests.
Don't do that.

Fixes golang/go#39578.

Change-Id: Ia8eceea38e63ac17e40f50d6837de9c7a668469b
Reviewed-on: https://go-review.googlesource.com/c/tools/+/237943
Run-TryBot: Heschi Kreinick <heschi@google.com>
Reviewed-by: zikaeroh <zikaeroh@gmail.com>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
diff --git a/internal/lsp/cache/load.go b/internal/lsp/cache/load.go
index d875119..1068784 100644
--- a/internal/lsp/cache/load.go
+++ b/internal/lsp/cache/load.go
@@ -222,13 +222,13 @@
 			continue
 		}
 
-		switch m.forTest {
-		case "":
+		switch {
+		case m.forTest == "":
 			// A normal package.
 			s.workspacePackages[m.id] = pkgPath
-		case m.pkgPath:
-			// The test variant of some workspace package. To load it, we need to
-			// load the non-test variant with -test.
+		case m.forTest == m.pkgPath, m.forTest+"_test" == m.pkgPath:
+			// The test variant of some workspace package or its x_test.
+			// To load it, we need to load the non-test variant with -test.
 			s.workspacePackages[m.id] = m.forTest
 		default:
 			// A test variant of some intermediate package. We don't care about it.
diff --git a/internal/lsp/testdata/lsp/primarymod/godef/a/a_x_test.go b/internal/lsp/testdata/lsp/primarymod/godef/a/a_x_test.go
index 85f21cc..4631eba 100644
--- a/internal/lsp/testdata/lsp/primarymod/godef/a/a_x_test.go
+++ b/internal/lsp/testdata/lsp/primarymod/godef/a/a_x_test.go
@@ -5,4 +5,5 @@
 )
 
 func TestA2(t *testing.T) { //@TestA2,godef(TestA2, TestA2)
+	Nonexistant() //@diag("Nonexistant", "compiler", "undeclared name: Nonexistant", "error")
 }
diff --git a/internal/lsp/testdata/lsp/summary.txt.golden b/internal/lsp/testdata/lsp/summary.txt.golden
index 7469015..26df609 100644
--- a/internal/lsp/testdata/lsp/summary.txt.golden
+++ b/internal/lsp/testdata/lsp/summary.txt.golden
@@ -7,7 +7,7 @@
 FuzzyCompletionsCount = 8
 RankedCompletionsCount = 120
 CaseSensitiveCompletionsCount = 4
-DiagnosticsCount = 43
+DiagnosticsCount = 44
 FoldingRangesCount = 2
 FormatCount = 6
 ImportCount = 8