mirror of
https://github.com/denoland/deno.git
synced 2024-12-22 15:24:46 -05:00
fix(npm): ignore npm cache directory creation errors (#15728)
This commit is contained in:
parent
67713f4b93
commit
3a601e56f4
3 changed files with 30 additions and 22 deletions
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
use std::borrow::Cow;
|
use std::borrow::Cow;
|
||||||
use std::fs;
|
use std::fs;
|
||||||
|
use std::path::Path;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
|
||||||
use deno_ast::ModuleSpecifier;
|
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
|
// 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
|
// it shouldn't ever actually access the DenoDir, so it doesn't support a
|
||||||
// custom root.
|
// 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 {
|
impl ReadonlyNpmCache {
|
||||||
pub fn new(root_dir: PathBuf) -> Result<Self, AnyError> {
|
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)
|
std::fs::create_dir_all(&root_dir)
|
||||||
.with_context(|| format!("Error creating {}", root_dir.display()))?;
|
.with_context(|| format!("Error creating {}", root_dir.display()))?;
|
||||||
let root_dir = crate::fs_util::canonicalize_path(&root_dir)?;
|
}
|
||||||
let root_dir_url = Url::from_directory_path(&root_dir).unwrap();
|
Ok(crate::fs_util::canonicalize_path(root_dir)?)
|
||||||
Ok(Self {
|
|
||||||
root_dir,
|
|
||||||
root_dir_url,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn from_deno_dir(dir: &DenoDir) -> Result<Self, AnyError> {
|
// 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();
|
||||||
|
Self {
|
||||||
|
root_dir,
|
||||||
|
root_dir_url,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn from_deno_dir(dir: &DenoDir) -> Self {
|
||||||
Self::new(dir.root.join("npm"))
|
Self::new(dir.root.join("npm"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -161,14 +172,11 @@ pub struct NpmCache {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl NpmCache {
|
impl NpmCache {
|
||||||
pub fn from_deno_dir(
|
pub fn from_deno_dir(dir: &DenoDir, cache_setting: CacheSetting) -> Self {
|
||||||
dir: &DenoDir,
|
Self {
|
||||||
cache_setting: CacheSetting,
|
readonly: ReadonlyNpmCache::from_deno_dir(dir),
|
||||||
) -> Result<Self, AnyError> {
|
|
||||||
Ok(Self {
|
|
||||||
readonly: ReadonlyNpmCache::from_deno_dir(dir)?,
|
|
||||||
cache_setting,
|
cache_setting,
|
||||||
})
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn as_readonly(&self) -> ReadonlyNpmCache {
|
pub fn as_readonly(&self) -> ReadonlyNpmCache {
|
||||||
|
@ -288,7 +296,7 @@ mod test {
|
||||||
#[test]
|
#[test]
|
||||||
fn should_get_lowercase_package_folder() {
|
fn should_get_lowercase_package_folder() {
|
||||||
let root_dir = crate::deno_dir::DenoDir::new(None).unwrap().root;
|
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 registry_url = Url::parse("https://registry.npmjs.org/").unwrap();
|
||||||
|
|
||||||
// all lowercase should be as-is
|
// all lowercase should be as-is
|
||||||
|
@ -311,7 +319,7 @@ mod test {
|
||||||
fn should_handle_non_all_lowercase_package_names() {
|
fn should_handle_non_all_lowercase_package_names() {
|
||||||
// it was possible at one point for npm packages to not just be lowercase
|
// 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 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 registry_url = Url::parse("https://registry.npmjs.org/").unwrap();
|
||||||
let json_uppercase_hash =
|
let json_uppercase_hash =
|
||||||
"db1a21a0bc2ef8fbe13ac4cf044e8c9116d29137d5ed8b916ab63dcb2d4290df";
|
"db1a21a0bc2ef8fbe13ac4cf044e8c9116d29137d5ed8b916ab63dcb2d4290df";
|
||||||
|
|
|
@ -85,13 +85,13 @@ impl GlobalNpmPackageResolver {
|
||||||
reload: bool,
|
reload: bool,
|
||||||
cache_setting: CacheSetting,
|
cache_setting: CacheSetting,
|
||||||
unstable: bool,
|
unstable: bool,
|
||||||
) -> Result<Self, AnyError> {
|
) -> Self {
|
||||||
Ok(Self::from_cache(
|
Self::from_cache(
|
||||||
NpmCache::from_deno_dir(dir, cache_setting.clone())?,
|
NpmCache::from_deno_dir(dir, cache_setting.clone()),
|
||||||
reload,
|
reload,
|
||||||
cache_setting,
|
cache_setting,
|
||||||
unstable,
|
unstable,
|
||||||
))
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn from_cache(
|
fn from_cache(
|
||||||
|
|
|
@ -229,7 +229,7 @@ impl ProcState {
|
||||||
cli_options.unstable()
|
cli_options.unstable()
|
||||||
// don't do the unstable error when in the lsp
|
// don't do the unstable error when in the lsp
|
||||||
|| matches!(cli_options.sub_command(), DenoSubcommand::Lsp),
|
|| matches!(cli_options.sub_command(), DenoSubcommand::Lsp),
|
||||||
)?;
|
);
|
||||||
|
|
||||||
Ok(ProcState(Arc::new(Inner {
|
Ok(ProcState(Arc::new(Inner {
|
||||||
dir,
|
dir,
|
||||||
|
|
Loading…
Reference in a new issue