blob: 41bc3773d7e1449b0a2a69a7704dd7b190ca1f19 [file] [log] [blame]
brandjon39ca7272020-12-03 14:40:09 -08001#!/bin/bash
2#
3# Copyright 2020 The Bazel Authors. All rights reserved.
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17# End-to-end test for builtins injection and the various values of
18# --experimental_builtins_bzl_path.
19
20# --- begin runfiles.bash initialization ---
21# Copy-pasted from Bazel's Bash runfiles library (tools/bash/runfiles/runfiles.bash).
22set -euo pipefail
23if [[ ! -d "${RUNFILES_DIR:-/dev/null}" && ! -f "${RUNFILES_MANIFEST_FILE:-/dev/null}" ]]; then
24 if [[ -f "$0.runfiles_manifest" ]]; then
25 export RUNFILES_MANIFEST_FILE="$0.runfiles_manifest"
26 elif [[ -f "$0.runfiles/MANIFEST" ]]; then
27 export RUNFILES_MANIFEST_FILE="$0.runfiles/MANIFEST"
28 elif [[ -f "$0.runfiles/bazel_tools/tools/bash/runfiles/runfiles.bash" ]]; then
29 export RUNFILES_DIR="$0.runfiles"
30 fi
31fi
32if [[ -f "${RUNFILES_DIR:-/dev/null}/bazel_tools/tools/bash/runfiles/runfiles.bash" ]]; then
33 source "${RUNFILES_DIR}/bazel_tools/tools/bash/runfiles/runfiles.bash"
34elif [[ -f "${RUNFILES_MANIFEST_FILE:-/dev/null}" ]]; then
35 source "$(grep -m1 "^bazel_tools/tools/bash/runfiles/runfiles.bash " \
36 "$RUNFILES_MANIFEST_FILE" | cut -d ' ' -f 2-)"
37else
38 echo >&2 "ERROR: cannot find @bazel_tools//tools/bash/runfiles:runfiles.bash"
39 exit 1
40fi
41# --- end runfiles.bash initialization ---
42
43source "$(rlocation "io_bazel/src/test/shell/integration_test_setup.sh")" \
44 || { echo "integration_test_setup.sh not found!" >&2; exit 1; }
45
46# `uname` returns the current platform, e.g "MSYS_NT-10.0" or "Linux".
47# `tr` converts all upper case letters to lower case.
48# `case` matches the result if the `uname | tr` expression to string prefixes
49# that use the same wildcards as names do in Bash, i.e. "msys*" matches strings
50# starting with "msys", and "*" matches everything (it's the default case).
51case "$(uname -s | tr [:upper:] [:lower:])" in
52msys*)
53 # As of 2018-08-14, Bazel on Windows only supports MSYS Bash.
54 declare -r is_windows=true
55 ;;
56*)
57 declare -r is_windows=false
58 ;;
59esac
60
61if "$is_windows"; then
62 # Disable MSYS path conversion that converts path-looking command arguments to
63 # Windows paths (even if they arguments are not in fact paths).
64 export MSYS_NO_PATHCONV=1
65 export MSYS2_ARG_CONV_EXCL="*"
66fi
67
Googlerc8a817a2023-06-23 10:10:42 -070068# override rules_java in bazel otherwise no build succeeds without injection
69[[ $(type -t mock_rules_java_to_avoid_downloading) == function ]] && mock_rules_java_to_avoid_downloading
brandjon39ca7272020-12-03 14:40:09 -080070
71function test_injection() {
72 # //pkg prints _builtins_dummy when loaded.
73 mkdir pkg
74 cat > pkg/BUILD <<'EOF'
75load(":foo.bzl", "foo")
76foo()
77EOF
78 cat > pkg/foo.bzl <<'EOF'
79def foo():
80 print("dummy :: " + str(_builtins_dummy))
81EOF
82 # Provide a different value for the dummy at the path where builtins_bzl is
83 # expected to be in a Bazel source tree.
84 mkdir -p "$BUILTINS_PACKAGE_PATH_IN_SOURCE"
85 cat > "$BUILTINS_PACKAGE_PATH_IN_SOURCE/exports.bzl" <<'EOF'
86exported_toplevels = {"_builtins_dummy": "workspace value"}
87exported_rules = {}
88exported_to_java = {}
89EOF
90 # Provide yet another value at a different arbitrary path.
91 mkdir alternate
92 cat > alternate/exports.bzl <<'EOF'
93exported_toplevels = {"_builtins_dummy": "alternate value"}
94exported_rules = {}
95exported_to_java = {}
96EOF
Googler49eda9f2023-11-17 09:10:43 -080097 # Override the default exec transition (which is in builtins) to avoid
98 # interfering with builtins injection.
99 mkdir exec
100 cat > exec/BUILD <<'EOF'
101EOF
102 cat > exec/dummy_exec_platforms.bzl <<'EOF'
103noop = transition(
104 implementation = lambda settings, attr: { '//command_line_option:is exec configuration': True },
105 inputs = [],
106 outputs = ['//command_line_option:is exec configuration']
107)
108EOF
brandjon39ca7272020-12-03 14:40:09 -0800109
110 # With injection disabled.
111 #
112 # TODO(#11437): Remove this case once builtins injection can no longer be
113 # disabled. Note that eventually, rule migration to Starlark will cause
114 # implicit dependencies/tools to rely on injection, so even a trivial build
115 # without injection may break. (That may also mean we have to update this test
116 # at some point, so that the other builtins roots are based on the one in the
117 # install base, instead of being virtually empty.)
jhorvitz03f8beb2022-04-29 08:58:25 -0700118 bazel build --nobuild //pkg:BUILD --experimental_builtins_dummy=true \
brandjon39ca7272020-12-03 14:40:09 -0800119 --experimental_builtins_bzl_path= \
Googler49eda9f2023-11-17 09:10:43 -0800120 --experimental_exec_config=//exec:dummy_exec_platforms.bzl%noop \
brandjon39ca7272020-12-03 14:40:09 -0800121 &>"$TEST_log" || fail "bazel build failed"
122 expect_log "dummy :: original value"
123
brandjon708e1ce2020-12-15 14:51:06 -0800124 # Using the builtins root that's bundled with bazel.
jhorvitz03f8beb2022-04-29 08:58:25 -0700125 bazel build --nobuild //pkg:BUILD --experimental_builtins_dummy=true \
brandjon708e1ce2020-12-15 14:51:06 -0800126 --experimental_builtins_bzl_path=%bundled% \
Googler49eda9f2023-11-17 09:10:43 -0800127 --experimental_exec_config=//exec:dummy_exec_platforms.bzl%noop \
brandjon708e1ce2020-12-15 14:51:06 -0800128 &>"$TEST_log" || fail "bazel build failed"
129 # "overridden value" comes from the exports.bzl in production Bazel.
130 expect_log "dummy :: overridden value"
brandjon39ca7272020-12-03 14:40:09 -0800131
132 # Using the builtins root located within the client workspace, as if we're
133 # running Bazel in its own source tree.
jhorvitz03f8beb2022-04-29 08:58:25 -0700134 bazel build --nobuild //pkg:BUILD --experimental_builtins_dummy=true \
brandjon39ca7272020-12-03 14:40:09 -0800135 --experimental_builtins_bzl_path=%workspace% \
Googler49eda9f2023-11-17 09:10:43 -0800136 --experimental_exec_config=//exec:dummy_exec_platforms.bzl%noop \
brandjon39ca7272020-12-03 14:40:09 -0800137 &>"$TEST_log" || fail "bazel build failed"
138 expect_log "dummy :: workspace value"
139
140 # Using the builtins root at the path given to the flag. (Need not be within
141 # workspace, though this one is.)
jhorvitz03f8beb2022-04-29 08:58:25 -0700142 bazel build --nobuild //pkg:BUILD --experimental_builtins_dummy=true \
brandjon39ca7272020-12-03 14:40:09 -0800143 --experimental_builtins_bzl_path=alternate \
Googler49eda9f2023-11-17 09:10:43 -0800144 --experimental_exec_config=//exec:dummy_exec_platforms.bzl%noop \
brandjon39ca7272020-12-03 14:40:09 -0800145 &>"$TEST_log" || fail "bazel build failed"
146 expect_log "dummy :: alternate value"
147
148 # Try an incremental updates to the builtins .bzl files.
149 cat > alternate/exports.bzl <<'EOF'
150exported_toplevels = {"_builtins_dummy": "second alternate value"}
151exported_rules = {}
152exported_to_java = {}
153EOF
jhorvitz03f8beb2022-04-29 08:58:25 -0700154 bazel build --nobuild //pkg:BUILD --experimental_builtins_dummy=true \
brandjon39ca7272020-12-03 14:40:09 -0800155 --experimental_builtins_bzl_path=alternate \
Googler49eda9f2023-11-17 09:10:43 -0800156 --experimental_exec_config=//exec:dummy_exec_platforms.bzl%noop \
brandjon39ca7272020-12-03 14:40:09 -0800157 &>"$TEST_log" || fail "bazel build failed"
158 expect_log "dummy :: second alternate value"
brandjon39ca7272020-12-03 14:40:09 -0800159
brandjon64e2a062021-02-01 10:38:21 -0800160 # Ensure builtins .bzl files aren't visible to bazel query the way normal .bzl
161 # files are.
162 bazel query 'buildfiles(//pkg:BUILD)' --experimental_builtins_dummy=true \
163 --experimental_builtins_bzl_path=alternate \
164 &>"$TEST_log" || fail "bazel query failed"
165 expect_not_log "exports.bzl"
166}
brandjon39ca7272020-12-03 14:40:09 -0800167
168run_suite "builtins_injection_test"