1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-01-21 04:52:26 -05:00

refactor: remove CliNpmReqResolver trait in deno_resolver (#27616)

This commit is contained in:
David Sherret 2025-01-10 14:48:43 -05:00 committed by GitHub
parent 1dd5bd667c
commit c27248a8f3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
15 changed files with 324 additions and 216 deletions

1
Cargo.lock generated
View file

@ -2207,6 +2207,7 @@ dependencies = [
"deno_package_json", "deno_package_json",
"deno_path_util", "deno_path_util",
"deno_semver", "deno_semver",
"log",
"node_resolver", "node_resolver",
"parking_lot", "parking_lot",
"sys_traits", "sys_traits",

View file

@ -720,11 +720,10 @@ impl CliFactory {
.get_or_try_init_async(async { .get_or_try_init_async(async {
let npm_resolver = self.npm_resolver().await?; let npm_resolver = self.npm_resolver().await?;
Ok(Arc::new(CliNpmReqResolver::new(NpmReqResolverOptions { Ok(Arc::new(CliNpmReqResolver::new(NpmReqResolverOptions {
byonm_resolver: (npm_resolver.clone()).into_maybe_byonm(),
sys: self.sys(), sys: self.sys(),
in_npm_pkg_checker: self.in_npm_pkg_checker()?.clone(), in_npm_pkg_checker: self.in_npm_pkg_checker()?.clone(),
node_resolver: self.node_resolver().await?.clone(), node_resolver: self.node_resolver().await?.clone(),
npm_req_resolver: npm_resolver.clone().into_npm_req_resolver(), npm_resolver: npm_resolver.clone().into_byonm_or_managed(),
}))) })))
}) })
.await .await

View file

@ -796,10 +796,9 @@ impl<'a> ResolverFactory<'a> {
let node_resolver = self.node_resolver()?; let node_resolver = self.node_resolver()?;
let npm_resolver = self.npm_resolver()?; let npm_resolver = self.npm_resolver()?;
Some(Arc::new(CliNpmReqResolver::new(NpmReqResolverOptions { Some(Arc::new(CliNpmReqResolver::new(NpmReqResolverOptions {
byonm_resolver: (npm_resolver.clone()).into_maybe_byonm(),
in_npm_pkg_checker: self.in_npm_pkg_checker().clone(), in_npm_pkg_checker: self.in_npm_pkg_checker().clone(),
node_resolver: node_resolver.clone(), node_resolver: node_resolver.clone(),
npm_req_resolver: npm_resolver.clone().into_npm_req_resolver(), npm_resolver: npm_resolver.clone().into_byonm_or_managed(),
sys: self.sys.clone(), sys: self.sys.clone(),
}))) })))
}) })

View file

@ -1,13 +1,17 @@
// Copyright 2018-2025 the Deno authors. MIT license. // Copyright 2018-2025 the Deno authors. MIT license.
use std::path::Path; use std::path::Path;
use std::path::PathBuf;
use std::sync::Arc; use std::sync::Arc;
use deno_core::serde_json; use deno_core::serde_json;
use deno_core::url::Url;
use deno_resolver::npm::ByonmNpmResolver; use deno_resolver::npm::ByonmNpmResolver;
use deno_resolver::npm::ByonmNpmResolverCreateOptions; use deno_resolver::npm::ByonmNpmResolverCreateOptions;
use deno_resolver::npm::CliNpmReqResolver; use deno_resolver::npm::ByonmOrManagedNpmResolver;
use deno_resolver::npm::ResolvePkgFolderFromDenoReqError;
use deno_runtime::ops::process::NpmProcessStateProvider; use deno_runtime::ops::process::NpmProcessStateProvider;
use deno_semver::package::PackageReq;
use node_resolver::NpmPackageFolderResolver; use node_resolver::NpmPackageFolderResolver;
use super::CliNpmResolver; use super::CliNpmResolver;
@ -44,18 +48,16 @@ impl CliNpmResolver for CliByonmNpmResolver {
self self
} }
fn into_npm_req_resolver(self: Arc<Self>) -> Arc<dyn CliNpmReqResolver> {
self
}
fn into_process_state_provider( fn into_process_state_provider(
self: Arc<Self>, self: Arc<Self>,
) -> Arc<dyn NpmProcessStateProvider> { ) -> Arc<dyn NpmProcessStateProvider> {
Arc::new(CliByonmWrapper(self)) Arc::new(CliByonmWrapper(self))
} }
fn into_maybe_byonm(self: Arc<Self>) -> Option<Arc<CliByonmNpmResolver>> { fn into_byonm_or_managed(
Some(self) self: Arc<Self>,
) -> ByonmOrManagedNpmResolver<CliSys> {
ByonmOrManagedNpmResolver::Byonm(self)
} }
fn clone_snapshotted(&self) -> Arc<dyn CliNpmResolver> { fn clone_snapshotted(&self) -> Arc<dyn CliNpmResolver> {
@ -75,4 +77,14 @@ impl CliNpmResolver for CliByonmNpmResolver {
// so we just return None to signify check caching is not supported // so we just return None to signify check caching is not supported
None None
} }
fn resolve_pkg_folder_from_deno_module_req(
&self,
req: &PackageReq,
referrer: &Url,
) -> Result<PathBuf, ResolvePkgFolderFromDenoReqError> {
self
.resolve_pkg_folder_from_deno_module_req(req, referrer)
.map_err(ResolvePkgFolderFromDenoReqError::Byonm)
}
} }

View file

