lincheney / fzf-tab-completion

Tab completion using fzf
GNU General Public License v3.0
652 stars 41 forks source link

[Bash in Linux] Incompatibility between gitstatus and fzf-tab-completion utilities, terminal exits #70

Closed bluz71 closed 1 year ago

bluz71 commented 1 year ago

Hello,

A couple days ago I wanted to use fzf-tab-completion in my Bash configuration which uses my bash-seafly-prompt script and the gitstatus utility.

No good, there is a bad interaction between gitstatus and fzf-tab-completion when doing Bash tab completion; basically the terminal GUI window (Alacritty & GNOME-Terminal) will close upon hitting ENTER after a completion.

I can repeat the issue with this minimal ~/.bashrc file:

_command_prompt() {
    if ! hash gitstatus_query 2>/dev/null || ! gitstatus_query -p; then
        return
    fi
}

if [[ -r ~/.gitstatus/gitstatus.plugin.sh ]]; then
    source ~/.gitstatus/gitstatus.plugin.sh
    gitstatus_stop && gitstatus_start -c 0 -d 0
fi

PROMPT_COMMAND=_command_prompt

. ~/fzf-tab-completion/bash/fzf-bash-completion.sh
bind -x '"\t": fzf_bash_completion'

I installed fzf-tab-completion via git clone https://github.com/lincheney/fzf-tab-completion in the home directory. And I installed gitstatus via git clone --depth 1 https://github.com/romkatv/gitstatus.git ~/.gitstatus.

Do cd <TAB>, select a directory from the fzf list followed by ENTER to execute the cd command. BOOM, the window will close.

Maybe gitstatus is at fault, I don't know.

Regards.

lincheney commented 1 year ago

I think this is fixed by https://github.com/lincheney/fzf-tab-completion/commit/7de33435821237aaaeec79137bb9d407fa5a17cb if you are able to test

Issue is caused by fzf-tab-completion (although on the flip side, it is gitstatus that is doing the exiting of the terminal; it could be a bit more fault tolerant)

bluz71 commented 1 year ago

Hello @lincheney,

I just tested your fix, and yes the issue is now fixed.

I am very grateful indeed; thank you very much.

Issue is caused by fzf-tab-completion (although on the flip side, it is gitstatus that is doing the exiting of the terminal; it could be a bit more fault tolerant)

I created this gitstatus issue but the author had no interest in debugging the issue.

All good now, I can use both gitstatus and fzf-tab-completion at the same time.

Regards.

romkatv commented 1 year ago

it is gitstatus that is doing the exiting of the terminal; it could be a bit more fault tolerant

Can you suggest how? I'm not versed in bash.

Basically, the shell gets killed by SIGPIPE when gitstatus attempts to write to a file descriptor.

echo -nE "$req_id"$'\x1f'"$dir"$'\x1f'"$no_diff"$'\x1e' >&$_GITSTATUS_REQ_FD

Here $_GITSTATUS_REQ_FD is a file descriptor to a fifo. The other end of the fifo is supposed to be open by another process. That process got killed by fzf-tab-completion, so echo causes SIGPIPE. By default there is no handler for SIGPIPE and the default one kills the process.

I see two ways of surviving this:

  1. Write in a subshell: (echo -nE "...") || return. If done this way, the subshell will get killed but the top-level shell will stay alive.
  2. Trap PIPE. Bash doesn't seem to have the equivalent of local_traps from zsh, so we would need to restore the trap manually. The only way I can find how to retrieve the current trap is trap -p PIPE, which requires forking (or file I/O).

Both of these options require forking. The whole point of gitstatus is to be fast, so an extra fork to survive a pathological case doesn't look appealing.

Is there a better way?

lincheney commented 1 year ago

Honestly I'm not sure (or perhaps it is as you say, would require introducing some forks into it). Personally though, I'm satisfied that fzf-tab-completion being the actual root cause, the ultimate fix lies there.