1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-28 16:20:57 -05:00

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 <subcommand> --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
This commit is contained in:
Bartek Iwańczuk 2023-03-13 22:44:16 -04:00 committed by GitHub
parent 48ede89f1f
commit 9aa20b3ba7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 34 additions and 24 deletions

View file

@ -691,10 +691,10 @@ pub async fn run_benchmarks_with_watch(
); );
if let Some(changed) = &changed { if let Some(changed) = &changed {
for path in changed.iter().filter_map(|path| { for path in changed
deno_core::resolve_url_or_path_deprecated(&path.to_string_lossy()) .iter()
.ok() .filter_map(|path| ModuleSpecifier::from_file_path(path).ok())
}) { {
if modules.contains(&path) { if modules.contains(&path) {
modules_to_reload.push(specifier); modules_to_reload.push(specifier);
break; break;

View file

@ -5,7 +5,7 @@ use std::sync::Arc;
use deno_core::error::AnyError; use deno_core::error::AnyError;
use deno_core::futures::FutureExt; 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_graph::Module;
use deno_runtime::colors; use deno_runtime::colors;
@ -35,16 +35,17 @@ pub async fn bundle(
"Use alternative bundlers like \"deno_emit\", \"esbuild\" or \"rollup\" instead." "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 resolver = |_| {
let cli_options = cli_options.clone(); let cli_options = cli_options.clone();
let source_file1 = &bundle_flags.source_file; let module_specifier = &module_specifier;
let source_file2 = &bundle_flags.source_file;
async move { async move {
let module_specifier = resolve_url_or_path_deprecated(source_file1)?;
log::debug!(">>>>> bundle START"); log::debug!(">>>>> bundle START");
let ps = ProcState::from_options(cli_options).await?; 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<PathBuf> = graph let mut paths_to_watch: Vec<PathBuf> = graph
.specifiers() .specifiers()
@ -74,7 +75,7 @@ pub async fn bundle(
result: Ok((ps, graph)), result: Ok((ps, graph)),
}, },
Err(e) => ResolutionResult::Restart { 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), result: Err(e),
}, },
}) })

View file

@ -9,7 +9,6 @@ use deno_core::anyhow::Context;
use deno_core::error::AnyError; use deno_core::error::AnyError;
use deno_core::resolve_path; use deno_core::resolve_path;
use deno_core::resolve_url_or_path; use deno_core::resolve_url_or_path;
use deno_core::resolve_url_or_path_deprecated;
use deno_graph::npm::NpmPackageReqReference; use deno_graph::npm::NpmPackageReqReference;
use deno_runtime::permissions::Permissions; use deno_runtime::permissions::Permissions;
use deno_runtime::permissions::PermissionsContainer; use deno_runtime::permissions::PermissionsContainer;
@ -104,10 +103,10 @@ pub async fn run_from_stdin(flags: Flags) -> Result<i32, AnyError> {
// code properly. // code properly.
async fn run_with_watch(flags: Flags, script: String) -> Result<i32, AnyError> { async fn run_with_watch(flags: Flags, script: String) -> Result<i32, AnyError> {
let flags = Arc::new(flags); let flags = Arc::new(flags);
let main_module = resolve_url_or_path_deprecated(&script)?;
let (sender, receiver) = tokio::sync::mpsc::unbounded_channel(); let (sender, receiver) = tokio::sync::mpsc::unbounded_channel();
let mut ps = let mut ps =
ProcState::build_for_file_watcher((*flags).clone(), sender.clone()).await?; 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| { let operation = |main_module: ModuleSpecifier| {
ps.reset_for_file_watcher(); ps.reset_for_file_watcher();

View file

@ -740,6 +740,7 @@ async fn test_specifier(
} }
fn extract_files_from_regex_blocks( fn extract_files_from_regex_blocks(
current_dir: &Path,
specifier: &ModuleSpecifier, specifier: &ModuleSpecifier,
source: &str, source: &str,
media_type: MediaType, media_type: MediaType,
@ -799,13 +800,16 @@ fn extract_files_from_regex_blocks(
writeln!(file_source, "{}", text.as_str()).unwrap(); writeln!(file_source, "{}", text.as_str()).unwrap();
} }
let file_specifier = deno_core::resolve_url_or_path_deprecated(&format!( let file_specifier = deno_core::resolve_url_or_path(
"{}${}-{}{}", &format!(
specifier, "{}${}-{}{}",
file_line_index + line_offset + 1, specifier,
file_line_index + line_offset + line_count + 1, file_line_index + line_offset + 1,
file_media_type.as_ts_extension(), file_line_index + line_offset + line_count + 1,
)) file_media_type.as_ts_extension(),
),
current_dir,
)
.unwrap(); .unwrap();
Some(File { Some(File {
@ -823,6 +827,7 @@ fn extract_files_from_regex_blocks(
} }
fn extract_files_from_source_comments( fn extract_files_from_source_comments(
current_dir: &Path,
specifier: &ModuleSpecifier, specifier: &ModuleSpecifier,
source: Arc<str>, source: Arc<str>,
media_type: MediaType, media_type: MediaType,
@ -850,6 +855,7 @@ fn extract_files_from_source_comments(
}) })
.flat_map(|comment| { .flat_map(|comment| {
extract_files_from_regex_blocks( extract_files_from_regex_blocks(
current_dir,
specifier, specifier,
&comment.text, &comment.text,
media_type, media_type,
@ -865,6 +871,7 @@ fn extract_files_from_source_comments(
} }
fn extract_files_from_fenced_blocks( fn extract_files_from_fenced_blocks(
current_dir: &Path,
specifier: &ModuleSpecifier, specifier: &ModuleSpecifier,
source: &str, source: &str,
media_type: MediaType, media_type: MediaType,
@ -878,6 +885,7 @@ fn extract_files_from_fenced_blocks(
let lines_regex = Regex::new(r"(?:\# ?)?(.*)")?; let lines_regex = Regex::new(r"(?:\# ?)?(.*)")?;
extract_files_from_regex_blocks( extract_files_from_regex_blocks(
current_dir,
specifier, specifier,
source, source,
media_type, media_type,
@ -898,12 +906,14 @@ async fn fetch_inline_files(
let inline_files = if file.media_type == MediaType::Unknown { let inline_files = if file.media_type == MediaType::Unknown {
extract_files_from_fenced_blocks( extract_files_from_fenced_blocks(
ps.options.initial_cwd(),
&file.specifier, &file.specifier,
&file.source, &file.source,
file.media_type, file.media_type,
) )
} else { } else {
extract_files_from_source_comments( extract_files_from_source_comments(
ps.options.initial_cwd(),
&file.specifier, &file.specifier,
file.source, file.source,
file.media_type, file.media_type,
@ -1427,10 +1437,10 @@ pub async fn run_tests_with_watch(
); );
if let Some(changed) = &changed { if let Some(changed) = &changed {
for path in changed.iter().filter_map(|path| { for path in changed
deno_core::resolve_url_or_path_deprecated(&path.to_string_lossy()) .iter()
.ok() .filter_map(|path| ModuleSpecifier::from_file_path(path).ok())
}) { {
if modules.contains(&path) { if modules.contains(&path) {
modules_to_reload.push(specifier); modules_to_reload.push(specifier);
break; break;