From d3ca953f16ec12de59726dbc18eac237122aec36 Mon Sep 17 00:00:00 2001 From: Peter Lobsinger Date: Fri, 27 Sep 2024 21:42:41 -0700 Subject: [PATCH 1/3] 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 --- lib/private/tar.bzl | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/lib/private/tar.bzl b/lib/private/tar.bzl index 3507dda06..fa7b83017 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) From 85c85821a7dda429e0e8ad9b1ea95bf496356631 Mon Sep 17 00:00:00 2001 From: Peter Lobsinger Date: Fri, 27 Sep 2024 21:47:40 -0700 Subject: [PATCH 2/3] pre-commit formatting --- lib/private/tar.bzl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/private/tar.bzl b/lib/private/tar.bzl index fa7b83017..7e4866aaf 100644 --- a/lib/private/tar.bzl +++ b/lib/private/tar.bzl @@ -259,7 +259,7 @@ def _mtree_impl(ctx): ) if repo_mapping != None: content.add( - _mtree_line(_vis_encode(runfiles_dir + "/_repo_mapping"), "file", content = _vis_encode(repo_mapping.path)) + _mtree_line(_vis_encode(runfiles_dir + "/_repo_mapping"), "file", content = _vis_encode(repo_mapping.path)), ) ctx.actions.write(out, content = content) From f18a3a83ca43e3de48b63f94e0fa22c327cb405e Mon Sep 17 00:00:00 2001 From: Peter Lobsinger Date: Mon, 30 Sep 2024 17:56:15 -0700 Subject: [PATCH 3/3] Enable runfiles test under bzlmod It works now. --- lib/tests/tar/BUILD.bazel | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/tests/tar/BUILD.bazel b/lib/tests/tar/BUILD.bazel index 242e5d06b..300f56eb1 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"],