diff options
author | Bartek IwaĆczuk <biwanczuk@gmail.com> | 2023-03-13 21:12:09 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-03-14 01:12:09 +0000 |
commit | 48ede89f1f192df28cc74822d7bb79b0b4bd0957 (patch) | |
tree | d6dd65cf03152fb0253aba551f7ed016e3b7b09f /cli/args | |
parent | c4771356f27b250e7fdbcede0de5682982720455 (diff) |
refactor(core): resolve_url_or_path and resolve_url_or_path_deprecated (#18170)
This commit changes current "deno_core::resolve_url_or_path" API to
"resolve_url_or_path_deprecated" and adds new "resolve_url_or_path"
API that requires to explicitly pass the directory from which paths
should be resolved to.
Some of the call sites were updated to use the new API, the reminder
of them will be updated in a follow up PR.
Towards landing https://github.com/denoland/deno/pull/15454
Diffstat (limited to 'cli/args')
-rw-r--r-- | cli/args/config_file.rs | 2 | ||||
-rw-r--r-- | cli/args/flags.rs | 29 | ||||
-rw-r--r-- | cli/args/mod.rs | 65 |
3 files changed, 66 insertions, 30 deletions
diff --git a/cli/args/config_file.rs b/cli/args/config_file.rs index b5ee8c50a..6df06da5e 100644 --- a/cli/args/config_file.rs +++ b/cli/args/config_file.rs @@ -506,7 +506,7 @@ impl ConfigFile { Ok(Some(ConfigFile::read(&config_path)?)) } ConfigFlag::Discover => { - if let Some(config_path_args) = flags.config_path_args() { + if let Some(config_path_args) = flags.config_path_args(cwd) { let mut checked = HashSet::new(); for f in config_path_args { if let Some(cf) = Self::discover_from(&f, &mut checked)? { diff --git a/cli/args/flags.rs b/cli/args/flags.rs index 26cf497f6..883a4d034 100644 --- a/cli/args/flags.rs +++ b/cli/args/flags.rs @@ -6,6 +6,7 @@ use clap::ColorChoice; use clap::Command; use clap::ValueHint; use deno_core::error::AnyError; +use deno_core::resolve_url_or_path; use deno_core::url::Url; use deno_runtime::permissions::parse_sys_kind; use log::debug; @@ -16,6 +17,7 @@ use std::net::SocketAddr; use std::num::NonZeroU32; use std::num::NonZeroU8; use std::num::NonZeroUsize; +use std::path::Path; use std::path::PathBuf; use std::str::FromStr; @@ -474,16 +476,17 @@ impl Flags { /// Extract path arguments for config search paths. /// If it returns Some(vec), the config should be discovered - /// from the current dir after trying to discover from each entry in vec. + /// from the passed `current_dir` after trying to discover from each entry in + /// the returned vector. /// If it returns None, the config file shouldn't be discovered at all. - pub fn config_path_args(&self) -> Option<Vec<PathBuf>> { + pub fn config_path_args(&self, current_dir: &Path) -> Option<Vec<PathBuf>> { use DenoSubcommand::*; match &self.subcommand { Fmt(FmtFlags { files, .. }) => Some(files.include.clone()), Lint(LintFlags { files, .. }) => Some(files.include.clone()), Run(RunFlags { script }) => { - if let Ok(module_specifier) = deno_core::resolve_url_or_path(script) { + if let Ok(module_specifier) = resolve_url_or_path(script, current_dir) { if module_specifier.scheme() == "file" || module_specifier.scheme() == "npm" { @@ -520,12 +523,12 @@ impl Flags { /// from the `path` dir. /// If it returns None, the `package.json` file shouldn't be discovered at /// all. - pub fn package_json_search_dir(&self) -> Option<PathBuf> { + pub fn package_json_search_dir(&self, current_dir: &Path) -> Option<PathBuf> { use DenoSubcommand::*; match &self.subcommand { Run(RunFlags { script }) => { - let module_specifier = deno_core::resolve_url_or_path(script).ok()?; + let module_specifier = resolve_url_or_path(script, current_dir).ok()?; if module_specifier.scheme() == "file" { let p = module_specifier .to_file_path() @@ -540,7 +543,7 @@ impl Flags { } } Task(TaskFlags { cwd: Some(cwd), .. }) => { - deno_core::resolve_url_or_path(cwd) + resolve_url_or_path(cwd, current_dir) .ok()? .to_file_path() .ok() @@ -6338,30 +6341,28 @@ mod tests { #[test] fn test_config_path_args() { let flags = flags_from_vec(svec!["deno", "run", "foo.js"]).unwrap(); - assert_eq!( - flags.config_path_args(), - Some(vec![std::env::current_dir().unwrap().join("foo.js")]) - ); + let cwd = std::env::current_dir().unwrap(); + assert_eq!(flags.config_path_args(&cwd), Some(vec![cwd.join("foo.js")])); let flags = flags_from_vec(svec!["deno", "run", "https://example.com/foo.js"]) .unwrap(); - assert_eq!(flags.config_path_args(), None); + assert_eq!(flags.config_path_args(&cwd), None); let flags = flags_from_vec(svec!["deno", "lint", "dir/a.js", "dir/b.js"]).unwrap(); assert_eq!( - flags.config_path_args(), + flags.config_path_args(&cwd), Some(vec![PathBuf::from("dir/a.js"), PathBuf::from("dir/b.js")]) ); let flags = flags_from_vec(svec!["deno", "lint"]).unwrap(); - assert!(flags.config_path_args().unwrap().is_empty()); + assert!(flags.config_path_args(&cwd).unwrap().is_empty()); let flags = flags_from_vec(svec!["deno", "fmt", "dir/a.js", "dir/b.js"]).unwrap(); assert_eq!( - flags.config_path_args(), + flags.config_path_args(&cwd), Some(vec![PathBuf::from("dir/a.js"), PathBuf::from("dir/b.js")]) ); } diff --git a/cli/args/mod.rs b/cli/args/mod.rs index 5be5fc7ab..25a1514f3 100644 --- a/cli/args/mod.rs +++ b/cli/args/mod.rs @@ -385,11 +385,12 @@ fn resolve_lint_rules_options( fn discover_package_json( flags: &Flags, maybe_stop_at: Option<PathBuf>, + current_dir: &Path, ) -> Result<Option<PackageJson>, AnyError> { // TODO(bartlomieju): discover for all subcommands, but print warnings that // `package.json` is ignored in bundle/compile/etc. - if let Some(package_json_dir) = flags.package_json_search_dir() { + if let Some(package_json_dir) = flags.package_json_search_dir(current_dir) { let package_json_dir = canonicalize_path_maybe_not_exists(&package_json_dir)?; return package_json::discover_from(&package_json_dir, maybe_stop_at); @@ -509,6 +510,7 @@ pub struct CliOptions { // the source of the options is a detail the rest of the // application need not concern itself with, so keep these private flags: Flags, + initial_cwd: PathBuf, maybe_node_modules_folder: Option<PathBuf>, maybe_config_file: Option<ConfigFile>, maybe_package_json: Option<PackageJson>, @@ -549,6 +551,7 @@ impl CliOptions { Ok(Self { flags, + initial_cwd, maybe_config_file, maybe_lockfile, maybe_package_json, @@ -577,10 +580,11 @@ impl CliOptions { .parent() .map(|p| p.to_path_buf()); - maybe_package_json = discover_package_json(&flags, maybe_stop_at)?; + maybe_package_json = + discover_package_json(&flags, maybe_stop_at, &initial_cwd)?; } } else { - maybe_package_json = discover_package_json(&flags, None)?; + maybe_package_json = discover_package_json(&flags, None, &initial_cwd)?; } let maybe_lock_file = @@ -594,6 +598,11 @@ impl CliOptions { ) } + #[inline(always)] + pub fn initial_cwd(&self) -> &Path { + &self.initial_cwd + } + pub fn maybe_config_file_specifier(&self) -> Option<ModuleSpecifier> { self.maybe_config_file.as_ref().map(|f| f.specifier.clone()) } @@ -641,6 +650,7 @@ impl CliOptions { None => resolve_import_map_specifier( self.flags.import_map_path.as_deref(), self.maybe_config_file.as_ref(), + &self.initial_cwd, ), } } @@ -1071,6 +1081,7 @@ fn resolve_local_node_modules_folder( fn resolve_import_map_specifier( maybe_import_map_path: Option<&str>, maybe_config_file: Option<&ConfigFile>, + current_dir: &Path, ) -> Result<Option<ModuleSpecifier>, AnyError> { if let Some(import_map_path) = maybe_import_map_path { if let Some(config_file) = &maybe_config_file { @@ -1078,8 +1089,9 @@ fn resolve_import_map_specifier( log::warn!("{} the configuration file \"{}\" contains an entry for \"importMap\" that is being ignored.", colors::yellow("Warning"), config_file.specifier); } } - let specifier = deno_core::resolve_url_or_path(import_map_path) - .context(format!("Bad URL (\"{import_map_path}\") for import map."))?; + let specifier = + deno_core::resolve_url_or_path(import_map_path, current_dir) + .context(format!("Bad URL (\"{import_map_path}\") for import map."))?; return Ok(Some(specifier)); } else if let Some(config_file) = &maybe_config_file { // if the config file is an import map we prefer to use it, over `importMap` @@ -1171,7 +1183,11 @@ mod test { let config_specifier = ModuleSpecifier::parse("file:///deno/deno.jsonc").unwrap(); let config_file = ConfigFile::new(config_text, &config_specifier).unwrap(); - let actual = resolve_import_map_specifier(None, Some(&config_file)); + let actual = resolve_import_map_specifier( + None, + Some(&config_file), + &PathBuf::from("/"), + ); assert!(actual.is_ok()); let actual = actual.unwrap(); assert_eq!( @@ -1188,7 +1204,11 @@ mod test { let config_specifier = ModuleSpecifier::parse("file:///deno/deno.jsonc").unwrap(); let config_file = ConfigFile::new(config_text, &config_specifier).unwrap(); - let actual = resolve_import_map_specifier(None, Some(&config_file)); + let actual = resolve_import_map_specifier( + None, + Some(&config_file), + &PathBuf::from("/"), + ); assert!(actual.is_ok()); let actual = actual.unwrap(); assert_eq!( @@ -1207,7 +1227,11 @@ mod test { let config_specifier = ModuleSpecifier::parse("https://example.com/deno.jsonc").unwrap(); let config_file = ConfigFile::new(config_text, &config_specifier).unwrap(); - let actual = resolve_import_map_specifier(None, Some(&config_file)); + let actual = resolve_import_map_specifier( + None, + Some(&config_file), + &PathBuf::from("/"), + ); assert!(actual.is_ok()); let actual = actual.unwrap(); assert_eq!( @@ -1223,13 +1247,16 @@ mod test { let config_text = r#"{ "importMap": "import_map.json" }"#; + let cwd = &PathBuf::from("/"); let config_specifier = ModuleSpecifier::parse("file:///deno/deno.jsonc").unwrap(); let config_file = ConfigFile::new(config_text, &config_specifier).unwrap(); - let actual = - resolve_import_map_specifier(Some("import-map.json"), Some(&config_file)); - let import_map_path = - std::env::current_dir().unwrap().join("import-map.json"); + let actual = resolve_import_map_specifier( + Some("import-map.json"), + Some(&config_file), + cwd, + ); + let import_map_path = cwd.join("import-map.json"); let expected_specifier = ModuleSpecifier::from_file_path(import_map_path).unwrap(); assert!(actual.is_ok()); @@ -1246,7 +1273,11 @@ mod test { let config_specifier = ModuleSpecifier::parse("file:///deno/deno.jsonc").unwrap(); let config_file = ConfigFile::new(config_text, &config_specifier).unwrap(); - let actual = resolve_import_map_specifier(None, Some(&config_file)); + let actual = resolve_import_map_specifier( + None, + Some(&config_file), + &PathBuf::from("/"), + ); assert!(actual.is_ok()); let actual = actual.unwrap(); assert_eq!(actual, Some(config_specifier)); @@ -1258,7 +1289,11 @@ mod test { let config_specifier = ModuleSpecifier::parse("file:///deno/deno.jsonc").unwrap(); let config_file = ConfigFile::new(config_text, &config_specifier).unwrap(); - let actual = resolve_import_map_specifier(None, Some(&config_file)); + let actual = resolve_import_map_specifier( + None, + Some(&config_file), + &PathBuf::from("/"), + ); assert!(actual.is_ok()); let actual = actual.unwrap(); assert_eq!(actual, None); @@ -1266,7 +1301,7 @@ mod test { #[test] fn resolve_import_map_no_config() { - let actual = resolve_import_map_specifier(None, None); + let actual = resolve_import_map_specifier(None, None, &PathBuf::from("/")); assert!(actual.is_ok()); let actual = actual.unwrap(); assert_eq!(actual, None); |