Skip to content

Commit 7aa22da

Browse files
fix(cli/util): terminate watch file with sigint (#30635)
Fix #30587 . I've implemented 2 tests. The first one checks when a program is terminated while the watcher is waiting for file changes and the user interrupts the process. The second one checks when the code is running: the SIGINT should be intercepted and the process should quit.
1 parent 2ec0768 commit 7aa22da

File tree

2 files changed

+75
-1
lines changed

2 files changed

+75
-1
lines changed

cli/util/file_watcher.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use deno_core::futures::FutureExt;
1515
use deno_core::parking_lot::Mutex;
1616
use deno_lib::util::result::js_error_downcast_ref;
1717
use deno_runtime::fmt_errors::format_js_error;
18+
use deno_signals;
1819
use log::info;
1920
use notify::Error as NotifyError;
2021
use notify::RecommendedWatcher;
@@ -374,6 +375,9 @@ where
374375

375376
select! {
376377
_ = receiver_future => {},
378+
_ = deno_signals::ctrl_c() => {
379+
return Ok(());
380+
},
377381
_ = restart_rx.recv() => {
378382
print_after_restart();
379383
continue;
@@ -407,6 +411,9 @@ where
407411
// watched paths has changed.
408412
select! {
409413
_ = receiver_future => {},
414+
_ = deno_signals::ctrl_c() => {
415+
return Ok(());
416+
},
410417
_ = restart_rx.recv() => {
411418
print_after_restart();
412419
continue;

tests/unit/process_test.ts

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -583,7 +583,7 @@ Deno.test(
583583
permissions: { run: true, read: true, write: true },
584584
ignore: Deno.build.os === "windows",
585585
},
586-
async function non_existent_cwd(): Promise<void> {
586+
async function nonExistentCwd(): Promise<void> {
587587
// @ts-ignore `Deno.run()` was soft-removed in Deno 2.
588588
const p = Deno.run({
589589
cmd: [
@@ -610,3 +610,70 @@ Deno.test(
610610
assertStringIncludes(stderr, "failed resolving cwd:");
611611
},
612612
);
613+
614+
Deno.test(
615+
{
616+
permissions: { run: true, read: true, write: true },
617+
ignore: Deno.build.os === "windows",
618+
},
619+
async function runWatchAndSigint(): Promise<void> {
620+
const tempDir = await Deno.makeTempDir();
621+
const tempFile = `${tempDir}/temp_watch_file.ts`;
622+
await Deno.writeTextFile(tempFile, "console.log('watch test');");
623+
624+
// @ts-ignore `Deno.run()` was soft-removed in Deno 2.
625+
const p = Deno.run({
626+
cmd: ["deno", "run", "--watch", tempFile],
627+
stdout: "piped",
628+
stderr: "null",
629+
});
630+
631+
Deno.kill(p.pid, "SIGINT");
632+
const data = new Uint8Array(10);
633+
const out = await p.stdout.read(data);
634+
assertEquals(out, null);
635+
p.stdout.close();
636+
p.close();
637+
638+
await Deno.remove(tempFile);
639+
await Deno.remove(tempDir);
640+
},
641+
);
642+
643+
Deno.test(
644+
{
645+
permissions: { run: true, read: true, write: true },
646+
ignore: Deno.build.os === "windows",
647+
},
648+
async function runWatchWaitForSigint(): Promise<void> {
649+
const tempDir = await Deno.makeTempDir();
650+
const tempFile = `${tempDir}/temp_watch_file.ts`;
651+
await Deno.writeTextFile(
652+
tempFile,
653+
`Deno.addSignalListener("SIGINT", () => {
654+
console.log("SIGINT");
655+
ac.abort();
656+
});
657+
658+
Deno.serve({ signal: ac.signal }, () => new Response("Hello World"));
659+
`,
660+
);
661+
662+
// @ts-ignore `Deno.run()` was soft-removed in Deno 2.
663+
const p = Deno.run({
664+
cmd: ["deno", "run", "--watch", tempFile],
665+
stdout: "piped",
666+
stderr: "null",
667+
});
668+
669+
Deno.kill(p.pid, "SIGINT");
670+
const data = new Uint8Array(10);
671+
const out = await p.stdout.read(data);
672+
assertEquals(out, null);
673+
p.stdout.close();
674+
p.close();
675+
676+
await Deno.remove(tempFile);
677+
await Deno.remove(tempDir);
678+
},
679+
);

0 commit comments

Comments
 (0)