Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FZF appears to swallow key presses when used with less on Git Bash with ZSH on Windows #4260

Open
6 of 10 tasks
marovira opened this issue Feb 19, 2025 · 9 comments
Open
6 of 10 tasks

Comments

@marovira
Copy link

Checklist

  • I have read through the manual page (man fzf)
  • I have searched through the existing issues
  • For bug reports, I have checked if the bug is reproducible in the latest version of fzf

Output of fzf --version

0.60.0 (3347d61)

OS

  • Linux
  • macOS
  • Windows
  • Etc.

Shell

  • bash
  • zsh
  • fish

Problem / Steps to reproduce

Steps

  1. Open a terminal using either Git Bash or ZSH within Git Bash.
  2. Enter echo "<file>" | fzf --bind="enter:execute(less {})" where <file> is any regular file.
  3. The fzf prompt appears with the given file. Press enter to select it.

Image

  1. The less prompt opens. Now try to press any key (j, k, etc) and see that nothing happens. Pressing Esc, <C-d>, <C-x> or <C-c> also have no effect. The only way to return to ZSH is to manually kill the less process.

Image

Notes

  • If only regular keys are pressed (i.e. keys without any modifiers) and the less process is killed, the pressed keys will appear in fzf's prompt, which seems to indicate that fzf is somehow swallowing the key presses that should've gone to less. In the example below, I pressed kl before killing the less process. See that the pressed keys appear in fzf's prompt.

Image

  • The issue is exclusive to Windows, Linux does not present this problem.
  • The issue is reproducible in either Git Bash or Git Bash + ZSH.
@marovira
Copy link
Author

The issue was first noticed in forgit and reported at wfxr/forgit#423. After diagnosing, it was determined that the problem is with fzf itself.

@marovira
Copy link
Author

marovira commented Mar 2, 2025

@junegunn I'm not overly familiar with debugging shell code, but I'm willing to help debug this if you have any ideas of what could be going on.

@junegunn
Copy link
Owner

junegunn commented Mar 2, 2025

I have no idea. One thing I'd like to check is if fzf is using winpty on your environment.

func isMintty345() bool {
return util.CompareVersions(os.Getenv("TERM_PROGRAM_VERSION"), "3.4.5") >= 0
}
func needWinpty(opts *Options) bool {
if os.Getenv("TERM_PROGRAM") != "mintty" {
return false
}
if isMintty345() {
/*
See: https://github.com/junegunn/fzf/issues/3809
"MSYS=enable_pcon" allows fzf to run properly on mintty 3.4.5 or later.
*/
if strings.Contains(os.Getenv("MSYS"), "enable_pcon") {
return false
}
// Setting the environment variable here unfortunately doesn't help,
// so we need to start a child process with "MSYS=enable_pcon"
// os.Setenv("MSYS", "enable_pcon")
return true
}
if opts.NoWinpty {
return false
}
if _, err := exec.LookPath("winpty"); err != nil {
return false
}
return true
}

  • What is the value of $TERM_PROGRAM_VERSION?
  • And $TERM_PROGRAM?
  • Does setting MSYS=enable_pcon make any difference?

@marovira
Copy link
Author

marovira commented Mar 2, 2025

For both Git bash with ZSH and vanilla Git bash:

  • Output of $TERM_PROGRAM_VERSION: none. The variable isn't set
  • Output of $TERM_PROGRAM: none. The variable isn't set
  • Using export MSYS=enable_pcon doesn't change the behaviour.

@junegunn
Copy link
Owner

junegunn commented Mar 3, 2025

I started a Windows Server 2025 instance on EC2, installed the latest Git bash, downloaded fzf 0.60.2, and tested it.

So here's what I've found.

seq 1000 > test

# less opens and closes immediately
echo test | ./fzf --bind 'enter:execute:less {}'

# But with the redirection, less works
# * NOTE: On non-Windows systems, this is not required
#   https://github.com/junegunn/fzf/commit/3dee8778d073199e0fe1e186e54f7eabc2fdef43
echo test | ./fzf --bind 'enter:execute:less {} > /dev/tty'

# However, when using a non-fullscreen renderer, less becomes unresponsive as reported
echo test | ./fzf --bind 'enter:execute:less {} > /dev/tty' --height 50%

