From e9a7054ff11a520e3b8aceb76a3ba44bb8da4c94 Mon Sep 17 00:00:00 2001 From: Moises Vega Date: Wed, 23 Feb 2022 11:09:34 -0800 Subject: [PATCH] Use merge_all() in emit_archive. (#3068) --- go/private/actions/archive.bzl | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/go/private/actions/archive.bzl b/go/private/actions/archive.bzl index f8f6b7449b..ff99a8e8a5 100644 --- a/go/private/actions/archive.bzl +++ b/go/private/actions/archive.bzl @@ -65,10 +65,26 @@ def emit_archive(go, source = None, _recompile_suffix = ""): direct = [get_archive(dep) for dep in source.deps] runfiles = source.runfiles data_files = runfiles.files - for a in direct: - runfiles = runfiles.merge(a.runfiles) - if a.source.mode != go.mode: - fail("Archive mode does not match {} is {} expected {}".format(a.data.label, mode_string(a.source.mode), mode_string(go.mode))) + + # After bazel version 5.0.0, skylark introduced a new API called merge_all(). + # There is a recommendation in the release notes to use merge_all() if we hit + # the depth limit because of using merge() in a loop; we saw some repositories + # hitting this issue after upgrading to 5.0.0. To keep it backward compatible, + # we will fall back to the old logic if the array doesn't have merge_all(). + # TODO: remove this check after the MINIMUM_BAZEL_VERSION becomes >= 5.0.0 + # https://blog.bazel.build/2022/01/19/bazel-5.0.html#starlark-build-language + if hasattr(runfiles, "merge_all"): + files = [] + for a in direct: + files.append(a.runfiles) + if a.source.mode != go.mode: + fail("Archive mode does not match {} is {} expected {}".format(a.data.label, mode_string(a.source.mode), mode_string(go.mode))) + runfiles.merge_all(files) + else: + for a in direct: + runfiles = runfiles.merge(a.runfiles) + if a.source.mode != go.mode: + fail("Archive mode does not match {} is {} expected {}".format(a.data.label, mode_string(a.source.mode), mode_string(go.mode))) importmap = "main" if source.library.is_main else source.library.importmap importpath, _ = effective_importpath_pkgpath(source.library)