junegunn / fzf

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

Crash when piping output from node/nodemon web server #3892

Closed hboon closed 1 week ago

hboon commented 1 week ago

Checklist

Output of fzf --version

0.53.0 (brew)

OS

Shell

Problem / Steps to reproduce

I can reproduce with the latest on github too. fish and bash.

If I do:

1. node_modules/.bin/ts-node  src/index.ts | fzf
2. Wait for a bit for logs to finish (~10 lines)
3. Press arrow up (or down)
4. fzf crashes which the node process still runs

I noticed that filtering by typing seems to be extremely slow, given the number of lines (and compared to running fzf with other cases on the same machine in the same shell).

I'm not sure what's going on, but is there a way to enable some logs so I can gather more information?

junegunn commented 1 week ago

Does node_modules/.bin/ts-node src/index.ts take user input from keyboard?

fzf crashes

Are there any error messages?

hboon commented 1 week ago

Does node_modules/.bin/ts-node src/index.ts take user input from keyboard?

No.

Are there any error messages?

No. So, at a loss what to do to debug. I could run the GitHub version again if there's a way to enable logging somewhere.

junegunn commented 1 week ago

Can you provide minimal steps to reproduce the problem from my side? I don't have your project with index.ts so there's no way I can further look into the problem. Also, please don't assume that I know anything about the thing called "ts-node".

hboon commented 1 week ago

Ah, sorry, I was hoping to look for a way to get debug logs so I can gather more information first. Thanks for helping to look into this.

I was wrong, it wasn't ts-node, but nodemon which reproduces the issue. From the tool's README:

Monitor for any changes in your node.js application and automatically restart the server - perfect for development

I made a repo to reproduce the issue for me — https://github.com/hboon/nodemon-fzf/. It's really just running nodemon or a single source file that logs 1 line to STDOUT. There are 2 branches. (B) should be easier to get started (but trust…).

A. main — you will need to have pnpm installed and run pnpm i first to install the nodemon that is causing this) B. with-node-modules — this includes nodemon already so you can skip (A), and don't need to install pnpm

Run this:

node_modules/nodemon/bin/nodemon.js index.js | fzf

index.js only logs a line to STDOUT so I suppose it's the way nodemon works that's causing an issue?

I'm hoping to use fzf for streaming server logs during development.

junegunn commented 1 week ago

Thanks for the repro. I can reproduce the problem, and it definitely looks like nodemon is trying to read user input. So nodemon and fzf are both competing to read from /dev/tty at the same time, and it causes the problems.

Workaround 1. Prevent nodemon from reading user input

By redirecting /dev/null to its standard input.

node_modules/nodemon/bin/nodemon.js index.js < /dev/null | fzf --ansi --tail 10000 --tac

The problem is nodemon doesn't terminate after fzf exits.

Workaround 2. Make fzf start the process

fzf --ansi --tail 10000 --tac --bind 'start:reload:node_modules/nodemon/bin/nodemon.js index.js'

fzf starts nodemon process when it starts, and can terminate it when it exits.

(Works better with https://github.com/junegunn/fzf/commit/5b5283378571cca88a993630db3307319d2cb56d)

hboon commented 1 week ago

Both workarounds work for me. I'll use (2). Doesn't seem like 5b52833 is needed yet.

But all good. Thanks again!