facebook / watchman

Watches files and records, or triggers actions, when they change.
https://facebook.github.io/watchman/
MIT License
12.57k stars 986 forks source link

Accepting more than one command when reading commands from stdin (-j/--json-command + -p/--persistent) #1063

Open camilstaps opened 1 year ago

camilstaps commented 1 year ago

The watchman executable with -j/--json-command accepts JSON commands from stdin, as in:

$ watchman -j
["version"]          
version: 20220924.070135.0
buildinfo: 0ef4921d101ab856d9c5d8f0539cc4b5e94ee602

When -p/--persistent is used, the executable does not exit, which is useful for commands like subscribe:

$ watchman --no-pretty -j
["watch","."]
{"version": "20220924.070135.0","watcher": "inotify","watch": "/path/to/working/directory"}
$ watchman --no-pretty -j -p
["subscribe",".","name",{"expression":["type","f"]}]
{"subscribe": "name","version": "20220924.070135.0","clock": "c:1664785894:368439:1:3","asserted-states": []}
{"is_fresh_instance": true,"files": [...], ...}
...

However, with -j -p, only one command is accepted from stdin. It is not possible to pass both the watch and the subscribe on stdin without restarting watchman:

$ watchman --no-pretty -j -p
["watch","."]
{"version":"20220924.070135.0","watcher":"inotify","watch":"/path/to/working/directory"}
["subscribe",".","name",{"expression":["type","f"]}]
(no response)

I have an application that needs to watch for changes in multiple roots, so I would like to run watchman and pass it several watch and subscribe commands. This sort of thing is possible with the socket interface, but in the language that I am working with there is no unified interface to Unix sockets and Windows named pipes, while there is a unified interface to subprocesses. Therefore it would be very easy if watchman -j -p would accept more than one command.

  1. Am I right in thinking this is currently not possible with a single watchman subprocess?
  2. Would it be possible to change this by moving the handling of the persistent flag from Command.cpp to main.cpp?