[gopls-release-branch.0.4] internal/lsp: don't show diagnostic in go.mod for direct dependencies

Fixes golang/go#40470.

Change-Id: I4831b837e6db5cc344c20480abde1e688ddab0dc
Reviewed-on: https://go-review.googlesource.com/c/tools/+/245537
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Cottrell <iancottrell@google.com>
(cherry picked from commit 8afe7e0893eea57229a96c71adc31b816a5b1352)
Reviewed-on: https://go-review.googlesource.com/c/tools/+/245541
diff --git a/internal/lsp/cache/mod_tidy.go b/internal/lsp/cache/mod_tidy.go
index 566babb..b622a51 100644
--- a/internal/lsp/cache/mod_tidy.go
+++ b/internal/lsp/cache/mod_tidy.go
@@ -436,8 +436,12 @@
 	for _, pkg := range pkgs {
 		missingPkgs := map[string]*modfile.Require{}
 		for _, imp := range pkg.Imports() {
-			if req, ok := missingMods[imp.PkgPath()]; ok {
+			found := func(req *modfile.Require) {
 				missingPkgs[imp.PkgPath()] = req
+				matchedMissingMods[req] = struct{}{}
+			}
+			if req, ok := missingMods[imp.PkgPath()]; ok {
+				found(req)
 				break
 			}
 			// If the import is a package of the dependency, then add the
@@ -457,8 +461,7 @@
 				}
 			}
 			if req, ok := missingMods[match]; ok {
-				missingPkgs[imp.PkgPath()] = req
-				matchedMissingMods[req] = struct{}{}
+				found(req)
 			}
 		}
 		if len(missingPkgs) > 0 {