0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-02-01 20:25:12 -05:00

fix: panic if $DENO_DIR is a relative path (#5375)

This commit fixes panic occurring if $DENO_DIR is set to a relative
path, eg. "DENO_DIR=denodir deno run main.ts".

Before creating DenoDir instance given path is checked and if necessary
resolved against current working directory.

Additional sanity checks were put in place to ensure all caches
receive absolute path for the location.
This commit is contained in:
Bartek Iwańczuk 2020-05-15 16:32:52 +02:00 committed by GitHub
parent d4afc9890d
commit 89fe81168e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 3 deletions

View file

@ -13,7 +13,7 @@ pub struct DenoDir {
} }
impl DenoDir { impl DenoDir {
pub fn new(custom_root: Option<PathBuf>) -> std::io::Result<Self> { pub fn new(maybe_custom_root: Option<PathBuf>) -> std::io::Result<Self> {
// Only setup once. // Only setup once.
let home_dir = dirs::home_dir().expect("Could not get home directory."); let home_dir = dirs::home_dir().expect("Could not get home directory.");
let fallback = home_dir.join(".deno"); let fallback = home_dir.join(".deno");
@ -24,7 +24,16 @@ impl DenoDir {
.map(|d| d.join("deno")) .map(|d| d.join("deno"))
.unwrap_or(fallback); .unwrap_or(fallback);
let root: PathBuf = custom_root.unwrap_or(default); let root: PathBuf = if let Some(root) = maybe_custom_root {
if root.is_absolute() {
root
} else {
std::env::current_dir()?.join(root)
}
} else {
default
};
assert!(root.is_absolute());
let gen_path = root.join("gen"); let gen_path = root.join("gen");
let deno_dir = Self { let deno_dir = Self {

View file

@ -22,7 +22,9 @@ fn with_io_context<T: AsRef<str>>(
} }
impl DiskCache { impl DiskCache {
/// `location` must be an absolute path.
pub fn new(location: &Path) -> Self { pub fn new(location: &Path) -> Self {
assert!(location.is_absolute());
Self { Self {
location: location.to_owned(), location: location.to_owned(),
} }
@ -211,7 +213,12 @@ mod tests {
#[test] #[test]
fn test_get_cache_filename_with_extension() { fn test_get_cache_filename_with_extension() {
let cache = DiskCache::new(&PathBuf::from("foo")); let p = if cfg!(target_os = "windows") {
"C:\\foo"
} else {
"/foo"
};
let cache = DiskCache::new(&PathBuf::from(p));
let mut test_cases = vec![ let mut test_cases = vec![
( (

View file

@ -103,7 +103,10 @@ impl Metadata {
impl HttpCache { impl HttpCache {
/// Returns a new instance. /// Returns a new instance.
///
/// `location` must be an absolute path.
pub fn new(location: &Path) -> Self { pub fn new(location: &Path) -> Self {
assert!(location.is_absolute());
Self { Self {
location: location.to_owned(), location: location.to_owned(),
} }