From 130a2592f1e946be563c7f167d7127f49a1014d1 Mon Sep 17 00:00:00 2001 From: Luca Casonato Date: Wed, 14 Aug 2024 15:21:11 +0200 Subject: [PATCH] fix(cli): support --watch when running cjs npm packages (#25038) --- cli/args/mod.rs | 2 -- cli/worker.rs | 17 +++++++++++++- tests/integration/watcher_tests.rs | 37 ++++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+), 3 deletions(-) diff --git a/cli/args/mod.rs b/cli/args/mod.rs index 0f6f050efe..65f9183d5c 100644 --- a/cli/args/mod.rs +++ b/cli/args/mod.rs @@ -1151,8 +1151,6 @@ impl CliOptions { resolve_url_or_path("./$deno$stdin.ts", &cwd) .map_err(AnyError::from) })? - } else if run_flags.watch.is_some() { - resolve_url_or_path(&run_flags.script, self.initial_cwd())? } else if NpmPackageReqReference::from_str(&run_flags.script).is_ok() { ModuleSpecifier::parse(&run_flags.script)? } else { diff --git a/cli/worker.rs b/cli/worker.rs index 8673804ab0..82051da6ce 100644 --- a/cli/worker.rs +++ b/cli/worker.rs @@ -280,7 +280,22 @@ impl CliMainWorker { /// Execute the given main module emitting load and unload events before and after execution /// respectively. pub async fn execute(&mut self) -> Result<(), AnyError> { - self.inner.execute_main_module_possibly_with_npm().await?; + if self.inner.is_main_cjs { + deno_node::load_cjs_module( + &mut self.inner.worker.js_runtime, + &self + .inner + .main_module + .to_file_path() + .unwrap() + .to_string_lossy(), + true, + self.inner.shared.options.inspect_brk, + )?; + } else { + self.inner.execute_main_module_possibly_with_npm().await?; + } + self.inner.worker.dispatch_load_event()?; self.pending_unload = true; diff --git a/tests/integration/watcher_tests.rs b/tests/integration/watcher_tests.rs index 3cec2f442b..6203a62d96 100644 --- a/tests/integration/watcher_tests.rs +++ b/tests/integration/watcher_tests.rs @@ -3,6 +3,7 @@ use flaky_test::flaky_test; use test_util as util; use test_util::assert_contains; +use test_util::env_vars_for_npm_tests; use test_util::TempDir; use tokio::io::AsyncBufReadExt; use util::DenoChild; @@ -707,6 +708,42 @@ async fn run_watch_no_dynamic() { check_alive_then_kill(child); } +#[flaky_test(tokio)] +async fn run_watch_npm_specifier() { + let _g = util::http_server(); + let t = TempDir::new(); + + let file_to_watch = t.path().join("file_to_watch.txt"); + file_to_watch.write("Hello world"); + + let mut child = util::deno_cmd() + .current_dir(t.path()) + .envs(env_vars_for_npm_tests()) + .arg("run") + .arg("--watch=file_to_watch.txt") + .arg("-L") + .arg("debug") + .arg("npm:@denotest/bin/cli-cjs") + .arg("Hello world") + .env("NO_COLOR", "1") + .piped_output() + .spawn() + .unwrap(); + let (mut stdout_lines, mut stderr_lines) = child_lines(&mut child); + + wait_contains("Hello world", &mut stdout_lines).await; + wait_for_watcher("file_to_watch.txt", &mut stderr_lines).await; + + // Change content of the file + file_to_watch.write("Hello world2"); + + wait_contains("Restarting", &mut stderr_lines).await; + wait_contains("Hello world", &mut stdout_lines).await; + wait_for_watcher("file_to_watch.txt", &mut stderr_lines).await; + + check_alive_then_kill(child); +} + // TODO(bartlomieju): this test became flaky on macOS runner; it is unclear // if that's because of a bug in code or the runner itself. We should reenable // it once we upgrade to XL runners for macOS.