Closed georglauterbach closed 7 months ago
This is a duplicate of #375. Maybe I should support it without any adjustments.
kubectl
seems to use cobra to generate its completion, but kubectl
seems to modify the result of the cobra completion. ble.sh
already does dynamic adjustments to the cobra completions to make it work with aliases, but maybe kubectl
's modification to the completion code breaks it.
$ kubectl completion bash
I should have searched the issue tracker more thoroughly, I am sorry. Here is what you asked for:
kubectl completion bash
I can also confirm that shopt -s progcomp_alias
works, as described in #375.
I could reproduce the problem (with a mock kubectl
command). I tried to investigate what is happening here, but I now suspect a bug of Bash. In the plain Bash (without ble.sh), I observe the following behavior:
$ alias e='echo ehllo'
$ (eval e&)
hello
$ (true && eval e&)
bash: line 5: e: command not found
$ (true && { true; eval e & })
hello
$ (true && { eval e & })
hello
It is strange that the behavior of alias expansion changes by the presence of the condition true &&
, and it is also strange that the behavior is only observed when eval e &
is directly specified after &&
. I observe the same behavior in all the currently available Bash versions 1.14 to 5.2 and the devel branch. I also tested other shells (including yash, zsh, ksh93+u, and mksh), but I couldn't find this strange behavior in any other shells.
I tried to find any relevant description in Bash Reference Manual, but I couldn't find it, though I haven't carefully checked all of the descriptions in this long manual. The only description that might be related and that I could find is that the alias expansion is only performed in interactive shells by default. What defines the interactive shells is ambiguous in Bash; (Bash internally has an "interactive" switch that frequently changes by the context even in the same interactive session).
Very interesting, I didn't know that! Thank you for investigating this. What do you think, what's the best way of going forward? I can live with shopt-s ...
.
I'll later add a workaround to ble.sh.
I'm now investigating further in the Bash source code. It seems that expand_aliases
becomes somehow 0
in true && xxxx &
. This can also be confirmed in the following commands:
$ (shopt -p expand_aliases)
shopt -s expand_aliases
$ (true && shopt -p expand_aliases)
shopt -s expand_aliases
$ (true && shopt -p expand_aliases &)
shopt -u expand_aliases
$ (true && { shopt -p expand_aliases & })
shopt -s expand_aliases
We observe that expand_aliases
is unset only in the third case.
This really is rather weird, and it indicates a bug in Bash to me as well; it least it is totally unexpected behavior.
Thanks for the reply. I actually found a code comment in Bash's source code (execute_cmd.c:1583
):
ois = interactive_shell;
interactive_shell = 0;
/* This test is to prevent alias expansion by interactive shells that
run `(command) &' but to allow scripts that have enabled alias
expansion with `shopt -s expand_alias' to continue to expand
aliases. */
if (ois != interactive_shell)
expand_aliases = expaliases_flag = 0;
}
This implies that Bash's behavior for the alias expansions in (command) &
is somehow an intended one. The following is an additional test cases (I found after seeing the above code comment):
$ (shopt -p expand_aliases &)
shopt -s expand_aliases
$ ({ shopt -p expand_aliases; } &)
shopt -u expand_aliases
$ ((shopt -p expand_aliases) &)
shopt -u expand_aliases
I still feel it strange that the alias expansion is performed for cmd &
, while it is not performed for (cmd) &
. The other shells do not behave in that way.
I added a fix in commit 67e2d1ab3afa28edc34d6ffae8ff1e861ede3337. Could you update ble.sh by running ble-update
and check if the problem is fixed (without the user's manual adjustment of shopt -s progcomp_alias
)?
Note: Actually, commit 67e2d1ab3afa28edc34d6ffae8ff1e861ede3337 is a fix for another bug that I found in the same part. Although that bug didn't affect the completion behavior reported here, the fix to the bug also works around the problem of Bash's strange behavior.
I ran ble-update
and I am now on ble.sh, version 0.4.0-devel4+50d6f1bb (noarch)
, but without shopt -s progcomp_alias
there is no proper completion for the alias k
.
Thank you for checking.
but without
shopt -s progcomp_alias
there is no proper completion for the aliask
.
Does that happen even with the following setting instructed by kubectl?
complete -o default -F __start_kubectl k
With
complete -o default -F __start_kubectl k
it works, nice! The shopt -s progcomp_alias
is not required anymore.
Thanks for the confirmation.
OK, then that's intended. With neither of complete -o default -F __start_kubectl k
or shopt -s progcomop_alias
, there is nothing to instruct the shell to complete k
and Bash indeed does nothing with that setup. So it is natural for ble.sh not to complete anything with that setup too.
For shopt -s progcomp_alias
, I actually recommend setting it. I added the fix this time to make completion settings for plain Bash also work with ble.sh, but turning on progcomp_alias
enables completion of more aliases without additional settings.
I understand, that's good to know. I'll add it to my setup then! This issue can be closed now I guess :)
Thanks again for the report!
ble version: 0.4.0-devel4+29cd8f10 Bash version: 5.2.15(1)-release (x86_64-pc-linux-gnu)
I use the alias
(in order to save myself from writing
kubectl
) all the time. I have installed the Bash completion forkubectl
withand it works fine. According to the official Kubernetes wiki, an alias can be created and completed with
When ble.sh is not
source
ed, this works fine as well. As soon as I source ble.sh, in my case withand later attach, the alias stops working.