-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
compat: support compat mode in REPL #12882
Changes from 6 commits
65307fc
53818d8
da7db0c
4680c0a
4f581d1
34d9ae5
086b42d
1c6fd04
8e48da6
a089952
cd33aa2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -167,6 +167,7 @@ pub struct Inner { | |
pub broadcast_channel: InMemoryBroadcastChannel, | ||
pub shared_array_buffer_store: SharedArrayBufferStore, | ||
pub compiled_wasm_module_store: CompiledWasmModuleStore, | ||
maybe_resolver: Option<Arc<dyn deno_graph::source::Resolver + Send + Sync>>, | ||
} | ||
|
||
impl Deref for ProcState { | ||
|
@@ -313,6 +314,34 @@ impl ProcState { | |
.clone() | ||
.or_else(|| env::var("DENO_UNSTABLE_COVERAGE_DIR").ok()); | ||
|
||
// FIXME(bartlomieju): `NodeEsmResolver` is not aware of JSX resolver | ||
// created below | ||
let node_resolver = NodeEsmResolver::new( | ||
maybe_import_map.clone().map(ImportMapResolver::new), | ||
); | ||
let maybe_import_map_resolver = | ||
maybe_import_map.clone().map(ImportMapResolver::new); | ||
let maybe_jsx_resolver = maybe_config_file | ||
.as_ref() | ||
.map(|cf| { | ||
cf.to_maybe_jsx_import_source_module() | ||
.map(|im| JsxResolver::new(im, maybe_import_map_resolver.clone())) | ||
}) | ||
.flatten(); | ||
let maybe_resolver: Option< | ||
Arc<dyn deno_graph::source::Resolver + Send + Sync>, | ||
> = if flags.compat { | ||
Some(Arc::new(node_resolver)) | ||
} else if let Some(jsx_resolver) = maybe_jsx_resolver { | ||
// the JSX resolver offloads to the import map if present, otherwise uses | ||
// the default Deno explicit import resolution. | ||
Some(Arc::new(jsx_resolver)) | ||
} else if let Some(import_map_resolver) = maybe_import_map_resolver { | ||
Some(Arc::new(import_map_resolver)) | ||
} else { | ||
None | ||
}; | ||
|
||
Ok(ProcState(Arc::new(Inner { | ||
dir, | ||
coverage_dir, | ||
|
@@ -328,6 +357,7 @@ impl ProcState { | |
broadcast_channel, | ||
shared_array_buffer_store, | ||
compiled_wasm_module_store, | ||
maybe_resolver, | ||
}))) | ||
} | ||
|
||
|
@@ -395,30 +425,12 @@ impl ProcState { | |
); | ||
let maybe_locker = as_maybe_locker(self.lockfile.clone()); | ||
let maybe_imports = self.get_maybe_imports()?; | ||
let node_resolver = NodeEsmResolver::new( | ||
self.maybe_import_map.clone().map(ImportMapResolver::new), | ||
); | ||
let maybe_import_map_resolver = | ||
self.maybe_import_map.clone().map(ImportMapResolver::new); | ||
let maybe_jsx_resolver = self | ||
.maybe_config_file | ||
.as_ref() | ||
.map(|cf| { | ||
cf.to_maybe_jsx_import_source_module() | ||
.map(|im| JsxResolver::new(im, maybe_import_map_resolver.clone())) | ||
}) | ||
.flatten(); | ||
let maybe_resolver = if self.flags.compat { | ||
Some(node_resolver.as_resolver()) | ||
} else if maybe_jsx_resolver.is_some() { | ||
// the JSX resolver offloads to the import map if present, otherwise uses | ||
// the default Deno explicit import resolution. | ||
maybe_jsx_resolver.as_ref().map(|jr| jr.as_resolver()) | ||
} else { | ||
maybe_import_map_resolver | ||
.as_ref() | ||
.map(|im| im.as_resolver()) | ||
}; | ||
let maybe_resolver: Option<&dyn deno_graph::source::Resolver> = | ||
if let Some(resolver) = &self.maybe_resolver { | ||
Some(resolver.as_ref()) | ||
} else { | ||
None | ||
}; | ||
let graph = create_graph( | ||
roots.clone(), | ||
is_dynamic, | ||
|
@@ -637,18 +649,26 @@ impl ProcState { | |
} | ||
} | ||
|
||
// FIXME(bartlomieju): hacky way to provide compatibility with repl | ||
// FIXME(bartlomieju): this is a hacky way to provide compatibility with REPL | ||
// and `Deno.core.evalContext` API. Ideally we should always have a referrer filled | ||
// but sadly that's not the case due to missing APIs in V8. | ||
let referrer = if referrer.is_empty() && self.flags.repl { | ||
deno_core::DUMMY_SPECIFIER | ||
deno_core::resolve_url_or_path("./$deno$repl.ts").unwrap() | ||
} else { | ||
referrer | ||
deno_core::resolve_url_or_path(referrer).unwrap() | ||
}; | ||
Comment on lines
+652
to
659
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure if I'm happy about this solution, but it's required because |
||
if let Some(import_map) = &self.maybe_import_map { | ||
import_map | ||
.resolve(specifier, referrer) | ||
.map_err(|err| err.into()) | ||
|
||
let maybe_resolver: Option<&dyn deno_graph::source::Resolver> = | ||
if let Some(resolver) = &self.maybe_resolver { | ||
Some(resolver.as_ref()) | ||
} else { | ||
None | ||
}; | ||
if let Some(resolver) = &maybe_resolver { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's not wholly clear to me why it needs to be converted to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure what you mean by converting - but we're using |
||
resolver.resolve(specifier, &referrer) | ||
} else { | ||
deno_core::resolve_import(specifier, referrer).map_err(|err| err.into()) | ||
deno_core::resolve_import(specifier, referrer.as_str()) | ||
.map_err(|err| err.into()) | ||
} | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
TypeError: Cannot load module "file:///[WILDCARD]/testdata/compat/foobar.js". | ||
TypeError: [ERR_MODULE_NOT_FOUND] Cannot find module '[WILDCARD]/testdata/compat/foobar.js' imported from file:///[WILDCARD]/testdata/compat/dyn_import_reject.js | ||
bartlomieju marked this conversation as resolved.
Show resolved
Hide resolved
|
||
ERR_MODULE_NOT_FOUND |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
require("pure-cjs"); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Longhand version of saying this?
(Or maybe
.as_deref()
. Likewise around line 661.)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried that before but it doesn't work:
I had to add additional trait boundaries (
Send + Sync
) to thedyn Resolver
and found that it's the only method to get it past type checking.