third_party/rules_go: add gopackagesdriver patch
Change-Id: I398dcc4a3a4812083f4b62c00ea21b727b5a8462
Reviewed-on: https://review.monogon.dev/c/monogon/+/4380
Tested-by: Jenkins CI
Reviewed-by: Lorenz Brun <lorenz@monogon.tech>
diff --git a/third_party/rules_go/cache-fix.patch b/third_party/rules_go/cache-fix.patch
new file mode 100644
index 0000000..cac7a72
--- /dev/null
+++ b/third_party/rules_go/cache-fix.patch
@@ -0,0 +1,77 @@
+commit f5e96331ea1f74fe956f0ef62a5e1ce4b81d8bc4
+Author: Jan Schär <jan@monogon.tech>
+Date: Wed Jul 2 13:41:44 2025 +0000
+
+ Request stdlib cache dir in gopackagesdriver
+
+ The stdlib_json_file contains paths of compiled cgo files, which are in
+ the stdlib cache_dir. The gopackagesdriver reads those files, but this
+ dependency was not explicitly declared previously.
+
+ Usually, it works despite the missing dependency. But when you have a
+ Bazel cache configured, and then run `bazel clean` and run the
+ gopackagesdriver again, Bazel will not restore the cache_dir from the
+ Bazel cache. The gopackagesdriver then fails with an error like this:
+
+ error: unable to load JSON files: unable to resolve imports: open
+ [...]/bin/external/rules_go+/stdlib_/gocache/a4/a4f91e8314b27a4bce0e8fbc01bb9736cb9ed10da747af722c5a4f2fcb1213a3-d:
+ no such file or directory
+
+ This change adds a new output group containing the stdlib cache_dir,
+ and requests this from the gopackagesdriver, fixing the problem.
+
+ Upstream PR: https://github.com/bazel-contrib/rules_go/pull/4391
+
+diff --git a/go/tools/gopackagesdriver/aspect.bzl b/go/tools/gopackagesdriver/aspect.bzl
+index c6013d76..dbe56633 100644
+--- a/go/tools/gopackagesdriver/aspect.bzl
++++ b/go/tools/gopackagesdriver/aspect.bzl
+@@ -65,6 +65,7 @@ def make_pkg_json(ctx, name, pkg_info):
+ def _go_pkg_info_aspect_impl(target, ctx):
+ # Fetch the stdlib JSON file from the inner most target
+ stdlib_json_file = None
++ stdlib_cache_dir = None
+
+ transitive_json_files = []
+ transitive_export_files = []
+@@ -87,6 +88,7 @@ def _go_pkg_info_aspect_impl(target, ctx):
+ # Fetch the stdlib json from the first dependency
+ if not stdlib_json_file:
+ stdlib_json_file = pkg_info.stdlib_json_file
++ stdlib_cache_dir = pkg_info.stdlib_cache_dir
+
+ pkg_json_files = []
+ compiled_go_files = []
+@@ -113,9 +115,11 @@ def _go_pkg_info_aspect_impl(target, ctx):
+ # current go_ node.
+ if not stdlib_json_file:
+ stdlib_json_file = ctx.attr._go_stdlib[GoStdLib]._list_json
++ stdlib_cache_dir = ctx.attr._go_stdlib[GoStdLib].cache_dir
+
+ pkg_info = GoPkgInfo(
+ stdlib_json_file = stdlib_json_file,
++ stdlib_cache_dir = stdlib_cache_dir,
+ pkg_json_files = depset(
+ direct = pkg_json_files,
+ transitive = transitive_json_files,
+@@ -137,6 +141,7 @@ def _go_pkg_info_aspect_impl(target, ctx):
+ go_pkg_driver_srcs = pkg_info.compiled_go_files,
+ go_pkg_driver_export_file = pkg_info.export_files,
+ go_pkg_driver_stdlib_json_file = depset([pkg_info.stdlib_json_file] if pkg_info.stdlib_json_file else []),
++ go_pkg_driver_stdlib_cache_dir = pkg_info.stdlib_cache_dir or depset([]),
+ ),
+ ]
+
+diff --git a/go/tools/gopackagesdriver/bazel_json_builder.go b/go/tools/gopackagesdriver/bazel_json_builder.go
+index 76386332..bb50f7a0 100644
+--- a/go/tools/gopackagesdriver/bazel_json_builder.go
++++ b/go/tools/gopackagesdriver/bazel_json_builder.go
+@@ -159,7 +159,7 @@ func NewBazelJSONBuilder(bazel *Bazel, includeTests bool) (*BazelJSONBuilder, er
+ }
+
+ func (b *BazelJSONBuilder) outputGroupsForMode(mode packages.LoadMode) string {
+- og := "go_pkg_driver_json_file,go_pkg_driver_stdlib_json_file,go_pkg_driver_srcs"
++ og := "go_pkg_driver_json_file,go_pkg_driver_stdlib_json_file,go_pkg_driver_stdlib_cache_dir,go_pkg_driver_srcs"
+ if mode&packages.NeedExportsFile != 0 {
+ og += ",go_pkg_driver_export_file"
+ }