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

perf: resolver - skip cwd lookup if able #23851

Merged
merged 7 commits into from
May 21, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Merge branch 'main' into perf_skip_cwd_check_if_able
  • Loading branch information
dsherret committed May 16, 2024
commit 88b4b61cb7b5fa9d3bf069f40da3d039c03c5ce5
50 changes: 7 additions & 43 deletions cli/module_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,6 @@ use deno_runtime::deno_node::NodeResolutionMode;
use deno_runtime::fs_util::code_timestamp;
use deno_runtime::permissions::PermissionsContainer;
use deno_semver::npm::NpmPackageReqReference;
use deno_terminal::colors;
use std::borrow::Cow;
use std::path::PathBuf;
use std::pin::Pin;
use std::rc::Rc;
use std::str;
use std::sync::Arc;

pub async fn load_top_level_deps(factory: &CliFactory) -> Result<(), AnyError> {
let npm_resolver = factory.npm_resolver().await?;
Expand Down Expand Up @@ -221,7 +214,6 @@ struct SharedCliModuleLoaderState {
graph_kind: GraphKind,
lib_window: TsTypeLib,
lib_worker: TsTypeLib,
initial_cwd: PathBuf,
is_inspecting: bool,
is_repl: bool,
code_cache: Option<Arc<CodeCache>>,
Expand Down Expand Up @@ -258,7 +250,6 @@ impl CliModuleLoaderFactory {
graph_kind: options.graph_kind(),
lib_window: options.ts_type_lib_window(),
lib_worker: options.ts_type_lib_worker(),
initial_cwd: options.initial_cwd().to_path_buf(),
is_inspecting: options.is_inspecting(),
is_repl: matches!(
options.sub_command(),
Expand Down Expand Up @@ -431,43 +422,16 @@ impl<TGraphContainer: ModuleGraphContainer> CliModuleLoader<TGraphContainer> {
&self,
referrer: &str,
) -> Result<ModuleSpecifier, AnyError> {
// todo(https://github.com/denoland/deno_core/pull/741): use function from deno_core
fn specifier_has_uri_scheme(specifier: &str) -> bool {
let mut chars = specifier.chars();
let mut len = 0usize;
// The first character must be a letter.
match chars.next() {
Some(c) if c.is_ascii_alphabetic() => len += 1,
_ => return false,
}
// Second and following characters must be either a letter, number,
// plus sign, minus sign, or dot.
loop {
match chars.next() {
Some(c) if c.is_ascii_alphanumeric() || "+-.".contains(c) => len += 1,
Some(':') if len >= 2 => return true,
_ => return false,
}
}
}

let referrer = if referrer.is_empty() && self.shared.is_repl {
// TODO(bartlomieju): ideally we shouldn't need to call `current_dir()` on each
// call - maybe it should be caller's responsibility to pass it as an arg?
let cwd = std::env::current_dir().context("Unable to get CWD")?;
if referrer.is_empty() && self.shared.is_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
"./$deno$repl.ts"
} else {
referrer
};

if referrer == "." {
// main module, use the initial cwd
deno_core::resolve_url_or_path(referrer, &self.shared.initial_cwd)
.map_err(|e| e.into())
} else if specifier_has_uri_scheme(referrer) {
deno_core::resolve_url(referrer).map_err(|e| e.into())
// but sadly that's not the case due to missing APIs in V8.
deno_core::resolve_path("./$deno$repl.ts", &cwd).map_err(|e| e.into())
} else {
let cwd = std::env::current_dir().context("Unable to get CWD")?;
deno_core::resolve_path(referrer, &cwd).map_err(|e| e.into())
deno_core::resolve_url_or_path(referrer, &cwd).map_err(|e| e.into())
}
}

Expand Down
You are viewing a condensed version of this merge commit. You can view the full changes here.