Skip to content

Commit 34b7564

Browse files
authored
feat: Enable some path-mapping support in common rules (#1217)
1 parent dbaa253 commit 34b7564

File tree

6 files changed

+53
-20
lines changed

6 files changed

+53
-20
lines changed

lib/private/copy_common.bzl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ COPY_EXECUTION_REQUIREMENTS = {
1111
# Sandboxing for this action is wasteful since there is a 1:1 mapping of input file/directory to
1212
# output file/directory so little room for non-hermetic inputs to sneak in to the execution.
1313
"no-sandbox": "1",
14+
"supports-path-mapping": "1",
1415
}

lib/private/copy_directory.bzl

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,26 +43,25 @@ def copy_directory_bin_action(
4343
See the caveats above about interactions with remote execution and caching.
4444
4545
"""
46-
args = [
47-
src.path,
48-
dst.path,
49-
]
46+
args = ctx.actions.args()
47+
args.add_all([src, dst], expand_directories = False)
48+
5049
if verbose:
51-
args.append("--verbose")
50+
args.add("--verbose")
5251

5352
if hardlink == "on":
54-
args.append("--hardlink")
53+
args.add("--hardlink")
5554
elif hardlink == "auto" and not src.is_source:
56-
args.append("--hardlink")
55+
args.add("--hardlink")
5756

5857
if preserve_mtime:
59-
args.append("--preserve-mtime")
58+
args.add("--preserve-mtime")
6059

6160
ctx.actions.run(
6261
inputs = [src],
6362
outputs = [dst],
6463
executable = copy_directory_bin,
65-
arguments = args,
64+
arguments = [args],
6665
# TODO: Drop this after https://github.com/bazel-contrib/bazel-lib/issues/1146
6766
env = {"GODEBUG": "winsymlink=0"},
6867
mnemonic = "CopyDirectory",

lib/private/copy_file.bzl

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -68,18 +68,47 @@ def copy_file_action(ctx, src, dst, dir_path = None):
6868

6969
if dst.is_directory:
7070
fail("dst must not be a TreeArtifact")
71+
72+
args = ctx.actions.args()
73+
args.add("cp")
74+
7175
if src.is_directory:
7276
if not dir_path:
7377
fail("dir_path must be set if src is a TreeArtifact")
74-
src_path = "/".join([src.path, dir_path])
78+
79+
# TODO(zbarsky): No path-mapping here, _copy_file_from_directory can support it though.
80+
# We can consider exposing it later.
81+
args.add("/".join([src.path, dir_path]))
7582
else:
76-
src_path = src.path
83+
args.add(src)
84+
85+
args.add(dst)
86+
_run_copy_file_action(ctx, src, dst, args)
87+
88+
def _directory_path(src):
89+
dir = src[DirectoryPathInfo].directory
90+
dir_path = src[DirectoryPathInfo].path
91+
if not dir_path:
92+
fail("dir_path must be set if src is a TreeArtifact")
93+
94+
return "/".join([dir.path, dir_path])
7795

96+
def _copy_file_from_directory(ctx, src, dst):
97+
if dst.is_directory:
98+
fail("dst must not be a TreeArtifact")
99+
100+
args = ctx.actions.args()
101+
args.add("cp")
102+
args.add_all([src], map_each = _directory_path)
103+
args.add(dst)
104+
_run_copy_file_action(ctx, src[DirectoryPathInfo].directory, dst, args)
105+
106+
def _run_copy_file_action(ctx, src, dst, args):
78107
coreutils = ctx.toolchains[_COREUTILS_TOOLCHAIN].coreutils_info
79108

80109
ctx.actions.run(
81110
executable = coreutils.bin,
82-
arguments = ["cp", src_path, dst.path],
111+
arguments = [args],
83112
inputs = [src],
84113
outputs = [dst],
85114
mnemonic = "CopyFile",
@@ -100,12 +129,7 @@ def _copy_file_impl(ctx):
100129
is_executable = ctx.attr.is_executable,
101130
)
102131
elif DirectoryPathInfo in ctx.attr.src:
103-
copy_file_action(
104-
ctx,
105-
ctx.attr.src[DirectoryPathInfo].directory,
106-
ctx.outputs.out,
107-
dir_path = ctx.attr.src[DirectoryPathInfo].path,
108-
)
132+
_copy_file_from_directory(ctx, ctx.attr.src, ctx.outputs.out)
109133
else:
110134
if len(ctx.files.src) != 1:
111135
fail("src must be a single file or a target that provides a DirectoryPathInfo")

lib/private/copy_to_directory.bzl

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -505,11 +505,18 @@ def copy_to_directory_bin_action(
505505
content = json.encode_indent(config),
506506
)
507507

508+
args = ctx.actions.args()
509+
args.add(config_file)
510+
args.add(ctx.label.workspace_name)
511+
512+
# TODO(zbarsky): We path-map this action but the config's content will still mismatch.
513+
# Needs a more holistic fix there.
514+
508515
ctx.actions.run(
509516
inputs = file_inputs + [config_file],
510517
outputs = [dst],
511518
executable = copy_to_directory_bin,
512-
arguments = [config_file.path, ctx.label.workspace_name],
519+
arguments = [args],
513520
# TODO: Drop this after https://github.com/bazel-contrib/bazel-lib/issues/1146
514521
env = {"GODEBUG": "winsymlink=0"},
515522
mnemonic = "CopyToDirectory",

lib/private/expand_template.bzl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ def _expand_template_impl(ctx):
5050
inputs = inputs,
5151
executable = expand_template_info.bin,
5252
toolchain = "@bazel_lib//lib:expand_template_toolchain_type",
53+
execution_requirements = {"supports-path-mapping": "1"},
5354
)
5455
else:
5556
ctx.actions.expand_template(

lib/private/run_binary.bzl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ Possible fixes:
7070
resource_set = resource_set(ctx.attr),
7171
mnemonic = ctx.attr.mnemonic if ctx.attr.mnemonic else None,
7272
progress_message = ctx.attr.progress_message if ctx.attr.progress_message else None,
73-
execution_requirements = ctx.attr.execution_requirements if ctx.attr.execution_requirements else None,
73+
# Target can override if they want to.
74+
execution_requirements = dicts.add({"supports-path-mapping": "1"}, ctx.attr.execution_requirements),
7475
use_default_shell_env = ctx.attr.use_default_shell_env,
7576
env = dicts.add(ctx.configuration.default_shell_env, envs),
7677
)

0 commit comments

Comments
 (0)