Skip to content

Commit 908bf14

Browse files
authored
Allow sandboxing for copy_* and fix copy_directory tests (#392)
After some thought, I have to say that forcing a local strategy for copy_file/copy_directory is inappropriate. The point of a sandbox is to catch hermeticity bugs; disabling the sandbox may be useful for performance, but it's up to the user to do it if they trust us - and they can do it via flag. The default should be paranoia and safety. And on the subject of strategies - using a genrule to create an empty directory fails in environments where genrules run remote by default (and thus, copy_directory tests fail). We could, of course, set local=1, but that disables the sandbox and causes scary warnings. Instead, add a proper empty_directory rule to test with.
1 parent 42abf5c commit 908bf14

File tree

3 files changed

+37
-13
lines changed

3 files changed

+37
-13
lines changed

rules/private/copy_common.bzl

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,6 @@ COPY_EXECUTION_REQUIREMENTS = {
2222
# ----------------+-----------------------------------------------------------------------------
2323
# no-cache | Results in the action or test never being cached (remotely or locally)
2424
# ----------------+-----------------------------------------------------------------------------
25-
# local | Precludes the action or test from being remotely cached, remotely executed,
26-
# | or run inside the sandbox. For genrules and tests, marking the rule with the
27-
# | local = True attribute has the same effect.
28-
# ----------------+-----------------------------------------------------------------------------
2925
# See https://bazel.build/reference/be/common-definitions#common-attributes
3026
#
3127
# Copying file & directories is entirely IO-bound and there is no point doing this work
@@ -44,11 +40,6 @@ COPY_EXECUTION_REQUIREMENTS = {
4440
# disk cache stores the directory artifact as a single entry, but the slight performance bump
4541
# comes at the cost of heavy disk cache usage, which is an unmanaged directory that grow beyond
4642
# the bounds of the physical disk.
47-
#
48-
# Sandboxing for this action is wasteful as well since there is a 1:1 mapping of input
49-
# file/directory to output file/directory and no room for non-hermetic inputs to sneak in to the
50-
# input.
5143
"no-remote": "1",
5244
"no-cache": "1",
53-
"local": "1",
5445
}

tests/copy_directory/BUILD.bazel

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# This package aids testing the 'copy_directory' rule.
22

33
load("//rules:copy_directory.bzl", "copy_directory")
4+
load(":empty_directory.bzl", "empty_directory")
45

56
licenses(["notice"])
67

@@ -13,10 +14,8 @@ copy_directory(
1314
out = "dir_copy",
1415
)
1516

16-
genrule(
17-
name = "empty_directory",
18-
outs = ["empty_dir"],
19-
cmd = "mkdir $@",
17+
empty_directory(
18+
name = "empty_dir",
2019
)
2120

2221
copy_directory(
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Copyright 2022 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+
"""Creates an empty directory."""
16+
17+
def _empty_directory_impl(ctx):
18+
out = ctx.actions.declare_directory(ctx.attr.name)
19+
ctx.actions.run_shell(
20+
outputs = [out],
21+
command = "mkdir -p $@",
22+
arguments = [out.path],
23+
mnemonic = "EmptyDirectory",
24+
progress_message = "Creating empty directory %s" % out.path,
25+
use_default_shell_env = True,
26+
execution_requirements = {"no-remote": "1"}, # see rules/private/copy_directory_private.bzl
27+
)
28+
return [DefaultInfo(files = depset(direct = [out]))]
29+
30+
empty_directory = rule(
31+
implementation = _empty_directory_impl,
32+
provides = [DefaultInfo],
33+
doc = "Creates an empty directory with the same name as the target",
34+
)

0 commit comments

Comments
 (0)