diff options
Diffstat (limited to 'cli/cache')
-rw-r--r-- | cli/cache/cache_db.rs | 1 | ||||
-rw-r--r-- | cli/cache/caches.rs | 49 | ||||
-rw-r--r-- | cli/cache/deno_dir.rs | 28 | ||||
-rw-r--r-- | cli/cache/mod.rs | 1 |
4 files changed, 67 insertions, 12 deletions
diff --git a/cli/cache/cache_db.rs b/cli/cache/cache_db.rs index e05ecd962..fd694062c 100644 --- a/cli/cache/cache_db.rs +++ b/cli/cache/cache_db.rs @@ -109,7 +109,6 @@ impl Drop for CacheDB { } impl CacheDB { - #[cfg(test)] pub fn in_memory( config: &'static CacheDBConfiguration, version: &'static str, diff --git a/cli/cache/caches.rs b/cli/cache/caches.rs index 62bec8a00..f630a82ff 100644 --- a/cli/cache/caches.rs +++ b/cli/cache/caches.rs @@ -1,19 +1,20 @@ // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. use std::path::PathBuf; +use std::sync::Arc; use once_cell::sync::OnceCell; use super::cache_db::CacheDB; use super::cache_db::CacheDBConfiguration; use super::check::TYPE_CHECK_CACHE_DB; +use super::deno_dir::DenoDirProvider; use super::incremental::INCREMENTAL_CACHE_DB; use super::node::NODE_ANALYSIS_CACHE_DB; use super::parsed_source::PARSED_SOURCE_CACHE_DB; -use super::DenoDir; pub struct Caches { - dir: DenoDir, + dir_provider: Arc<DenoDirProvider>, fmt_incremental_cache_db: OnceCell<CacheDB>, lint_incremental_cache_db: OnceCell<CacheDB>, dep_analysis_db: OnceCell<CacheDB>, @@ -22,9 +23,9 @@ pub struct Caches { } impl Caches { - pub fn new(dir: DenoDir) -> Self { + pub fn new(dir: Arc<DenoDirProvider>) -> Self { Self { - dir, + dir_provider: dir, fmt_incremental_cache_db: Default::default(), lint_incremental_cache_db: Default::default(), dep_analysis_db: Default::default(), @@ -36,10 +37,16 @@ impl Caches { fn make_db( cell: &OnceCell<CacheDB>, config: &'static CacheDBConfiguration, - path: PathBuf, + path: Option<PathBuf>, ) -> CacheDB { cell - .get_or_init(|| CacheDB::from_path(config, path, crate::version::deno())) + .get_or_init(|| { + if let Some(path) = path { + CacheDB::from_path(config, path, crate::version::deno()) + } else { + CacheDB::in_memory(config, crate::version::deno()) + } + }) .clone() } @@ -47,7 +54,11 @@ impl Caches { Self::make_db( &self.fmt_incremental_cache_db, &INCREMENTAL_CACHE_DB, - self.dir.fmt_incremental_cache_db_file_path(), + self + .dir_provider + .get_or_create() + .ok() + .map(|dir| dir.fmt_incremental_cache_db_file_path()), ) } @@ -55,7 +66,11 @@ impl Caches { Self::make_db( &self.lint_incremental_cache_db, &INCREMENTAL_CACHE_DB, - self.dir.lint_incremental_cache_db_file_path(), + self + .dir_provider + .get_or_create() + .ok() + .map(|dir| dir.lint_incremental_cache_db_file_path()), ) } @@ -63,7 +78,11 @@ impl Caches { Self::make_db( &self.dep_analysis_db, &PARSED_SOURCE_CACHE_DB, - self.dir.dep_analysis_db_file_path(), + self + .dir_provider + .get_or_create() + .ok() + .map(|dir| dir.dep_analysis_db_file_path()), ) } @@ -71,7 +90,11 @@ impl Caches { Self::make_db( &self.node_analysis_db, &NODE_ANALYSIS_CACHE_DB, - self.dir.node_analysis_db_file_path(), + self + .dir_provider + .get_or_create() + .ok() + .map(|dir| dir.node_analysis_db_file_path()), ) } @@ -79,7 +102,11 @@ impl Caches { Self::make_db( &self.type_checking_cache_db, &TYPE_CHECK_CACHE_DB, - self.dir.type_checking_cache_db_file_path(), + self + .dir_provider + .get_or_create() + .ok() + .map(|dir| dir.type_checking_cache_db_file_path()), ) } } diff --git a/cli/cache/deno_dir.rs b/cli/cache/deno_dir.rs index 2cee0e7a7..07bd4a61d 100644 --- a/cli/cache/deno_dir.rs +++ b/cli/cache/deno_dir.rs @@ -1,10 +1,36 @@ // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. +use once_cell::sync::OnceCell; + use super::DiskCache; use std::env; use std::path::PathBuf; +/// Lazily creates the deno dir which might be useful in scenarios +/// where functionality wants to continue if the DENO_DIR can't be created. +pub struct DenoDirProvider { + maybe_custom_root: Option<PathBuf>, + deno_dir: OnceCell<std::io::Result<DenoDir>>, +} + +impl DenoDirProvider { + pub fn new(maybe_custom_root: Option<PathBuf>) -> Self { + Self { + maybe_custom_root, + deno_dir: Default::default(), + } + } + + pub fn get_or_create(&self) -> Result<&DenoDir, std::io::Error> { + self + .deno_dir + .get_or_init(|| DenoDir::new(self.maybe_custom_root.clone())) + .as_ref() + .map_err(|err| std::io::Error::new(err.kind(), err.to_string())) + } +} + /// `DenoDir` serves as coordinator for multiple `DiskCache`s containing them /// in single directory that can be controlled with `$DENO_DIR` env variable. #[derive(Clone)] @@ -18,6 +44,8 @@ pub struct DenoDir { impl DenoDir { pub fn new(maybe_custom_root: Option<PathBuf>) -> std::io::Result<Self> { + let maybe_custom_root = + maybe_custom_root.or_else(|| env::var("DENO_DIR").map(String::into).ok()); let root: PathBuf = if let Some(root) = maybe_custom_root { root } else if let Some(cache_dir) = dirs::cache_dir() { diff --git a/cli/cache/mod.rs b/cli/cache/mod.rs index 40d74ff66..772ba10a5 100644 --- a/cli/cache/mod.rs +++ b/cli/cache/mod.rs @@ -30,6 +30,7 @@ pub use caches::Caches; pub use check::TypeCheckCache; pub use common::FastInsecureHasher; pub use deno_dir::DenoDir; +pub use deno_dir::DenoDirProvider; pub use disk_cache::DiskCache; pub use emit::EmitCache; pub use http_cache::CachedUrlMetadata; |