Skip to content

Commit 60f55c9

Browse files
authored
Cleanup GoArchiveData runfiles handling (#4024)
**What type of PR is this?** Starlark cleanup **What does this PR do? Why is it needed?** We don't need to make copies of the lists **Which issues(s) does this PR fix?** Fixes # **Other notes for review**
1 parent d01b20e commit 60f55c9

File tree

4 files changed

+22
-16
lines changed

4 files changed

+22
-16
lines changed

go/private/actions/archive.bzl

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,13 @@ def emit_archive(go, source = None, _recompile_suffix = "", recompile_internal_d
6969
out_nogo_validation = go.declare_file(go, name = source.library.name, ext = pre_ext + ".nogo")
7070

7171
direct = [get_archive(dep) for dep in source.deps]
72-
runfiles = source.runfiles
73-
data_files = runfiles.files
7472

7573
files = []
7674
for a in direct:
7775
files.append(a.runfiles)
7876
if a.source.mode != go.mode:
7977
fail("Archive mode does not match {} is {} expected {}".format(a.data.label, mode_string(a.source.mode), mode_string(go.mode)))
80-
runfiles = runfiles.merge_all(files)
78+
runfiles = source.runfiles.merge_all(files)
8179

8280
importmap = "main" if source.library.is_main else source.library.importmap
8381
importpath, _ = effective_importpath_pkgpath(source.library)
@@ -189,7 +187,7 @@ def emit_archive(go, source = None, _recompile_suffix = "", recompile_internal_d
189187
file = out_lib,
190188
export_file = out_export,
191189
facts_file = out_facts,
192-
data_files = as_tuple(data_files),
190+
runfiles = source.runfiles,
193191
_validation_output = out_nogo_validation,
194192
_cgo_deps = as_tuple(cgo_deps),
195193
)

go/private/rules/test.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -679,7 +679,7 @@ def _recompile_external_deps(go, external_source, internal_archive, library_labe
679679
x_defs = dict(arc_data._x_defs),
680680
deps = deps,
681681
gc_goopts = as_list(arc_data._gc_goopts),
682-
runfiles = go._ctx.runfiles(files = arc_data.data_files),
682+
runfiles = arc_data.runfiles,
683683
cgo = arc_data._cgo,
684684
cdeps = as_list(arc_data._cdeps),
685685
cppopts = as_list(arc_data._cppopts),

go/private/tools/path.bzl

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,13 @@ def _go_path_impl(ctx):
5858
importpath = importpath,
5959
dir = "src/" + pkgpath,
6060
srcs = as_list(archive.orig_srcs),
61-
data = as_list(archive.data_files),
61+
runfiles = archive.runfiles,
6262
embedsrcs = as_list(archive._embedsrcs),
6363
pkgs = {mode: archive.file},
6464
)
6565
if pkgpath in pkg_map:
66-
_merge_pkg(pkg_map[pkgpath], pkg)
67-
else:
68-
pkg_map[pkgpath] = pkg
66+
pkg = _merge_pkg(pkg_map[pkgpath], pkg)
67+
pkg_map[pkgpath] = pkg
6968

7069
# Build a manifest file that includes all files to copy/link/zip.
7170
inputs = []
@@ -95,7 +94,7 @@ def _go_path_impl(ctx):
9594
_add_manifest_entry(manifest_entries, manifest_entry_map, inputs, f, dst)
9695
if ctx.attr.include_data:
9796
for pkg in pkg_map.values():
98-
for f in pkg.data:
97+
for f in pkg.runfiles.files.to_list():
9998
parts = f.path.split("/")
10099
if "testdata" in parts:
101100
i = parts.index("testdata")
@@ -261,12 +260,21 @@ go_path = rule(
261260

262261
def _merge_pkg(x, y):
263262
x_srcs = {f.path: None for f in x.srcs}
264-
x_data = {f.path: None for f in x.data}
265263
x_embedsrcs = {f.path: None for f in x.embedsrcs}
266-
x.srcs.extend([f for f in y.srcs if f.path not in x_srcs])
267-
x.data.extend([f for f in y.data if f.path not in x_data])
268-
x.embedsrcs.extend([f for f in y.embedsrcs if f.path not in x_embedsrcs])
269-
x.pkgs.update(y.pkgs)
264+
265+
# Not all bazel versions support `dict1 | dict2` yet.
266+
pkgs = dict()
267+
pkgs.update(x.pkgs)
268+
pkgs.update(y.pkgs)
269+
270+
return struct(
271+
importpath = x.importpath,
272+
dir = x.dir,
273+
srcs = x.srcs + [f for f in y.srcs if f.path not in x_srcs],
274+
runfiles = x.runfiles.merge(y.runfiles),
275+
embedsrcs = x.embedsrcs + [f for f in y.embedsrcs if f.path not in x_embedsrcs],
276+
pkgs = pkgs,
277+
)
270278

271279
def _add_manifest_entry(entries, entry_map, inputs, src, dst):
272280
if dst in entry_map:

go/providers.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ rule. Instead, it's referenced in the ``data`` field of GoArchive_.
274274
+--------------------------------+-----------------------------------------------------------------+
275275
| The unmodified sources provided to the rule, including .go, .s, .h, .c files. |
276276
+--------------------------------+-----------------------------------------------------------------+
277-
| :param:`data_files` | :type:`tuple of File` |
277+
| :param:`runfiles` | :type:`runfiles` |
278278
+--------------------------------+-----------------------------------------------------------------+
279279
| Data files that should be available at runtime to binaries and tests built |
280280
| from this archive. |

0 commit comments

Comments
 (0)