From 0db9fbe519b6cc2c7d0f3e66e600914b537202a0 Mon Sep 17 00:00:00 2001 From: Peter Lobsinger Date: Mon, 30 Sep 2024 21:10:06 -0700 Subject: [PATCH] feat: support bzlmod runfiles lookups (#953) * feat: support bzlmod repo name aliases in tarred runfiles Under bzlmod, repos have aliases in addition to their canonical names; in order for lookups using these canonical names to function properly, a file name `_repo_mapping` is located in the root of the runfiles tree and consulted to perform repo-name translation. See: https://github.com/bazelbuild/proposals/blob/main/designs/2022-07-21-locating-runfiles-with-bzlmod.md * pre-commit formatting * Enable runfiles test under bzlmod It works now. --- lib/private/tar.bzl | 11 +++++++++++ lib/tests/tar/BUILD.bazel | 2 -- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/lib/private/tar.bzl b/lib/private/tar.bzl index 3507dda..7e4866a 100644 --- a/lib/private/tar.bzl +++ b/lib/private/tar.bzl @@ -147,6 +147,12 @@ def _tar_impl(ctx): args.add(ctx.file.mtree, format = "@%s") inputs.append(ctx.file.mtree) + repo_mappings = [ + src[DefaultInfo].files_to_run.repo_mapping_manifest + for src in ctx.attr.srcs + ] + inputs.extend([m for m in repo_mappings if m != None]) + ctx.actions.run( executable = bsdtar.tarinfo.binary, inputs = depset(direct = inputs, transitive = [bsdtar.default.files] + [ @@ -228,6 +234,7 @@ def _mtree_impl(ctx): continue runfiles_dir = _calculate_runfiles_dir(default_info) + repo_mapping = default_info.files_to_run.repo_mapping_manifest # copy workspace name here just in case to prevent ctx # to be transferred to execution phase. @@ -250,6 +257,10 @@ def _mtree_impl(ctx): map_each = lambda f, e: _expand(f, e, lambda f: _to_rlocation_path(f, workspace_name)), allow_closure = True, ) + if repo_mapping != None: + content.add( + _mtree_line(_vis_encode(runfiles_dir + "/_repo_mapping"), "file", content = _vis_encode(repo_mapping.path)), + ) ctx.actions.write(out, content = content) diff --git a/lib/tests/tar/BUILD.bazel b/lib/tests/tar/BUILD.bazel index 242e5d0..300f56e 100644 --- a/lib/tests/tar/BUILD.bazel +++ b/lib/tests/tar/BUILD.bazel @@ -185,8 +185,6 @@ genrule( # requires runfiles tree, otherwise get # ERROR: cannot find bazel_tools/tools/bash/runfiles/runfiles.bash "@platforms//os:windows": ["@platforms//:incompatible"], - # TODO(sahin): incompatible with bzlmod - "@aspect_bazel_lib//lib:bzlmod": ["@platforms//:incompatible"], "//conditions:default": [], }), toolchains = ["@bsd_tar_toolchains//:resolved_toolchain"],