diff options
author | David Sherret <dsherret@users.noreply.github.com> | 2022-09-01 12:37:14 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-09-01 12:37:14 -0400 |
commit | 3a601e56f4401fefb235b01e1405f697397b9790 (patch) | |
tree | 512a657a0d6be1eec43c004c184cf1875cbc5aa7 /cli/npm/cache.rs | |
parent | 67713f4b9354799c4013dda9558470ce18989627 (diff) |
fix(npm): ignore npm cache directory creation errors (#15728)
Diffstat (limited to 'cli/npm/cache.rs')
-rw-r--r-- | cli/npm/cache.rs | 42 |
1 files changed, 25 insertions, 17 deletions
diff --git a/cli/npm/cache.rs b/cli/npm/cache.rs index 4ab39f6bd..e5193c77c 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<Self, AnyError> { - 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<PathBuf, AnyError> { + 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<Self, AnyError> { + 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<Self, AnyError> { - 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"; |