From 52a66c2796f97f5a08d679389172c39c0652cb16 Mon Sep 17 00:00:00 2001 From: Nayeem Rahman Date: Thu, 15 Aug 2019 18:58:04 +0100 Subject: [PATCH] Fix import map panics, use import map's location as its base URL (#2770) --- cli/import_map.rs | 34 ++++++++++++++++++++++++---------- cli/main.rs | 1 + cli/state.rs | 23 +++++++++++++++-------- 3 files changed, 40 insertions(+), 18 deletions(-) diff --git a/cli/import_map.rs b/cli/import_map.rs index 7bfe7476..d2916c19 100644 --- a/cli/import_map.rs +++ b/cli/import_map.rs @@ -1,3 +1,4 @@ +use deno::ErrBox; use deno::ModuleSpecifier; use indexmap::IndexMap; use serde_json::Map; @@ -6,6 +7,7 @@ use std::cmp::Ordering; use std::error::Error; use std::fmt; use std::fs; +use std::io; use url::Url; #[derive(Debug)] @@ -44,22 +46,28 @@ pub struct ImportMap { } impl ImportMap { - pub fn load(base_url: &str, file_name: &str) -> Result { - let cwd = std::env::current_dir().unwrap(); - let resolved_path = cwd.join(file_name); + pub fn load(file_path: &str) -> Result { + let file_url = ModuleSpecifier::resolve_url_or_path(file_path)?.to_string(); + let resolved_path = std::env::current_dir().unwrap().join(file_path); debug!( "Attempt to load import map: {}", resolved_path.to_str().unwrap() ); // Load the contents of import map - match fs::read_to_string(&resolved_path) { - Ok(json_string) => ImportMap::from_json(base_url, &json_string), - _ => panic!( - "Error retrieving import map file at \"{}\"", - resolved_path.to_str().unwrap() - ), - } + let json_string = fs::read_to_string(&resolved_path).map_err(|err| { + io::Error::new( + io::ErrorKind::InvalidInput, + format!( + "Error retrieving import map file at \"{}\": {}", + resolved_path.to_str().unwrap(), + err.to_string() + ) + .as_str(), + ) + })?; + // The URL of the import map is the base URL for its values. + ImportMap::from_json(&file_url, &json_string).map_err(ErrBox::from) } pub fn from_json( @@ -472,6 +480,12 @@ impl ImportMap { mod tests { use super::*; + #[test] + fn load_nonexistent() { + let file_path = "nonexistent_import_map.json"; + assert!(ImportMap::load(file_path).is_err()); + } + #[test] fn from_json_1() { let base_url = "https://deno.land"; diff --git a/cli/main.rs b/cli/main.rs index 020ace5a..2d3c70c9 100644 --- a/cli/main.rs +++ b/cli/main.rs @@ -115,6 +115,7 @@ fn create_worker_and_state( // TODO(kevinkassimo): maybe make include_deno_namespace also configurable? let state = ThreadSafeState::new(flags, argv, ops::op_selector_std, progress, true) + .map_err(print_err_and_exit) .unwrap(); let worker = Worker::new( "main".to_string(), diff --git a/cli/state.rs b/cli/state.rs index 0b0f3b1a..77738290 100644 --- a/cli/state.rs +++ b/cli/state.rs @@ -200,14 +200,7 @@ impl ThreadSafeState { let import_map: Option = match &flags.import_map_path { None => None, - Some(file_name) => { - let base_url = match &main_module { - Some(module_specifier) => module_specifier.clone(), - None => unreachable!(), - }; - let import_map = ImportMap::load(&base_url.to_string(), file_name)?; - Some(import_map) - } + Some(file_path) => Some(ImportMap::load(file_path)?), }; let mut seeded_rng = None; @@ -380,3 +373,17 @@ fn thread_safe() { String::from("hello.js"), ])); } + +#[test] +fn import_map_given_for_repl() { + let _result = ThreadSafeState::new( + flags::DenoFlags { + import_map_path: Some("import_map.json".to_string()), + ..flags::DenoFlags::default() + }, + vec![String::from("./deno")], + ops::op_selector_std, + Progress::new(), + true, + ); +} -- GitLab