Skip to content

Commit c66e192

Browse files
committed
Bring tar runfiles up to feature parity with pkg_files.runfiles.
The bulk of the change is to have pkg_tar use pkg_files%add_label_list to process srcs. That brings it in conformance with pkg_files and pkg_zip. This changes the behavior of runfiles to be useful, but it is a breaking change. Fixes: #153 Fixes: #579 Other bits: - do not fail verify_archive_test without a min_size or max_size - change `data` file in the sample executable from BUILD to foo.cc so that it is easier to disambiguate from other files. - fix bug in runfiles support where files were not added to file_deps list. - fix bug in runfiles support where the runfiles base was not computed correctly. - buildifier to make the linters happy.
1 parent dd4cb3c commit c66e192

File tree

8 files changed

+70
-82
lines changed

8 files changed

+70
-82
lines changed

pkg/private/pkg_files.bzl

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ _MappingContext = provider(
7575
"include_runfiles": "bool: include runfiles",
7676
"strip_prefix": "strip_prefix",
7777

78+
"path_mapper": "function to map destination paths",
79+
7880
# Defaults
7981
"default_mode": "Default mode to apply to file without a mode setting",
8082
"default_user": "Default user name to apply to file without a user",
@@ -91,7 +93,9 @@ def create_mapping_context_from_ctx(
9193
allow_duplicates_with_different_content = None,
9294
strip_prefix = None,
9395
include_runfiles = None,
94-
default_mode = None):
96+
default_mode = None,
97+
path_mapper = None
98+
):
9599
"""Construct a MappingContext.
96100
97101
Args: See the provider definition.
@@ -116,6 +120,7 @@ def create_mapping_context_from_ctx(
116120
strip_prefix = strip_prefix,
117121
include_runfiles = include_runfiles,
118122
default_mode = default_mode,
123+
path_mapper = path_mapper or (lambda x: x),
119124
# TODO(aiuto): allow these to be passed in as needed. But, before doing
120125
# that, explore defauilt_uid/gid as 0 rather than None
121126
default_user = "",
@@ -340,7 +345,7 @@ def add_label_list(mapping_context, srcs):
340345
341346
Args:
342347
mapping_context: (r/w) a MappingContext
343-
srcs: List of source objects.
348+
srcs: List of source objects
344349
"""
345350

