workspace: make query regexes anchored in gopackagesdriver

Before this fix, when using VS Code with the Go extension, the initial 
query of all go packages from the gopackagesdriver with a ./... query 
failed with the error:
root package @@//cloud/bmaas/bmdb/model:sqlc_model is missing

This happened because an unanchored regex was used to query for 
go_library rules, and this query also returned sqlc_go_library rules. 
This patches rules_go to add the missing regex anchors.

It turns out that this error caused gopls to discard the result of the 
initial query of everything, and it then queried individual files as 
they were opened in the editor. This no longer happens after this fix, 
so the editor features are now available immediately when opening a go 
source file.

Change-Id: I03ec50777d1cc543eb1cd767b58f61bb5b5261f5
Reviewed-on: https://review.monogon.dev/c/monogon/+/3187
Tested-by: Jenkins CI
Reviewed-by: Lorenz Brun <lorenz@monogon.tech>
diff --git a/WORKSPACE b/WORKSPACE
index d14256d..3bdba33 100644
--- a/WORKSPACE
+++ b/WORKSPACE
@@ -24,6 +24,7 @@
     patch_args = ["-p1"],
     patches = [
         "//third_party/go/patches:rules_go_absolute_embedsrc.patch",
+        "//third_party/go/patches:rules_go_gopackagesdriver_anchor_kind.patch",
     ],
     sha256 = "80a98277ad1311dacd837f9b16db62887702e9f1d1c4c9f796d0121a46c8e184",
     urls = [
diff --git a/third_party/go/patches/rules_go_gopackagesdriver_anchor_kind.patch b/third_party/go/patches/rules_go_gopackagesdriver_anchor_kind.patch
new file mode 100644
index 0000000..b9e4c00
--- /dev/null
+++ b/third_party/go/patches/rules_go_gopackagesdriver_anchor_kind.patch
@@ -0,0 +1,37 @@
+    gopackagesdriver: Make kind query regexes anchored
+
+    A query like kind("go_library", ...) also returns rules that are not go
+    libraries but have go_library as a substring of the rule name, such as
+    sqlc_go_library in the Monogon monorepo. To fix that, add regex anchors.
+
+diff --git a/go/tools/gopackagesdriver/bazel_json_builder.go b/go/tools/gopackagesdriver/bazel_json_builder.go
+index c19f0308..242eeef8 100644
+--- a/go/tools/gopackagesdriver/bazel_json_builder.go
++++ b/go/tools/gopackagesdriver/bazel_json_builder.go
+@@ -80,7 +80,7 @@ func (b *BazelJSONBuilder) fileQuery(filename string) string {
+ 	}
+
+ 	kinds := append(_defaultKinds, additionalKinds...)
+-	return fmt.Sprintf(`kind("%s", same_pkg_direct_rdeps("%s"))`, strings.Join(kinds, "|"), label)
++	return fmt.Sprintf(`kind("^(%s) rule$", same_pkg_direct_rdeps("%s"))`, strings.Join(kinds, "|"), label)
+ }
+
+ func (b *BazelJSONBuilder) getKind() string {
+@@ -104,7 +104,7 @@ func (b *BazelJSONBuilder) localQuery(request string) string {
+ 		request = fmt.Sprintf("%s:*", request)
+ 	}
+
+-	return fmt.Sprintf(`kind("%s", %s)`, b.getKind(), request)
++	return fmt.Sprintf(`kind("^(%s) rule$", %s)`, b.getKind(), request)
+ }
+
+ func (b *BazelJSONBuilder) packageQuery(importPath string) string {
+@@ -113,7 +113,7 @@ func (b *BazelJSONBuilder) packageQuery(importPath string) string {
+ 	}
+
+ 	return fmt.Sprintf(
+-		`kind("%s", attr(importpath, "%s", deps(%s)))`,
++		`kind("^(%s) rule$", attr(importpath, "%s", deps(%s)))`,
+ 		b.getKind(),
+ 		importPath,
+ 		bazelQueryScope)