blob: cac7a722777d73bea427b60b836afcb6ebf2cf36 [file] [log] [blame]
Jan Schär80402492025-07-02 14:07:34 +00001commit f5e96331ea1f74fe956f0ef62a5e1ce4b81d8bc4
2Author: Jan Schär <jan@monogon.tech>
3Date: Wed Jul 2 13:41:44 2025 +0000
4
5 Request stdlib cache dir in gopackagesdriver
6
7 The stdlib_json_file contains paths of compiled cgo files, which are in
8 the stdlib cache_dir. The gopackagesdriver reads those files, but this
9 dependency was not explicitly declared previously.
10
11 Usually, it works despite the missing dependency. But when you have a
12 Bazel cache configured, and then run `bazel clean` and run the
13 gopackagesdriver again, Bazel will not restore the cache_dir from the
14 Bazel cache. The gopackagesdriver then fails with an error like this:
15
16 error: unable to load JSON files: unable to resolve imports: open
17 [...]/bin/external/rules_go+/stdlib_/gocache/a4/a4f91e8314b27a4bce0e8fbc01bb9736cb9ed10da747af722c5a4f2fcb1213a3-d:
18 no such file or directory
19
20 This change adds a new output group containing the stdlib cache_dir,
21 and requests this from the gopackagesdriver, fixing the problem.
22
23 Upstream PR: https://github.com/bazel-contrib/rules_go/pull/4391
24
25diff --git a/go/tools/gopackagesdriver/aspect.bzl b/go/tools/gopackagesdriver/aspect.bzl
26index c6013d76..dbe56633 100644
27--- a/go/tools/gopackagesdriver/aspect.bzl
28+++ b/go/tools/gopackagesdriver/aspect.bzl
29@@ -65,6 +65,7 @@ def make_pkg_json(ctx, name, pkg_info):
30 def _go_pkg_info_aspect_impl(target, ctx):
31 # Fetch the stdlib JSON file from the inner most target
32 stdlib_json_file = None
33+ stdlib_cache_dir = None
34
35 transitive_json_files = []
36 transitive_export_files = []
37@@ -87,6 +88,7 @@ def _go_pkg_info_aspect_impl(target, ctx):
38 # Fetch the stdlib json from the first dependency
39 if not stdlib_json_file:
40 stdlib_json_file = pkg_info.stdlib_json_file
41+ stdlib_cache_dir = pkg_info.stdlib_cache_dir
42
43 pkg_json_files = []
44 compiled_go_files = []
45@@ -113,9 +115,11 @@ def _go_pkg_info_aspect_impl(target, ctx):
46 # current go_ node.
47 if not stdlib_json_file:
48 stdlib_json_file = ctx.attr._go_stdlib[GoStdLib]._list_json
49+ stdlib_cache_dir = ctx.attr._go_stdlib[GoStdLib].cache_dir
50
51 pkg_info = GoPkgInfo(
52 stdlib_json_file = stdlib_json_file,
53+ stdlib_cache_dir = stdlib_cache_dir,
54 pkg_json_files = depset(
55 direct = pkg_json_files,
56 transitive = transitive_json_files,
57@@ -137,6 +141,7 @@ def _go_pkg_info_aspect_impl(target, ctx):
58 go_pkg_driver_srcs = pkg_info.compiled_go_files,
59 go_pkg_driver_export_file = pkg_info.export_files,
60 go_pkg_driver_stdlib_json_file = depset([pkg_info.stdlib_json_file] if pkg_info.stdlib_json_file else []),
61+ go_pkg_driver_stdlib_cache_dir = pkg_info.stdlib_cache_dir or depset([]),
62 ),
63 ]
64
65diff --git a/go/tools/gopackagesdriver/bazel_json_builder.go b/go/tools/gopackagesdriver/bazel_json_builder.go
66index 76386332..bb50f7a0 100644
67--- a/go/tools/gopackagesdriver/bazel_json_builder.go
68+++ b/go/tools/gopackagesdriver/bazel_json_builder.go
69@@ -159,7 +159,7 @@ func NewBazelJSONBuilder(bazel *Bazel, includeTests bool) (*BazelJSONBuilder, er
70 }
71
72 func (b *BazelJSONBuilder) outputGroupsForMode(mode packages.LoadMode) string {
73- og := "go_pkg_driver_json_file,go_pkg_driver_stdlib_json_file,go_pkg_driver_srcs"
74+ og := "go_pkg_driver_json_file,go_pkg_driver_stdlib_json_file,go_pkg_driver_stdlib_cache_dir,go_pkg_driver_srcs"
75 if mode&packages.NeedExportsFile != 0 {
76 og += ",go_pkg_driver_export_file"
77 }