brandjon | 39ca727 | 2020-12-03 14:40:09 -0800 | [diff] [blame] | 1 | #!/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). |
| 22 | set -euo pipefail |
| 23 | if [[ ! -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 |
| 31 | fi |
| 32 | if [[ -f "${RUNFILES_DIR:-/dev/null}/bazel_tools/tools/bash/runfiles/runfiles.bash" ]]; then |
| 33 | source "${RUNFILES_DIR}/bazel_tools/tools/bash/runfiles/runfiles.bash" |
| 34 | elif [[ -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-)" |
| 37 | else |
| 38 | echo >&2 "ERROR: cannot find @bazel_tools//tools/bash/runfiles:runfiles.bash" |
| 39 | exit 1 |
| 40 | fi |
| 41 | # --- end runfiles.bash initialization --- |
| 42 | |
| 43 | source "$(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). |
| 51 | case "$(uname -s | tr [:upper:] [:lower:])" in |
| 52 | msys*) |
| 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 | ;; |
| 59 | esac |
| 60 | |
| 61 | if "$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="*" |
| 66 | fi |
| 67 | |
Googler | c8a817a | 2023-06-23 10:10:42 -0700 | [diff] [blame] | 68 | # 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 |
brandjon | 39ca727 | 2020-12-03 14:40:09 -0800 | [diff] [blame] | 70 | |
| 71 | function test_injection() { |
| 72 | # //pkg prints _builtins_dummy when loaded. |
| 73 | mkdir pkg |
| 74 | cat > pkg/BUILD <<'EOF' |
| 75 | load(":foo.bzl", "foo") |
| 76 | foo() |
| 77 | EOF |
| 78 | cat > pkg/foo.bzl <<'EOF' |
| 79 | def foo(): |
| 80 | print("dummy :: " + str(_builtins_dummy)) |
| 81 | EOF |
| 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' |
| 86 | exported_toplevels = {"_builtins_dummy": "workspace value"} |
| 87 | exported_rules = {} |
| 88 | exported_to_java = {} |
| 89 | EOF |
| 90 | # Provide yet another value at a different arbitrary path. |
| 91 | mkdir alternate |
| 92 | cat > alternate/exports.bzl <<'EOF' |
| 93 | exported_toplevels = {"_builtins_dummy": "alternate value"} |
| 94 | exported_rules = {} |
| 95 | exported_to_java = {} |
| 96 | EOF |
Googler | 49eda9f | 2023-11-17 09:10:43 -0800 | [diff] [blame] | 97 | # Override the default exec transition (which is in builtins) to avoid |
| 98 | # interfering with builtins injection. |
| 99 | mkdir exec |
| 100 | cat > exec/BUILD <<'EOF' |
| 101 | EOF |
| 102 | cat > exec/dummy_exec_platforms.bzl <<'EOF' |
| 103 | noop = transition( |
| 104 | implementation = lambda settings, attr: { '//command_line_option:is exec configuration': True }, |
| 105 | inputs = [], |
| 106 | outputs = ['//command_line_option:is exec configuration'] |
| 107 | ) |
| 108 | EOF |
brandjon | 39ca727 | 2020-12-03 14:40:09 -0800 | [diff] [blame] | 109 | |
| 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.) |
jhorvitz | 03f8beb | 2022-04-29 08:58:25 -0700 | [diff] [blame] | 118 | bazel build --nobuild //pkg:BUILD --experimental_builtins_dummy=true \ |
brandjon | 39ca727 | 2020-12-03 14:40:09 -0800 | [diff] [blame] | 119 | --experimental_builtins_bzl_path= \ |
Googler | 49eda9f | 2023-11-17 09:10:43 -0800 | [diff] [blame] | 120 | --experimental_exec_config=//exec:dummy_exec_platforms.bzl%noop \ |
brandjon | 39ca727 | 2020-12-03 14:40:09 -0800 | [diff] [blame] | 121 | &>"$TEST_log" || fail "bazel build failed" |
| 122 | expect_log "dummy :: original value" |
| 123 | |
brandjon | 708e1ce | 2020-12-15 14:51:06 -0800 | [diff] [blame] | 124 | # Using the builtins root that's bundled with bazel. |
jhorvitz | 03f8beb | 2022-04-29 08:58:25 -0700 | [diff] [blame] | 125 | bazel build --nobuild //pkg:BUILD --experimental_builtins_dummy=true \ |
brandjon | 708e1ce | 2020-12-15 14:51:06 -0800 | [diff] [blame] | 126 | --experimental_builtins_bzl_path=%bundled% \ |
Googler | 49eda9f | 2023-11-17 09:10:43 -0800 | [diff] [blame] | 127 | --experimental_exec_config=//exec:dummy_exec_platforms.bzl%noop \ |
brandjon | 708e1ce | 2020-12-15 14:51:06 -0800 | [diff] [blame] | 128 | &>"$TEST_log" || fail "bazel build failed" |
| 129 | # "overridden value" comes from the exports.bzl in production Bazel. |
| 130 | expect_log "dummy :: overridden value" |
brandjon | 39ca727 | 2020-12-03 14:40:09 -0800 | [diff] [blame] | 131 | |
| 132 | # Using the builtins root located within the client workspace, as if we're |
| 133 | # running Bazel in its own source tree. |
jhorvitz | 03f8beb | 2022-04-29 08:58:25 -0700 | [diff] [blame] | 134 | bazel build --nobuild //pkg:BUILD --experimental_builtins_dummy=true \ |
brandjon | 39ca727 | 2020-12-03 14:40:09 -0800 | [diff] [blame] | 135 | --experimental_builtins_bzl_path=%workspace% \ |
Googler | 49eda9f | 2023-11-17 09:10:43 -0800 | [diff] [blame] | 136 | --experimental_exec_config=//exec:dummy_exec_platforms.bzl%noop \ |
brandjon | 39ca727 | 2020-12-03 14:40:09 -0800 | [diff] [blame] | 137 | &>"$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.) |
jhorvitz | 03f8beb | 2022-04-29 08:58:25 -0700 | [diff] [blame] | 142 | bazel build --nobuild //pkg:BUILD --experimental_builtins_dummy=true \ |
brandjon | 39ca727 | 2020-12-03 14:40:09 -0800 | [diff] [blame] | 143 | --experimental_builtins_bzl_path=alternate \ |
Googler | 49eda9f | 2023-11-17 09:10:43 -0800 | [diff] [blame] | 144 | --experimental_exec_config=//exec:dummy_exec_platforms.bzl%noop \ |
brandjon | 39ca727 | 2020-12-03 14:40:09 -0800 | [diff] [blame] | 145 | &>"$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' |
| 150 | exported_toplevels = {"_builtins_dummy": "second alternate value"} |
| 151 | exported_rules = {} |
| 152 | exported_to_java = {} |
| 153 | EOF |
jhorvitz | 03f8beb | 2022-04-29 08:58:25 -0700 | [diff] [blame] | 154 | bazel build --nobuild //pkg:BUILD --experimental_builtins_dummy=true \ |
brandjon | 39ca727 | 2020-12-03 14:40:09 -0800 | [diff] [blame] | 155 | --experimental_builtins_bzl_path=alternate \ |
Googler | 49eda9f | 2023-11-17 09:10:43 -0800 | [diff] [blame] | 156 | --experimental_exec_config=//exec:dummy_exec_platforms.bzl%noop \ |
brandjon | 39ca727 | 2020-12-03 14:40:09 -0800 | [diff] [blame] | 157 | &>"$TEST_log" || fail "bazel build failed" |
| 158 | expect_log "dummy :: second alternate value" |
brandjon | 39ca727 | 2020-12-03 14:40:09 -0800 | [diff] [blame] | 159 | |
brandjon | 64e2a06 | 2021-02-01 10:38:21 -0800 | [diff] [blame] | 160 | # 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 | } |
brandjon | 39ca727 | 2020-12-03 14:40:09 -0800 | [diff] [blame] | 167 | |
| 168 | run_suite "builtins_injection_test" |