summaryrefslogtreecommitdiff
path: root/cli/cache/deno_dir.rs
diff options
context:
space:
mode:
authorDavid Sherret <dsherret@users.noreply.github.com>2023-05-25 14:27:45 -0400
committerGitHub <noreply@github.com>2023-05-25 14:27:45 -0400
commit2ebd61ee1b09c8060c2da66890bf6ac024d97b67 (patch)
tree014c6d35e1f820811a2ff6dcb0e96c60f360de1f /cli/cache/deno_dir.rs
parent76400149a49f44b734b5cacf438722bc3c07c1d1 (diff)
fix(compile): handle when DENO_DIR is readonly (#19257)
Closes #19253
Diffstat (limited to 'cli/cache/deno_dir.rs')
-rw-r--r--cli/cache/deno_dir.rs28
1 files changed, 28 insertions, 0 deletions
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() {