watchexec/watchexec

Cannot direct watchexec stdout through multiple pipes

Closed this issue · 4 comments

I'm seeing a strange behavior when trying to filter watchexec's output via bash pipes.
The following command works as expected, printing file creation events:

watchexec --emit-events-to=stdio cat | grep create

After running rm -f foo && touch foo in another terminal, we get the following output:

[Running: cat]
create:/home/homestar/dev/mrepo_master/pysc/src/foo
[Command was successful]

BUT adding an extra pipe at the end of the command results in no useful output:

watchexec --emit-events-to=stdio cat | grep create | cat

Now running rm -f foo && touch foo gives the following output:

[Running: cat]
[Command was successful]

The only difference between the two commands is the addition of an extra | cat at the end of the line.

$ watchexec --version  # installed via nix
watchexec 1.25.0 (1980-01-01) +pid1
build-date: 1980-01-01
release: 1.25.0
features: default,pid1
$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 22.04.3 LTS
Release:        22.04
Codename:       jammy

Edit:

I've uploaded the files one-pipe.log and two-pipes.log, which were produced by running each of the below commands together with rm -f foo && touch foo in a separate terminal.

watchexec -vvvv --log-file=../one-pipe.log --emit-events-to=stdio cat | grep create
watchexec -vvvv --log-file=../two-pipes.log --emit-events-to=stdio cat | grep create | cat

one-pipe.log
two-pipes.log

can you use the --only-emit-events flag?

Ok, here are the updated commands and output logs:

watchexec -vvvv --log-file=../one-pipe-events-only.log --emit-events-to=stdio --only-emit-events | grep create
watchexec -vvvv --log-file=../two-pipes-events-only.log --emit-events-to=stdio --only-emit-events | grep create | cat

one-pipe-events-only.log
two-pipes-events-only.log

It's grep's buffering. Try:

watchexec --emit-events-to=stdio --only-emit-events | grep --line-buffered create | cat

Aha, that works! Thank you kindly.