scop / bash-completion

Programmable completion functions for bash
GNU General Public License v2.0
2.9k stars 380 forks source link

Bash completions causing syntax error near unexpected token `(' #1162

Closed satmandu closed 5 months ago

satmandu commented 5 months ago

Describe the bug

Getting this after installing 2.12 and 2.13 whenever I open a new shell using bash 5.2 patchlevel 26:

bash: _comp_complete_longopt: line 6: syntax error near unexpected token `('
bash: _comp_complete_longopt: line 6: ` --!(no-*)dir*)'
bash: error importing function definition for `_comp_complete_longopt'
bash: _comp_compgen_usage: line 11: syntax error near unexpected token `('
bash: _comp_compgen_usage: line 11: ` -?(\[)+([a-zA-Z0-9?]))'
bash: error importing function definition for `_comp_compgen_usage'
bash: _comp_complete_longopt: line 6: syntax error near unexpected token `('
bash: _comp_complete_longopt: line 6: ` --!(no-*)dir*)'
bash: error importing function definition for `_comp_complete_longopt'
bash: _comp_compgen_usage: line 11: syntax error near unexpected token `('
bash: _comp_compgen_usage: line 11: ` -?(\[)+([a-zA-Z0-9?]))'
bash: error importing function definition for `_comp_compgen_usage'

To reproduce

Install bash-completion, and open a new shell.

Expected behavior

Versions (please complete the following information)

Additional context

Debug trace

chronos@cheekon-i686 /output/git/satmandu $ set -x
chronos@cheekon-i686 /output/git/satmandu $ bash
+ bash
bash: _comp_complete_longopt: line 6: syntax error near unexpected token `('
bash: _comp_complete_longopt: line 6: ` --!(no-*)dir*)'
bash: error importing function definition for `_comp_complete_longopt'
bash: _comp_compgen_usage: line 11: syntax error near unexpected token `('
bash: _comp_compgen_usage: line 11: ` -?(\[)+([a-zA-Z0-9?]))'
bash: error importing function definition for `_comp_compgen_usage'
bash: _comp_complete_longopt: line 6: syntax error near unexpected token `('
bash: _comp_complete_longopt: line 6: ` --!(no-*)dir*)'
bash: error importing function definition for `_comp_complete_longopt'
bash: _comp_compgen_usage: line 11: syntax error near unexpected token `('
bash: _comp_compgen_usage: line 11: ` -?(\[)+([a-zA-Z0-9?]))'
bash: error importing function definition for `_comp_compgen_usage'
/usr/local/bin/bash: _comp_complete_longopt: line 6: syntax error near unexpected token `('
/usr/local/bin/bash: _comp_complete_longopt: line 6: ` --!(no-*)dir*)'
/usr/local/bin/bash: error importing function definition for `_comp_complete_longopt'
/usr/local/bin/bash: _comp_compgen_usage: line 11: syntax error near unexpected token `('
/usr/local/bin/bash: _comp_compgen_usage: line 11: ` -?(\[)+([a-zA-Z0-9?]))'
/usr/local/bin/bash: error importing function definition for `_comp_compgen_usage'
satmandu commented 5 months ago

Same issue using bash completion built from https://github.com/scop/bash-completion/commit/5cb74f805d13fb49989aee08da132cb24135631f

akinomyoga commented 5 months ago

Do you unset extglob in your ~/.bashrc? bash-completion relies on the Bash option extglob. bash_completion turns extglob on, but if extglob is turned off later, it will cause the problem. extglob shouldn't be turned off to make bash-completion work.

akinomyoga commented 5 months ago
bash: error importing function definition for `_comp_complete_longopt'
bash: error importing function definition for `_comp_compgen_usage'

Hmm, those functions are exported by the parent shell and then evaluated by the child shells before extglob is set. Do you mark those functions with the export attribute?

satmandu commented 5 months ago
bash: error importing function definition for `_comp_complete_longopt'
bash: error importing function definition for `_comp_compgen_usage'

Hmm, those functions are exported by the parent shell and then evaluated by the child shells before extglob is set. Do you mark those functions with the export attribute?

I'm trying to figure out where the parent shell is exporting those. The problem we have is that /bin/bash is from ChromeOS, and a such we are stuck with whatever it sets before we get our shell...

satmandu commented 5 months ago

Do you unset extglob in your ~/.bashrc? bash-completion relies on the Bash option extglob. bash_completion turns extglob on, but if extglob is turned off later, it will cause the problem. extglob shouldn't be turned off to make bash-completion work.

Ah, yes it appears that somewhere extglob is being unset. Let us see if we can get it to stay set, and also set our bash build to have it set by default...

akinomyoga commented 5 months ago

OK, this line causes the issue:

https://github.com/chromebrew/crew-profile-base/pull/20/files#diff-7770cfc5c095e582016d9fcc985c3f04b492f281beb28a271459e53c452897b4L43-L44

Because set -a is specified in the parent shell, all the functions including the ones bash-completion defines are automatically exported and passed to the new shell instance. The new shell instance loads the functions defined in the parent shell, which require a specific set of shell options, before evaluating any initialization scripts. Since this is performed on the stage earlier than any setups of the shell options, the exported functions cause the syntax error.

You seem to have added shopt -s extglob in https://github.com/chromebrew/crew-profile-base/pull/20, but I think it doesn't have an effect. Or have you confirmed that it fixes the issue?

In addition, as far as I read README of chromebrew/crew-profile-base, the Bash-specific option shopt -s extglob shouldn't be put in profile because it should contain only Bourne-shell compatible settings.

satmandu commented 5 months ago

Thanks all for debugging this issue for our users.

I will make these changes and see if that helps!