Skip to content

Commit ad3dd45

Browse files
fmeumcopybara-github
authored andcommitted
Offer Shell completion for top-level packages without subpackages
`bazel build //` now completes to `bazel build //:` when there are no packages beneath the top-level package. While this is mostly an edge case that doesn't apply to real projects, it will become more relevant when completion support is added for external repositories in a future PR. Closes #20679. PiperOrigin-RevId: 597873995 Change-Id: Ida2b26536325acf130a9792b915ed8f3fe425a24
1 parent 63e5f41 commit ad3dd45

2 files changed

Lines changed: 23 additions & 0 deletions

File tree

scripts/bash_completion_test.sh

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -794,4 +794,19 @@ test_workspace_boundary() {
794794
'build //sub_repo/'
795795
}
796796

797+
test_complete_root_package() {
798+
# This test only works for Bazel
799+
if [[ ! " ${COMMAND_ALIASES[*]} " =~ " bazel " ]]; then return; fi
800+
801+
mkdir pkgs_repo
802+
touch pkgs_repo/WORKSPACE
803+
cat > pkgs_repo/BUILD <<'EOF'
804+
cc_binary(name = "main")
805+
EOF
806+
cd pkgs_repo 2>/dev/null
807+
808+
assert_expansion 'build //' \
809+
'build //:'
810+
}
811+
797812
run_suite "Tests of bash completion of 'blaze' command."

scripts/bazel-complete-template.bash

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,14 @@ _bazel__expand_package_name() {
269269
fi
270270
fi
271271
done
272+
# The loop over the compgen -d output above does not include the top-level
273+
# package.
274+
if [ -f $root$current/BUILD.bazel -o -f $root$current/BUILD ]; then
275+
found=1
276+
if [ "${type}" != "label-package" ]; then
277+
echo "${current}:"
278+
fi
279+
fi
272280
[ $found -gt 0 ] && break # Stop searching package path upon first match.
273281
done
274282
}

0 commit comments

Comments
 (0)