esbuild icon indicating copy to clipboard operation
esbuild copied to clipboard

esbuild --watch can't run in background when started from shell script

Open sehugg opened this issue 4 years ago • 3 comments
trafficstars

It looks as if you run esbuild --watch as a background process in a shell script (with "&") it exits immediately. Probably related to issue #1449, as it didn't do this in 0.12.16. Example:

#!/bin/sh
./node_modules/.bin/esbuild src/main.ts --outfile=/tmp/bundle.js --watch &
sleep 3
cat /tmp/bundle.js # no file created

To prevent this, it would probably be best to hide the exit-on-stdin-close behavior behind a --watch-stdin option. I don't see any other bundlers with a more elegant solution.

sehugg avatar Aug 09 '21 18:08 sehugg

try:

sleep 99999 | esbuild main.ts --outfile=out.mjs --watch &

or

sleep 99999 | nohup esbuild main.ts --outfile=out.mjs --watch &

kzc avatar Aug 09 '21 20:08 kzc

tl;dr Redirect pipe </dev/zero

Perhaps the clearest and most versatile option here is this:

make esbuild-watch </dev/null &
# or in the above example
./node_modules/.bin/esbuild src/main.ts --outfile=/tmp/bundle.js --watch  </dev/zero &

(thanks @ctcarton)

This fills the Stdin pipe with 512 bytes of zeros so it doesn't auto-close.

setsid

Another option is setsid e.g.

In our case we use a Makefile so it looks like this:

setsid make esbuild-watch &

This is Linux-specific i.e. it won't work on Mac or Windows.

true |

An alternative option might be:

true | make esbuild-watch &
  • [ ]

brianmhunt avatar Feb 07 '22 14:02 brianmhunt

I've had a similar problem when I was trying to run that as a preLaunchTask for a VS Code extension. However, in my case the fix was to set a correct "problemWatcher" to be "$esbuild-watch" in .vscode/tasks.json file. Perhaps this could help someone else struggling with the same problem.

Reemh avatar Jun 30 '22 11:06 Reemh