diff options
author | Laszlo Csomor <laszlocsomor@google.com> | 2018-04-26 06:36:50 -0700 |
---|---|---|
committer | Copybara-Service <copybara-piper@google.com> | 2018-04-26 06:38:06 -0700 |
commit | 753f0f64bc6f42f318c56dc5e063409601011529 (patch) | |
tree | 1c7a5bca7508df6879ca28ed1b945d0daa785546 /tools/bash | |
parent | f2f26cf0339c817609ed49ec61bdaec477124a01 (diff) |
Bash,runfiles: ignore RUNFILES_MANIFEST_ONLY
The Bash runfiles library now ignores
$RUNFILES_MANIFEST_ONLY, instead it checks if the
requested runfile exists either under
$RUNFILES_DIR or under the path that
$RUNFILES_MANIFEST_FILE associates it with.
In this sense, the library now reflects "reality"
(returns the file if it exists) rather than a
"promise" (returns paths based on faith in
envvars).
See https://github.com/bazelbuild/bazel/issues/4460
Change-Id: Idbcfede0757e4b8217313b234c94c88bfd780691
PiperOrigin-RevId: 194388639
Diffstat (limited to 'tools/bash')
-rw-r--r-- | tools/bash/runfiles/runfiles.bash | 104 | ||||
-rwxr-xr-x | tools/bash/runfiles/runfiles_test.bash | 55 |
2 files changed, 80 insertions, 79 deletions
diff --git a/tools/bash/runfiles/runfiles.bash b/tools/bash/runfiles/runfiles.bash index b5e3416f7a..8dd82e23b4 100644 --- a/tools/bash/runfiles/runfiles.bash +++ b/tools/bash/runfiles/runfiles.bash @@ -15,12 +15,9 @@ # This Bash script defines functions to handle sh_binary/sh_test runfiles. # # REQUIREMENTS: -# - The RUNFILES_MANIFEST_FILE and/or the RUNFILES_DIR environment variable must -# be set to the absolute path of the runfiles manifest or the -# <rulename>.runfiles directory, respectively. -# - If RUNFILES_MANIFEST_ONLY=1 is set, then RUNFILES_MANIFEST_FILE must be set -# to the absolute path of the runfiles manifest. RUNFILES_DIR may be unset in -# this case. +# - At least one of RUNFILES_MANIFEST_FILE and RUNFILES_DIR environment +# variables must be set, to the absolute path of the runfiles manifest or the +# <rulename>.runfiles directory respectively. # - If RUNFILES_LIB_DEBUG=1 is set, the script will print errors to stderr. # # USAGE: @@ -39,22 +36,20 @@ # # set -euo pipefail # # --- begin runfiles.bash initialization --- -# if [[ "${RUNFILES_MANIFEST_ONLY:-}" != 1 && -z "${RUNFILES_DIR:-}" ]]; then -# if [[ -f "$0.runfiles_manifest" ]]; then -# export RUNFILES_MANIFEST_ONLY=1 -# export RUNFILES_MANIFEST_FILE="$0.runfiles_manifest" -# elif [[ -f "$0.runfiles/MANIFEST" ]]; then -# export RUNFILES_MANIFEST_ONLY=1 -# export RUNFILES_MANIFEST_FILE="$0.runfiles/MANIFEST" -# elif [[ -d "$0.runfiles" ]]; then -# export RUNFILES_DIR="$0.runfiles" -# fi +# if [[ ! -d "${RUNFILES_DIR:-/dev/null}" && ! -f "${RUNFILES_MANIFEST_FILE:-/dev/null}" ]]; then +# if [[ -f "$0.runfiles_manifest" ]]; then +# export RUNFILES_MANIFEST_FILE="$0.runfiles_manifest" +# elif [[ -f "$0.runfiles/MANIFEST" ]]; then +# export RUNFILES_MANIFEST_FILE="$0.runfiles/MANIFEST" +# elif [[ -f "$0.runfiles/bazel_tools/tools/bash/runfiles/runfiles.bash" ]]; then +# export RUNFILES_DIR="$0.runfiles" +# fi # fi -# if [[ "${RUNFILES_MANIFEST_ONLY:-}" == 1 && -f "${RUNFILES_MANIFEST_FILE:-}" ]]; then +# if [[ -f "${RUNFILES_DIR:-/dev/null}/bazel_tools/tools/bash/runfiles/runfiles.bash" ]]; then +# source "${RUNFILES_DIR}/bazel_tools/tools/bash/runfiles/runfiles.bash" +# elif [[ -f "${RUNFILES_MANIFEST_FILE:-/dev/null}" ]]; then # source "$(grep -m1 "^bazel_tools/tools/bash/runfiles/runfiles.bash " \ # "$RUNFILES_MANIFEST_FILE" | cut -d ' ' -f 2-)" -# elif [[ -n "${RUNFILES_DIR:-}" && -d "${RUNFILES_DIR}" ]]; then -# source "${RUNFILES_DIR}/bazel_tools/tools/bash/runfiles/runfiles.bash" # else # echo >&2 "ERROR: cannot find @bazel_tools//tools/bash/runfiles:runfiles.bash" # exit 1 @@ -89,28 +84,15 @@ function rlocation() { fi return 1 else - if [[ "${RUNFILES_MANIFEST_ONLY:-}" == 1 ]]; then - if [[ -n "${RUNFILES_MANIFEST_FILE:-}" \ - && -f "${RUNFILES_MANIFEST_FILE}" ]]; then - grep -m1 "^$1 " "${RUNFILES_MANIFEST_FILE}" | cut -d ' ' -f 2- \ - || return 1 - else - if [[ "${RUNFILES_LIB_DEBUG:-}" == 1 ]]; then - echo >&2 "ERROR[runfiles.bash]: trying to use manifest-based" \ - "runfiles but RUNFILES_MANIFEST_FILE is unset or" \ - "non-existent" \ - "(RUNFILES_MANIFEST_ONLY=\"${RUNFILES_MANIFEST_ONLY:-}\"," \ - "RUNFILES_DIR=\"${RUNFILES_DIR:-}\")" - fi - return 1 - fi - elif [[ -n "${RUNFILES_DIR:-}" && -d "${RUNFILES_DIR}" ]]; then + if [[ -f "${RUNFILES_DIR:-/dev/null}/$1" ]]; then echo "${RUNFILES_DIR}/$1" + elif [[ -f "${RUNFILES_MANIFEST_FILE:-/dev/null}" ]]; then + local -r result=$(grep -m1 "^$1 " "${RUNFILES_MANIFEST_FILE}" | cut -d ' ' -f 2-) + [[ -f "${result:-/dev/null}" ]] && echo "$result" || echo "" else if [[ "${RUNFILES_LIB_DEBUG:-}" == 1 ]]; then - echo >&2 "ERROR[runfiles.bash]: trying to use directory-based" \ - "runfiles but RUNFILES_DIR is unset or non-existent" \ - "(RUNFILES_MANIFEST_ONLY=\"${RUNFILES_MANIFEST_ONLY:-}\"," \ + echo >&2 "ERROR[runfiles.bash]: cannot look up runfile \"$1\" " \ + "(RUNFILES_DIR=\"${RUNFILES_DIR:-}\"," \ "RUNFILES_MANIFEST_FILE=\"${RUNFILES_MANIFEST_FILE:-}\")" fi return 1 @@ -119,31 +101,37 @@ function rlocation() { } export -f rlocation -# Exports the environment variables that subprocesses may need to use runfiles. +# Exports the environment variables that subprocesses need in order to use +# runfiles. # If a subprocess is a Bazel-built binary rule that also uses the runfiles -# libraries under @bazel_tools//tools/bash/runfiles, then that binary needs +# libraries under @bazel_tools//tools/<lang>/runfiles, then that binary needs # these envvars in order to initialize its own runfiles library. function runfiles_export_envvars() { - if [[ "${RUNFILES_MANIFEST_ONLY:-}" == 1 ]]; then - if [[ -z "${RUNFILES_MANIFEST_FILE:-}" \ - && ! -f "$RUNFILES_MANIFEST_FILE" ]]; then - return 1 + if [[ ! -f "${RUNFILES_MANIFEST_FILE:-/dev/null}" \ + && ! -d "${RUNFILES_DIR:-/dev/null}" ]]; then + return 1 + fi + + if [[ ! -f "${RUNFILES_MANIFEST_FILE:-/dev/null}" ]]; then + if [[ -f "$RUNFILES_DIR/MANIFEST" ]]; then + export RUNFILES_MANIFEST_FILE="$RUNFILES_DIR/MANIFEST" + elif [[ -f "${RUNFILES_DIR}_manifest" ]]; then + export RUNFILES_MANIFEST_FILE="${RUNFILES_DIR}_manifest" + else + export RUNFILES_MANIFEST_FILE= fi - if [[ -z "${RUNFILES_DIR:-}" ]]; then - if [[ "$RUNFILES_MANIFEST_FILE" == */MANIFEST \ - && -d "${RUNFILES_MANIFEST_FILE%/MANIFEST}" ]]; then - export RUNFILES_DIR="${RUNFILES_MANIFEST_FILE%/MANIFEST}" - elif [[ "$RUNFILES_MANIFEST_FILE" == *runfiles_manifest \ - && -d "${RUNFILES_MANIFEST_FILE%_manifest}" ]]; then - export RUNFILES_DIR="${RUNFILES_MANIFEST_FILE%_manifest}" - fi + elif [[ ! -d "${RUNFILES_DIR:-/dev/null}" ]]; then + if [[ "$RUNFILES_MANIFEST_FILE" == */MANIFEST \ + && -d "${RUNFILES_MANIFEST_FILE%/MANIFEST}" ]]; then + export RUNFILES_DIR="${RUNFILES_MANIFEST_FILE%/MANIFEST}" + export JAVA_RUNFILES="$RUNFILES_DIR" + elif [[ "$RUNFILES_MANIFEST_FILE" == *_manifest \ + && -d "${RUNFILES_MANIFEST_FILE%_manifest}" ]]; then + export RUNFILES_DIR="${RUNFILES_MANIFEST_FILE%_manifest}" + export JAVA_RUNFILES="$RUNFILES_DIR" + else + export RUNFILES_DIR= fi fi - # No need to define anything if RUNFILES_MANIFEST_ONLY is not 1: it makes no - # difference whether RUNFILES_DIR is defined or not. - - export RUNFILES_MANIFEST_FILE="${RUNFILES_MANIFEST_FILE:-}" - export RUNFILES_DIR="${RUNFILES_DIR:-}" - export JAVA_RUNFILES="${RUNFILES_DIR:-}" } export -f runfiles_export_envvars diff --git a/tools/bash/runfiles/runfiles_test.bash b/tools/bash/runfiles/runfiles_test.bash index 0c1211c310..88ba63cd9d 100755 --- a/tools/bash/runfiles/runfiles_test.bash +++ b/tools/bash/runfiles/runfiles_test.bash @@ -53,14 +53,23 @@ function find_runfiles_lib() { unset runfiles_export_envvars fi - if [[ "${RUNFILES_MANIFEST_ONLY:-}" == "1" ]]; then - grep -m1 "^io_bazel/tools/bash/runfiles/runfiles.bash" \ - "${RUNFILES_MANIFEST_FILE}" | cut -d ' ' -f 2- - elif [[ -n "${RUNFILES_DIR:-}" && -d "$RUNFILES_DIR" ]]; then + if [[ ! -d "${RUNFILES_DIR:-/dev/null}" && ! -f "${RUNFILES_MANIFEST_FILE:-/dev/null}" ]]; then + if [[ -f "$0.runfiles_manifest" ]]; then + export RUNFILES_MANIFEST_FILE="$0.runfiles_manifest" + elif [[ -f "$0.runfiles/MANIFEST" ]]; then + export RUNFILES_MANIFEST_FILE="$0.runfiles/MANIFEST" + elif [[ -f "$0.runfiles/io_bazel/tools/bash/runfiles/runfiles.bash" ]]; then + export RUNFILES_DIR="$0.runfiles" + fi + fi + if [[ -f "${RUNFILES_DIR:-/dev/null}/io_bazel/tools/bash/runfiles/runfiles.bash" ]]; then echo "${RUNFILES_DIR}/io_bazel/tools/bash/runfiles/runfiles.bash" + elif [[ -f "${RUNFILES_MANIFEST_FILE:-/dev/null}" ]]; then + grep -m1 "^io_bazel/tools/bash/runfiles/runfiles.bash " \ + "$RUNFILES_MANIFEST_FILE" | cut -d ' ' -f 2- else echo >&2 "ERROR: cannot find //tools/bash/runfiles:runfiles.bash" - return 1 + exit 1 fi } @@ -126,20 +135,24 @@ function test_rlocation_abs_path() { function test_init_manifest_based_runfiles() { local tmpdir="$(mktemp -d $TEST_TMPDIR/tmp.XXXXXXXX)" - cat > $tmpdir/foo.runfiles_manifest << 'EOF' -a/b c/d -e/f g h + cat > $tmpdir/foo.runfiles_manifest << EOF +a/b $tmpdir/c/d +e/f $tmpdir/g h EOF + mkdir "${tmpdir}/c" + touch "${tmpdir}/c/d" "${tmpdir}/g h" export RUNFILES_DIR= export RUNFILES_MANIFEST_FILE=$tmpdir/foo.runfiles_manifest - export RUNFILES_MANIFEST_ONLY=1 source "$runfiles_lib_path" [[ -z "$(rlocation a)" ]] || fail - [[ "$(rlocation a/b)" == "c/d" ]] || fail - [[ "$(rlocation e/f)" == "g h" ]] || fail [[ -z "$(rlocation c/d)" ]] || fail + [[ "$(rlocation a/b)" == "$tmpdir/c/d" ]] || fail + [[ "$(rlocation e/f)" == "$tmpdir/g h" ]] || fail + rm "$tmpdir/c/d" "$tmpdir/g h" + [[ -z "$(rlocation a/b)" ]] || fail + [[ -z "$(rlocation e/f)" ]] || fail } function test_manifest_based_envvars() { @@ -148,14 +161,12 @@ function test_manifest_based_envvars() { export RUNFILES_DIR= export RUNFILES_MANIFEST_FILE=$tmpdir/foo.runfiles_manifest - export RUNFILES_MANIFEST_ONLY=1 mkdir -p $tmpdir/foo.runfiles source "$runfiles_lib_path" runfiles_export_envvars [[ "${RUNFILES_DIR:-}" == "$tmpdir/foo.runfiles" ]] || fail [[ "${RUNFILES_MANIFEST_FILE:-}" == "$tmpdir/foo.runfiles_manifest" ]] || fail - [[ "${RUNFILES_MANIFEST_ONLY:-}" == 1 ]] || fail } function test_init_directory_based_runfiles() { @@ -163,28 +174,31 @@ function test_init_directory_based_runfiles() { export RUNFILES_DIR=${tmpdir}/mock/runfiles export RUNFILES_MANIFEST_FILE= - export RUNFILES_MANIFEST_ONLY= source "$runfiles_lib_path" - mkdir -p "$RUNFILES_DIR" - [[ "$(rlocation a)" == */mock/runfiles/a ]] || fail - [[ "$(rlocation a/b)" == *mock/runfiles/a/b ]] || fail + mkdir -p "$RUNFILES_DIR/a" + touch "$RUNFILES_DIR/a/b" "$RUNFILES_DIR/c d" + [[ -z "$(rlocation a)" ]] || fail + [[ -z "$(rlocation c/d)" ]] || fail + [[ "$(rlocation a/b)" == "$RUNFILES_DIR/a/b" ]] || fail + [[ "$(rlocation "c d")" == "$RUNFILES_DIR/c d" ]] || fail + [[ -z "$(rlocation "c")" ]] || fail + rm "$RUNFILES_DIR/a/b" "$RUNFILES_DIR/c d" + [[ -z "$(rlocation a/b)" ]] || fail + [[ -z "$(rlocation "c d")" ]] || fail } function test_directory_based_envvars() { export RUNFILES_DIR=mock/runfiles export RUNFILES_MANIFEST_FILE= - export RUNFILES_MANIFEST_ONLY= source "$runfiles_lib_path" runfiles_export_envvars [[ "${RUNFILES_DIR:-}" == "mock/runfiles" ]] || fail [[ -z "${RUNFILES_MANIFEST_FILE:-}" ]] || fail - [[ -z "${RUNFILES_MANIFEST_ONLY:-}" ]] || fail } function main() { - local -r manifest_only="${RUNFILES_MANIFEST_ONLY:-}" local -r manifest_file="${RUNFILES_MANIFEST_FILE:-}" local -r dir="${RUNFILES_DIR:-}" local -r runfiles_lib_path=$(find_runfiles_lib) @@ -192,7 +206,6 @@ function main() { local -r tests=$(declare -F | grep " -f test" | awk '{print $3}') local failure=0 for t in $tests; do - export RUNFILES_MANIFEST_ONLY="$manifest_only" export RUNFILES_MANIFEST_FILE="$manifest_file" export RUNFILES_DIR="$dir" if ! ($t); then |