So the issue has to do with the LightRenderer implementation for Windows.

https://github.com/junegunn/fzf/blob/master/src/tui/light_windows.go

@junegunn
Copy link
Owner

junegunn commented Mar 3, 2025

This seems to be the culprit.

// the following allows for non-blocking IO.
// syscall.SetNonblock() is a NOOP under Windows.
go func() {
fd := int(r.inHandle)
b := make([]byte, 1)
for !r.closed.Get() {
// HACK: if run from PSReadline, something resets ConsoleMode to remove ENABLE_VIRTUAL_TERMINAL_INPUT.
_ = windows.SetConsoleMode(windows.Handle(r.inHandle), consoleFlagsInput)
_, err := util.Read(fd, b)
if err == nil {
r.ttyinChannel <- b[0]
}
}
}()

@junegunn junegunn self-assigned this Mar 3, 2025
@junegunn junegunn added the bug label Mar 3, 2025
@junegunn junegunn reopened this Mar 3, 2025
@junegunn
Copy link
Owner

junegunn commented Mar 3, 2025

Can you test 8916cbc?

Attaching the amd64 binary. fzf.exe.zip

It still has a problem of consuming a key for execute program, but it's not trivial to properly fix it.

@marovira
Copy link
Author

marovira commented Mar 3, 2025

This pretty much solves it. The first key press is still being swallowed, but subsequent key presses work as expected. With the repro I gave for this issue, if I want to exit less I need to press q twice. That said, this is significantly better than before as I can use commands invoked through fzf! Thanks for the quick turnaround!

@junegunn
Copy link
Owner

junegunn commented Mar 3, 2025

Thanks for the confirmation. I'll release a new patch version with the (partial) fix and keep this open until we find the perfect solution.

Some pointers:

tmeijn pushed a commit to tmeijn/dotfiles that referenced this issue Mar 4, 2025
This MR contains the following updates:

| Package | Update | Change |
|---|---|---|
| [junegunn/fzf](https://github.com/junegunn/fzf) | patch | `v0.60.2` -> `v0.60.3` |

MR created with the help of [el-capitano/tools/renovate-bot](https://gitlab.com/el-capitano/tools/renovate-bot).

**Proposed changes to behavior should be submitted there as MRs.**

---

### Release Notes

<details>
<summary>junegunn/fzf (junegunn/fzf)</summary>

### [`v0.60.3`](https://github.com/junegunn/fzf/releases/tag/v0.60.3): 0.60.3

[Compare Source](junegunn/fzf@v0.60.2...v0.60.3)

-   Bug fixes and improvements
    -   \[fish] Enable multiple history commands insertion ([#&#8203;4280](junegunn/fzf#4280)) ([@&#8203;bitraid](https://github.com/bitraid))
    -   \[walker] Append '/' to directory entries on MSYS2 ([#&#8203;4281](junegunn/fzf#4281))
    -   Trim trailing whitespaces after processing ANSI sequences ([#&#8203;4282](junegunn/fzf#4282))
    -   Remove temp files before `become` when using `--tmux` option ([#&#8203;4283](junegunn/fzf#4283))
    -   Fix condition for using item numlines cache ([#&#8203;4285](junegunn/fzf#4285)) ([@&#8203;alex-huff](https://github.com/alex-huff))
    -   Make `--accept-nth` compatible with `--select-1` ([#&#8203;4287](junegunn/fzf#4287))
    -   Increase the query length limit from 300 to 1000 ([#&#8203;4292](junegunn/fzf#4292))
    -   \[windows] Prevent fzf from consuming user input while paused ([#&#8203;4260](junegunn/fzf#4260))

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined).

🚦 **Automerge**: Enabled.

♻ **Rebasing**: Whenever MR is behind base branch, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this MR and you won't be reminded about this update again.

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this MR, check this box

---

This MR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate).
<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzOS4xODUuMCIsInVwZGF0ZWRJblZlciI6IjM5LjE4NS4wIiwidGFyZ2V0QnJhbmNoIjoibWFpbiIsImxhYmVscyI6WyJSZW5vdmF0ZSBCb3QiXX0=-->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants