19

I have created some bash completion functions that all appear to work in isolation but give me unexpected results when I attempt to tab complete with them.

Is it possible to connect bashdb, then attempt to tab complete something and step through to debug and determine what is going wrong?

Dan Midwood
  • 301
  • 2
  • 7
  • 2
    Just to add my to cents: you should use the `set -x` in bash to debug your scripts. Use `set +x` to disable. –  Jan 06 '12 at 20:29

3 Answers3

9

set -x

If you set -x either in the interactive session or the autocomplete script itself, (nearly?) every command and its results will be printed out. This includes the work done inside the autocomplete script.

This can then be quieted back down again with set +x.

-x

After expanding each simple command, for command, case command, select command, or arithmetic for command, display the expanded value of PS4, followed by the command and its expanded arguments or associated word list.

-from Bash manual #The Set Builtin

  • This is useless due to massive output each time in interactive shell (at least in zsh with oh-my-zsh installed) and it almost useless in autocomplete script if you want to debug COMP_* variables. – tamerlaha Aug 21 '20 at 18:23
  • "If you set -x either in the interactive session or the autocomplete script itself" -- if you have a noisy shell config, `-x` can be set just inside the script. Up to you! – Christopher Peterson Aug 21 '20 at 19:41
  • 1
    "and it almost useless in autocomplete script if you want to debug COMP_* variables" -- not sure what you mean by this? I can see COMPREPLY etc in my debug output after a quick test – Christopher Peterson Aug 21 '20 at 19:42
  • 1
    yeah, my bad, I was debugging "bash-cli" framework autocomplete where complete file returns list of suggestions via "source" and main COMP_X logic is in different place. But just setting 'set -x' also might cause some glitches with complete options. Therefore I recommend to redirect output to file, as it was mentioned before. P.S. I posted function (below) which I used for debugging. I found it helpful in my case. – tamerlaha Aug 21 '20 at 21:29
  • Unfortunately, this doesn't work when debugging completion scripts while they're being autoloaded (in newer versions of Bash), because the completion scripts get sourced with all of their output (yes, _including_ standard error, a very bad design choice IMHO) redirected to `/dev/null`. The only way to solve this that I can think of is to write the trace output of `set -x` to a file, by adding something like `exec 2>/tmp/bashtrace-$$.log; set -x` and `set +x` around the part of the script which you want to debug. – ack Jan 14 '22 at 08:30
3

I used this simple function to redirect everything I need to file.

function debug_me {
    echo $@
    echo "COMP_CWORD:" $COMP_CWORD
    echo "COMP_POINT:" $COMP_POINT
    echo "COMP_LINE:" $COMP_LINE
    echo "COMP_LINE#:" ${#COMP_LINE}
    echo "COMP_KEY:" $COMP_KEY
    echo "COMP_WORDS:" ${COMP_WORDS[@]}
    echo "COMPREPLY:" ${COMPREPLY[@]}
    echo
} >> ~/com.debug

And call it as: debug_me $someadditional_variable "If should be true here"

tamerlaha
  • 130
  • 6
1

Just log / output from the bash completion functions; as they are most likely no rocket science, this should be fairly easy to do. It might be possible to connect bashdb, but it feels unnecessary...

Tamara Wijsman
  • 57,083
  • 27
  • 185
  • 256
  • Connecting bashdb would have been much simpler and, I expect, quicker than adding log statements to the scripts. However, I don't know if it is possible, and the lack of answers would suggest that it isn't. I managed to solve my problem by logging state to a file like you suggest, but outputting logging information to the terminal is not helpful when working with completions. – Dan Midwood Mar 26 '12 at 20:53
  • 5
    @DanMidwood: Use two terminals (e.g. screen + split). In one, run the completion, in the second, `tail -f` the log. – choroba Mar 30 '12 at 14:13
  • @choroba: Nice one. :D – Tamara Wijsman Mar 30 '12 at 14:15