Skip to content

Commit 43e478d

Browse files
authored
fix(builtin): improve execution requirements for copy file operations (bazel-contrib#3413)
1 parent f710f49 commit 43e478d

File tree

4 files changed

+100
-20
lines changed

4 files changed

+100
-20
lines changed

internal/pkg_web/pkg_web.bzl

+2-8
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"""Contains the pkg_web rule.
1616
"""
1717

18+
load("//third_party/github.com/bazelbuild/bazel-skylib:rules/copy_common.bzl", "COPY_EXECUTION_REQUIREMENTS")
1819
load("@rules_nodejs//nodejs:providers.bzl", "STAMP_ATTR", "StampSettingInfo")
1920

2021
_DOC = """Assembles a web application from source files."""
@@ -42,13 +43,6 @@ See the section on stamping in the README.""",
4243
),
4344
}
4445

45-
# Hints for Bazel spawn strategy
46-
_execution_requirements = {
47-
# Copying files is entirely IO-bound and there is no point doing this work
48-
# remotely.
49-
"no-remote-exec": "1",
50-
}
51-
5246
def _move_files(ctx, root_paths):
5347
"""Moves files into an output directory
5448
@@ -81,7 +75,7 @@ def _move_files(ctx, root_paths):
8175
outputs = [www_dir],
8276
executable = ctx.executable._assembler,
8377
arguments = [args],
84-
execution_requirements = _execution_requirements,
78+
execution_requirements = COPY_EXECUTION_REQUIREMENTS,
8579
env = {"COMPILATION_MODE": ctx.var["COMPILATION_MODE"]},
8680
)
8781
return depset([www_dir])
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Copyright 2019 The Bazel Authors. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
"""Exposes common helpers to help on copying files rules"""
16+
17+
load(
18+
"//third_party/github.com/bazelbuild/bazel-skylib:rules/private/copy_common_private.bzl",
19+
_COPY_EXECUTION_REQUIREMENTS = "COPY_EXECUTION_REQUIREMENTS",
20+
)
21+
22+
COPY_EXECUTION_REQUIREMENTS = _COPY_EXECUTION_REQUIREMENTS
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# Copyright 2019 The Bazel Authors. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
"Helpers for copy rules"
16+
17+
# Hints for Bazel spawn strategy
18+
COPY_EXECUTION_REQUIREMENTS = {
19+
# ----------------+-----------------------------------------------------------------------------
20+
# no-remote | Prevents the action or test from being executed remotely or cached remotely.
21+
# | This is equivalent to using both `no-remote-cache` and `no-remote-exec`.
22+
# ----------------+-----------------------------------------------------------------------------
23+
# no-remote-cache | Results in the action or test never being cached remotely (but it may
24+
# | be cached locally; it may also be executed remotely). Note: for the purposes
25+
# | of this tag, the disk-cache is considered a local cache, whereas the http
26+
# | and gRPC caches are considered remote. If a combined cache is specified
27+
# | (i.e. a cache with local and remote components), it's treated as a remote
28+
# | cache and disabled entirely unless --incompatible_remote_results_ignore_disk
29+
# | is set in which case the local components will be used.
30+
# ----------------+-----------------------------------------------------------------------------
31+
# no-remote-exec | Results in the action or test never being executed remotely (but it may be
32+
# | cached remotely).
33+
# ----------------+-----------------------------------------------------------------------------
34+
# no-cache | Results in the action or test never being cached (remotely or locally)
35+
# ----------------+-----------------------------------------------------------------------------
36+
# no-sandbox | Results in the action or test never being sandboxed; it can still be cached
37+
# | or run remotely - use no-cache or no-remote to prevent either or both of
38+
# | those.
39+
# ----------------+-----------------------------------------------------------------------------
40+
# local | Precludes the action or test from being remotely cached, remotely executed,
41+
# | or run inside the sandbox. For genrules and tests, marking the rule with the
42+
# | local = True attribute has the same effect.
43+
# ----------------+-----------------------------------------------------------------------------
44+
# See https://bazel.google.cn/reference/be/common-definitions?hl=en&authuser=0#common-attributes
45+
#
46+
# Copying file & directories is entirely IO-bound and there is no point doing this work
47+
# remotely.
48+
#
49+
# Also, remote-execution does not allow source directory inputs, see
50+
# https://github.com/bazelbuild/bazel/commit/c64421bc35214f0414e4f4226cc953e8c55fa0d2 So we must
51+
# not attempt to execute remotely in that case.
52+
#
53+
# There is also no point pulling the output file or directory from the remote cache since the
54+
# bytes to copy are already available locally. Conversely, no point in writing to the cache if
55+
# no one has any reason to check it for this action.
56+
#
57+
# Read and writing to disk cache is disabled as well primarily to reduce disk usage on the local
58+
# machine. A disk cache hit of a directory copy could be slghtly faster than a copy since the
59+
# disk cache stores the directory artifact as a single entry, but the slight performance bump
60+
# comes at the cost of heavy disk cache usage, which is an unmanaged directory that grow beyond
61+
# the bounds of the physical disk.
62+
# TODO: run benchmarks to measure the impact on copy_directory
63+
#
64+
# Sandboxing for this action is wasteful as well since there is a 1:1 mapping of input
65+
# file/directory to output file/directory and no room for non-hermetic inputs to sneak in to the
66+
# input.
67+
"no-remote": "1",
68+
"no-remote-cache": "1",
69+
"no-remote-exec": "1",
70+
"no-cache": "1",
71+
"no-sandbox": "1",
72+
"local": "1",
73+
}

third_party/github.com/bazelbuild/bazel-skylib/rules/private/copy_file_private.bzl

+3-12
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,7 @@ cmd.exe (on Windows). `_copy_xfile` marks the resulting file executable,
2424
`_copy_file` does not.
2525
"""
2626

27-
# Hints for Bazel spawn strategy
28-
_execution_requirements = {
29-
# Copying files is entirely IO-bound and there is no point doing this work remotely.
30-
# Also, remote-execution does not allow source directory inputs, see
31-
# https://github.com/bazelbuild/bazel/commit/c64421bc35214f0414e4f4226cc953e8c55fa0d2
32-
# So we must not attempt to execute remotely in that case.
33-
# no-remote | Prevents the action or test from being executed remotely or cached remotely.
34-
# | This is equivalent to using both `no-remote-cache` and `no-remote-exec`.
35-
"no-remote": "1",
36-
}
27+
load(":rules/private/copy_common_private.bzl", _COPY_EXECUTION_REQUIREMENTS = "COPY_EXECUTION_REQUIREMENTS")
3728

3829
def _hash_file(file):
3930
return str(hash(file.path))
@@ -81,7 +72,7 @@ def copy_cmd(ctx, src, dst):
8172
mnemonic = mnemonic,
8273
progress_message = progress_message,
8374
use_default_shell_env = True,
84-
execution_requirements = _execution_requirements,
75+
execution_requirements = _COPY_EXECUTION_REQUIREMENTS,
8576
)
8677

8778
# buildifier: disable=function-docstring
@@ -103,7 +94,7 @@ def copy_bash(ctx, src, dst):
10394
mnemonic = mnemonic,
10495
progress_message = progress_message,
10596
use_default_shell_env = True,
106-
execution_requirements = _execution_requirements,
97+
execution_requirements = _COPY_EXECUTION_REQUIREMENTS,
10798
)
10899

109100
def _copy_file_impl(ctx):

0 commit comments

Comments
 (0)