From ab2200afd8c2a745ca84ec8450336b743d7a0cda Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Mon, 13 Mar 2023 22:44:16 -0400 Subject: [PATCH] refactor: --watch commands use deno_core::resolve_url_or_path (#18172) This commit changes various CLI subcommands that have support for the "--watch" flag to use initial current working directory when resolving "main module". This is part of migration towards explicitly passing current working directory to "deno_core::resolve_url_or_path" API. As a side effect this makes "deno --watch" more aligned to user expectations, where calling "Deno.chdir()" during program doesn't break watcher. Towards landing https://github.com/denoland/deno/pull/15454 --- cli/tools/bench.rs | 8 ++++---- cli/tools/bundle.rs | 15 ++++++++------- cli/tools/run.rs | 3 +-- cli/tools/test.rs | 32 +++++++++++++++++++++----------- 4 files changed, 34 insertions(+), 24 deletions(-) diff --git a/cli/tools/bench.rs b/cli/tools/bench.rs index 70fc34ca1f..0aa7cc365c 100644 --- a/cli/tools/bench.rs +++ b/cli/tools/bench.rs @@ -691,10 +691,10 @@ pub async fn run_benchmarks_with_watch( ); if let Some(changed) = &changed { - for path in changed.iter().filter_map(|path| { - deno_core::resolve_url_or_path_deprecated(&path.to_string_lossy()) - .ok() - }) { + for path in changed + .iter() + .filter_map(|path| ModuleSpecifier::from_file_path(path).ok()) + { if modules.contains(&path) { modules_to_reload.push(specifier); break; diff --git a/cli/tools/bundle.rs b/cli/tools/bundle.rs index c1d4befb12..5a42f834ea 100644 --- a/cli/tools/bundle.rs +++ b/cli/tools/bundle.rs @@ -5,7 +5,7 @@ use std::sync::Arc; use deno_core::error::AnyError; use deno_core::futures::FutureExt; -use deno_core::resolve_url_or_path_deprecated; +use deno_core::resolve_url_or_path; use deno_graph::Module; use deno_runtime::colors; @@ -35,16 +35,17 @@ pub async fn bundle( "Use alternative bundlers like \"deno_emit\", \"esbuild\" or \"rollup\" instead." ); + let module_specifier = + resolve_url_or_path(&bundle_flags.source_file, cli_options.initial_cwd())?; + let resolver = |_| { let cli_options = cli_options.clone(); - let source_file1 = &bundle_flags.source_file; - let source_file2 = &bundle_flags.source_file; + let module_specifier = &module_specifier; async move { - let module_specifier = resolve_url_or_path_deprecated(source_file1)?; - log::debug!(">>>>> bundle START"); let ps = ProcState::from_options(cli_options).await?; - let graph = create_graph_and_maybe_check(module_specifier, &ps).await?; + let graph = + create_graph_and_maybe_check(module_specifier.clone(), &ps).await?; let mut paths_to_watch: Vec = graph .specifiers() @@ -74,7 +75,7 @@ pub async fn bundle( result: Ok((ps, graph)), }, Err(e) => ResolutionResult::Restart { - paths_to_watch: vec![PathBuf::from(source_file2)], + paths_to_watch: vec![module_specifier.to_file_path().unwrap()], result: Err(e), }, }) diff --git a/cli/tools/run.rs b/cli/tools/run.rs index 04ddcb4d99..84ec75e1a5 100644 --- a/cli/tools/run.rs +++ b/cli/tools/run.rs @@ -9,7 +9,6 @@ use deno_core::anyhow::Context; use deno_core::error::AnyError; use deno_core::resolve_path; use deno_core::resolve_url_or_path; -use deno_core::resolve_url_or_path_deprecated; use deno_graph::npm::NpmPackageReqReference; use deno_runtime::permissions::Permissions; use deno_runtime::permissions::PermissionsContainer; @@ -104,10 +103,10 @@ pub async fn run_from_stdin(flags: Flags) -> Result { // code properly. async fn run_with_watch(flags: Flags, script: String) -> Result { let flags = Arc::new(flags); - let main_module = resolve_url_or_path_deprecated(&script)?; let (sender, receiver) = tokio::sync::mpsc::unbounded_channel(); let mut ps = ProcState::build_for_file_watcher((*flags).clone(), sender.clone()).await?; + let main_module = resolve_url_or_path(&script, ps.options.initial_cwd())?; let operation = |main_module: ModuleSpecifier| { ps.reset_for_file_watcher(); diff --git a/cli/tools/test.rs b/cli/tools/test.rs index 12f5d7c1f9..d200a30298 100644 --- a/cli/tools/test.rs +++ b/cli/tools/test.rs @@ -740,6 +740,7 @@ async fn test_specifier( } fn extract_files_from_regex_blocks( + current_dir: &Path, specifier: &ModuleSpecifier, source: &str, media_type: MediaType, @@ -799,13 +800,16 @@ fn extract_files_from_regex_blocks( writeln!(file_source, "{}", text.as_str()).unwrap(); } - let file_specifier = deno_core::resolve_url_or_path_deprecated(&format!( - "{}${}-{}{}", - specifier, - file_line_index + line_offset + 1, - file_line_index + line_offset + line_count + 1, - file_media_type.as_ts_extension(), - )) + let file_specifier = deno_core::resolve_url_or_path( + &format!( + "{}${}-{}{}", + specifier, + file_line_index + line_offset + 1, + file_line_index + line_offset + line_count + 1, + file_media_type.as_ts_extension(), + ), + current_dir, + ) .unwrap(); Some(File { @@ -823,6 +827,7 @@ fn extract_files_from_regex_blocks( } fn extract_files_from_source_comments( + current_dir: &Path, specifier: &ModuleSpecifier, source: Arc, media_type: MediaType, @@ -850,6 +855,7 @@ fn extract_files_from_source_comments( }) .flat_map(|comment| { extract_files_from_regex_blocks( + current_dir, specifier, &comment.text, media_type, @@ -865,6 +871,7 @@ fn extract_files_from_source_comments( } fn extract_files_from_fenced_blocks( + current_dir: &Path, specifier: &ModuleSpecifier, source: &str, media_type: MediaType, @@ -878,6 +885,7 @@ fn extract_files_from_fenced_blocks( let lines_regex = Regex::new(r"(?:\# ?)?(.*)")?; extract_files_from_regex_blocks( + current_dir, specifier, source, media_type, @@ -898,12 +906,14 @@ async fn fetch_inline_files( let inline_files = if file.media_type == MediaType::Unknown { extract_files_from_fenced_blocks( + ps.options.initial_cwd(), &file.specifier, &file.source, file.media_type, ) } else { extract_files_from_source_comments( + ps.options.initial_cwd(), &file.specifier, file.source, file.media_type, @@ -1427,10 +1437,10 @@ pub async fn run_tests_with_watch( ); if let Some(changed) = &changed { - for path in changed.iter().filter_map(|path| { - deno_core::resolve_url_or_path_deprecated(&path.to_string_lossy()) - .ok() - }) { + for path in changed + .iter() + .filter_map(|path| ModuleSpecifier::from_file_path(path).ok()) + { if modules.contains(&path) { modules_to_reload.push(specifier); break;