diff options
Diffstat (limited to 'cli/args/config_file.rs')
-rw-r--r-- | cli/args/config_file.rs | 58 |
1 files changed, 32 insertions, 26 deletions
diff --git a/cli/args/config_file.rs b/cli/args/config_file.rs index 0d84de2e4..154692376 100644 --- a/cli/args/config_file.rs +++ b/cli/args/config_file.rs @@ -18,6 +18,7 @@ use deno_core::serde_json; use deno_core::serde_json::json; use deno_core::serde_json::Value; use deno_core::ModuleSpecifier; +use std::borrow::Cow; use std::collections::BTreeMap; use std::collections::HashMap; use std::collections::HashSet; @@ -483,10 +484,21 @@ pub struct ConfigFile { } impl ConfigFile { - pub fn discover(flags: &Flags) -> Result<Option<ConfigFile>, AnyError> { + pub fn discover( + flags: &Flags, + cwd: &Path, + ) -> Result<Option<ConfigFile>, AnyError> { match &flags.config_flag { ConfigFlag::Disabled => Ok(None), - ConfigFlag::Path(config_path) => Ok(Some(ConfigFile::read(config_path)?)), + ConfigFlag::Path(config_path) => { + let config_path = PathBuf::from(config_path); + let config_path = if config_path.is_absolute() { + config_path + } else { + cwd.join(config_path) + }; + Ok(Some(ConfigFile::read(&config_path)?)) + } ConfigFlag::Discover => { if let Some(config_path_args) = flags.config_path_args() { let mut checked = HashSet::new(); @@ -508,8 +520,7 @@ impl ConfigFile { } }; // From CWD walk up to root looking for deno.json or deno.jsonc - let cwd = std::env::current_dir()?; - Self::discover_from(&cwd, &mut checked) + Self::discover_from(cwd, &mut checked) } else { Ok(None) } @@ -524,6 +535,14 @@ impl ConfigFile { /// Filenames that Deno will recognize when discovering config. const CONFIG_FILE_NAMES: [&str; 2] = ["deno.json", "deno.jsonc"]; + // todo(dsherret): in the future, we should force all callers + // to provide a resolved path + let start = if start.is_absolute() { + Cow::Borrowed(start) + } else { + Cow::Owned(std::env::current_dir()?.join(start)) + }; + for ancestor in start.ancestors() { if checked.insert(ancestor.to_path_buf()) { for config_filename in CONFIG_FILE_NAMES { @@ -556,34 +575,29 @@ impl ConfigFile { Ok(None) } - pub fn read(path_ref: impl AsRef<Path>) -> Result<Self, AnyError> { - let path = Path::new(path_ref.as_ref()); - let config_file = if path.is_absolute() { - path.to_path_buf() - } else { - std::env::current_dir()?.join(path_ref) - }; + pub fn read(config_path: &Path) -> Result<Self, AnyError> { + debug_assert!(config_path.is_absolute()); // perf: Check if the config file exists before canonicalizing path. - if !config_file.exists() { + if !config_path.exists() { return Err( std::io::Error::new( std::io::ErrorKind::InvalidInput, format!( "Could not find the config file: {}", - config_file.to_string_lossy() + config_path.to_string_lossy() ), ) .into(), ); } - let config_path = canonicalize_path(&config_file).map_err(|_| { + let config_path = canonicalize_path(config_path).map_err(|_| { std::io::Error::new( std::io::ErrorKind::InvalidInput, format!( "Could not find the config file: {}", - config_file.to_string_lossy() + config_path.to_string_lossy() ), ) })?; @@ -991,24 +1005,16 @@ mod tests { use pretty_assertions::assert_eq; #[test] - fn read_config_file_relative() { - let config_file = - ConfigFile::read("tests/testdata/module_graph/tsconfig.json") - .expect("Failed to load config file"); - assert!(config_file.json.compiler_options.is_some()); - } - - #[test] fn read_config_file_absolute() { let path = test_util::testdata_path().join("module_graph/tsconfig.json"); - let config_file = ConfigFile::read(path.to_str().unwrap()) - .expect("Failed to load config file"); + let config_file = ConfigFile::read(&path).unwrap(); assert!(config_file.json.compiler_options.is_some()); } #[test] fn include_config_path_on_error() { - let error = ConfigFile::read("404.json").err().unwrap(); + let path = test_util::testdata_path().join("404.json"); + let error = ConfigFile::read(&path).err().unwrap(); assert!(error.to_string().contains("404.json")); } |