From 286ee1d8b68b54d39e698f3b78e3ce9e257fa674 Mon Sep 17 00:00:00 2001 From: "Kevin (Kun) \"Kassimo\" Qian" Date: Fri, 9 Aug 2019 16:33:59 -0700 Subject: Fix dynamic import base path problem for REPL and eval (#2757) --- core/module_specifier.rs | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) (limited to 'core/module_specifier.rs') diff --git a/core/module_specifier.rs b/core/module_specifier.rs index 3cfabd03f..9194b9082 100644 --- a/core/module_specifier.rs +++ b/core/module_specifier.rs @@ -46,6 +46,10 @@ impl fmt::Display for ModuleResolutionError { pub struct ModuleSpecifier(Url); impl ModuleSpecifier { + fn is_dummy_specifier(specifier: &str) -> bool { + specifier == "" + } + pub fn as_url(&self) -> &Url { &self.0 } @@ -80,7 +84,18 @@ impl ModuleSpecifier { // 3. Return the result of applying the URL parser to specifier with base // URL as the base URL. Err(ParseError::RelativeUrlWithoutBase) => { - let base = Url::parse(base).map_err(InvalidBaseUrl)?; + let base = if ModuleSpecifier::is_dummy_specifier(base) { + // Handle case, happening under e.g. repl. + // Use CWD for such case. + + // Forcefully join base to current dir. + // Otherwise, later joining in Url would be interpreted in + // the parent directory (appending trailing slash does not work) + let path = current_dir().unwrap().join(base); + Url::from_file_path(path).unwrap() + } else { + Url::parse(base).map_err(InvalidBaseUrl)? + }; base.join(&specifier).map_err(InvalidUrl)? } -- cgit v1.2.3