0

I have tool that is able to create a completion file for bash, zsh and fish. I normally use zsh, but i cannot get this completion file to work on zsh. So as a test i installed fish and created the completion file for fish. Also that one i cannot get to work. All other completions are working fine in zsh and in fish, so i suspect that the tool creates a broken completion file. Is there a way to check the syntax of a completion file for errors?

2
  • 2
    This seems like a bit of an xy problem where you are asking about your attempted solution (a validation method for completions), but the real problem you are trying to solve is why a specific completion isn't working. I'm assuming you have a reason for not stating the name of the tool in your question (or the completion function), but it does make it more difficult to help you! Dec 30, 2021 at 16:41
  • For zsh, you can use zcompile to check the syntax of a script without running it. But there is a lot more that could be wrong besides the syntax.
    – okapi
    Jan 4 at 20:03

3 Answers 3

3

My answer addresses zsh (with the “new” completion system, i.e. after calling compinit) and bash. For fish, see NotTheDr01ds's answer.

If there is a syntax error in the completion file, you'll see an error message when the completion code is loaded. In bash, this happens when /etc/bash_completion is loaded. In zsh, this happens the first time the completion function is invoked. But syntax errors are rare. Most errors are not syntax errors, and there's no way to check whether a completion function works other than invoking it.

If an actual error happens while generating the completions, you'll see an error message on the terminal. But if the code doesn't trigger an error, but doesn't generate the desired completions, then there's no error to display.

The first thing to check is whether the completion function you want is actually invoked. For zsh, check

echo $_comps[foo]

where foo is the command whose options/arguments are to be completed. For bash, check

complete -p foo

If your custom completion function isn't loaded, see Dynamic zsh autocomplete for custom commands or Custom bash tab completion.

If you want to debug the completion code, in zsh, press ^X? (Ctrl+X ?) instead of Tab to run _complete_debug. This places a trace of the completion code into a file which you can view by pressing Up Enter.

2

There may be more techniques of which I'm not aware, but for fish, I'd start with:

complete -c <appname>

... to see what completion options have been loaded so far. If you aren't getting any completions, it is likely empty at the moment.

Then move on to:

fish --debug=complete

If there are issues seeing the output on stdout (a likely issue for messages popping up in the middle of a completion), you can specify a filename for the log:

fish --debug='complete' --debug-output=/tmp/fish.log

Also, do you know where the <toolname>.fish completion function is being generated? It should be in /usr/share/fish/vendor_completions.d/, I believe. However, there are some other locations that are searched for completion functions.

Once you have the <toolname>.fish function, I might try:

fish_trace=1 source /path/to/<toolname>.fish
1

Is there a way to check the syntax of a completion file for errors?

To check a file for syntax errors, you can run it through fish --no-execute. E.g.

fish --no-execute /path/to/file.fish

However, this won't look inside the complete statements. E.g. in something like

complete -c mycommand --condition 'banana; && rama' --short f

it won't complain about the && rama because it's inside a string that is evaluated later, when the completion is requested.

So, you can run the completions with a fake commandline via complete --do-complete. E.g.

 complete --do-complete="mycommand someoption"

which will print the completion candidates that fish generates for the commandline mycommand someoption.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.