mirror of
https://github.com/denoland/deno.git
synced 2025-01-18 03:44:05 -05:00
fix(cli/repl): use a default referrer when empty (#7794)
This makes use of a default referrer when its empty in repl mode so that dynamic imports work in the global evaluation context. Co-authored-by: Bartek Iwanczuk <biwanczuk@gmail.com>
This commit is contained in:
parent
454de99680
commit
6825d7f13d
4 changed files with 60 additions and 12 deletions
14
cli/state.rs
14
cli/state.rs
|
@ -46,10 +46,23 @@ impl CliModuleLoader {
|
||||||
impl ModuleLoader for CliModuleLoader {
|
impl ModuleLoader for CliModuleLoader {
|
||||||
fn resolve(
|
fn resolve(
|
||||||
&self,
|
&self,
|
||||||
|
op_state: Rc<RefCell<OpState>>,
|
||||||
specifier: &str,
|
specifier: &str,
|
||||||
referrer: &str,
|
referrer: &str,
|
||||||
is_main: bool,
|
is_main: bool,
|
||||||
) -> Result<ModuleSpecifier, AnyError> {
|
) -> Result<ModuleSpecifier, AnyError> {
|
||||||
|
let global_state = {
|
||||||
|
let state = op_state.borrow();
|
||||||
|
state.borrow::<Arc<GlobalState>>().clone()
|
||||||
|
};
|
||||||
|
|
||||||
|
// FIXME(bartlomieju): hacky way to provide compatibility with repl
|
||||||
|
let referrer = if referrer.is_empty() && global_state.flags.repl {
|
||||||
|
"<unknown>"
|
||||||
|
} else {
|
||||||
|
referrer
|
||||||
|
};
|
||||||
|
|
||||||
if !is_main {
|
if !is_main {
|
||||||
if let Some(import_map) = &self.import_map {
|
if let Some(import_map) = &self.import_map {
|
||||||
let result = import_map.resolve(specifier, referrer)?;
|
let result = import_map.resolve(specifier, referrer)?;
|
||||||
|
@ -58,6 +71,7 @@ impl ModuleLoader for CliModuleLoader {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let module_specifier =
|
let module_specifier =
|
||||||
ModuleSpecifier::resolve_import(specifier, referrer)?;
|
ModuleSpecifier::resolve_import(specifier, referrer)?;
|
||||||
|
|
||||||
|
|
|
@ -1252,6 +1252,18 @@ fn repl_test_multiline() {
|
||||||
assert!(err.is_empty());
|
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]
|
#[test]
|
||||||
fn repl_test_eval_unterminated() {
|
fn repl_test_eval_unterminated() {
|
||||||
let (out, err) = util::run_and_collect_output(
|
let (out, err) = util::run_and_collect_output(
|
||||||
|
|
|
@ -65,6 +65,7 @@ pub trait ModuleLoader {
|
||||||
/// apply import map for child imports.
|
/// apply import map for child imports.
|
||||||
fn resolve(
|
fn resolve(
|
||||||
&self,
|
&self,
|
||||||
|
op_state: Rc<RefCell<OpState>>,
|
||||||
specifier: &str,
|
specifier: &str,
|
||||||
referrer: &str,
|
referrer: &str,
|
||||||
_is_main: bool,
|
_is_main: bool,
|
||||||
|
@ -109,6 +110,7 @@ pub(crate) struct NoopModuleLoader;
|
||||||
impl ModuleLoader for NoopModuleLoader {
|
impl ModuleLoader for NoopModuleLoader {
|
||||||
fn resolve(
|
fn resolve(
|
||||||
&self,
|
&self,
|
||||||
|
_op_state: Rc<RefCell<OpState>>,
|
||||||
_specifier: &str,
|
_specifier: &str,
|
||||||
_referrer: &str,
|
_referrer: &str,
|
||||||
_is_main: bool,
|
_is_main: bool,
|
||||||
|
@ -207,14 +209,23 @@ impl RecursiveModuleLoad {
|
||||||
pub async fn prepare(self) -> (ModuleLoadId, Result<Self, AnyError>) {
|
pub async fn prepare(self) -> (ModuleLoadId, Result<Self, AnyError>) {
|
||||||
let (module_specifier, maybe_referrer) = match self.state {
|
let (module_specifier, maybe_referrer) = match self.state {
|
||||||
LoadState::ResolveMain(ref specifier, _) => {
|
LoadState::ResolveMain(ref specifier, _) => {
|
||||||
let spec = match self.loader.resolve(specifier, ".", true) {
|
let spec =
|
||||||
Ok(spec) => spec,
|
match self
|
||||||
Err(e) => return (self.id, Err(e)),
|
.loader
|
||||||
};
|
.resolve(self.op_state.clone(), specifier, ".", true)
|
||||||
|
{
|
||||||
|
Ok(spec) => spec,
|
||||||
|
Err(e) => return (self.id, Err(e)),
|
||||||
|
};
|
||||||
(spec, None)
|
(spec, None)
|
||||||
}
|
}
|
||||||
LoadState::ResolveImport(ref specifier, ref referrer) => {
|
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,
|
Ok(spec) => spec,
|
||||||
Err(e) => return (self.id, Err(e)),
|
Err(e) => return (self.id, Err(e)),
|
||||||
};
|
};
|
||||||
|
@ -243,11 +254,13 @@ impl RecursiveModuleLoad {
|
||||||
fn add_root(&mut self) -> Result<(), AnyError> {
|
fn add_root(&mut self) -> Result<(), AnyError> {
|
||||||
let module_specifier = match self.state {
|
let module_specifier = match self.state {
|
||||||
LoadState::ResolveMain(ref specifier, _) => {
|
LoadState::ResolveMain(ref specifier, _) => {
|
||||||
self.loader.resolve(specifier, ".", true)?
|
self
|
||||||
}
|
.loader
|
||||||
LoadState::ResolveImport(ref specifier, ref referrer) => {
|
.resolve(self.op_state.clone(), specifier, ".", true)?
|
||||||
self.loader.resolve(specifier, referrer, false)?
|
|
||||||
}
|
}
|
||||||
|
LoadState::ResolveImport(ref specifier, ref referrer) => self
|
||||||
|
.loader
|
||||||
|
.resolve(self.op_state.clone(), specifier, referrer, false)?,
|
||||||
|
|
||||||
_ => unreachable!(),
|
_ => unreachable!(),
|
||||||
};
|
};
|
||||||
|
@ -571,6 +584,7 @@ mod tests {
|
||||||
impl ModuleLoader for MockLoader {
|
impl ModuleLoader for MockLoader {
|
||||||
fn resolve(
|
fn resolve(
|
||||||
&self,
|
&self,
|
||||||
|
_op_state: Rc<RefCell<OpState>>,
|
||||||
specifier: &str,
|
specifier: &str,
|
||||||
referrer: &str,
|
referrer: &str,
|
||||||
_is_root: bool,
|
_is_root: bool,
|
||||||
|
|
|
@ -608,7 +608,7 @@ impl JsRuntimeState {
|
||||||
let referrer = self.modules.get_name(referrer_id).unwrap();
|
let referrer = self.modules.get_name(referrer_id).unwrap();
|
||||||
let specifier = self
|
let specifier = self
|
||||||
.loader
|
.loader
|
||||||
.resolve(specifier, referrer, false)
|
.resolve(self.op_state.clone(), specifier, referrer, false)
|
||||||
.expect("Module should have been already resolved");
|
.expect("Module should have been already resolved");
|
||||||
self.modules.get_id(specifier.as_str()).unwrap_or(0)
|
self.modules.get_id(specifier.as_str()).unwrap_or(0)
|
||||||
}
|
}
|
||||||
|
@ -813,8 +813,12 @@ impl JsRuntime {
|
||||||
let import_specifier =
|
let import_specifier =
|
||||||
module.get_module_request(i).to_rust_string_lossy(tc_scope);
|
module.get_module_request(i).to_rust_string_lossy(tc_scope);
|
||||||
let state = state_rc.borrow();
|
let state = state_rc.borrow();
|
||||||
let module_specifier =
|
let module_specifier = state.loader.resolve(
|
||||||
state.loader.resolve(&import_specifier, name, false)?;
|
state.op_state.clone(),
|
||||||
|
&import_specifier,
|
||||||
|
name,
|
||||||
|
false,
|
||||||
|
)?;
|
||||||
import_specifiers.push(module_specifier);
|
import_specifiers.push(module_specifier);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1917,6 +1921,7 @@ pub mod tests {
|
||||||
impl ModuleLoader for ModsLoader {
|
impl ModuleLoader for ModsLoader {
|
||||||
fn resolve(
|
fn resolve(
|
||||||
&self,
|
&self,
|
||||||
|
_op_state: Rc<RefCell<OpState>>,
|
||||||
specifier: &str,
|
specifier: &str,
|
||||||
referrer: &str,
|
referrer: &str,
|
||||||
_is_main: bool,
|
_is_main: bool,
|
||||||
|
@ -2029,6 +2034,7 @@ pub mod tests {
|
||||||
impl ModuleLoader for DynImportErrLoader {
|
impl ModuleLoader for DynImportErrLoader {
|
||||||
fn resolve(
|
fn resolve(
|
||||||
&self,
|
&self,
|
||||||
|
_op_state: Rc<RefCell<OpState>>,
|
||||||
specifier: &str,
|
specifier: &str,
|
||||||
referrer: &str,
|
referrer: &str,
|
||||||
_is_main: bool,
|
_is_main: bool,
|
||||||
|
@ -2091,6 +2097,7 @@ pub mod tests {
|
||||||
impl ModuleLoader for DynImportOkLoader {
|
impl ModuleLoader for DynImportOkLoader {
|
||||||
fn resolve(
|
fn resolve(
|
||||||
&self,
|
&self,
|
||||||
|
_op_state: Rc<RefCell<OpState>>,
|
||||||
specifier: &str,
|
specifier: &str,
|
||||||
referrer: &str,
|
referrer: &str,
|
||||||
_is_main: bool,
|
_is_main: bool,
|
||||||
|
@ -2219,6 +2226,7 @@ pub mod tests {
|
||||||
impl ModuleLoader for ModsLoader {
|
impl ModuleLoader for ModsLoader {
|
||||||
fn resolve(
|
fn resolve(
|
||||||
&self,
|
&self,
|
||||||
|
_op_state: Rc<RefCell<OpState>>,
|
||||||
specifier: &str,
|
specifier: &str,
|
||||||
referrer: &str,
|
referrer: &str,
|
||||||
_is_main: bool,
|
_is_main: bool,
|
||||||
|
|
Loading…
Add table
Reference in a new issue