From 3a601e56f4401fefb235b01e1405f697397b9790 Mon Sep 17 00:00:00 2001 From: David Sherret Date: Thu, 1 Sep 2022 12:37:14 -0400 Subject: [PATCH] fix(npm): ignore npm cache directory creation errors (#15728) --- cli/npm/cache.rs | 42 +++++++++++++++++++++++++----------------- cli/npm/mod.rs | 8 ++++---- cli/proc_state.rs | 2 +- 3 files changed, 30 insertions(+), 22 deletions(-) diff --git a/cli/npm/cache.rs b/cli/npm/cache.rs index 4ab39f6bd0..e5193c77c5 100644 --- a/cli/npm/cache.rs +++ b/cli/npm/cache.rs @@ -2,6 +2,7 @@ use std::borrow::Cow; use std::fs; +use std::path::Path; use std::path::PathBuf; use deno_ast::ModuleSpecifier; @@ -41,23 +42,33 @@ impl Default for ReadonlyNpmCache { // This only gets used when creating the tsc runtime and for testing, and so // it shouldn't ever actually access the DenoDir, so it doesn't support a // custom root. - Self::from_deno_dir(&crate::deno_dir::DenoDir::new(None).unwrap()).unwrap() + Self::from_deno_dir(&crate::deno_dir::DenoDir::new(None).unwrap()) } } impl ReadonlyNpmCache { - pub fn new(root_dir: PathBuf) -> Result { - std::fs::create_dir_all(&root_dir) - .with_context(|| format!("Error creating {}", root_dir.display()))?; - let root_dir = crate::fs_util::canonicalize_path(&root_dir)?; + pub fn new(root_dir: PathBuf) -> Self { + fn try_get_canonicalized_root_dir( + root_dir: &Path, + ) -> Result { + if !root_dir.exists() { + std::fs::create_dir_all(&root_dir) + .with_context(|| format!("Error creating {}", root_dir.display()))?; + } + Ok(crate::fs_util::canonicalize_path(root_dir)?) + } + + // this may fail on readonly file systems, so just ignore if so + let root_dir = + try_get_canonicalized_root_dir(&root_dir).unwrap_or(root_dir); let root_dir_url = Url::from_directory_path(&root_dir).unwrap(); - Ok(Self { + Self { root_dir, root_dir_url, - }) + } } - pub fn from_deno_dir(dir: &DenoDir) -> Result { + pub fn from_deno_dir(dir: &DenoDir) -> Self { Self::new(dir.root.join("npm")) } @@ -161,14 +172,11 @@ pub struct NpmCache { } impl NpmCache { - pub fn from_deno_dir( - dir: &DenoDir, - cache_setting: CacheSetting, - ) -> Result { - Ok(Self { - readonly: ReadonlyNpmCache::from_deno_dir(dir)?, + pub fn from_deno_dir(dir: &DenoDir, cache_setting: CacheSetting) -> Self { + Self { + readonly: ReadonlyNpmCache::from_deno_dir(dir), cache_setting, - }) + } } pub fn as_readonly(&self) -> ReadonlyNpmCache { @@ -288,7 +296,7 @@ mod test { #[test] fn should_get_lowercase_package_folder() { let root_dir = crate::deno_dir::DenoDir::new(None).unwrap().root; - let cache = ReadonlyNpmCache::new(root_dir.clone()).unwrap(); + let cache = ReadonlyNpmCache::new(root_dir.clone()); let registry_url = Url::parse("https://registry.npmjs.org/").unwrap(); // all lowercase should be as-is @@ -311,7 +319,7 @@ mod test { fn should_handle_non_all_lowercase_package_names() { // it was possible at one point for npm packages to not just be lowercase let root_dir = crate::deno_dir::DenoDir::new(None).unwrap().root; - let cache = ReadonlyNpmCache::new(root_dir.clone()).unwrap(); + let cache = ReadonlyNpmCache::new(root_dir.clone()); let registry_url = Url::parse("https://registry.npmjs.org/").unwrap(); let json_uppercase_hash = "db1a21a0bc2ef8fbe13ac4cf044e8c9116d29137d5ed8b916ab63dcb2d4290df"; diff --git a/cli/npm/mod.rs b/cli/npm/mod.rs index 7bb515e9e2..5b49afb3d0 100644 --- a/cli/npm/mod.rs +++ b/cli/npm/mod.rs @@ -85,13 +85,13 @@ impl GlobalNpmPackageResolver { reload: bool, cache_setting: CacheSetting, unstable: bool, - ) -> Result { - Ok(Self::from_cache( - NpmCache::from_deno_dir(dir, cache_setting.clone())?, + ) -> Self { + Self::from_cache( + NpmCache::from_deno_dir(dir, cache_setting.clone()), reload, cache_setting, unstable, - )) + ) } fn from_cache( diff --git a/cli/proc_state.rs b/cli/proc_state.rs index 7175f18e16..42c34686f7 100644 --- a/cli/proc_state.rs +++ b/cli/proc_state.rs @@ -229,7 +229,7 @@ impl ProcState { cli_options.unstable() // don't do the unstable error when in the lsp || matches!(cli_options.sub_command(), DenoSubcommand::Lsp), - )?; + ); Ok(ProcState(Arc::new(Inner { dir,