From 3fd583c6efd4e5122761a4a10b6f35f2961f0472 Mon Sep 17 00:00:00 2001 From: Fabian Meumertzheim Date: Thu, 17 Aug 2023 14:09:15 -0700 Subject: [PATCH] Add support for more workspace boundary files to bash completion Bash completion now also recognized `WORKSPACE.bazel`, `MODULE.bazel` and `REPO.bazel`. Also fixes a shellcheck finding. Closes #19268. PiperOrigin-RevId: 557931944 Change-Id: Ia2b6e463f4643d9c0d829845fee4228103cf90a0 --- scripts/bash_completion_test.sh | 26 ++++++++++++++++++++++++++ scripts/bazel-complete-template.bash | 10 +++++++--- 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/scripts/bash_completion_test.sh b/scripts/bash_completion_test.sh index 6f6929e70ae5d8..07d7819f9197ce 100755 --- a/scripts/bash_completion_test.sh +++ b/scripts/bash_completion_test.sh @@ -759,4 +759,30 @@ test_info() { 'info --show_make_env ' } +test_workspace_boundary() { + # "Test that workspace boundary files are recognized" + # this test only works for Bazel + if [[ ! " ${COMMAND_ALIASES[*]} " =~ " bazel " ]]; then return; fi + + mkdir -p sub_repo/some/pkg + touch sub_repo/some/pkg/BUILD + cd sub_repo 2>/dev/null + + touch WORKSPACE.bazel + assert_expansion 'build //s' \ + 'build //some/' + + mv WORKSPACE.bazel MODULE.bazel + assert_expansion 'build //s' \ + 'build //some/' + + mv MODULE.bazel REPO.bazel + assert_expansion 'build //s' \ + 'build //some/' + + rm REPO.bazel + assert_expansion 'build //s' \ + 'build //sub_repo/' +} + run_suite "Tests of bash completion of 'blaze' command." diff --git a/scripts/bazel-complete-template.bash b/scripts/bazel-complete-template.bash index 3b6812f387c52c..320d77670c7820 100644 --- a/scripts/bazel-complete-template.bash +++ b/scripts/bazel-complete-template.bash @@ -79,13 +79,17 @@ _bazel__get_rule_match_pattern() { } # Compute workspace directory. Search for the innermost -# enclosing directory with a WORKSPACE file. +# enclosing directory with a boundary file (see +# src/main/cpp/workspace_layout.cc). _bazel__get_workspace_path() { local workspace=$PWD while true; do - if [ -f "${workspace}/WORKSPACE" ]; then + if [ -f "${workspace}/WORKSPACE" ] || \ + [ -f "${workspace}/WORKSPACE.bazel" ] || \ + [ -f "${workspace}/MODULE.bazel" ] || \ + [ -f "${workspace}/REPO.bazel" ]; then break - elif [ -z "$workspace" -o "$workspace" = "/" ]; then + elif [ -z "$workspace" ] || [ "$workspace" = "/" ]; then workspace=$PWD break; fi