diff --git a/cli/state.rs b/cli/state.rs index 7a3541af77..ef63dad97f 100644 --- a/cli/state.rs +++ b/cli/state.rs @@ -46,10 +46,23 @@ impl CliModuleLoader { impl ModuleLoader for CliModuleLoader { fn resolve( &self, + op_state: Rc>, specifier: &str, referrer: &str, is_main: bool, ) -> Result { + let global_state = { + let state = op_state.borrow(); + state.borrow::>().clone() + }; + + // FIXME(bartlomieju): hacky way to provide compatibility with repl + let referrer = if referrer.is_empty() && global_state.flags.repl { + "" + } else { + referrer + }; + if !is_main { if let Some(import_map) = &self.import_map { let result = import_map.resolve(specifier, referrer)?; @@ -58,6 +71,7 @@ impl ModuleLoader for CliModuleLoader { } } } + let module_specifier = ModuleSpecifier::resolve_import(specifier, referrer)?; diff --git a/cli/tests/integration_tests.rs b/cli/tests/integration_tests.rs index 20e43ca298..2e2f91d0ae 100644 --- a/cli/tests/integration_tests.rs +++ b/cli/tests/integration_tests.rs @@ -1252,6 +1252,18 @@ fn repl_test_multiline() { assert!(err.is_empty()); } +#[test] +fn repl_test_import() { + let (out, _) = util::run_and_collect_output( + true, + "repl", + Some(vec!["import('./subdir/auto_print_hello.ts')"]), + Some(vec![("NO_COLOR".to_owned(), "1".to_owned())]), + false, + ); + assert!(out.contains("hello!\n")); +} + #[test] fn repl_test_eval_unterminated() { let (out, err) = util::run_and_collect_output( diff --git a/core/modules.rs b/core/modules.rs index 294bcfb051..235bfeb4e4 100644 --- a/core/modules.rs +++ b/core/modules.rs @@ -65,6 +65,7 @@ pub trait ModuleLoader { /// apply import map for child imports. fn resolve( &self, + op_state: Rc>, specifier: &str, referrer: &str, _is_main: bool, @@ -109,6 +110,7 @@ pub(crate) struct NoopModuleLoader; impl ModuleLoader for NoopModuleLoader { fn resolve( &self, + _op_state: Rc>, _specifier: &str, _referrer: &str, _is_main: bool, @@ -207,14 +209,23 @@ impl RecursiveModuleLoad { pub async fn prepare(self) -> (ModuleLoadId, Result) { let (module_specifier, maybe_referrer) = match self.state { LoadState::ResolveMain(ref specifier, _) => { - let spec = match self.loader.resolve(specifier, ".", true) { - Ok(spec) => spec, - Err(e) => return (self.id, Err(e)), - }; + let spec = + match self + .loader + .resolve(self.op_state.clone(), specifier, ".", true) + { + Ok(spec) => spec, + Err(e) => return (self.id, Err(e)), + }; (spec, None) } LoadState::ResolveImport(ref specifier, ref referrer) => { - let spec = match self.loader.resolve(specifier, referrer, false) { + let spec = match self.loader.resolve( + self.op_state.clone(), + specifier, + referrer, + false, + ) { Ok(spec) => spec, Err(e) => return (self.id, Err(e)), }; @@ -243,11 +254,13 @@ impl RecursiveModuleLoad { fn add_root(&mut self) -> Result<(), AnyError> { let module_specifier = match self.state { LoadState::ResolveMain(ref specifier, _) => { - self.loader.resolve(specifier, ".", true)? - } - LoadState::ResolveImport(ref specifier, ref referrer) => { - self.loader.resolve(specifier, referrer, false)? + self + .loader + .resolve(self.op_state.clone(), specifier, ".", true)? } + LoadState::ResolveImport(ref specifier, ref referrer) => self + .loader + .resolve(self.op_state.clone(), specifier, referrer, false)?, _ => unreachable!(), }; @@ -571,6 +584,7 @@ mod tests { impl ModuleLoader for MockLoader { fn resolve( &self, + _op_state: Rc>, specifier: &str, referrer: &str, _is_root: bool, diff --git a/core/runtime.rs b/core/runtime.rs index 64396c1f52..ecb9828f46 100644 --- a/core/runtime.rs +++ b/core/runtime.rs @@ -608,7 +608,7 @@ impl JsRuntimeState { let referrer = self.modules.get_name(referrer_id).unwrap(); let specifier = self .loader - .resolve(specifier, referrer, false) + .resolve(self.op_state.clone(), specifier, referrer, false) .expect("Module should have been already resolved"); self.modules.get_id(specifier.as_str()).unwrap_or(0) } @@ -813,8 +813,12 @@ impl JsRuntime { let import_specifier = module.get_module_request(i).to_rust_string_lossy(tc_scope); let state = state_rc.borrow(); - let module_specifier = - state.loader.resolve(&import_specifier, name, false)?; + let module_specifier = state.loader.resolve( + state.op_state.clone(), + &import_specifier, + name, + false, + )?; import_specifiers.push(module_specifier); } @@ -1917,6 +1921,7 @@ pub mod tests { impl ModuleLoader for ModsLoader { fn resolve( &self, + _op_state: Rc>, specifier: &str, referrer: &str, _is_main: bool, @@ -2029,6 +2034,7 @@ pub mod tests { impl ModuleLoader for DynImportErrLoader { fn resolve( &self, + _op_state: Rc>, specifier: &str, referrer: &str, _is_main: bool, @@ -2091,6 +2097,7 @@ pub mod tests { impl ModuleLoader for DynImportOkLoader { fn resolve( &self, + _op_state: Rc>, specifier: &str, referrer: &str, _is_main: bool, @@ -2219,6 +2226,7 @@ pub mod tests { impl ModuleLoader for ModsLoader { fn resolve( &self, + _op_state: Rc>, specifier: &str, referrer: &str, _is_main: bool,