Skip to contentSkip to conversation

RFC: Fix on bash completion script for `_spatch not found` issue

cocci Ella Ma 1 participant 1 message Dec 4, 2024

lore
Share & tools
lore

Ella Ma

author
#0 lore raw

Hi all,

In another email, Victor Gambier mentioned that

Quoted 6 lines
Quoted 6 lines

I also noticed weird things around bash autocompletion. Regardless of whether or not the autocompletion finds any matches, if your command started with "spatch", the line "bash: completion: function `_spatch' not found" will show up. I think that could be related to the standard stream weirdness. To be more precise:

Quoted 6 lines
Quoted 6 lines
$ spatch /h
+ [TAB]
will transform into:
$ spatch /hbash: completion: function `_spatch' not found
$ spatch /home/

So I checked the bash completion script for spatch (scripts/spatch.bash_completion) and found this

```scripts/spatch.bash_completion
...
have spatch &&
_spatch() { ... } # function body omitted for simplicity

complete -F _spatch ... spatch
...
```

The have command will check whether the argument is an executable in PATH or a function defined in the current context.
In our case, the have command checks whether spatch command is available. If not, the complete function _spatch will not be defined.

But, no matter whether the have check passes or not, function _spatch will always be assigned for the completion of spatch. So, if it is not defined, we will encounter the problem we observed now.

I think this may be due to the order of
(1) adding the path of spatch to PATH (the opam switch dir or other prefixes), and
(2) loading bash completion files.

I did not dig deeply into the order which happens first.
But I believe its the problem of PATH variable according to what we have observed now. So, I suggest removing the have check protecting the definition of function _spatch. I propose this solution because all the complete scripts in the system, i.e. /etc/bash_completion.d and /usr/share/bash-completion/completions, do not perform the have check;
and if spatch is installed to opam switch root, the have check may never pass, as the opam paths are appended to the end of the $HOME/.bashrc file.

What do you think? Any suggestions are welcome.

ella

P.S.: Suggested patch:

```
diff --git a/scripts/spatch.bash_completion b/scripts/spatch.bash_completion
index ae217af9e..3ff48d9c7 100644
--- a/scripts/spatch.bash_completion
+++ b/scripts/spatch.bash_completion
@@ -34,7 +34,6 @@ __spatchcomp_1 ()
         done
 }
 
-have spatch &&
 _spatch()
 {
     local cur prev xspec
```