junegunn / fzf

:cherry_blossom: A command-line fuzzy finder
https://junegunn.github.io/fzf/
MIT License
63.4k stars 2.36k forks source link

BrokenPipeError: [Errno 32] Broken pipe #3689

Closed siferati closed 5 months ago

siferati commented 5 months ago

Checklist

Output of fzf --version

0.48.1 (d579e33)

OS

Shell

Problem / Steps to reproduce

I'm trying to have fzf to autocomplete the output of a python command.

_fzf_complete_hello() {
  _fzf_complete -- "$@" < <(
    python -c "print('hello\nworld')"
  )
}

[ -n "$BASH" ] && complete -F _fzf_complete_hello -o default -o bashdefault hello

It works fine if I type hello ** + TAB. But if I do hello + TAB (i.e. the usual bash completion without fzf) I get the error

Exception ignored in: <_io.TextIOWrapper name='<stdout>' mode='w' encoding='utf-8'>
BrokenPipeError: [Errno 32] Broken pipe

The above error only happens when using python output. If I replace the python line with echo hello\nworld it works as expected with no errors.

How can I fix the error when pressing tab without **?

junegunn commented 5 months ago

You can just suppress the message by redirecting the standard error stream to /dev/null.

i.e. python -c "print('hello\nworld')" 2> /dev/null

But this made me realize that there's a flaw in the current API design. The command is executed regardless of whether the prefix has the trigger or not. I'll think about it.

junegunn commented 5 months ago

On second thought, this is probably not too bad. Most programs will immediately terminate because of the broken pipe (like in your case).