summaryrefslogtreecommitdiff
path: root/cli
diff options
context:
space:
mode:
authorYoshiya Hinosawa <stibium121@gmail.com>2020-05-07 21:32:57 +0900
committerGitHub <noreply@github.com>2020-05-07 14:32:57 +0200
commitdabe88f854b7cc04518c5ff75c55fa437360a91f (patch)
tree9505e8cce910ab80162e2e7ab7f145334b55860c /cli
parent761b7efb3b8a140caad12803619a2e8a535cc178 (diff)
fix(deno_dir): better error message (#5120)
Add better error messages when a cache subdirectory in `DENO_DIR` cannot be created.
Diffstat (limited to 'cli')
-rw-r--r--cli/deno_dir.rs1
-rw-r--r--cli/disk_cache.rs23
-rw-r--r--cli/file_fetcher.rs2
-rw-r--r--cli/global_state.rs3
-rw-r--r--cli/http_cache.rs31
5 files changed, 45 insertions, 15 deletions
diff --git a/cli/deno_dir.rs b/cli/deno_dir.rs
index 40496704a..645a053d0 100644
--- a/cli/deno_dir.rs
+++ b/cli/deno_dir.rs
@@ -31,6 +31,7 @@ impl DenoDir {
root,
gen_cache: DiskCache::new(&gen_path),
};
+ deno_dir.gen_cache.ensure_location()?;
Ok(deno_dir)
}
diff --git a/cli/disk_cache.rs b/cli/disk_cache.rs
index e458bb135..f486e88af 100644
--- a/cli/disk_cache.rs
+++ b/cli/disk_cache.rs
@@ -1,6 +1,7 @@
use crate::fs as deno_fs;
use std::ffi::OsStr;
use std::fs;
+use std::io;
use std::path::Component;
use std::path::Path;
use std::path::PathBuf;
@@ -22,14 +23,24 @@ fn with_io_context<T: AsRef<str>>(
impl DiskCache {
pub fn new(location: &Path) -> Self {
- if !&location.is_dir() {
- fs::create_dir_all(&location).ok();
- }
Self {
location: location.to_owned(),
}
}
+ /// Ensures the location of the cache.
+ pub fn ensure_location(&self) -> io::Result<()> {
+ if self.location.is_dir() {
+ return Ok(());
+ }
+ fs::create_dir_all(&self.location).map_err(|e| {
+ io::Error::new(e.kind(), format!(
+ "Could not create TypeScript compiler cache location: {:?}\nCheck the permission of the directory.",
+ self.location
+ ))
+ })
+ }
+
pub fn get_cache_filename(&self, url: &Url) -> PathBuf {
let mut out = PathBuf::new();
@@ -140,7 +151,8 @@ mod tests {
let cache_location = TempDir::new().unwrap();
let mut cache_path = cache_location.path().to_owned();
cache_path.push("foo");
- DiskCache::new(&cache_path);
+ let cache = DiskCache::new(&cache_path);
+ cache.ensure_location().expect("Testing expect:");
assert!(cache_path.is_dir());
}
@@ -151,7 +163,8 @@ mod tests {
assert!(fs::remove_dir(&cache_location).is_ok());
cache_location.push("foo");
assert_eq!(cache_location.is_dir(), false);
- DiskCache::new(&cache_location);
+ let cache = DiskCache::new(&cache_location);
+ cache.ensure_location().expect("Testing expect:");
assert_eq!(cache_location.is_dir(), true);
}
diff --git a/cli/file_fetcher.rs b/cli/file_fetcher.rs
index 30c9d2436..1a95fd547 100644
--- a/cli/file_fetcher.rs
+++ b/cli/file_fetcher.rs
@@ -661,7 +661,7 @@ mod tests {
fn setup_file_fetcher(dir_path: &Path) -> SourceFileFetcher {
SourceFileFetcher::new(
- HttpCache::new(&dir_path.to_path_buf().join("deps")).unwrap(),
+ HttpCache::new(&dir_path.to_path_buf().join("deps")),
true,
vec![],
false,
diff --git a/cli/global_state.rs b/cli/global_state.rs
index a682e8de2..dbea5f827 100644
--- a/cli/global_state.rs
+++ b/cli/global_state.rs
@@ -54,7 +54,8 @@ impl GlobalState {
let custom_root = env::var("DENO_DIR").map(String::into).ok();
let dir = deno_dir::DenoDir::new(custom_root)?;
let deps_cache_location = dir.root.join("deps");
- let http_cache = http_cache::HttpCache::new(&deps_cache_location)?;
+ let http_cache = http_cache::HttpCache::new(&deps_cache_location);
+ http_cache.ensure_location()?;
let file_fetcher = SourceFileFetcher::new(
http_cache,
diff --git a/cli/http_cache.rs b/cli/http_cache.rs
index 1f507316c..2771cb763 100644
--- a/cli/http_cache.rs
+++ b/cli/http_cache.rs
@@ -11,6 +11,7 @@ use serde::Serialize;
use serde_derive::Deserialize;
use std::fs;
use std::fs::File;
+use std::io;
use std::path::Path;
use std::path::PathBuf;
use url::Url;
@@ -101,12 +102,26 @@ impl Metadata {
}
impl HttpCache {
- /// Returns error if unable to create directory
- /// at specified location.
- pub fn new(location: &Path) -> Result<Self, ErrBox> {
- fs::create_dir_all(&location)?;
- Ok(Self {
+ /// Returns a new instance.
+ pub fn new(location: &Path) -> Self {
+ Self {
location: location.to_owned(),
+ }
+ }
+
+ /// Ensures the location of the cache.
+ pub fn ensure_location(&self) -> io::Result<()> {
+ if self.location.is_dir() {
+ return Ok(());
+ }
+ fs::create_dir_all(&self.location).map_err(|e| {
+ io::Error::new(
+ e.kind(),
+ format!(
+ "Could not create remote modules cache location: {:?}\nCheck the permission of the directory.",
+ self.location
+ ),
+ )
})
}
@@ -169,15 +184,15 @@ mod tests {
let dir = TempDir::new().unwrap();
let mut cache_path = dir.path().to_owned();
cache_path.push("foobar");
- let r = HttpCache::new(&cache_path);
- assert!(r.is_ok());
+ let cache = HttpCache::new(&cache_path);
+ assert!(cache.ensure_location().is_ok());
assert!(cache_path.is_dir());
}
#[test]
fn test_get_set() {
let dir = TempDir::new().unwrap();
- let cache = HttpCache::new(dir.path()).unwrap();
+ let cache = HttpCache::new(dir.path());
let url = Url::parse("https://deno.land/x/welcome.ts").unwrap();
let mut headers = HashMap::new();
headers.insert(