Skip to content

Commit

Permalink
refactor(npm): extract out some npm fs resolution code from the cli (#…
Browse files Browse the repository at this point in the history
…27607)

Moves the npm fs resolvers into the deno_resolution crate.

This does not entirely move things out, but is a step in that direction.
  • Loading branch information
dsherret authored Jan 9, 2025
1 parent ce0968e commit 093f3ba
Show file tree
Hide file tree
Showing 24 changed files with 312 additions and 213 deletions.
6 changes: 4 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions cli/http_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ impl HttpClientProvider {
}
}

pub fn get_or_create(&self) -> Result<HttpClient, AnyError> {
pub fn get_or_create(&self) -> Result<HttpClient, JsErrorBox> {
use std::collections::hash_map::Entry;
let thread_id = std::thread::current().id();
let mut clients = self.clients_by_thread_id.lock();
Expand All @@ -87,7 +87,8 @@ impl HttpClientProvider {
},
..self.options.clone()
},
)?;
)
.map_err(JsErrorBox::from_err)?;
entry.insert(client.clone());
Ok(HttpClient::new(client))
}
Expand Down
2 changes: 1 addition & 1 deletion cli/npm/managed/installer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ use deno_npm::resolution::AddPkgReqsOptions;
use deno_npm::resolution::NpmResolutionError;
use deno_npm::resolution::NpmResolutionSnapshot;
use deno_npm::NpmResolutionPackage;
use deno_resolver::npm::managed::NpmResolution;
use deno_semver::jsr::JsrDepPackageReq;
use deno_semver::package::PackageNv;
use deno_semver::package::PackageReq;
use deno_semver::SmallStackString;
use deno_semver::VersionReq;

use super::resolution::NpmResolution;
use crate::args::CliLockfile;
use crate::npm::CliNpmRegistryInfoProvider;
use crate::util::sync::TaskQueue;
Expand Down
2 changes: 1 addition & 1 deletion cli/npm/managed/installers/global.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ use deno_core::futures::StreamExt;
use deno_error::JsErrorBox;
use deno_npm::NpmResolutionPackage;
use deno_npm::NpmSystemInfo;
use deno_resolver::npm::managed::NpmResolution;

use super::super::resolution::NpmResolution;
use super::common::lifecycle_scripts::LifecycleScriptsStrategy;
use super::common::NpmPackageFsInstaller;
use crate::args::LifecycleScriptsConfig;
Expand Down
4 changes: 2 additions & 2 deletions cli/npm/managed/installers/local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,19 @@ use deno_npm::resolution::NpmResolutionSnapshot;
use deno_npm::NpmResolutionPackage;
use deno_npm::NpmSystemInfo;
use deno_path_util::fs::atomic_write_file_with_retries;
use deno_resolver::npm::get_package_folder_id_folder_name;
use deno_resolver::npm::managed::NpmResolution;
use deno_semver::package::PackageNv;
use deno_semver::StackString;
use serde::Deserialize;
use serde::Serialize;

use super::super::resolution::NpmResolution;
use super::common::bin_entries;
use super::common::NpmPackageFsInstaller;
use crate::args::LifecycleScriptsConfig;
use crate::args::NpmInstallDepsProvider;
use crate::cache::CACHE_PERM;
use crate::colors;
use crate::npm::managed::resolvers::get_package_folder_id_folder_name;
use crate::npm::managed::PackageCaching;
use crate::npm::CliNpmCache;
use crate::npm::CliNpmTarballCache;
Expand Down
2 changes: 1 addition & 1 deletion cli/npm/managed/installers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ use std::path::PathBuf;
use std::sync::Arc;

use deno_npm::NpmSystemInfo;
use deno_resolver::npm::managed::NpmResolution;

pub use self::common::NpmPackageFsInstaller;
use self::global::GlobalNpmPackageInstaller;
use self::local::LocalNpmPackageInstaller;
use super::resolution::NpmResolution;
use crate::args::LifecycleScriptsConfig;
use crate::args::NpmInstallDepsProvider;
use crate::npm::CliNpmCache;
Expand Down
34 changes: 24 additions & 10 deletions cli/npm/managed/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ use deno_npm::NpmResolutionPackage;
use deno_npm::NpmSystemInfo;
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::CliNpmReqResolver;
use deno_runtime::colors;
use deno_runtime::ops::process::NpmProcessStateProvider;
Expand All @@ -37,9 +41,6 @@ use node_resolver::errors::PackageFolderResolveIoError;
use node_resolver::InNpmPackageChecker;
use node_resolver::NpmPackageFolderResolver;

