Skip to content
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

Merged
merged 11 commits into from
Nov 24, 2021
Merged

Conversation

bartlomieju
Copy link
Member

@bartlomieju bartlomieju commented Nov 24, 2021

This commit introduces "ProcState::maybe_resolver" field, which
stores a single instance of resolver for the whole lifetime of the
process, instead of creating these resolvers for each creation
of module graph. As a result, this resolver can be used in fallback
case where graph is not constructed (REPL, loading modules using
"require") unifying resolution logic.

Closes #12628
Closes #12872

@bartlomieju
Copy link
Member Author

Ref #12684 as it's somewhat needed to address this problem

@bartlomieju bartlomieju changed the title compat: support compat mode in REPL [WIP] compat: support compat mode in REPL Nov 24, 2021
@bartlomieju bartlomieju changed the title [WIP] compat: support compat mode in REPL compat: support compat mode in REPL Nov 24, 2021
Comment on lines +652 to 659
// 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()
};
Copy link
Member Author

Choose a reason for hiding this comment

The 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 deno_graph::source::Resolver requires &Url type for referrer arg

@bartlomieju
Copy link
Member Author

Ref #12684 as it's somewhat needed to address this problem

I kicked can down the road - this still needs to be addressed, but with this PR I think it's a bit more obvious how to tackle this "aggregate resolver" situation.

Comment on lines +428 to +433
let maybe_resolver: Option<&dyn deno_graph::source::Resolver> =
if let Some(resolver) = &self.maybe_resolver {
Some(resolver.as_ref())
} else {
None
};
Copy link
Contributor

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?

Suggested change
let maybe_resolver: Option<&dyn deno_graph::source::Resolver> =
if let Some(resolver) = &self.maybe_resolver {
Some(resolver.as_ref())
} else {
None
};
let maybe_resolver: Option<&dyn deno_graph::source::Resolver> =
self.maybe_resolver.as_ref();

(Or maybe .as_deref(). Likewise around line 661.)

Copy link
Member Author

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:

error[E0308]: mismatched types
   --> cli/proc_state.rs:429:7
    |
428 |     let maybe_resolver: Option<&dyn deno_graph::source::Resolver> =
    |                         ----------------------------------------- expected due to this
429 |       self.maybe_resolver.as_ref();
    |       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected trait object `dyn Resolver`, found struct `Arc`
    |
    = note: expected enum `std::option::Option<&dyn Resolver>`
               found enum `std::option::Option<&Arc<dyn Resolver + std::marker::Send + std::marker::Sync>>`

I had to add additional trait boundaries (Send + Sync) to the dyn Resolver and found that it's the only method to get it past type checking.

} else {
None
};
if let Some(resolver) = &maybe_resolver {
Copy link
Contributor

Choose a reason for hiding this comment

The 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 &dyn Resolver first.

Copy link
Member Author

Choose a reason for hiding this comment

The 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 dyn Resolver to have a single interface from deno_graph; that can be used in prepare_module_load method and resolve methods on the ProcState.

Copy link
Member

@dsherret dsherret left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@bartlomieju bartlomieju merged commit 1117d2d into denoland:main Nov 24, 2021
@bartlomieju bartlomieju deleted the compat_repl branch November 24, 2021 15:55
piscisaureus pushed a commit that referenced this pull request Nov 24, 2021
This commit introduces "ProcState::maybe_resolver" field, which
stores a single instance of resolver for the whole lifetime of the
process, instead of creating these resolvers for each creation
of module graph. As a result, this resolver can be used in fallback
case where graph is not constructed (REPL, loading modules using
"require") unifying resolution logic.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

compat: Dynamic import from CJS module fails REPL should support compat mode
3 participants