Skip to content

Commit

Permalink
perf: remove now needless canonicalization getting closest package.js…
Browse files Browse the repository at this point in the history
…on (#27437)

This is no longer required because we now store everything in the file
system for deno compile instead of in an eszip.
  • Loading branch information
dsherret authored Dec 20, 2024
1 parent ece718e commit 77e1af7
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 49 deletions.
17 changes: 0 additions & 17 deletions resolvers/node/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,6 @@ impl NodeJsErrorCoded for PackageJsonLoadError {
impl NodeJsErrorCoded for ClosestPkgJsonError {
fn code(&self) -> NodeJsErrorCode {
match self.as_kind() {
ClosestPkgJsonErrorKind::CanonicalizingDir(e) => e.code(),
ClosestPkgJsonErrorKind::Load(e) => e.code(),
}
}
Expand All @@ -331,26 +330,10 @@ pub struct ClosestPkgJsonError(pub Box<ClosestPkgJsonErrorKind>);

#[derive(Debug, Error)]
pub enum ClosestPkgJsonErrorKind {
#[error(transparent)]
CanonicalizingDir(#[from] CanonicalizingPkgJsonDirError),
#[error(transparent)]
Load(#[from] PackageJsonLoadError),
}

#[derive(Debug, Error)]
#[error("[{}] Failed canonicalizing package.json directory '{}'.", self.code(), dir_path.display())]
pub struct CanonicalizingPkgJsonDirError {
pub dir_path: PathBuf,
#[source]
pub source: std::io::Error,
}

impl NodeJsErrorCoded for CanonicalizingPkgJsonDirError {
fn code(&self) -> NodeJsErrorCode {
NodeJsErrorCode::ERR_MODULE_NOT_FOUND
}
}

// todo(https://github.com/denoland/deno_core/issues/810): make this a TypeError
#[derive(Debug, Error)]
#[error(
Expand Down
33 changes: 1 addition & 32 deletions resolvers/node/package_json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

use deno_package_json::PackageJson;
use deno_package_json::PackageJsonRc;
use deno_path_util::strip_unc_prefix;
use std::cell::RefCell;
use std::collections::HashMap;
use std::io::ErrorKind;
Expand All @@ -11,7 +10,6 @@ use std::path::PathBuf;
use url::Url;

use crate::env::NodeResolverEnv;
use crate::errors::CanonicalizingPkgJsonDirError;
use crate::errors::ClosestPkgJsonError;
use crate::errors::PackageJsonLoadError;

Expand Down Expand Up @@ -67,37 +65,8 @@ impl<TEnv: NodeResolverEnv> PackageJsonResolver<TEnv> {
&self,
file_path: &Path,
) -> Result<Option<PackageJsonRc>, ClosestPkgJsonError> {
// we use this for deno compile using byonm because the script paths
// won't be in virtual file system, but the package.json paths will be
fn canonicalize_first_ancestor_exists<TEnv: NodeResolverEnv>(
dir_path: &Path,
env: &TEnv,
) -> Result<Option<PathBuf>, std::io::Error> {
for ancestor in dir_path.ancestors() {
match env.realpath_sync(ancestor) {
Ok(dir_path) => return Ok(Some(dir_path)),
Err(err) if err.kind() == std::io::ErrorKind::NotFound => {
// keep searching
}
Err(err) => return Err(err),
}
}
Ok(None)
}

let parent_dir = file_path.parent().unwrap();
let Some(start_dir) = canonicalize_first_ancestor_exists(
parent_dir, &self.env,
)
.map_err(|source| CanonicalizingPkgJsonDirError {
dir_path: parent_dir.to_path_buf(),
source,
})?
else {
return Ok(None);
};
let start_dir = strip_unc_prefix(start_dir);
for current_dir in start_dir.ancestors() {
for current_dir in parent_dir.ancestors() {
let package_json_path = current_dir.join("package.json");
if let Some(pkg_json) = self.load_package_json(&package_json_path)? {
return Ok(Some(pkg_json));
Expand Down

0 comments on commit 77e1af7

Please sign in to comment.