gopls/internal/golang/definition: find the right unsafe

The old code had a comment explaining why it was wrong.
The new code looks up the package 'unsafe' refers to.

The old code used a PackageID which might vary in other build systems,
while the new code uses the package path.

Fixes: golang/go#71947
Change-Id: Ie2ead938277df9cbb1e23dc1dd14cb0a3bd6bc24
Reviewed-on: https://go-review.googlesource.com/c/tools/+/809140
LUCI-TryBot-Result: golang-scoped@luci-project-accounts.iam.gserviceaccount.com <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Hongxiang Jiang <hxjiang@golang.org>
diff --git a/gopls/internal/golang/definition.go b/gopls/internal/golang/definition.go
index f28fa9b..0c6d899 100644
--- a/gopls/internal/golang/definition.go
+++ b/gopls/internal/golang/definition.go
@@ -266,16 +266,18 @@
 	if obj.Pkg() == types.Unsafe {
 		// package "unsafe":
 		// parse $GOROOT/src/unsafe/unsafe.go
-		//
-		// (Strictly, we shouldn't assume that the ID of a std
-		// package is its PkgPath, but no Bazel+gopackagesdriver
-		// users have complained about this yet.)
-		unsafe := snapshot.Metadata("unsafe")
+		var unsafe *metadata.Package
+		if mps := snapshot.MetadataGraph().ForPackagePath[metadata.PackagePath("unsafe")]; len(mps) > 0 {
+			unsafe = mps[0]
+		}
 		if unsafe == nil {
 			// If the type checker somehow resolved 'unsafe', we must have metadata
 			// for it.
 			return nil, nil, bug.Errorf("no metadata for package 'unsafe'")
 		}
+		if len(unsafe.GoFiles) == 0 {
+			return nil, nil, bug.Errorf("no files for package 'unsafe'")
+		}
 		uri := unsafe.GoFiles[0]
 		fh, err := snapshot.ReadFile(ctx, uri)
 		if err != nil {