deno
0be857f4 - fix(watch): keep watcher alive when script calls Deno.exit() (#35021)

Commit
146 days ago
fix(watch): keep watcher alive when script calls Deno.exit() (#35021) Fixes #7590. Closes #33043. When a script run with `deno run --watch` called `Deno.exit()`, the entire watcher process was terminated instead of just the current run. The expected behaviour, like nodemon, is for the watcher to keep running and restart the script on the next file change. The cause is that `Deno.exit()` goes through `op_exit`, which calls `std::process::exit()` directly. Under `--watch` that takes the watcher process down with it, so the watcher never reaches the branch where it waits for the next file change. The test runner already faces the same situation: a test calling `Deno.exit()` must not kill `deno test`. It solves this by terminating the V8 isolate rather than the process. This change mirrors that approach for watch mode. When a watcher is active, the isolate handle is installed in `OpState`, so `op_exit` terminates the current isolate instead of the process and records the requested exit code. After the run, the watcher clears V8's termination flag and treats it as a normal end of run, then waits for the next change. The non-watch exit path is unchanged. This covers all three watch paths: - `deno run --watch` installs the handle in `run_for_watcher`. - `deno run --watch-hmr` installs it in `run()`. - `deno serve --watch` also goes through `run()`. To make `run()` recognise it is under a watcher, the watcher communicator is now provided to the worker whenever running under the file watcher rather than only for HMR (HMR-specific behaviour stays gated on `create_hmr_runner`). The watcher reports the requested exit code in its finished message, e.g. `Process finished with exit code 42`.
Author
Parents
Loading