346351
# Compute the relative path
@@ -390,12 +395,13 @@ def add_from_default_info(
390395
the_executable = get_my_executable(src)
391396
all_files = src[DefaultInfo].files.to_list()
392397
for f in all_files:
393-
d_path = dest_path(f, data_path, data_path_without_prefix)
398+
d_path = mapping_context.path_mapper(
399+
dest_path(f, data_path, data_path_without_prefix))
394400
if f.is_directory:
395401
add_tree_artifact(
396402
mapping_context.content_map,
397-
d_path,
398-
f,
403+
dest_path = d_path,
404+
src = f,
399405
origin = src.label,
400406
mode = mapping_context.default_mode,
401407
user = mapping_context.default_user,
@@ -415,7 +421,16 @@ def add_from_default_info(
415421
if include_runfiles:
416422
runfiles = src[DefaultInfo].default_runfiles
417423
if runfiles:
418-
base_path = d_path + ".runfiles"
424+
mapping_context.file_deps.append(runfiles.files)
425+
426+
# Computing the runfiles root is subtle. It should be based off of
427+
# the executable, but that is not always obvious. When in doubt,
428+
# the first file of DefaultInfo.files should be the right target.
429+
base_file = the_executable or all_files[0]
430+
base_file_path = mapping_context.path_mapper(
431+
dest_path(base_file, data_path, data_path_without_prefix))
432+
base_path = base_file_path + ".runfiles"
433+
419434
for rf in runfiles.files.to_list():
420435
d_path = base_path + "/" + rf.short_path
421436
fmode = "0755" if rf == the_executable else mapping_context.default_mode

pkg/private/tar/tar.bzl

Lines changed: 27 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,15 @@
1313
# limitations under the License.
1414
"""Rules for making .tar files."""
1515

16-
load("//pkg:path.bzl", "compute_data_path", "dest_path")
1716
load("//pkg:providers.bzl", "PackageVariablesInfo")
1817
load(
1918
"//pkg/private:pkg_files.bzl",
2019
"add_directory",
2120
"add_empty_file",
21+
"add_label_list",
2222
"add_single_file",
2323
"add_symlink",
24-
"add_tree_artifact",
2524
"create_mapping_context_from_ctx",
26-
"process_src",
2725
"write_manifest",
2826
)
2927
load("//pkg/private:util.bzl", "setup_output_files", "substitute_package_variables")
@@ -58,16 +56,8 @@ def _pkg_tar_impl(ctx):
5856

5957
# Files needed by rule implementation at runtime
6058
files = []
61-
6259
outputs, output_file, _ = setup_output_files(ctx)
6360

64-
# Compute the relative path
65-
data_path = compute_data_path(ctx.label, ctx.attr.strip_prefix)
66-
data_path_without_prefix = compute_data_path(ctx.label, ".")
67-
68-
# Find a list of path remappings to apply.
69-
remap_paths = ctx.attr.remap_paths
70-
7161
# Start building the arguments.
7262
args = ctx.actions.args()
7363
args.add("--output", output_file.path)
@@ -112,53 +102,36 @@ def _pkg_tar_impl(ctx):
112102
args.add("--mtime", "%d" % ctx.attr.mtime)
113103
if ctx.attr.portable_mtime:
114104
args.add("--mtime", "portable")
105+
if ctx.attr.modes:
106+
for key in ctx.attr.modes:
107+
args.add("--modes", "%s=%s" % (_quote(key), ctx.attr.modes[key]))
108+
if ctx.attr.owners:
109+
for key in ctx.attr.owners:
110+
args.add("--owners", "%s=%s" % (_quote(key), ctx.attr.owners[key]))
111+
if ctx.attr.ownernames:
112+
for key in ctx.attr.ownernames:
113+
args.add(
114+
"--owner_names",
115+
"%s=%s" % (_quote(key), ctx.attr.ownernames[key]),
116+
)
115117

116118
# Now we begin processing the files.
119+
path_mapper = None
120+
if ctx.attr.remap_paths:
121+
path_mapper = lambda path: _remap(ctx.attr.remap_paths, path)
122+
117123
mapping_context = create_mapping_context_from_ctx(
118124
ctx,
119125
label = ctx.label,
120-
include_runfiles = False, # TODO(aiuto): ctx.attr.include_runfiles,
126+
include_runfiles = ctx.attr.include_runfiles,
121127
strip_prefix = ctx.attr.strip_prefix,
122-
default_mode = ctx.attr.mode,
128+
# build_tar does the default modes. Consider moving attribute mapping
129+
# into mapping_context.
130+
default_mode = None,
131+
path_mapper = path_mapper,
123132
)
124133

125-
# Start with all the pkg_* inputs
126-
for src in ctx.attr.srcs:
127-
if not process_src(
128-
mapping_context,
129-
src = src,
130-
origin = src.label,
131-
):
132-
src_files = src[DefaultInfo].files.to_list()
133-
if ctx.attr.include_runfiles:
134-
runfiles = src[DefaultInfo].default_runfiles
135-
if runfiles:
136-
mapping_context.file_deps.append(runfiles.files)
137-
src_files.extend(runfiles.files.to_list())
138-
139-
# Add in the files of srcs which are not pkg_* types
140-
for f in src_files:
141-
d_path = dest_path(f, data_path, data_path_without_prefix)
142-
143-
# Note: This extra remap is the bottleneck preventing this
144-
# large block from being a utility method as shown below.
145-
# Should we disallow mixing pkg_files in srcs with remap?
146-
# I am fine with that if it makes the code more readable.
147-
dest = _remap(remap_paths, d_path)
148-
if f.is_directory:
149-
add_tree_artifact(mapping_context.content_map, dest, f, src.label)
150-
else:
151-
# Note: This extra remap is the bottleneck preventing this
152-
# large block from being a utility method as shown below.
153-
# Should we disallow mixing pkg_files in srcs with remap?
154-
# I am fine with that if it makes the code more readable.
155-
dest = _remap(remap_paths, d_path)
156-
add_single_file(mapping_context, dest, f, src.label)
157-
158-
# TODO(aiuto): I want the code to look like this, but we don't have lambdas.
159-
# transform_path = lambda f: _remap(
160-
# remap_paths, dest_path(f, data_path, data_path_without_prefix))
161-
# add_label_list(mapping_context, ctx.attr.srcs, transform_path)
134+
add_label_list(mapping_context, srcs = ctx.attr.srcs)
162135

163136
# The files attribute is a map of labels to destinations. We can add them
164137
# directly to the content map.
@@ -174,18 +147,6 @@ def _pkg_tar_impl(ctx):
174147
target.label,
175148
)
176149

177-
if ctx.attr.modes:
178-
for key in ctx.attr.modes:
179-
args.add("--modes", "%s=%s" % (_quote(key), ctx.attr.modes[key]))
180-
if ctx.attr.owners:
181-
for key in ctx.attr.owners:
182-
args.add("--owners", "%s=%s" % (_quote(key), ctx.attr.owners[key]))
183-
if ctx.attr.ownernames:
184-
for key in ctx.attr.ownernames:
185-
args.add(
186-
"--owner_names",
187-
"%s=%s" % (_quote(key), ctx.attr.ownernames[key]),
188-
)
189150
for empty_file in ctx.attr.empty_files:
190151
add_empty_file(mapping_context, empty_file, ctx.label)
191152
for empty_dir in ctx.attr.empty_dirs or []:
@@ -289,7 +250,10 @@ pkg_tar_impl = rule(
289250
"extension": attr.string(default = "tar"),
290251
"symlinks": attr.string_dict(),
291252
"empty_files": attr.string_list(),
292-
"include_runfiles": attr.bool(),
253+
"include_runfiles": attr.bool(
254+
doc = ("""Include runfiles for executables. These appear as they would in bazel-bin."""
255+
+ """For example: 'path/to/myprog.runfiles/path/to/my_data.txt'."""),
256+
),
293257
"empty_dirs": attr.string_list(),
294258
"remap_paths": attr.string_dict(),
295259
"compressor": attr.label(

pkg/verify_archive_test_main.py.tpl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ class VerifyArchiveTest(unittest.TestCase):
5050
Args:
5151
min_size: The minimum number of targets we expect.
5252
"""
53+
if min_size <= 0:
54+
return
5355
actual_size = len(self.paths)
5456
self.assertGreaterEqual(
5557
len(self.paths),
@@ -63,7 +65,7 @@ class VerifyArchiveTest(unittest.TestCase):
6365
Args:
6466
max_size: The maximum number of targets we expect.
6567
"""
66-
if max_size < 0:
68+
if max_size <= 0:
6769
return
6870
actual_size = len(self.paths)
6971
self.assertLessEqual(

tests/BUILD

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ cc_library(
116116
cc_binary(
117117
name = "an_executable",
118118
srcs = ["foo.cc"],
119-
data = ["BUILD"],
119+
data = ["foo.cc"],
120120
deps = [
121121
":liba",
122122
":libb",

tests/mappings/executable.manifest.golden

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[
2-
{"dest":"an_executable.runfiles/tests/BUILD","gid":null,"group":null,"mode":"","origin":"@//tests:an_executable","src":"tests/BUILD","type":"file","uid":null,"user":null},
2+
{"dest":"an_executable.runfiles/tests/foo.cc","gid":null,"group":null,"mode":"","origin":"@//tests:an_executable","src":"tests/foo.cc","type":"file","uid":null,"user":null},
33
{"dest":"an_executable.runfiles/tests/an_executable","gid":null,"group":null,"mode":"0755","origin":"@//tests:an_executable","src":"tests/an_executable","type":"file","uid":null,"user":null},
44
{"dest":"an_executable.runfiles/tests/testdata/hello.txt","gid":null,"group":null,"mode":"","origin":"@//tests:an_executable","src":"tests/testdata/hello.txt","type":"file","uid":null,"user":null},
55
{"dest":"an_executable","gid":null,"group":null,"mode":"0755","origin":"@//tests:an_executable","src":"tests/an_executable","type":"file","uid":null,"user":null},

tests/mappings/executable.manifest.windows.golden

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[
2-
{"dest":"an_executable.exe.runfiles/tests/BUILD","gid":null,"group":null,"mode":"","origin":"@//tests:an_executable","src":"tests/BUILD","type":"file","uid":null,"user":null},
2+
{"dest":"an_executable.exe.runfiles/tests/foo.cc","gid":null,"group":null,"mode":"","origin":"@//tests:an_executable","src":"tests/foo.cc","type":"file","uid":null,"user":null},
33
{"dest":"an_executable.exe.runfiles/tests/an_executable.exe","gid":null,"group":null,"mode":"0755","origin":"@//tests:an_executable","src":"tests/an_executable.exe","type":"file","uid":null,"user":null},
44
{"dest":"an_executable.exe.runfiles/tests/testdata/hello.txt","gid":null,"group":null,"mode":"","origin":"@//tests:an_executable","src":"tests/testdata/hello.txt","type":"file","uid":null,"user":null},
55
{"dest":"an_executable.exe","gid":null,"group":null,"mode":"0755","origin":"@//tests:an_executable","src":"tests/an_executable.exe","type":"file","uid":null,"user":null},

tests/tar/BUILD

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,16 +323,32 @@ fake_artifact(
323323
name = "a_program",
324324
files = ["//tests:testdata/executable.sh"],
325325
runfiles = ["BUILD"],
326+
executable = True,
326327
)
327328

328329
pkg_tar(
329330
name = "test-tar-with-runfiles",
330331
srcs = [
331332
":a_program",
333+
"//tests:an_executable",
332334
],
333335
include_runfiles = True,
334336
)
335337

338+
verify_archive_test(
339+
name = "runfiles_test",
340+
target = ":test-tar-with-runfiles",
341+
must_contain = [
342+
"a_program",
343+
"a_program.runfiles/tests/tar/BUILD",
344+
"executable.sh",
345+
"an_executable",
346+
"an_executable.runfiles/tests/foo.cc",
347+
"an_executable.runfiles/tests/an_executable",
348+
"an_executable.runfiles/tests/testdata/hello.txt",
349+
],
350+
)
351+
336352
pkg_tar(
337353
name = "test_tar_leading_dotslash",
338354
srcs = [
@@ -364,7 +380,6 @@ py_test(
364380
":test-tar-strip_prefix-substring.tar",
365381
":test-tar-tree-artifact",
366382
":test-tar-tree-artifact-noroot",
367-
":test-tar-with-runfiles",
368383
":test-tree-input-with-strip-prefix",
369384
":test_tar_leading_dotslash",
370385
":test_tar_package_dir_substitution.tar",

tests/tar/pkg_tar_test.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -244,14 +244,6 @@ def test_tar_with_tree_artifact(self):
244244
self.assertTarFileContent('test-tar-tree-artifact-noroot.tar',
245245
noroot_content)
246246

247-
def test_tar_with_runfiles(self):
248-
content = [
249-
{'name': 'BUILD' },
250-
{'name': 'a_program' },
251-
{'name': 'executable.sh' },
252-
]
253-
self.assertTarFileContent('test-tar-with-runfiles.tar', content)
254-
255247
def test_tar_leading_dotslash(self):
256248
content = [
257249
{'name': './loremipsum.txt'},

0 commit comments

Comments
 (0)