From 503147fed6025efc4b4bbc6d4c28b3ff510bda77 Mon Sep 17 00:00:00 2001 From: DanieleAurilio Date: Sun, 7 Sep 2025 18:41:26 +0200 Subject: [PATCH 1/2] fix(cli/util): terminate watch file with sigint --- cli/util/file_watcher.rs | 7 ++++ tests/unit/process_test.ts | 67 +++++++++++++++++++++++++++++++++++++- 2 files changed, 73 insertions(+), 1 deletion(-) diff --git a/cli/util/file_watcher.rs b/cli/util/file_watcher.rs index d80664dfa1..2df445840c 100644 --- a/cli/util/file_watcher.rs +++ b/cli/util/file_watcher.rs @@ -15,6 +15,7 @@ use deno_core::futures::FutureExt; use deno_core::parking_lot::Mutex; use deno_lib::util::result::js_error_downcast_ref; use deno_runtime::fmt_errors::format_js_error; +use deno_signals; use log::info; use notify::Error as NotifyError; use notify::RecommendedWatcher; @@ -374,6 +375,9 @@ where select! { _ = receiver_future => {}, + _ = deno_signals::ctrl_c() => { + return Ok(()); + }, _ = restart_rx.recv() => { print_after_restart(); continue; @@ -407,6 +411,9 @@ where // watched paths has changed. select! { _ = receiver_future => {}, + _ = deno_signals::ctrl_c() => { + return Ok(()); + }, _ = restart_rx.recv() => { print_after_restart(); continue; diff --git a/tests/unit/process_test.ts b/tests/unit/process_test.ts index 17589d8581..c4116ad053 100644 --- a/tests/unit/process_test.ts +++ b/tests/unit/process_test.ts @@ -583,7 +583,7 @@ Deno.test( permissions: { run: true, read: true, write: true }, ignore: Deno.build.os === "windows", }, - async function non_existent_cwd(): Promise { + async function nonExistentCwd(): Promise { // @ts-ignore `Deno.run()` was soft-removed in Deno 2. const p = Deno.run({ cmd: [ @@ -610,3 +610,68 @@ Deno.test( assertStringIncludes(stderr, "failed resolving cwd:"); }, ); + +Deno.test( + { + permissions: { run: true, read: true, write: true }, + }, + async function runWatchAndSigint(): Promise { + const tempDir = await Deno.makeTempDir(); + const tempFile = `${tempDir}/temp_watch_file.ts`; + await Deno.writeTextFile(tempFile, "console.log('watch test');"); + + // @ts-ignore `Deno.run()` was soft-removed in Deno 2. + const p = Deno.run({ + cmd: ["deno", "run", "--watch", tempFile], + stdout: "piped", + stderr: "null", + }); + + Deno.kill(p.pid, "SIGINT"); + const data = new Uint8Array(10); + const out = await p.stdout.read(data); + assertEquals(out, null); + p.stdout.close(); + p.close(); + + await Deno.remove(tempFile); + await Deno.remove(tempDir); + }, +); + +Deno.test( + { + permissions: { run: true, read: true, write: true }, + }, + async function runWatchWaitForSigint(): Promise { + const tempDir = await Deno.makeTempDir(); + const tempFile = `${tempDir}/temp_watch_file.ts`; + await Deno.writeTextFile( + tempFile, + `Deno.addSignalListener("SIGINT", () => { + console.log("SIGINT"); + ac.abort(); +}); + +Deno.serve({ signal: ac.signal }, () => new Response("Hello World")); +`, + ); + + // @ts-ignore `Deno.run()` was soft-removed in Deno 2. + const p = Deno.run({ + cmd: ["deno", "run", "--watch", tempFile], + stdout: "piped", + stderr: "null", + }); + + Deno.kill(p.pid, "SIGINT"); + const data = new Uint8Array(10); + const out = await p.stdout.read(data); + assertEquals(out, null); + p.stdout.close(); + p.close(); + + await Deno.remove(tempFile); + await Deno.remove(tempDir); + }, +); From e72f26e159e16164addea028ccbfa0861354d2e5 Mon Sep 17 00:00:00 2001 From: DanieleAurilio Date: Sun, 7 Sep 2025 19:34:17 +0200 Subject: [PATCH 2/2] ingore windows --- tests/unit/process_test.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/unit/process_test.ts b/tests/unit/process_test.ts index c4116ad053..7b82fe5ba7 100644 --- a/tests/unit/process_test.ts +++ b/tests/unit/process_test.ts @@ -614,6 +614,7 @@ Deno.test( Deno.test( { permissions: { run: true, read: true, write: true }, + ignore: Deno.build.os === "windows", }, async function runWatchAndSigint(): Promise { const tempDir = await Deno.makeTempDir(); @@ -642,6 +643,7 @@ Deno.test( Deno.test( { permissions: { run: true, read: true, write: true }, + ignore: Deno.build.os === "windows", }, async function runWatchWaitForSigint(): Promise { const tempDir = await Deno.makeTempDir();