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.
This commit is contained in:
Peter Lobsinger 2024-09-30 21:10:06 -07:00 committed by GitHub
parent a75770bef3
commit 0db9fbe519
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 11 additions and 2 deletions

View File

@ -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)

View File

@ -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"],