Skip to content

Commit a529298

Browse files
Feature: Enable variable expansion for remap_paths in pkg_tar (#947)
1 parent 106429f commit a529298

File tree

6 files changed

+39
-1
lines changed

6 files changed

+39
-1
lines changed

MODULE.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ module(
99
bazel_dep(name = "rules_license", version = "1.0.0")
1010
bazel_dep(name = "rules_python", version = "1.0.0")
1111
bazel_dep(name = "bazel_skylib", version = "1.7.1")
12+
bazel_dep(name = "aspect_bazel_lib", version = "2.14.0")
1213

1314
# Only for development
1415
bazel_dep(name = "platforms", version = "0.0.10", dev_dependency = True)

WORKSPACE

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,12 @@ http_archive(
8181
load("@rules_cc//cc:repositories.bzl", "rules_cc_dependencies")
8282

8383
rules_cc_dependencies()
84+
85+
http_archive(
86+
name = "aspect_bazel_lib",
87+
sha256 = "40ba9d0f62deac87195723f0f891a9803a7b720d7b89206981ca5570ef9df15b",
88+
strip_prefix = "bazel-lib-2.14.0",
89+
url = "https://github.com/bazel-contrib/bazel-lib/releases/download/v2.14.0/bazel-lib-v2.14.0.tar.gz",
90+
)
91+
92+
aspect_bazel_lib_dependencies()

doc_build/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ genrule(
7272
deps = [
7373
":rules_pkg_lib",
7474
"//toolchains/rpm:standard_package",
75+
"@aspect_bazel_lib//lib:expand_make_vars",
7576
],
7677
)
7778
for rule, src in ORDER

pkg/private/tar/tar.bzl

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# limitations under the License.
1414
"""Rules for making .tar files."""
1515

16+
load("@aspect_bazel_lib//lib:expand_make_vars.bzl", "expand_variables")
1617
load("//pkg:providers.bzl", "PackageVariablesInfo")
1718
load(
1819
"//pkg/private:pkg_files.bzl",
@@ -119,8 +120,14 @@ def _pkg_tar_impl(ctx):
119120

120121
# Now we begin processing the files.
121122
path_mapper = None
123+
expanded_remap_paths = {}
122124
if ctx.attr.remap_paths:
123-
path_mapper = lambda path: _remap(ctx.attr.remap_paths, path)
125+
for prefix, replacement in ctx.attr.remap_paths.items():
126+
expanded_prefix = expand_variables(ctx, prefix)
127+
expanded_replacement = expand_variables(ctx, replacement)
128+
expanded_remap_paths[expanded_prefix] = expanded_replacement
129+
130+
path_mapper = lambda path: _remap(expanded_remap_paths, path)
124131

125132
mapping_context = create_mapping_context_from_ctx(
126133
ctx,

tests/tar/BUILD

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,7 @@ py_test(
464464
":test-pkg-tar-from-pkg-files-with-attributes",
465465
":test-pkg-tar-with-attributes",
466466
":test-remap-paths-tree-artifact",
467+
":test-remap-paths-variables",
467468
":test-respect-externally-defined-duplicates.tar",
468469
":test-tar-compression_level--1",
469470
":test-tar-compression_level-3",
@@ -626,6 +627,16 @@ pkg_tar(
626627
},
627628
)
628629

630+
pkg_tar(
631+
name = "test-remap-paths-variables",
632+
srcs = [
633+
":tree_artifact_to_rename",
634+
],
635+
remap_paths = {
636+
"/rename_me": "$(COMPILATION_MODE)",
637+
},
638+
)
639+
629640
#
630641
# Test with symlinks
631642
#

tests/tar/pkg_tar_test.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,15 @@ def test_remap_paths_tree_artifact(self):
285285
]
286286
self.assertTarFileContent('test-remap-paths-tree-artifact.tar', content)
287287

288+
def test_remap_paths_make_variables(self):
289+
content = [
290+
{'name': 'fastbuild', 'isdir': True},
291+
{'name': 'fastbuild/a'},
292+
{'name': 'fastbuild/rename_me', 'isdir': True},
293+
{'name': 'fastbuild/rename_me/should_not_rename'},
294+
]
295+
self.assertTarFileContent('test-remap-paths-variables.tar', content)
296+
288297
def test_externally_defined_duplicate_structure(self):
289298
content = [
290299
{'name': './a'},

0 commit comments

Comments
 (0)