Skip to content

Commit 055c93d

Browse files
quvalcopybara-github
authored andcommitted
Switch to path autocompletion after -- for bazel run commands.
Closes #11292. Closes #12957. PiperOrigin-RevId: 363136663
1 parent f0c32de commit 055c93d

File tree

2 files changed

+88
-40
lines changed

2 files changed

+88
-40
lines changed

scripts/bash_completion_test.sh

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ assert_expansion_function() {
171171
local ws=${PWD}
172172
local function="$1" displacement="$2" type="$3" expected="$4" current="$5"
173173
disable_errexit
174-
local actual_result=$(eval "_bazel__${function} \"${ws}\" \"${displacement}\" \"${current}\" \"${type}\"")
174+
local actual_result=$(eval "_bazel__${function} \"${ws}\" \"${displacement}\" \"${current}\" \"${type}\"" | sort)
175175
enable_errexit
176176
assert_equals "$(echo -ne "${expected}")" "${actual_result}"
177177
}
@@ -196,7 +196,7 @@ test_expand_rules_in_package() {
196196

197197
# label should match test and non-test rules
198198
assert_expansion_function "expand_rules_in_package" "" label \
199-
'token_bucket_test \ntoken_bucket_binary ' \
199+
'token_bucket_binary \ntoken_bucket_test ' \
200200
'video/streamer2:token_bucket_'
201201
assert_expansion_function "expand_rules_in_package" "" label \
202202
'stuff ' 'video/streamer2/stuff:s'
@@ -234,7 +234,7 @@ test_expand_rules_in_package() {
234234
# with BAZEL_COMPLETION_ALLOW_TESTS_FOR_RUN set.
235235
BAZEL_COMPLETION_ALLOW_TESTS_FOR_RUN=true \
236236
assert_expansion_function "expand_rules_in_package" "" label-bin \
237-
'token_bucket_test \ntoken_bucket_binary ' 'video/streamer2:to'
237+
'token_bucket_binary \ntoken_bucket_test ' 'video/streamer2:to'
238238

239239
# Test the label-bin expands for test rules, with
240240
# BAZEL_COMPLETION_ALLOW_TESTS_FOR_RUN set.
@@ -303,7 +303,7 @@ test_expand_package_name() {
303303

304304
# label-package
305305
assert_expansion_function "expand_package_name" "" "label-package" \
306-
"//video/streamer2/stuff/\n//video/streamer2/stuff " \
306+
"//video/streamer2/stuff \n//video/streamer2/stuff/" \
307307
"//video/streamer2/stu"
308308
assert_expansion_function "expand_package_name" "" "label-package" \
309309
"//video/notapackage/" \
@@ -357,7 +357,7 @@ test_complete_pattern() {
357357
"//video/streamer2/stu"
358358

359359
assert_expansion_function "complete_pattern" "" label-package \
360-
"//video/streamer2/stuff/\n//video/streamer2/stuff " \
360+
"//video/streamer2/stuff \n//video/streamer2/stuff/" \
361361
"//video/streamer2/stu"
362362

363363
assert_expansion_function "complete_pattern" "" command \
@@ -392,6 +392,19 @@ test_complete_pattern() {
392392
'' 'video/streamer2:ta'
393393
assert_expansion_function "complete_pattern" "video/" label \
394394
'with_special+_,=-.@~chars ' 'streamer2:with_s'
395+
396+
# Path expansion
397+
if [[ -z $PACKAGE_PATH_PREFIX ]]; then
398+
assert_expansion_function "complete_pattern" "" path \
399+
"video/streamer2/BUILD \nvideo/streamer2/names/\nvideo/streamer2/stuff/\nvideo/streamer2/testing/" \
400+
"video/streamer2/"
401+
else
402+
# When $PACKAGE_PATH_PREFIX is set, the "stuff" directory will not be in
403+
# the same directory as the others, so we have to omit it.
404+
assert_expansion_function "complete_pattern" "" path \
405+
"video/streamer2/BUILD \nvideo/streamer2/names/\nvideo/streamer2/testing/" \
406+
"video/streamer2/"
407+
fi
395408
}
396409

397410
#### TESTS #############################################################
@@ -707,6 +720,22 @@ test_target_expansion_in_package() {
707720
'build :s'
708721
}
709722

723+
test_filename_expansion_after_double_dash() {
724+
make_packages
725+
assert_expansion 'run :target -- vid' \
726+
'run :target -- video/'
727+
assert_expansion 'run :target -- video/st' \
728+
'run :target -- video/streamer2/'
729+
assert_expansion 'run :target -- video/streamer2/B' \
730+
'run :target -- video/streamer2/BUILD '
731+
assert_expansion 'run :target -- video/streamer2/n' \
732+
'run :target -- video/streamer2/names/'
733+
734+
# Autocomplete arguments as well.
735+
assert_expansion 'run :target -- --arg=video/streamer2/n' \
736+
'run :target -- --arg=video/streamer2/names/'
737+
}
738+
710739
test_help() {
711740
# "Test that bazel help expands subcommand names"
712741
assert_expansion 'help qu' \

scripts/bazel-complete-template.bash

Lines changed: 54 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,13 @@ _bazel__complete_pattern() {
350350
compgen -S " " -W "${commands}" -- "$current"
351351
;;
352352
path)
353-
compgen -f -- "$current"
353+
for file in $(compgen -f -- "$current"); do
354+
if [[ -d "$file" ]]; then
355+
echo "$file/"
356+
else
357+
echo "$file "
358+
fi
359+
done
354360
;;
355361
*)
356362
compgen -S " " -W "$type" -- "$current"
@@ -571,6 +577,15 @@ _bazel__expand_config() {
571577
compgen -S " " -W "$all_configs" -- "$cur"
572578
}
573579

580+
_bazel__is_after_doubledash() {
581+
for word in "${COMP_WORDS[@]:1:COMP_CWORD-1}"; do
582+
if [[ "$word" == "--" ]]; then
583+
return 0
584+
fi
585+
done
586+
return 1
587+
}
588+
574589
_bazel__complete_stdout() {
575590
local cur=$(_bazel__get_cword) word command displacement workspace
576591

@@ -580,41 +595,45 @@ _bazel__complete_stdout() {
580595
workspace="$(_bazel__get_workspace_path)"
581596
displacement="$(_bazel__get_displacement ${workspace})"
582597

583-
case "$command" in
584-
"") # Expand startup-options or commands
585-
local commands=$(echo "${BAZEL_COMMAND_LIST}" \
586-
| tr " " "\n" | "grep" -v "^${BAZEL_IGNORED_COMMAND_REGEX}$")
587-
_bazel__expand_options "$workspace" "$displacement" "$cur" \
588-
"${commands}\
589-
${BAZEL_STARTUP_OPTIONS}"
590-
;;
598+
if _bazel__is_after_doubledash && [[ "$command" == "run" ]]; then
599+
_bazel__complete_pattern "$workspace" "$displacement" "${cur#*=}" "path"
600+
else
601+
case "$command" in
602+
"") # Expand startup-options or commands
603+
local commands=$(echo "${BAZEL_COMMAND_LIST}" \
604+
| tr " " "\n" | "grep" -v "^${BAZEL_IGNORED_COMMAND_REGEX}$")
605+
_bazel__expand_options "$workspace" "$displacement" "$cur" \
606+
"${commands}\
607+
${BAZEL_STARTUP_OPTIONS}"
608+
;;
591609

592-
*)
593-
case "$cur" in
594-
--config=*) # Expand options:
595-
_bazel__expand_config "$workspace" "$command" "${cur#"--config="}"
596-
;;
597-
-*) # Expand options:
598-
_bazel__expand_options "$workspace" "$displacement" "$cur" \
599-
"$(_bazel__options_for $command)"
600-
;;
601-
*) # Expand target pattern
602-
expansion_pattern="$(_bazel__expansion_for $command)"
603-
NON_QUOTE_REGEX="^[\"']"
604-
if [[ $command = query && $cur =~ $NON_QUOTE_REGEX ]]; then
605-
: # Ideally we would expand query expressions---it's not
606-
# that hard, conceptually---but readline is just too
607-
# damn complex when it comes to quotation. Instead,
608-
# for query, we just expand target patterns, unless
609-
# the first char is a quote.
610-
elif [ -n "$expansion_pattern" ]; then
611-
_bazel__complete_pattern \
612-
"$workspace" "$displacement" "$cur" "$expansion_pattern"
613-
fi
614-
;;
615-
esac
616-
;;
617-
esac
610+
*)
611+
case "$cur" in
612+
--config=*) # Expand options:
613+
_bazel__expand_config "$workspace" "$command" "${cur#"--config="}"
614+
;;
615+
-*) # Expand options:
616+
_bazel__expand_options "$workspace" "$displacement" "$cur" \
617+
"$(_bazel__options_for $command)"
618+
;;
619+
*) # Expand target pattern
620+
expansion_pattern="$(_bazel__expansion_for $command)"
621+
NON_QUOTE_REGEX="^[\"']"
622+
if [[ $command = query && $cur =~ $NON_QUOTE_REGEX ]]; then
623+
: # Ideally we would expand query expressions---it's not
624+
# that hard, conceptually---but readline is just too
625+
# damn complex when it comes to quotation. Instead,
626+
# for query, we just expand target patterns, unless
627+
# the first char is a quote.
628+
elif [ -n "$expansion_pattern" ]; then
629+
_bazel__complete_pattern \
630+
"$workspace" "$displacement" "$cur" "$expansion_pattern"
631+
fi
632+
;;
633+
esac
634+
;;
635+
esac
636+
fi
618637
}
619638

620639
_bazel__to_compreply() {

0 commit comments

Comments
 (0)