internal/lsp: elide details for non-package files

When gc_details is invoked on a file, it can add diagnostics for files
not in that file's package. Some of these will not go away when
gc_details is toggled off. The fix is not emitting compiler details
diagnostics about files not in the package.

Fixes: https://github.com/golang/go/issues/42198
Fixes golang/go#42198
Change-Id: Ib69cdd8b7be96d73ee417711f7241f56fb529836
Reviewed-on: https://go-review.googlesource.com/c/tools/+/266342
Trust: Peter Weinberger <pjw@google.com>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
diff --git a/internal/lsp/source/gc_annotations.go b/internal/lsp/source/gc_annotations.go
index 8bf8ad7..4aac541 100644
--- a/internal/lsp/source/gc_annotations.go
+++ b/internal/lsp/source/gc_annotations.go
@@ -67,6 +67,12 @@
 		if fh == nil {
 			continue
 		}
+		if pkgDir.Filename() != filepath.Dir(fh.URI().Filename()) {
+			// https://github.com/golang/go/issues/42198
+			// sometimes the detail diagnostics generated for files
+			// outside the package can never be taken back.
+			continue
+		}
 		reports[fh.VersionedFileIdentity()] = diagnostics
 	}
 	return reports, parseError