@ -22,12 +22,11 @@ use deno_npm::NpmPackageId;
use deno_npm::NpmResolutionPackage; use deno_npm::NpmResolutionPackage;
use deno_npm::NpmSystemInfo; use deno_npm::NpmSystemInfo;
use deno_npm_cache::NpmCacheSetting; use deno_npm_cache::NpmCacheSetting;
use deno_path_util::fs::canonicalize_path_maybe_not_exists;
use deno_resolver::npm::managed::create_npm_fs_resolver;
use deno_resolver::npm::managed::NpmPackageFsResolver;
use deno_resolver::npm::managed::NpmPackageFsResolverPackageFolderError;
use deno_resolver::npm::managed::NpmResolution; use deno_resolver::npm::managed::NpmResolution;
use deno_resolver::npm::CliNpmReqResolver; use deno_resolver::npm::managed::ResolvePkgFolderFromPkgIdError;
use deno_resolver::npm::ByonmOrManagedNpmResolver;
use deno_resolver::npm::ManagedNpmResolver;
use deno_resolver::npm::ResolvePkgFolderFromDenoReqError;
use deno_runtime::colors; use deno_runtime::colors;
use deno_runtime::ops::process::NpmProcessStateProvider; use deno_runtime::ops::process::NpmProcessStateProvider;
use deno_semver::package::PackageNv; use deno_semver::package::PackageNv;
@ -36,8 +35,6 @@ use installer::AddPkgReqsResult;
use installer::NpmResolutionInstaller; use installer::NpmResolutionInstaller;
use installers::create_npm_fs_installer; use installers::create_npm_fs_installer;
use installers::NpmPackageFsInstaller; use installers::NpmPackageFsInstaller;
use node_resolver::errors::PackageFolderResolveError;
use node_resolver::errors::PackageFolderResolveIoError;
use node_resolver::NpmPackageFolderResolver; use node_resolver::NpmPackageFolderResolver;
use super::CliNpmCache; use super::CliNpmCache;
@ -46,7 +43,6 @@ use super::CliNpmRegistryInfoProvider;
use super::CliNpmResolver; use super::CliNpmResolver;
use super::CliNpmTarballCache; use super::CliNpmTarballCache;
use super::InnerCliNpmResolverRef; use super::InnerCliNpmResolverRef;
use super::ResolvePkgFolderFromDenoReqError;
use crate::args::CliLockfile; use crate::args::CliLockfile;
use crate::args::LifecycleScriptsConfig; use crate::args::LifecycleScriptsConfig;
use crate::args::NpmInstallDepsProvider; use crate::args::NpmInstallDepsProvider;
@ -181,22 +177,23 @@ fn create_inner(
npm_system_info.clone(), npm_system_info.clone(),
lifecycle_scripts.clone(), lifecycle_scripts.clone(),
); );
let fs_resolver = create_npm_fs_resolver( let managed_npm_resolver =
Arc::new(ManagedNpmResolver::<CliSys>::new::<CliSys>(
&npm_cache_dir, &npm_cache_dir,
&npm_rc, &npm_rc,
resolution.clone(), resolution.clone(),
sys.clone(), sys.clone(),
node_modules_dir_path, node_modules_dir_path,
); ));
Arc::new(ManagedCliNpmResolver::new( Arc::new(ManagedCliNpmResolver::new(
fs_installer, fs_installer,
fs_resolver,
maybe_lockfile, maybe_lockfile,
registry_info_provider, managed_npm_resolver,
npm_cache, npm_cache,
npm_cache_dir, npm_cache_dir,
npm_install_deps_provider, npm_install_deps_provider,
npm_rc, npm_rc,
registry_info_provider,
resolution, resolution,
sys, sys,
tarball_cache, tarball_cache,
@ -281,13 +278,23 @@ pub enum PackageCaching<'a> {
All, All,
} }
#[derive(Debug, thiserror::Error, deno_error::JsError)]
pub enum ResolvePkgFolderFromDenoModuleError {
#[class(inherit)]
#[error(transparent)]
PackageNvNotFound(#[from] deno_npm::resolution::PackageNvNotFoundError),
#[class(inherit)]
#[error(transparent)]
ResolvePkgFolderFromPkgId(#[from] ResolvePkgFolderFromPkgIdError),
}
/// An npm resolver where the resolution is managed by Deno rather than /// An npm resolver where the resolution is managed by Deno rather than
/// the user bringing their own node_modules (BYONM) on the file system. /// the user bringing their own node_modules (BYONM) on the file system.
pub struct ManagedCliNpmResolver { pub struct ManagedCliNpmResolver {
fs_installer: Arc<dyn NpmPackageFsInstaller>, fs_installer: Arc<dyn NpmPackageFsInstaller>,
fs_resolver: Arc<dyn NpmPackageFsResolver>,
maybe_lockfile: Option<Arc<CliLockfile>>, maybe_lockfile: Option<Arc<CliLockfile>>,
registry_info_provider: Arc<CliNpmRegistryInfoProvider>, registry_info_provider: Arc<CliNpmRegistryInfoProvider>,
managed_npm_resolver: Arc<ManagedNpmResolver<CliSys>>,
npm_cache: Arc<CliNpmCache>, npm_cache: Arc<CliNpmCache>,
npm_cache_dir: Arc<NpmCacheDir>, npm_cache_dir: Arc<NpmCacheDir>,
npm_install_deps_provider: Arc<NpmInstallDepsProvider>, npm_install_deps_provider: Arc<NpmInstallDepsProvider>,
@ -304,45 +311,23 @@ pub struct ManagedCliNpmResolver {
impl std::fmt::Debug for ManagedCliNpmResolver { impl std::fmt::Debug for ManagedCliNpmResolver {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("ManagedNpmResolver") f.debug_struct("ManagedCliNpmResolver")
.field("<omitted>", &"<omitted>") .field("<omitted>", &"<omitted>")
.finish() .finish()
} }
} }
#[derive(Debug, thiserror::Error, deno_error::JsError)]
pub enum ResolvePkgFolderFromPkgIdError {
#[class(inherit)]
#[error("{0}")]
NpmPackageFsResolverPackageFolder(
#[from] NpmPackageFsResolverPackageFolderError,
),
#[class(inherit)]
#[error("{0}")]
Io(#[from] std::io::Error),
}
#[derive(Debug, thiserror::Error, deno_error::JsError)]
pub enum ResolvePkgFolderFromDenoModuleError {
#[class(inherit)]
#[error("{0}")]
PackageNvNotFound(#[from] deno_npm::resolution::PackageNvNotFoundError),
#[class(inherit)]
#[error("{0}")]
ResolvePkgFolderFromPkgId(#[from] ResolvePkgFolderFromPkgIdError),
}
impl ManagedCliNpmResolver { impl ManagedCliNpmResolver {
#[allow(clippy::too_many_arguments)] #[allow(clippy::too_many_arguments)]
pub fn new( pub fn new(
fs_installer: Arc<dyn NpmPackageFsInstaller>, fs_installer: Arc<dyn NpmPackageFsInstaller>,
fs_resolver: Arc<dyn NpmPackageFsResolver>,
maybe_lockfile: Option<Arc<CliLockfile>>, maybe_lockfile: Option<Arc<CliLockfile>>,
registry_info_provider: Arc<CliNpmRegistryInfoProvider>, managed_npm_resolver: Arc<ManagedNpmResolver<CliSys>>,
npm_cache: Arc<CliNpmCache>, npm_cache: Arc<CliNpmCache>,
npm_cache_dir: Arc<NpmCacheDir>, npm_cache_dir: Arc<NpmCacheDir>,
npm_install_deps_provider: Arc<NpmInstallDepsProvider>, npm_install_deps_provider: Arc<NpmInstallDepsProvider>,
npm_rc: Arc<ResolvedNpmRc>, npm_rc: Arc<ResolvedNpmRc>,
registry_info_provider: Arc<CliNpmRegistryInfoProvider>,
resolution: Arc<NpmResolution>, resolution: Arc<NpmResolution>,
sys: CliSys, sys: CliSys,
tarball_cache: Arc<CliNpmTarballCache>, tarball_cache: Arc<CliNpmTarballCache>,
@ -357,13 +342,13 @@ impl ManagedCliNpmResolver {
); );
Self { Self {
fs_installer, fs_installer,
fs_resolver,
maybe_lockfile, maybe_lockfile,
registry_info_provider, managed_npm_resolver,
npm_cache, npm_cache,
npm_cache_dir, npm_cache_dir,
npm_install_deps_provider, npm_install_deps_provider,
npm_rc, npm_rc,
registry_info_provider,
text_only_progress_bar, text_only_progress_bar,
resolution, resolution,
resolution_installer, resolution_installer,
@ -379,14 +364,9 @@ impl ManagedCliNpmResolver {
&self, &self,
pkg_id: &NpmPackageId, pkg_id: &NpmPackageId,
) -> Result<PathBuf, ResolvePkgFolderFromPkgIdError> { ) -> Result<PathBuf, ResolvePkgFolderFromPkgIdError> {
let path = self.fs_resolver.package_folder(pkg_id)?; self
let path = canonicalize_path_maybe_not_exists(&self.sys, &path)?; .managed_npm_resolver
log::debug!( .resolve_pkg_folder_from_pkg_id(pkg_id)
"Resolved package folder of {} to {}",
pkg_id.as_serialized(),
path.display()
);
Ok(path)
} }
/// Resolves the package id from the provided specifier. /// Resolves the package id from the provided specifier.
@ -395,7 +375,7 @@ impl ManagedCliNpmResolver {
specifier: &ModuleSpecifier, specifier: &ModuleSpecifier,
) -> Result<Option<NpmPackageId>, AnyError> { ) -> Result<Option<NpmPackageId>, AnyError> {
let Some(cache_folder_id) = self let Some(cache_folder_id) = self
.fs_resolver .managed_npm_resolver
.resolve_package_cache_folder_id_from_specifier(specifier)? .resolve_package_cache_folder_id_from_specifier(specifier)?
else { else {
return Ok(None); return Ok(None);
@ -419,7 +399,9 @@ impl ManagedCliNpmResolver {
&self, &self,
package_id: &NpmPackageId, package_id: &NpmPackageId,
) -> Result<u64, AnyError> { ) -> Result<u64, AnyError> {
let package_folder = self.fs_resolver.package_folder(package_id)?; let package_folder = self
.managed_npm_resolver
.resolve_pkg_folder_from_pkg_id(package_id)?;
Ok(crate::util::fs::dir_size(&package_folder)?) Ok(crate::util::fs::dir_size(&package_folder)?)
} }
@ -435,7 +417,12 @@ impl ManagedCliNpmResolver {
self self
.resolve_pkg_id_from_pkg_req(req) .resolve_pkg_id_from_pkg_req(req)
.ok() .ok()
.and_then(|id| self.fs_resolver.package_folder(&id).ok()) .and_then(|id| {
self
.managed_npm_resolver
.resolve_pkg_folder_from_pkg_id(&id)
.ok()
})
.map(|folder| folder.exists()) .map(|folder| folder.exists())
.unwrap_or(false) .unwrap_or(false)
} }
@ -648,7 +635,7 @@ impl ManagedCliNpmResolver {
} }
pub fn maybe_node_modules_path(&self) -> Option<&Path> { pub fn maybe_node_modules_path(&self) -> Option<&Path> {
self.fs_resolver.node_modules_path() self.managed_npm_resolver.node_modules_path()
} }
pub fn global_cache_root_path(&self) -> &Path { pub fn global_cache_root_path(&self) -> &Path {
@ -672,61 +659,20 @@ fn npm_process_state(
.unwrap() .unwrap()
} }
impl NpmPackageFolderResolver for ManagedCliNpmResolver {
fn resolve_package_folder_from_package(
&self,
name: &str,
referrer: &ModuleSpecifier,
) -> Result<PathBuf, PackageFolderResolveError> {
let path = self
.fs_resolver
.resolve_package_folder_from_package(name, referrer)?;
let path =
canonicalize_path_maybe_not_exists(&self.sys, &path).map_err(|err| {
PackageFolderResolveIoError {
package_name: name.to_string(),
referrer: referrer.clone(),
source: err,
}
})?;
log::debug!("Resolved {} from {} to {}", name, referrer, path.display());
Ok(path)
}
}
impl NpmProcessStateProvider for ManagedCliNpmResolver { impl NpmProcessStateProvider for ManagedCliNpmResolver {
fn get_npm_process_state(&self) -> String { fn get_npm_process_state(&self) -> String {
npm_process_state( npm_process_state(
self.resolution.serialized_valid_snapshot(), self.resolution.serialized_valid_snapshot(),
self.fs_resolver.node_modules_path(), self.managed_npm_resolver.node_modules_path(),
) )
} }
} }
impl CliNpmReqResolver for ManagedCliNpmResolver {
fn resolve_pkg_folder_from_deno_module_req(
&self,
req: &PackageReq,
_referrer: &ModuleSpecifier,
) -> Result<PathBuf, ResolvePkgFolderFromDenoReqError> {
let pkg_id = self.resolve_pkg_id_from_pkg_req(req).map_err(|err| {
ResolvePkgFolderFromDenoReqError::Managed(Box::new(err))
})?;
self
.resolve_pkg_folder_from_pkg_id(&pkg_id)
.map_err(|err| ResolvePkgFolderFromDenoReqError::Managed(Box::new(err)))
}
}
impl CliNpmResolver for ManagedCliNpmResolver { impl CliNpmResolver for ManagedCliNpmResolver {
fn into_npm_pkg_folder_resolver( fn into_npm_pkg_folder_resolver(
self: Arc<Self>, self: Arc<Self>,
) -> Arc<dyn NpmPackageFolderResolver> { ) -> Arc<dyn NpmPackageFolderResolver> {
self self.managed_npm_resolver.clone()
}
fn into_npm_req_resolver(self: Arc<Self>) -> Arc<dyn CliNpmReqResolver> {
self
} }
fn into_process_state_provider( fn into_process_state_provider(
@ -735,6 +681,12 @@ impl CliNpmResolver for ManagedCliNpmResolver {
self self
} }
fn into_byonm_or_managed(
self: Arc<Self>,
) -> ByonmOrManagedNpmResolver<CliSys> {
ByonmOrManagedNpmResolver::Managed(self.managed_npm_resolver.clone())
}
fn clone_snapshotted(&self) -> Arc<dyn CliNpmResolver> { fn clone_snapshotted(&self) -> Arc<dyn CliNpmResolver> {
// create a new snapshotted npm resolution and resolver // create a new snapshotted npm resolution and resolver
let npm_resolution = let npm_resolution =
@ -752,19 +704,19 @@ impl CliNpmResolver for ManagedCliNpmResolver {
self.npm_system_info.clone(), self.npm_system_info.clone(),
self.lifecycle_scripts.clone(), self.lifecycle_scripts.clone(),
), ),
create_npm_fs_resolver( self.maybe_lockfile.clone(),
Arc::new(ManagedNpmResolver::<CliSys>::new::<CliSys>(
&self.npm_cache_dir, &self.npm_cache_dir,
&self.npm_rc, &self.npm_rc,
npm_resolution.clone(), npm_resolution.clone(),
self.sys.clone(), self.sys.clone(),
self.root_node_modules_path().map(ToOwned::to_owned), self.root_node_modules_path().map(ToOwned::to_owned),
), )),
self.maybe_lockfile.clone(),
self.registry_info_provider.clone(),
self.npm_cache.clone(), self.npm_cache.clone(),
self.npm_cache_dir.clone(), self.npm_cache_dir.clone(),
self.npm_install_deps_provider.clone(), self.npm_install_deps_provider.clone(),
self.npm_rc.clone(), self.npm_rc.clone(),
self.registry_info_provider.clone(),
npm_resolution, npm_resolution,
self.sys.clone(), self.sys.clone(),
self.tarball_cache.clone(), self.tarball_cache.clone(),
@ -779,7 +731,7 @@ impl CliNpmResolver for ManagedCliNpmResolver {
} }
fn root_node_modules_path(&self) -> Option<&Path> { fn root_node_modules_path(&self) -> Option<&Path> {
self.fs_resolver.node_modules_path() self.managed_npm_resolver.node_modules_path()
} }
fn check_state_hash(&self) -> Option<u64> { fn check_state_hash(&self) -> Option<u64> {
@ -794,11 +746,23 @@ impl CliNpmResolver for ManagedCliNpmResolver {
let mut hasher = FastInsecureHasher::new_without_deno_version(); let mut hasher = FastInsecureHasher::new_without_deno_version();
// ensure the cache gets busted when turning nodeModulesDir on or off // ensure the cache gets busted when turning nodeModulesDir on or off
// as this could cause changes in resolution // as this could cause changes in resolution
hasher.write_hashable(self.fs_resolver.node_modules_path().is_some()); hasher
.write_hashable(self.managed_npm_resolver.node_modules_path().is_some());
for (pkg_req, pkg_nv) in package_reqs { for (pkg_req, pkg_nv) in package_reqs {
hasher.write_hashable(&pkg_req); hasher.write_hashable(&pkg_req);
hasher.write_hashable(&pkg_nv); hasher.write_hashable(&pkg_nv);
} }
Some(hasher.finish()) Some(hasher.finish())
} }
fn resolve_pkg_folder_from_deno_module_req(
&self,
req: &PackageReq,
referrer: &Url,
) -> Result<PathBuf, ResolvePkgFolderFromDenoReqError> {
self
.managed_npm_resolver
.resolve_pkg_folder_from_deno_module_req(req, referrer)
.map_err(ResolvePkgFolderFromDenoReqError::Managed)
}
} }

View file

@ -5,6 +5,7 @@ mod managed;
mod permission_checker; mod permission_checker;
use std::path::Path; use std::path::Path;
use std::path::PathBuf;
use std::sync::Arc; use std::sync::Arc;
use dashmap::DashMap; use dashmap::DashMap;
@ -15,7 +16,7 @@ use deno_error::JsErrorBox;
use deno_npm::npm_rc::ResolvedNpmRc; use deno_npm::npm_rc::ResolvedNpmRc;
use deno_npm::registry::NpmPackageInfo; use deno_npm::registry::NpmPackageInfo;
use deno_resolver::npm::ByonmNpmResolver; use deno_resolver::npm::ByonmNpmResolver;
use deno_resolver::npm::CliNpmReqResolver; use deno_resolver::npm::ByonmOrManagedNpmResolver;
use deno_resolver::npm::ResolvePkgFolderFromDenoReqError; use deno_resolver::npm::ResolvePkgFolderFromDenoReqError;
use deno_runtime::ops::process::NpmProcessStateProvider; use deno_runtime::ops::process::NpmProcessStateProvider;
use deno_semver::package::PackageNv; use deno_semver::package::PackageNv;
@ -136,17 +137,17 @@ pub enum InnerCliNpmResolverRef<'a> {
Byonm(&'a CliByonmNpmResolver), Byonm(&'a CliByonmNpmResolver),
} }
pub trait CliNpmResolver: CliNpmReqResolver { // todo(dsherret): replace with an enum
pub trait CliNpmResolver: Send + Sync + std::fmt::Debug {
fn into_npm_pkg_folder_resolver( fn into_npm_pkg_folder_resolver(
self: Arc<Self>, self: Arc<Self>,
) -> Arc<dyn NpmPackageFolderResolver>; ) -> Arc<dyn NpmPackageFolderResolver>;
fn into_npm_req_resolver(self: Arc<Self>) -> Arc<dyn CliNpmReqResolver>;
fn into_process_state_provider( fn into_process_state_provider(
self: Arc<Self>, self: Arc<Self>,
) -> Arc<dyn NpmProcessStateProvider>; ) -> Arc<dyn NpmProcessStateProvider>;
fn into_maybe_byonm(self: Arc<Self>) -> Option<Arc<CliByonmNpmResolver>> { fn into_byonm_or_managed(
None self: Arc<Self>,
} ) -> ByonmOrManagedNpmResolver<CliSys>;
fn clone_snapshotted(&self) -> Arc<dyn CliNpmResolver>; fn clone_snapshotted(&self) -> Arc<dyn CliNpmResolver>;
@ -166,6 +167,12 @@ pub trait CliNpmResolver: CliNpmReqResolver {
} }
} }
fn resolve_pkg_folder_from_deno_module_req(
&self,
req: &PackageReq,
referrer: &Url,
) -> Result<PathBuf, ResolvePkgFolderFromDenoReqError>;
fn root_node_modules_path(&self) -> Option<&Path>; fn root_node_modules_path(&self) -> Option<&Path>;
/// Returns a hash returning the state of the npm resolver /// Returns a hash returning the state of the npm resolver

View file

@ -849,11 +849,10 @@ pub async fn run(
let node_analysis_cache = NodeAnalysisCache::new(cache_db.node_analysis_db()); let node_analysis_cache = NodeAnalysisCache::new(cache_db.node_analysis_db());
let npm_req_resolver = let npm_req_resolver =
Arc::new(CliNpmReqResolver::new(NpmReqResolverOptions { Arc::new(CliNpmReqResolver::new(NpmReqResolverOptions {
byonm_resolver: (npm_resolver.clone()).into_maybe_byonm(),
sys: sys.clone(), sys: sys.clone(),
in_npm_pkg_checker: in_npm_pkg_checker.clone(), in_npm_pkg_checker: in_npm_pkg_checker.clone(),
node_resolver: node_resolver.clone(), node_resolver: node_resolver.clone(),
npm_req_resolver: npm_resolver.clone().into_npm_req_resolver(), npm_resolver: npm_resolver.clone().into_byonm_or_managed(),
})); }));
let cjs_esm_code_analyzer = CliCjsCodeAnalyzer::new( let cjs_esm_code_analyzer = CliCjsCodeAnalyzer::new(
node_analysis_cache, node_analysis_cache,

View file

@ -30,6 +30,7 @@ deno_npm.workspace = true
deno_package_json.workspace = true deno_package_json.workspace = true
deno_path_util.workspace = true deno_path_util.workspace = true
deno_semver.workspace = true deno_semver.workspace = true
log.workspace = true
node_resolver.workspace = true node_resolver.workspace = true
parking_lot.workspace = true parking_lot.workspace = true
sys_traits.workspace = true sys_traits.workspace = true

View file

@ -27,8 +27,8 @@ use thiserror::Error;
use url::Url; use url::Url;
use super::local::normalize_pkg_name_for_node_modules_deno_folder; use super::local::normalize_pkg_name_for_node_modules_deno_folder;
use super::CliNpmReqResolver; use crate::sync::MaybeSend;
use super::ResolvePkgFolderFromDenoReqError; use crate::sync::MaybeSync;
#[derive(Debug, Error, deno_error::JsError)] #[derive(Debug, Error, deno_error::JsError)]
pub enum ByonmResolvePkgFolderFromDenoReqError { pub enum ByonmResolvePkgFolderFromDenoReqError {
@ -377,35 +377,13 @@ impl<TSys: FsCanonicalize + FsRead + FsMetadata + FsReadDir>
} }
} }
impl<
Sys: FsCanonicalize
+ FsMetadata
+ FsRead
+ FsReadDir
+ Send
+ Sync
+ std::fmt::Debug,
> CliNpmReqResolver for ByonmNpmResolver<Sys>
{
fn resolve_pkg_folder_from_deno_module_req(
&self,
req: &PackageReq,
referrer: &Url,
) -> Result<PathBuf, ResolvePkgFolderFromDenoReqError> {
ByonmNpmResolver::resolve_pkg_folder_from_deno_module_req(
self, req, referrer,
)
.map_err(ResolvePkgFolderFromDenoReqError::Byonm)
}
}
impl< impl<
TSys: FsCanonicalize TSys: FsCanonicalize
+ FsMetadata + FsMetadata
+ FsRead + FsRead
+ FsReadDir + FsReadDir
+ Send + MaybeSend
+ Sync + MaybeSync
+ std::fmt::Debug, + std::fmt::Debug,
> NpmPackageFolderResolver for ByonmNpmResolver<TSys> > NpmPackageFolderResolver for ByonmNpmResolver<TSys>
{ {

View file

@ -12,14 +12,9 @@ use crate::sync::MaybeSend;
use crate::sync::MaybeSync; use crate::sync::MaybeSync;
#[allow(clippy::disallowed_types)] #[allow(clippy::disallowed_types)]
pub(super) type NpmPackageFsResolverRc = pub type NpmPackageFsResolverRc =
crate::sync::MaybeArc<dyn NpmPackageFsResolver>; crate::sync::MaybeArc<dyn NpmPackageFsResolver>;
#[derive(Debug, thiserror::Error, deno_error::JsError)]
#[class(generic)]
#[error("Package folder not found for '{0}'")]
pub struct NpmPackageFsResolverPackageFolderError(deno_semver::StackString);
/// Part of the resolution that interacts with the file system. /// Part of the resolution that interacts with the file system.
pub trait NpmPackageFsResolver: pub trait NpmPackageFsResolver:
NpmPackageFolderResolver + MaybeSend + MaybeSync NpmPackageFolderResolver + MaybeSend + MaybeSync
@ -29,15 +24,6 @@ pub trait NpmPackageFsResolver:
fn maybe_package_folder(&self, package_id: &NpmPackageId) -> Option<PathBuf>; fn maybe_package_folder(&self, package_id: &NpmPackageId) -> Option<PathBuf>;
fn package_folder(
&self,
package_id: &NpmPackageId,
) -> Result<PathBuf, NpmPackageFsResolverPackageFolderError> {
self.maybe_package_folder(package_id).ok_or_else(|| {
NpmPackageFsResolverPackageFolderError(package_id.as_serialized())
})
}
fn resolve_package_cache_folder_id_from_specifier( fn resolve_package_cache_folder_id_from_specifier(
&self, &self,
specifier: &Url, specifier: &Url,

View file

@ -23,12 +23,14 @@ use super::resolution::NpmResolutionRc;
use super::NpmPackageFsResolver; use super::NpmPackageFsResolver;
use crate::npm::local::get_package_folder_id_folder_name_from_parts; use crate::npm::local::get_package_folder_id_folder_name_from_parts;
use crate::npm::local::get_package_folder_id_from_folder_name; use crate::npm::local::get_package_folder_id_from_folder_name;
use crate::sync::MaybeSend;
use crate::sync::MaybeSync;
/// Resolver that creates a local node_modules directory /// Resolver that creates a local node_modules directory
/// and resolves packages from it. /// and resolves packages from it.
#[derive(Debug)] #[derive(Debug)]
pub struct LocalNpmPackageResolver< pub struct LocalNpmPackageResolver<
TSys: FsCanonicalize + FsMetadata + Send + Sync, TSys: FsCanonicalize + FsMetadata + MaybeSend + MaybeSync,
> { > {
resolution: NpmResolutionRc, resolution: NpmResolutionRc,
sys: TSys, sys: TSys,
@ -36,7 +38,7 @@ pub struct LocalNpmPackageResolver<
root_node_modules_url: Url, root_node_modules_url: Url,
} }
impl<TSys: FsCanonicalize + FsMetadata + Send + Sync> impl<TSys: FsCanonicalize + FsMetadata + MaybeSend + MaybeSync>
LocalNpmPackageResolver<TSys> LocalNpmPackageResolver<TSys>
{ {
#[allow(clippy::too_many_arguments)] #[allow(clippy::too_many_arguments)]
@ -99,8 +101,9 @@ impl<TSys: FsCanonicalize + FsMetadata + Send + Sync>
} }
} }
impl<TSys: FsCanonicalize + FsMetadata + Send + Sync + std::fmt::Debug> impl<
NpmPackageFolderResolver for LocalNpmPackageResolver<TSys> TSys: FsCanonicalize + FsMetadata + MaybeSend + MaybeSync + std::fmt::Debug,
> NpmPackageFolderResolver for LocalNpmPackageResolver<TSys>
{ {
fn resolve_package_folder_from_package( fn resolve_package_folder_from_package(
&self, &self,
@ -160,8 +163,9 @@ impl<TSys: FsCanonicalize + FsMetadata + Send + Sync + std::fmt::Debug>
} }
} }
impl<TSys: FsCanonicalize + FsMetadata + Send + Sync + std::fmt::Debug> impl<
NpmPackageFsResolver for LocalNpmPackageResolver<TSys> TSys: FsCanonicalize + FsMetadata + MaybeSend + MaybeSync + std::fmt::Debug,
> NpmPackageFsResolver for LocalNpmPackageResolver<TSys>
{ {
fn node_modules_path(&self) -> Option<&Path> { fn node_modules_path(&self) -> Option<&Path> {
Some(self.root_node_modules_path.as_ref()) Some(self.root_node_modules_path.as_ref())

View file

@ -8,42 +8,180 @@ mod resolution;
use std::path::Path; use std::path::Path;
use std::path::PathBuf; use std::path::PathBuf;
use deno_npm::resolution::PackageReqNotFoundError;
use deno_npm::NpmPackageCacheFolderId;
use deno_npm::NpmPackageId;
use deno_path_util::fs::canonicalize_path_maybe_not_exists;
use deno_semver::package::PackageReq;
use node_resolver::InNpmPackageChecker; use node_resolver::InNpmPackageChecker;
use node_resolver::NpmPackageFolderResolver;
use sys_traits::FsCanonicalize; use sys_traits::FsCanonicalize;
use sys_traits::FsMetadata; use sys_traits::FsMetadata;
use url::Url; use url::Url;
pub use self::common::NpmPackageFsResolver; use self::common::NpmPackageFsResolver;
pub use self::common::NpmPackageFsResolverPackageFolderError;
use self::common::NpmPackageFsResolverRc; use self::common::NpmPackageFsResolverRc;
use self::global::GlobalNpmPackageResolver; use self::global::GlobalNpmPackageResolver;
use self::local::LocalNpmPackageResolver; use self::local::LocalNpmPackageResolver;
pub use self::resolution::NpmResolution; pub use self::resolution::NpmResolution;
use self::resolution::NpmResolutionRc; pub use self::resolution::NpmResolutionRc;
use crate::sync::new_rc; use crate::sync::new_rc;
use crate::sync::MaybeSend;
use crate::sync::MaybeSync;
use crate::NpmCacheDirRc; use crate::NpmCacheDirRc;
use crate::ResolvedNpmRcRc; use crate::ResolvedNpmRcRc;
pub fn create_npm_fs_resolver< #[derive(Debug, thiserror::Error, deno_error::JsError)]
TSys: FsCanonicalize + FsMetadata + std::fmt::Debug + Send + Sync + 'static, #[error(transparent)]
pub enum ResolvePkgFolderFromPkgIdError {
#[class(inherit)]
#[error(transparent)]
NotFound(#[from] NpmManagedPackageFolderNotFoundError),
#[class(inherit)]
#[error(transparent)]
FailedCanonicalizing(#[from] FailedCanonicalizingError),
}
#[derive(Debug, thiserror::Error, deno_error::JsError)]
#[class(generic)]
#[error("Package folder not found for '{0}'")]
pub struct NpmManagedPackageFolderNotFoundError(deno_semver::StackString);
#[derive(Debug, thiserror::Error, deno_error::JsError)]
#[class(generic)]
#[error("Failed canonicalizing '{}'", path.display())]
pub struct FailedCanonicalizingError {
path: PathBuf,
#[source]
source: std::io::Error,
}
#[derive(Debug, thiserror::Error, deno_error::JsError)]
pub enum ManagedResolvePkgFolderFromDenoReqError {
#[class(inherit)]
#[error(transparent)]
PackageReqNotFound(#[from] PackageReqNotFoundError),
#[class(inherit)]
#[error(transparent)]
ResolvePkgFolderFromPkgId(#[from] ResolvePkgFolderFromPkgIdError),
}
#[allow(clippy::disallowed_types)]
pub type ManagedNpmResolverRc<TSys> =
crate::sync::MaybeArc<ManagedNpmResolver<TSys>>;
#[derive(Debug)]
pub struct ManagedNpmResolver<TSys: FsCanonicalize> {
fs_resolver: NpmPackageFsResolverRc,
resolution: NpmResolutionRc,
sys: TSys,
}
impl<TSys: FsCanonicalize> ManagedNpmResolver<TSys> {
pub fn new<
TCreateSys: FsCanonicalize
+ FsMetadata
+ std::fmt::Debug
+ MaybeSend
+ MaybeSync
+ Clone
+ 'static,
>( >(
npm_cache_dir: &NpmCacheDirRc, npm_cache_dir: &NpmCacheDirRc,
npm_rc: &ResolvedNpmRcRc, npm_rc: &ResolvedNpmRcRc,
resolution: NpmResolutionRc, resolution: NpmResolutionRc,
sys: TSys, sys: TCreateSys,
maybe_node_modules_path: Option<PathBuf>, maybe_node_modules_path: Option<PathBuf>,
) -> NpmPackageFsResolverRc { ) -> ManagedNpmResolver<TCreateSys> {
match maybe_node_modules_path { let fs_resolver: NpmPackageFsResolverRc = match maybe_node_modules_path {
Some(node_modules_folder) => new_rc(LocalNpmPackageResolver::new( Some(node_modules_folder) => new_rc(LocalNpmPackageResolver::new(
resolution, resolution.clone(),
sys, sys.clone(),
node_modules_folder, node_modules_folder,
)), )),
None => new_rc(GlobalNpmPackageResolver::new( None => new_rc(GlobalNpmPackageResolver::new(
npm_cache_dir.clone(), npm_cache_dir.clone(),
npm_rc.clone(), npm_rc.clone(),
resolution, resolution.clone(),
)), )),
};
ManagedNpmResolver {
fs_resolver,
sys,
resolution,
}
}
#[inline]
pub fn node_modules_path(&self) -> Option<&Path> {
self.fs_resolver.node_modules_path()
}
pub fn resolve_pkg_folder_from_pkg_id(
&self,
package_id: &NpmPackageId,
) -> Result<PathBuf, ResolvePkgFolderFromPkgIdError> {
let path = self
.fs_resolver
.maybe_package_folder(package_id)
.ok_or_else(|| {
NpmManagedPackageFolderNotFoundError(package_id.as_serialized())
})?;
// todo(dsherret): investigate if this canonicalization is always
// necessary. For example, maybe it's not necessary for the global cache
let path = canonicalize_path_maybe_not_exists(&self.sys, &path).map_err(
|source| FailedCanonicalizingError {
path: path.to_path_buf(),
source,
},
)?;
log::debug!(
"Resolved package folder of {} to {}",
package_id.as_serialized(),
path.display()
);
Ok(path)
}
pub fn resolve_pkg_folder_from_deno_module_req(
&self,
req: &PackageReq,
_referrer: &Url,
) -> Result<PathBuf, ManagedResolvePkgFolderFromDenoReqError> {
let pkg_id = self.resolution.resolve_pkg_id_from_pkg_req(req)?;
Ok(self.resolve_pkg_folder_from_pkg_id(&pkg_id)?)
}
#[inline]
pub fn resolve_package_cache_folder_id_from_specifier(
&self,
specifier: &Url,
) -> Result<Option<NpmPackageCacheFolderId>, std::io::Error> {
self
.fs_resolver
.resolve_package_cache_folder_id_from_specifier(specifier)
}
}
impl<TSys: FsCanonicalize + std::fmt::Debug + MaybeSend + MaybeSync>
NpmPackageFolderResolver for ManagedNpmResolver<TSys>
{
fn resolve_package_folder_from_package(
&self,
specifier: &str,
referrer: &Url,
) -> Result<PathBuf, node_resolver::errors::PackageFolderResolveError> {
let path = self
.fs_resolver
.resolve_package_folder_from_package(specifier, referrer)?;
log::debug!(
"Resolved {} from {} to {}",
specifier,
referrer,
path.display()
);
Ok(path)
} }
} }

View file

@ -18,7 +18,7 @@ use deno_semver::package::PackageReq;
use parking_lot::RwLock; use parking_lot::RwLock;
#[allow(clippy::disallowed_types)] #[allow(clippy::disallowed_types)]
pub(super) type NpmResolutionRc = crate::sync::MaybeArc<NpmResolution>; pub type NpmResolutionRc = crate::sync::MaybeArc<NpmResolution>;
/// Handles updating and storing npm resolution in memory. /// Handles updating and storing npm resolution in memory.
/// ///

View file

@ -36,9 +36,9 @@ pub use self::local::get_package_folder_id_folder_name;
pub use self::local::normalize_pkg_name_for_node_modules_deno_folder; pub use self::local::normalize_pkg_name_for_node_modules_deno_folder;
use self::managed::create_managed_in_npm_pkg_checker; use self::managed::create_managed_in_npm_pkg_checker;
use self::managed::ManagedInNpmPkgCheckerCreateOptions; use self::managed::ManagedInNpmPkgCheckerCreateOptions;
pub use self::managed::ManagedNpmResolver;
pub use self::managed::ManagedNpmResolverRc;
use crate::sync::new_rc; use crate::sync::new_rc;
use crate::sync::MaybeSend;
use crate::sync::MaybeSync;
mod byonm; mod byonm;
mod local; mod local;
@ -108,37 +108,50 @@ pub enum ResolveReqWithSubPathErrorKind {
#[derive(Debug, Error, JsError)] #[derive(Debug, Error, JsError)]
pub enum ResolvePkgFolderFromDenoReqError { pub enum ResolvePkgFolderFromDenoReqError {
#[class(inherit)] #[class(inherit)]
#[error("{0}")] #[error(transparent)]
Managed(Box<dyn deno_error::JsErrorClass>), Managed(managed::ManagedResolvePkgFolderFromDenoReqError),
#[class(inherit)] #[class(inherit)]
#[error(transparent)] #[error(transparent)]
Byonm(#[from] ByonmResolvePkgFolderFromDenoReqError), Byonm(byonm::ByonmResolvePkgFolderFromDenoReqError),
} }
#[allow(clippy::disallowed_types)] #[derive(Debug, Clone)]
pub type CliNpmReqResolverRc = crate::sync::MaybeArc<dyn CliNpmReqResolver>; pub enum ByonmOrManagedNpmResolver<
TSys: FsCanonicalize + FsMetadata + FsRead + FsReadDir,
> {
/// The resolver when "bring your own node_modules" is enabled where Deno
/// does not setup the node_modules directories automatically, but instead
/// uses what already exists on the file system.
Byonm(ByonmNpmResolverRc<TSys>),
Managed(ManagedNpmResolverRc<TSys>),
}
// todo(dsherret): a temporary trait until we extract impl<TSys: FsCanonicalize + FsMetadata + FsRead + FsReadDir>
// out the CLI npm resolver into here ByonmOrManagedNpmResolver<TSys>
pub trait CliNpmReqResolver: Debug + MaybeSend + MaybeSync { {
fn resolve_pkg_folder_from_deno_module_req( pub fn resolve_pkg_folder_from_deno_module_req(
&self, &self,
req: &PackageReq, req: &PackageReq,
referrer: &Url, referrer: &Url,
) -> Result<PathBuf, ResolvePkgFolderFromDenoReqError>; ) -> Result<PathBuf, ResolvePkgFolderFromDenoReqError> {
match self {
ByonmOrManagedNpmResolver::Byonm(byonm_resolver) => byonm_resolver
.resolve_pkg_folder_from_deno_module_req(req, referrer)
.map_err(ResolvePkgFolderFromDenoReqError::Byonm),
ByonmOrManagedNpmResolver::Managed(managed_resolver) => managed_resolver
.resolve_pkg_folder_from_deno_module_req(req, referrer)
.map_err(ResolvePkgFolderFromDenoReqError::Managed),
}
}
} }
pub struct NpmReqResolverOptions< pub struct NpmReqResolverOptions<
TIsBuiltInNodeModuleChecker: IsBuiltInNodeModuleChecker, TIsBuiltInNodeModuleChecker: IsBuiltInNodeModuleChecker,
TSys: FsCanonicalize + FsMetadata + FsRead + FsReadDir, TSys: FsCanonicalize + FsMetadata + FsRead + FsReadDir,
> { > {
/// The resolver when "bring your own node_modules" is enabled where Deno
/// does not setup the node_modules directories automatically, but instead
/// uses what already exists on the file system.
pub byonm_resolver: Option<ByonmNpmResolverRc<TSys>>,
pub in_npm_pkg_checker: InNpmPackageCheckerRc, pub in_npm_pkg_checker: InNpmPackageCheckerRc,
pub node_resolver: NodeResolverRc<TIsBuiltInNodeModuleChecker, TSys>, pub node_resolver: NodeResolverRc<TIsBuiltInNodeModuleChecker, TSys>,
pub npm_req_resolver: CliNpmReqResolverRc, pub npm_resolver: ByonmOrManagedNpmResolver<TSys>,
pub sys: TSys, pub sys: TSys,
} }
@ -151,11 +164,10 @@ pub struct NpmReqResolver<
TIsBuiltInNodeModuleChecker: IsBuiltInNodeModuleChecker, TIsBuiltInNodeModuleChecker: IsBuiltInNodeModuleChecker,
TSys: FsCanonicalize + FsMetadata + FsRead + FsReadDir, TSys: FsCanonicalize + FsMetadata + FsRead + FsReadDir,
> { > {
byonm_resolver: Option<ByonmNpmResolverRc<TSys>>,
sys: TSys, sys: TSys,
in_npm_pkg_checker: InNpmPackageCheckerRc, in_npm_pkg_checker: InNpmPackageCheckerRc,
node_resolver: NodeResolverRc<TIsBuiltInNodeModuleChecker, TSys>, node_resolver: NodeResolverRc<TIsBuiltInNodeModuleChecker, TSys>,
npm_resolver: CliNpmReqResolverRc, npm_resolver: ByonmOrManagedNpmResolver<TSys>,
} }
impl< impl<
@ -167,11 +179,10 @@ impl<
options: NpmReqResolverOptions<TIsBuiltInNodeModuleChecker, TSys>, options: NpmReqResolverOptions<TIsBuiltInNodeModuleChecker, TSys>,
) -> Self { ) -> Self {
Self { Self {
byonm_resolver: options.byonm_resolver,
sys: options.sys, sys: options.sys,
in_npm_pkg_checker: options.in_npm_pkg_checker, in_npm_pkg_checker: options.in_npm_pkg_checker,
node_resolver: options.node_resolver, node_resolver: options.node_resolver,
npm_resolver: options.npm_req_resolver, npm_resolver: options.npm_resolver,
} }
} }
@ -213,7 +224,7 @@ impl<
match resolution_result { match resolution_result {
Ok(url) => Ok(url), Ok(url) => Ok(url),
Err(err) => { Err(err) => {
if self.byonm_resolver.is_some() { if matches!(self.npm_resolver, ByonmOrManagedNpmResolver::Byonm(_)) {
let package_json_path = package_folder.join("package.json"); let package_json_path = package_folder.join("package.json");
if !self.sys.fs_exists_no_err(&package_json_path) { if !self.sys.fs_exists_no_err(&package_json_path) {
return Err( return Err(
@ -281,7 +292,10 @@ impl<
.into_box(), .into_box(),
); );
} }
if let Some(byonm_npm_resolver) = &self.byonm_resolver { if let ByonmOrManagedNpmResolver::Byonm(
byonm_npm_resolver,
) = &self.npm_resolver
{
if byonm_npm_resolver if byonm_npm_resolver
.find_ancestor_package_json_with_dep( .find_ancestor_package_json_with_dep(
package_name, package_name,

View file

@ -43,7 +43,13 @@ mod inner {
} }
impl<K: Eq + Hash, V, S: BuildHasher> MaybeDashMap<K, V, S> { impl<K: Eq + Hash, V, S: BuildHasher> MaybeDashMap<K, V, S> {
pub fn get<'a>(&'a self, key: &K) -> Option<Ref<'a, V>> { pub fn get<'a, Q: Eq + Hash + ?Sized>(
&'a self,
key: &Q,
) -> Option<Ref<'a, V>>
where
K: std::borrow::Borrow<Q>,
{
Ref::filter_map(self.0.borrow(), |map| map.get(key)).ok() Ref::filter_map(self.0.borrow(), |map| map.get(key)).ok()
} }