use self::resolution::NpmResolution;
use self::resolvers::create_npm_fs_resolver;
use self::resolvers::NpmPackageFsResolver;
use super::CliNpmCache;
use super::CliNpmCacheHttpClient;
use super::CliNpmRegistryInfoProvider;
Expand All @@ -60,8 +61,6 @@ use crate::util::sync::AtomicFlag;

mod installer;
mod installers;
mod resolution;
mod resolvers;

pub enum CliNpmResolverManagedSnapshotOption {
ResolveFromLockfile(Arc<CliLockfile>),
Expand Down Expand Up @@ -104,6 +103,7 @@ pub async fn create_managed_npm_resolver_for_lsp(
create_inner(
http_client,
npm_cache,
options.npm_cache_dir,
options.npm_install_deps_provider,
npm_api,
options.sys,
Expand Down Expand Up @@ -133,6 +133,7 @@ pub async fn create_managed_npm_resolver(
Ok(create_inner(
http_client,
npm_cache,
options.npm_cache_dir,
options.npm_install_deps_provider,
api,
options.sys,
Expand All @@ -150,6 +151,7 @@ pub async fn create_managed_npm_resolver(
fn create_inner(
http_client: Arc<CliNpmCacheHttpClient>,
npm_cache: Arc<CliNpmCache>,
npm_cache_dir: Arc<NpmCacheDir>,
npm_install_deps_provider: Arc<NpmInstallDepsProvider>,
registry_info_provider: Arc<CliNpmRegistryInfoProvider>,
sys: CliSys,
Expand Down Expand Up @@ -181,7 +183,8 @@ fn create_inner(
lifecycle_scripts.clone(),
);
let fs_resolver = create_npm_fs_resolver(
npm_cache.clone(),
&npm_cache_dir,
&npm_rc,
resolution.clone(),
sys.clone(),
node_modules_dir_path,
Expand All @@ -192,7 +195,9 @@ fn create_inner(
maybe_lockfile,
registry_info_provider,
npm_cache,
npm_cache_dir,
npm_install_deps_provider,
npm_rc,
resolution,
sys,
tarball_cache,
Expand Down Expand Up @@ -314,7 +319,9 @@ pub struct ManagedCliNpmResolver {
maybe_lockfile: Option<Arc<CliLockfile>>,
registry_info_provider: Arc<CliNpmRegistryInfoProvider>,
npm_cache: Arc<CliNpmCache>,
npm_cache_dir: Arc<NpmCacheDir>,
npm_install_deps_provider: Arc<NpmInstallDepsProvider>,
npm_rc: Arc<ResolvedNpmRc>,
sys: CliSys,
resolution: Arc<NpmResolution>,
resolution_installer: NpmResolutionInstaller,
Expand All @@ -338,7 +345,7 @@ pub enum ResolvePkgFolderFromPkgIdError {
#[class(inherit)]
#[error("{0}")]
NpmPackageFsResolverPackageFolder(
#[from] resolvers::NpmPackageFsResolverPackageFolderError,
#[from] NpmPackageFsResolverPackageFolderError,
),
#[class(inherit)]
#[error("{0}")]
Expand All @@ -363,7 +370,9 @@ impl ManagedCliNpmResolver {
maybe_lockfile: Option<Arc<CliLockfile>>,
registry_info_provider: Arc<CliNpmRegistryInfoProvider>,
npm_cache: Arc<CliNpmCache>,
npm_cache_dir: Arc<NpmCacheDir>,
npm_install_deps_provider: Arc<NpmInstallDepsProvider>,
npm_rc: Arc<ResolvedNpmRc>,
resolution: Arc<NpmResolution>,
sys: CliSys,
tarball_cache: Arc<CliNpmTarballCache>,
Expand All @@ -382,7 +391,9 @@ impl ManagedCliNpmResolver {
maybe_lockfile,
registry_info_provider,
npm_cache,
npm_cache_dir,
npm_install_deps_provider,
npm_rc,
text_only_progress_bar,
resolution,
resolution_installer,
Expand Down Expand Up @@ -671,11 +682,11 @@ impl ManagedCliNpmResolver {
}

pub fn global_cache_root_path(&self) -> &Path {
self.npm_cache.root_dir_path()
self.npm_cache_dir.root_dir()
}

pub fn global_cache_root_url(&self) -> &Url {
self.npm_cache.root_dir_url()
self.npm_cache_dir.root_dir_url()
}
}

Expand Down Expand Up @@ -772,15 +783,18 @@ impl CliNpmResolver for ManagedCliNpmResolver {
self.lifecycle_scripts.clone(),
),
create_npm_fs_resolver(
self.npm_cache.clone(),
&self.npm_cache_dir,
&self.npm_rc,
npm_resolution.clone(),
self.sys.clone(),
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_dir.clone(),
self.npm_install_deps_provider.clone(),
self.npm_rc.clone(),
npm_resolution,
self.sys.clone(),
self.tarball_cache.clone(),
Expand Down
33 changes: 0 additions & 33 deletions cli/npm/managed/resolvers/mod.rs

This file was deleted.

3 changes: 2 additions & 1 deletion cli/npm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use dashmap::DashMap;
use deno_core::error::AnyError;
use deno_core::serde_json;
use deno_core::url::Url;
use deno_error::JsErrorBox;
use deno_npm::npm_rc::ResolvedNpmRc;
use deno_npm::registry::NpmPackageInfo;
use deno_resolver::npm::ByonmInNpmPackageChecker;
Expand Down Expand Up @@ -100,7 +101,7 @@ impl deno_npm_cache::NpmCacheHttpClient for CliNpmCacheHttpClient {
};
deno_npm_cache::DownloadError {
status_code,
error: err.into(),
error: JsErrorBox::from_err(err),
}
})
}
Expand Down
10 changes: 6 additions & 4 deletions resolvers/deno/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,20 @@ sync = ["dashmap"]

[dependencies]
anyhow.workspace = true
async-trait.workspace = true
base32.workspace = true
boxed_error.workspace = true
dashmap = { workspace = true, optional = true }
deno_cache_dir.workspace = true
deno_config.workspace = true
deno_error.workspace = true
deno_media_type.workspace = true
deno_package_json.workspace = true
deno_package_json.features = ["sync"]
deno_npm.workspace = true
deno_package_json = { workspace = true, features = ["sync"] }
deno_path_util.workspace = true
deno_semver.workspace = true
node_resolver.workspace = true
node_resolver.features = ["sync"]
node_resolver = { workspace = true, features = ["sync"] }
parking_lot.workspace = true
sys_traits.workspace = true
thiserror.workspace = true
url.workspace = true
Expand Down
8 changes: 8 additions & 0 deletions resolvers/deno/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
use std::path::PathBuf;

use boxed_error::Boxed;
use deno_cache_dir::npm::NpmCacheDir;
use deno_config::workspace::MappedResolution;
use deno_config::workspace::MappedResolutionDiagnostic;
use deno_config::workspace::MappedResolutionError;
use deno_config::workspace::WorkspaceResolvePkgJsonFolderError;
use deno_config::workspace::WorkspaceResolver;
use deno_error::JsError;
use deno_npm::npm_rc::ResolvedNpmRc;
use deno_package_json::PackageJsonDepValue;
use deno_package_json::PackageJsonDepValueParseError;
use deno_semver::npm::NpmPackageReqReference;
Expand Down Expand Up @@ -47,6 +49,12 @@ mod sync;
#[allow(clippy::disallowed_types)]
pub type WorkspaceResolverRc = crate::sync::MaybeArc<WorkspaceResolver>;

#[allow(clippy::disallowed_types)]
pub(crate) type ResolvedNpmRcRc = crate::sync::MaybeArc<ResolvedNpmRc>;

#[allow(clippy::disallowed_types)]
pub(crate) type NpmCacheDirRc = crate::sync::MaybeArc<NpmCacheDir>;

#[derive(Debug, Clone)]
pub struct DenoResolution {
pub url: Url,
Expand Down
Loading

0 comments on commit 093f3ba

Please sign in to comment.