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
Next Next commit
perf: cache cwd lookup in CliModuleLoader
  • Loading branch information
dsherret committed May 15, 2024
commit 80e78881425c990472f2bac4430f51bca34d9934
10 changes: 6 additions & 4 deletions cli/module_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ 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;
Expand Down Expand Up @@ -342,6 +343,7 @@ impl PreparedModuleLoader {
struct SharedCliModuleLoaderState {
lib_window: TsTypeLib,
lib_worker: TsTypeLib,
initial_cwd: PathBuf,
is_inspecting: bool,
is_repl: bool,
graph_container: Arc<ModuleGraphContainer>,
Expand Down Expand Up @@ -376,6 +378,7 @@ impl CliModuleLoaderFactory {
shared: Arc::new(SharedCliModuleLoaderState {
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 @@ -544,16 +547,15 @@ impl CliModuleLoader {
&self,
referrer: &str,
) -> Result<ModuleSpecifier, AnyError> {
// 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
// but sadly that's not the case due to missing APIs in V8.
let cwd = std::env::current_dir().context("Unable to get CWD")?;
deno_core::resolve_path("./$deno$repl.ts", &cwd).map_err(|e| e.into())
} else {
deno_core::resolve_url_or_path(referrer, &cwd).map_err(|e| e.into())
deno_core::resolve_url_or_path(referrer, &self.shared.initial_cwd)
.map_err(|e| e.into())
}
}

Expand Down