fix(npm): ignore npm cache directory creation errors (#15728)

This commit is contained in:
David Sherret 2022-09-01 12:37:14 -04:00 committed by GitHub
parent 67713f4b93
commit 3a601e56f4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 30 additions and 22 deletions

View file

@ -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";

View file

@ -85,13 +85,13 @@ impl GlobalNpmPackageResolver {
reload: bool,
cache_setting: CacheSetting,
unstable: bool,
) -> Result<Self, AnyError> {
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(

View file

@ -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,