diff options
Diffstat (limited to 'cli/npm')
-rw-r--r-- | cli/npm/cache.rs | 8 | ||||
-rw-r--r-- | cli/npm/resolution/mod.rs | 7 | ||||
-rw-r--r-- | cli/npm/resolvers/common.rs | 3 | ||||
-rw-r--r-- | cli/npm/resolvers/global.rs | 4 | ||||
-rw-r--r-- | cli/npm/resolvers/local.rs | 15 | ||||
-rw-r--r-- | cli/npm/resolvers/mod.rs | 23 |
6 files changed, 45 insertions, 15 deletions
diff --git a/cli/npm/cache.rs b/cli/npm/cache.rs index 9f58bcd0d..1bc2f8487 100644 --- a/cli/npm/cache.rs +++ b/cli/npm/cache.rs @@ -179,6 +179,10 @@ impl ReadonlyNpmCache { Self::new(dir.npm_folder_path()) } + pub fn root_dir_url(&self) -> &Url { + &self.root_dir_url + } + pub fn package_folder_for_id( &self, folder_id: &NpmPackageCacheFolderId, @@ -345,6 +349,10 @@ impl NpmCache { &self.cache_setting } + pub fn root_dir_url(&self) -> &Url { + self.readonly.root_dir_url() + } + /// Checks if the cache should be used for the provided name and version. /// NOTE: Subsequent calls for the same package will always return `true` /// to ensure a package is only downloaded once per run of the CLI. This diff --git a/cli/npm/resolution/mod.rs b/cli/npm/resolution/mod.rs index 8584958b5..53284d712 100644 --- a/cli/npm/resolution/mod.rs +++ b/cli/npm/resolution/mod.rs @@ -292,17 +292,18 @@ impl NpmResolution { pub async fn set_package_reqs( &self, - package_reqs: HashSet<NpmPackageReq>, + package_reqs: Vec<NpmPackageReq>, ) -> Result<(), AnyError> { let inner = &self.0; // only allow one thread in here at a time let _permit = inner.update_semaphore.acquire().await?; let snapshot = inner.snapshot.read().clone(); + let reqs_set = package_reqs.iter().collect::<HashSet<_>>(); let has_removed_package = !snapshot .package_reqs .keys() - .all(|req| package_reqs.contains(req)); + .all(|req| reqs_set.contains(req)); // if any packages were removed, we need to completely recreate the npm resolution snapshot let snapshot = if has_removed_package { NpmResolutionSnapshot::default() @@ -311,7 +312,7 @@ impl NpmResolution { }; let snapshot = add_package_reqs_to_snapshot( &inner.api, - package_reqs.into_iter().collect(), + package_reqs, snapshot, self.0.maybe_lockfile.clone(), ) diff --git a/cli/npm/resolvers/common.rs b/cli/npm/resolvers/common.rs index 8c1ecd892..3f1c2a704 100644 --- a/cli/npm/resolvers/common.rs +++ b/cli/npm/resolvers/common.rs @@ -20,6 +20,9 @@ use crate::npm::NpmResolutionPackage; /// Part of the resolution that interacts with the file system. #[async_trait] pub trait NpmPackageFsResolver: Send + Sync { + /// Specifier for the root directory. + fn root_dir_url(&self) -> &Url; + fn resolve_package_folder_from_deno_module( &self, id: &NpmPackageId, diff --git a/cli/npm/resolvers/global.rs b/cli/npm/resolvers/global.rs index 1d4d14ac8..87ad92675 100644 --- a/cli/npm/resolvers/global.rs +++ b/cli/npm/resolvers/global.rs @@ -68,6 +68,10 @@ impl GlobalNpmPackageResolver { #[async_trait] impl NpmPackageFsResolver for GlobalNpmPackageResolver { + fn root_dir_url(&self) -> &Url { + self.cache.root_dir_url() + } + fn resolve_package_folder_from_deno_module( &self, id: &NpmPackageId, diff --git a/cli/npm/resolvers/local.rs b/cli/npm/resolvers/local.rs index ba395d1b6..bf5b8529c 100644 --- a/cli/npm/resolvers/local.rs +++ b/cli/npm/resolvers/local.rs @@ -44,7 +44,7 @@ pub struct LocalNpmPackageResolver { resolution: NpmResolution, registry_url: Url, root_node_modules_path: PathBuf, - root_node_modules_specifier: ModuleSpecifier, + root_node_modules_url: Url, } impl LocalNpmPackageResolver { @@ -58,10 +58,8 @@ impl LocalNpmPackageResolver { cache, resolution, registry_url, - root_node_modules_specifier: ModuleSpecifier::from_directory_path( - &node_modules_folder, - ) - .unwrap(), + root_node_modules_url: Url::from_directory_path(&node_modules_folder) + .unwrap(), root_node_modules_path: node_modules_folder, } } @@ -92,8 +90,7 @@ impl LocalNpmPackageResolver { &self, specifier: &ModuleSpecifier, ) -> Option<PathBuf> { - let relative_url = - self.root_node_modules_specifier.make_relative(specifier)?; + let relative_url = self.root_node_modules_url.make_relative(specifier)?; if relative_url.starts_with("../") { return None; } @@ -126,6 +123,10 @@ impl LocalNpmPackageResolver { #[async_trait] impl NpmPackageFsResolver for LocalNpmPackageResolver { + fn root_dir_url(&self) -> &Url { + &self.root_node_modules_url + } + fn resolve_package_folder_from_deno_module( &self, node_id: &NpmPackageId, diff --git a/cli/npm/resolvers/mod.rs b/cli/npm/resolvers/mod.rs index 039864c5f..583a1c955 100644 --- a/cli/npm/resolvers/mod.rs +++ b/cli/npm/resolvers/mod.rs @@ -19,7 +19,7 @@ use deno_runtime::deno_node::RequireNpmResolver; use global::GlobalNpmPackageResolver; use serde::Deserialize; use serde::Serialize; -use std::collections::HashSet; +use std::collections::HashMap; use std::path::Path; use std::path::PathBuf; use std::sync::Arc; @@ -214,9 +214,9 @@ impl NpmPackageResolver { /// Gets if the provided specifier is in an npm package. pub fn in_npm_package(&self, specifier: &ModuleSpecifier) -> bool { - self - .resolve_package_folder_from_specifier(specifier) - .is_ok() + let root_dir_url = self.fs_resolver.root_dir_url(); + debug_assert!(root_dir_url.as_str().ends_with('/')); + specifier.as_ref().starts_with(root_dir_url.as_str()) } /// If the resolver has resolved any npm packages. @@ -224,6 +224,19 @@ impl NpmPackageResolver { self.resolution.has_packages() } + /// Adds the package reqs from a package.json if they exist. + pub async fn add_package_json_deps( + &self, + maybe_package_json_deps: Option<&HashMap<String, NpmPackageReq>>, + ) -> Result<(), AnyError> { + if let Some(deps) = maybe_package_json_deps { + let mut package_reqs = deps.values().cloned().collect::<Vec<_>>(); + package_reqs.sort(); // deterministic resolution + self.add_package_reqs(package_reqs).await?; + } + Ok(()) + } + /// Adds package requirements to the resolver and ensures everything is setup. pub async fn add_package_reqs( &self, @@ -250,7 +263,7 @@ impl NpmPackageResolver { /// This will retrieve and resolve package information, but not cache any package files. pub async fn set_package_reqs( &self, - packages: HashSet<NpmPackageReq>, + packages: Vec<NpmPackageReq>, ) -> Result<(), AnyError> { self.resolution.set_package_reqs(packages).await } |