blob: 42966b772fe42682d2b032bb0959996d6808e871 [file]
#!/usr/bin/env bash
#
# Copyright 2026 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# End-to-end tests involving shared actions.
# --- begin runfiles.bash initialization v3 ---
set -uo pipefail; set +e; f=bazel_tools/tools/bash/runfiles/runfiles.bash
source "${RUNFILES_DIR:-/dev/null}/$f" 2>/dev/null || \
source "$(grep -sm1 "^$f " "${RUNFILES_MANIFEST_FILE:-/dev/null}" | cut -f2- -d' ')" 2>/dev/null || \
source "$0.runfiles/$f" 2>/dev/null || \
source "$(grep -sm1 "^$f " "$0.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \
source "$(grep -sm1 "^$f " "$0.exe.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \
{ echo>&2 "ERROR: cannot find $f"; exit 1; }; f=; set -e
# --- end runfiles.bash initialization v3 ---
source "$(rlocation "io_bazel/src/test/shell/integration_test_setup.sh")" \
|| { echo "integration_test_setup.sh not found!" >&2; exit 1; }
function set_up() {
# Clean up all the files possibly written by the test cases.
rm -rf foo
}
function tear_down() {
bazel --noblock_for_lock shutdown 2> /dev/null || \
fail "Failed to shutdown ${PRODUCT_NAME} -- did the test leave a lingering command?"
}
function test_shared_actions_different_rule_class() {
mkdir -p foo
cat > foo/rule.bzl <<'EOF'
def _get_path(file):
return file.path
def r_impl(ctx):
file = ctx.actions.declare_file("file")
args = ctx.actions.args()
args.add_all([file], map_each=_get_path)
ctx.actions.run_shell(
outputs=[file],
command="touch \"$1\"",
arguments=[args])
return DefaultInfo(files=depset([file]))
r = rule(implementation=r_impl)
EOF
cat > foo/rule2.bzl <<'EOF'
load(":rule.bzl", "r", "r_impl")
r2 = rule(implementation=r_impl)
EOF
cat > "foo/BUILD" <<'EOF'
load(":rule.bzl", "r")
load(":rule2.bzl", "r2")
r(name="a")
r2(name="b")
EOF
bazel build //foo:a //foo:b
}
function test_shared_actions_different_rule_class_tree_artifact() {
mkdir -p foo
cat > foo/rule.bzl <<'EOF'
def _get_path(file):
return file.path.replace("/dir/", "/dir2/")
def r_impl(ctx):
dir = ctx.actions.declare_directory("dir")
ctx.actions.run_shell(outputs=[dir], command="touch %s/file" % dir.path)
dir2 = ctx.actions.declare_directory("dir2")
args = ctx.actions.args()
args.add_all([dir], map_each=_get_path, expand_directories=True)
ctx.actions.run_shell(
inputs=[dir],
outputs=[dir2],
command="touch \"$1\"",
arguments=[args])
return DefaultInfo(files=depset([dir2]))
r = rule(implementation=r_impl)
EOF
cat > foo/rule2.bzl <<'EOF'
load(":rule.bzl", "r", "r_impl")
r2 = rule(implementation=r_impl)
EOF
cat > foo/BUILD <<'EOF'
load(":rule.bzl", "r")
load(":rule2.bzl", "r2")
r(name="a")
r2(name="b")
EOF
bazel build //foo:a //foo:b
}
function test_shared_action_directory_input_false_sharing() {
mkdir -p foo
cat > foo/rule.bzl <<'EOF'
def _get_a(file):
if file.is_directory:
return "constant"
return "a"
def _get_b(file):
if file.is_directory:
return "constant"
return "b"
def _r(ctx):
dir = ctx.actions.declare_directory("dir")
ctx.actions.run_shell(outputs=[dir], command="touch %s/file" % dir.path)
file = ctx.actions.declare_file("file")
map_fn = _get_a if ctx.attr.get_a else _get_b
args = ctx.actions.args()
args.add_all([dir], map_each=map_fn)
ctx.actions.run_shell(
inputs = [dir],
outputs = [file],
command = "echo \"$1\" > %s" % file.path,
arguments = [args])
file2 = ctx.actions.declare_file("file_%s" % ctx.label.name)
ctx.actions.run_shell(
inputs = [file],
outputs = [file2],
command = "cp %s %s" % (file.path, file2.path)
)
return DefaultInfo(files=depset([file2]))
r = rule(implementation=_r, attrs={"get_a" : attr.bool()})
EOF
cat > foo/BUILD <<'EOF'
load("rule.bzl", "r")
r(name="a", get_a=True)
r(name="b", get_a=False)
EOF
# TODO(b/160181927): This build should fail.
bazel build //foo:a //foo:b
# TODO(b/160181927): Both will randomly take the value of "a" or "b".
assert_equals \
"$(cat "${PRODUCT_NAME}-bin/foo/file_a")" \
"$(cat "${PRODUCT_NAME}-bin/foo/file_b")"
}
function test_shared_action_cached_and_uncached_key_match() {
mkdir foo
cat > foo/def.bzl <<'EOF'
def _r(ctx):
dir = ctx.actions.declare_directory("dir")
ctx.actions.run_shell(outputs=[dir], command="")
args = ctx.actions.args()
args.add_all([dir], map_each=str)
dir2 = ctx.actions.declare_directory("dir2")
ctx.actions.run_shell(
outputs=[dir2],
inputs=[dir],
command="",
arguments=[args]
)
file = ctx.actions.declare_file("file_" + ctx.label.name)
ctx.actions.run_shell(
inputs=[dir2],
outputs=[file],
command="touch %s" % file.path
)
return DefaultInfo(files=depset([file]))
r = rule(implementation=_r)
EOF
cat > foo/BUILD <<'EOF'
load(":def.bzl", "r")
r(name="a")
r(name="b")
EOF
# Cache the key for the shared action.
bazel build //foo:a
# At analysis time, we cannot use the cached key -- make sure we match it in
# shared actions detection.
bazel build //foo:a //foo:b
}
run_suite "shared actions tests"