2023-04-06 18:46:44 -04:00
|
|
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
|
|
|
|
|
|
|
use std::collections::HashSet;
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
|
|
use deno_core::error::AnyError;
|
|
|
|
use deno_core::parking_lot::Mutex;
|
|
|
|
use deno_core::parking_lot::RwLock;
|
|
|
|
use deno_core::TaskQueue;
|
|
|
|
use deno_lockfile::NpmPackageDependencyLockfileInfo;
|
|
|
|
use deno_lockfile::NpmPackageLockfileInfo;
|
|
|
|
use deno_npm::registry::NpmPackageInfo;
|
2023-04-06 21:41:19 -04:00
|
|
|
use deno_npm::resolution::NpmPackageVersionResolutionError;
|
2023-04-06 18:46:44 -04:00
|
|
|
use deno_npm::resolution::NpmPackagesPartitioned;
|
2023-04-06 21:41:19 -04:00
|
|
|
use deno_npm::resolution::NpmResolutionError;
|
2023-04-06 18:46:44 -04:00
|
|
|
use deno_npm::resolution::NpmResolutionSnapshot;
|
2023-04-13 10:47:45 -04:00
|
|
|
use deno_npm::resolution::NpmResolutionSnapshotCreateOptions;
|
2023-04-06 21:41:19 -04:00
|
|
|
use deno_npm::resolution::PackageNotFoundFromReferrerError;
|
|
|
|
use deno_npm::resolution::PackageNvNotFoundError;
|
|
|
|
use deno_npm::resolution::PackageReqNotFoundError;
|
2023-04-13 10:47:45 -04:00
|
|
|
use deno_npm::resolution::SerializedNpmResolutionSnapshot;
|
|
|
|
use deno_npm::resolution::ValidSerializedNpmResolutionSnapshot;
|
2023-04-06 18:46:44 -04:00
|
|
|
use deno_npm::NpmPackageCacheFolderId;
|
|
|
|
use deno_npm::NpmPackageId;
|
|
|
|
use deno_npm::NpmResolutionPackage;
|
|
|
|
use deno_semver::npm::NpmPackageNv;
|
|
|
|
use deno_semver::npm::NpmPackageNvReference;
|
|
|
|
use deno_semver::npm::NpmPackageReq;
|
|
|
|
use deno_semver::npm::NpmPackageReqReference;
|
2023-04-13 10:47:45 -04:00
|
|
|
use deno_semver::VersionReq;
|
2023-04-06 18:46:44 -04:00
|
|
|
|
|
|
|
use crate::args::Lockfile;
|
|
|
|
|
2023-04-12 08:36:11 -04:00
|
|
|
use super::registry::CliNpmRegistryApi;
|
2023-04-06 18:46:44 -04:00
|
|
|
|
|
|
|
/// Handles updating and storing npm resolution in memory where the underlying
|
|
|
|
/// snapshot can be updated concurrently. Additionally handles updating the lockfile
|
|
|
|
/// based on changes to the resolution.
|
|
|
|
///
|
|
|
|
/// This does not interact with the file system.
|
2023-04-14 16:22:33 -04:00
|
|
|
pub struct NpmResolution {
|
|
|
|
api: Arc<CliNpmRegistryApi>,
|
2023-04-06 18:46:44 -04:00
|
|
|
snapshot: RwLock<NpmResolutionSnapshot>,
|
|
|
|
update_queue: TaskQueue,
|
|
|
|
maybe_lockfile: Option<Arc<Mutex<Lockfile>>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl std::fmt::Debug for NpmResolution {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
2023-04-14 16:22:33 -04:00
|
|
|
let snapshot = self.snapshot.read();
|
2023-04-06 18:46:44 -04:00
|
|
|
f.debug_struct("NpmResolution")
|
2023-04-13 10:47:45 -04:00
|
|
|
.field("snapshot", &snapshot.as_serialized())
|
2023-04-06 18:46:44 -04:00
|
|
|
.finish()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl NpmResolution {
|
2023-04-13 10:47:45 -04:00
|
|
|
pub fn from_serialized(
|
2023-04-14 16:22:33 -04:00
|
|
|
api: Arc<CliNpmRegistryApi>,
|
2023-04-13 10:47:45 -04:00
|
|
|
initial_snapshot: Option<ValidSerializedNpmResolutionSnapshot>,
|
|
|
|
maybe_lockfile: Option<Arc<Mutex<Lockfile>>>,
|
|
|
|
) -> Self {
|
|
|
|
let snapshot =
|
|
|
|
NpmResolutionSnapshot::new(NpmResolutionSnapshotCreateOptions {
|
2023-04-14 16:22:33 -04:00
|
|
|
api: api.clone(),
|
2023-04-13 10:47:45 -04:00
|
|
|
snapshot: initial_snapshot.unwrap_or_default(),
|
|
|
|
// WARNING: When bumping this version, check if anything needs to be
|
|
|
|
// updated in the `setNodeOnlyGlobalNames` call in 99_main_compiler.js
|
|
|
|
types_node_version_req: Some(
|
|
|
|
VersionReq::parse_from_npm("18.0.0 - 18.11.18").unwrap(),
|
|
|
|
),
|
|
|
|
});
|
|
|
|
Self::new(api, snapshot, maybe_lockfile)
|
|
|
|
}
|
|
|
|
|
2023-04-06 18:46:44 -04:00
|
|
|
pub fn new(
|
2023-04-14 16:22:33 -04:00
|
|
|
api: Arc<CliNpmRegistryApi>,
|
2023-04-13 10:47:45 -04:00
|
|
|
initial_snapshot: NpmResolutionSnapshot,
|
2023-04-06 18:46:44 -04:00
|
|
|
maybe_lockfile: Option<Arc<Mutex<Lockfile>>>,
|
|
|
|
) -> Self {
|
2023-04-14 16:22:33 -04:00
|
|
|
Self {
|
2023-04-06 18:46:44 -04:00
|
|
|
api,
|
2023-04-13 10:47:45 -04:00
|
|
|
snapshot: RwLock::new(initial_snapshot),
|
2023-04-06 18:46:44 -04:00
|
|
|
update_queue: Default::default(),
|
|
|
|
maybe_lockfile,
|
2023-04-14 16:22:33 -04:00
|
|
|
}
|
2023-04-06 18:46:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn add_package_reqs(
|
|
|
|
&self,
|
|
|
|
package_reqs: Vec<NpmPackageReq>,
|
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
// only allow one thread in here at a time
|
2023-04-14 16:22:33 -04:00
|
|
|
let _permit = self.update_queue.acquire().await;
|
2023-04-06 18:46:44 -04:00
|
|
|
let snapshot = add_package_reqs_to_snapshot(
|
2023-04-14 16:22:33 -04:00
|
|
|
&self.api,
|
2023-04-06 18:46:44 -04:00
|
|
|
package_reqs,
|
2023-04-14 16:22:33 -04:00
|
|
|
self.maybe_lockfile.clone(),
|
|
|
|
|| self.snapshot.read().clone(),
|
2023-04-06 18:46:44 -04:00
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
|
2023-04-14 16:22:33 -04:00
|
|
|
*self.snapshot.write() = snapshot;
|
2023-04-06 18:46:44 -04:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn set_package_reqs(
|
|
|
|
&self,
|
|
|
|
package_reqs: Vec<NpmPackageReq>,
|
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
// only allow one thread in here at a time
|
2023-04-14 16:22:33 -04:00
|
|
|
let _permit = self.update_queue.acquire().await;
|
2023-04-06 21:41:19 -04:00
|
|
|
|
|
|
|
let reqs_set = package_reqs.iter().cloned().collect::<HashSet<_>>();
|
2023-04-06 18:46:44 -04:00
|
|
|
let snapshot = add_package_reqs_to_snapshot(
|
2023-04-14 16:22:33 -04:00
|
|
|
&self.api,
|
2023-04-06 18:46:44 -04:00
|
|
|
package_reqs,
|
2023-04-14 16:22:33 -04:00
|
|
|
self.maybe_lockfile.clone(),
|
2023-04-06 21:41:19 -04:00
|
|
|
|| {
|
2023-04-14 16:22:33 -04:00
|
|
|
let snapshot = self.snapshot.read().clone();
|
2023-04-06 21:41:19 -04:00
|
|
|
let has_removed_package = !snapshot
|
|
|
|
.package_reqs()
|
|
|
|
.keys()
|
|
|
|
.all(|req| reqs_set.contains(req));
|
|
|
|
// if any packages were removed, we need to completely recreate the npm resolution snapshot
|
|
|
|
if has_removed_package {
|
2023-04-13 10:47:45 -04:00
|
|
|
snapshot.into_empty()
|
2023-04-06 21:41:19 -04:00
|
|
|
} else {
|
|
|
|
snapshot
|
|
|
|
}
|
|
|
|
},
|
2023-04-06 18:46:44 -04:00
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
|
2023-04-14 16:22:33 -04:00
|
|
|
*self.snapshot.write() = snapshot;
|
2023-04-06 18:46:44 -04:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn resolve_pending(&self) -> Result<(), AnyError> {
|
|
|
|
// only allow one thread in here at a time
|
2023-04-14 16:22:33 -04:00
|
|
|
let _permit = self.update_queue.acquire().await;
|
2023-04-06 18:46:44 -04:00
|
|
|
|
|
|
|
let snapshot = add_package_reqs_to_snapshot(
|
2023-04-14 16:22:33 -04:00
|
|
|
&self.api,
|
2023-04-06 18:46:44 -04:00
|
|
|
Vec::new(),
|
2023-04-14 16:22:33 -04:00
|
|
|
self.maybe_lockfile.clone(),
|
|
|
|
|| self.snapshot.read().clone(),
|
2023-04-06 18:46:44 -04:00
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
|
2023-04-14 16:22:33 -04:00
|
|
|
*self.snapshot.write() = snapshot;
|
2023-04-06 18:46:44 -04:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-04-21 21:02:46 -04:00
|
|
|
pub fn resolve_nv_ref_from_pkg_req_ref(
|
2023-04-06 18:46:44 -04:00
|
|
|
&self,
|
2023-04-17 15:36:23 -04:00
|
|
|
req_ref: &NpmPackageReqReference,
|
2023-04-06 21:41:19 -04:00
|
|
|
) -> Result<NpmPackageNvReference, PackageReqNotFoundError> {
|
2023-04-06 18:46:44 -04:00
|
|
|
let node_id = self.resolve_pkg_id_from_pkg_req(&req_ref.req)?;
|
|
|
|
Ok(NpmPackageNvReference {
|
|
|
|
nv: node_id.nv,
|
2023-04-17 15:36:23 -04:00
|
|
|
sub_path: req_ref.sub_path.clone(),
|
2023-04-06 18:46:44 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn resolve_package_cache_folder_id_from_id(
|
|
|
|
&self,
|
|
|
|
id: &NpmPackageId,
|
|
|
|
) -> Option<NpmPackageCacheFolderId> {
|
|
|
|
self
|
|
|
|
.snapshot
|
|
|
|
.read()
|
|
|
|
.package_from_id(id)
|
|
|
|
.map(|p| p.get_package_cache_folder_id())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn resolve_package_from_package(
|
|
|
|
&self,
|
|
|
|
name: &str,
|
|
|
|
referrer: &NpmPackageCacheFolderId,
|
2023-04-06 21:41:19 -04:00
|
|
|
) -> Result<NpmResolutionPackage, Box<PackageNotFoundFromReferrerError>> {
|
2023-04-06 18:46:44 -04:00
|
|
|
self
|
|
|
|
.snapshot
|
|
|
|
.read()
|
|
|
|
.resolve_package_from_package(name, referrer)
|
|
|
|
.cloned()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Resolve a node package from a deno module.
|
|
|
|
pub fn resolve_pkg_id_from_pkg_req(
|
|
|
|
&self,
|
|
|
|
req: &NpmPackageReq,
|
2023-04-06 21:41:19 -04:00
|
|
|
) -> Result<NpmPackageId, PackageReqNotFoundError> {
|
2023-04-06 18:46:44 -04:00
|
|
|
self
|
|
|
|
.snapshot
|
|
|
|
.read()
|
|
|
|
.resolve_pkg_from_pkg_req(req)
|
|
|
|
.map(|pkg| pkg.pkg_id.clone())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn resolve_pkg_id_from_deno_module(
|
|
|
|
&self,
|
|
|
|
id: &NpmPackageNv,
|
2023-04-06 21:41:19 -04:00
|
|
|
) -> Result<NpmPackageId, PackageNvNotFoundError> {
|
2023-04-06 18:46:44 -04:00
|
|
|
self
|
|
|
|
.snapshot
|
|
|
|
.read()
|
|
|
|
.resolve_package_from_deno_module(id)
|
|
|
|
.map(|pkg| pkg.pkg_id.clone())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Resolves a package requirement for deno graph. This should only be
|
|
|
|
/// called by deno_graph's NpmResolver or for resolving packages in
|
|
|
|
/// a package.json
|
|
|
|
pub fn resolve_package_req_as_pending(
|
|
|
|
&self,
|
|
|
|
pkg_req: &NpmPackageReq,
|
2023-04-06 21:41:19 -04:00
|
|
|
) -> Result<NpmPackageNv, NpmPackageVersionResolutionError> {
|
2023-04-06 18:46:44 -04:00
|
|
|
// we should always have this because it should have been cached before here
|
2023-04-14 16:22:33 -04:00
|
|
|
let package_info = self.api.get_cached_package_info(&pkg_req.name).unwrap();
|
2023-04-06 18:46:44 -04:00
|
|
|
self.resolve_package_req_as_pending_with_info(pkg_req, &package_info)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Resolves a package requirement for deno graph. This should only be
|
|
|
|
/// called by deno_graph's NpmResolver or for resolving packages in
|
|
|
|
/// a package.json
|
|
|
|
pub fn resolve_package_req_as_pending_with_info(
|
|
|
|
&self,
|
|
|
|
pkg_req: &NpmPackageReq,
|
|
|
|
package_info: &NpmPackageInfo,
|
2023-04-06 21:41:19 -04:00
|
|
|
) -> Result<NpmPackageNv, NpmPackageVersionResolutionError> {
|
2023-04-06 18:46:44 -04:00
|
|
|
debug_assert_eq!(pkg_req.name, package_info.name);
|
2023-04-14 16:22:33 -04:00
|
|
|
let mut snapshot = self.snapshot.write();
|
2023-04-06 18:46:44 -04:00
|
|
|
let nv = snapshot.resolve_package_req_as_pending(pkg_req, package_info)?;
|
|
|
|
Ok(nv)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn all_packages_partitioned(&self) -> NpmPackagesPartitioned {
|
2023-04-14 16:22:33 -04:00
|
|
|
self.snapshot.read().all_packages_partitioned()
|
2023-04-06 18:46:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn has_packages(&self) -> bool {
|
2023-04-14 16:22:33 -04:00
|
|
|
!self.snapshot.read().is_empty()
|
2023-04-06 18:46:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn snapshot(&self) -> NpmResolutionSnapshot {
|
2023-04-14 16:22:33 -04:00
|
|
|
self.snapshot.read().clone()
|
2023-04-06 18:46:44 -04:00
|
|
|
}
|
|
|
|
|
2023-04-13 10:47:45 -04:00
|
|
|
pub fn serialized_snapshot(&self) -> SerializedNpmResolutionSnapshot {
|
2023-04-14 16:22:33 -04:00
|
|
|
self.snapshot.read().as_serialized()
|
2023-04-13 10:47:45 -04:00
|
|
|
}
|
|
|
|
|
2023-04-06 18:46:44 -04:00
|
|
|
pub fn lock(&self, lockfile: &mut Lockfile) -> Result<(), AnyError> {
|
2023-04-14 16:22:33 -04:00
|
|
|
let snapshot = self.snapshot.read();
|
2023-04-06 18:46:44 -04:00
|
|
|
populate_lockfile_from_snapshot(lockfile, &snapshot)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn add_package_reqs_to_snapshot(
|
2023-04-12 08:36:11 -04:00
|
|
|
api: &CliNpmRegistryApi,
|
2023-04-06 21:41:19 -04:00
|
|
|
// todo(18079): it should be possible to pass &[NpmPackageReq] in here
|
|
|
|
// and avoid all these clones, but the LSP complains because of its
|
|
|
|
// `Send` requirement
|
2023-04-06 18:46:44 -04:00
|
|
|
package_reqs: Vec<NpmPackageReq>,
|
|
|
|
maybe_lockfile: Option<Arc<Mutex<Lockfile>>>,
|
2023-04-06 21:41:19 -04:00
|
|
|
get_new_snapshot: impl Fn() -> NpmResolutionSnapshot,
|
2023-04-06 18:46:44 -04:00
|
|
|
) -> Result<NpmResolutionSnapshot, AnyError> {
|
2023-04-06 21:41:19 -04:00
|
|
|
let snapshot = get_new_snapshot();
|
2023-04-06 18:46:44 -04:00
|
|
|
if !snapshot.has_pending()
|
|
|
|
&& package_reqs
|
|
|
|
.iter()
|
|
|
|
.all(|req| snapshot.package_reqs().contains_key(req))
|
|
|
|
{
|
|
|
|
return Ok(snapshot); // already up to date
|
|
|
|
}
|
|
|
|
|
2023-04-13 10:47:45 -04:00
|
|
|
let result = snapshot.resolve_pending(package_reqs.clone()).await;
|
2023-04-06 18:46:44 -04:00
|
|
|
api.clear_memory_cache();
|
2023-04-06 21:41:19 -04:00
|
|
|
let snapshot = match result {
|
|
|
|
Ok(snapshot) => snapshot,
|
|
|
|
Err(NpmResolutionError::Resolution(err)) if api.mark_force_reload() => {
|
|
|
|
log::debug!("{err:#}");
|
|
|
|
log::debug!("npm resolution failed. Trying again...");
|
|
|
|
|
|
|
|
// try again
|
|
|
|
let snapshot = get_new_snapshot();
|
2023-04-13 10:47:45 -04:00
|
|
|
let result = snapshot.resolve_pending(package_reqs).await;
|
2023-04-06 21:41:19 -04:00
|
|
|
api.clear_memory_cache();
|
|
|
|
// now surface the result after clearing the cache
|
|
|
|
result?
|
|
|
|
}
|
|
|
|
Err(err) => return Err(err.into()),
|
|
|
|
};
|
2023-04-06 18:46:44 -04:00
|
|
|
|
|
|
|
if let Some(lockfile_mutex) = maybe_lockfile {
|
|
|
|
let mut lockfile = lockfile_mutex.lock();
|
|
|
|
populate_lockfile_from_snapshot(&mut lockfile, &snapshot)?;
|
|
|
|
Ok(snapshot)
|
|
|
|
} else {
|
|
|
|
Ok(snapshot)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn populate_lockfile_from_snapshot(
|
|
|
|
lockfile: &mut Lockfile,
|
|
|
|
snapshot: &NpmResolutionSnapshot,
|
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
for (package_req, nv) in snapshot.package_reqs() {
|
|
|
|
lockfile.insert_npm_specifier(
|
|
|
|
package_req.to_string(),
|
|
|
|
snapshot
|
|
|
|
.resolve_package_from_deno_module(nv)
|
|
|
|
.unwrap()
|
|
|
|
.pkg_id
|
|
|
|
.as_serialized(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
for package in snapshot.all_packages() {
|
|
|
|
lockfile
|
|
|
|
.check_or_insert_npm_package(npm_package_to_lockfile_info(package))?;
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn npm_package_to_lockfile_info(
|
|
|
|
pkg: NpmResolutionPackage,
|
|
|
|
) -> NpmPackageLockfileInfo {
|
|
|
|
let dependencies = pkg
|
|
|
|
.dependencies
|
|
|
|
.into_iter()
|
|
|
|
.map(|(name, id)| NpmPackageDependencyLockfileInfo {
|
|
|
|
name,
|
|
|
|
id: id.as_serialized(),
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
NpmPackageLockfileInfo {
|
|
|
|
display_id: pkg.pkg_id.nv.to_string(),
|
|
|
|
serialized_id: pkg.pkg_id.as_serialized(),
|
|
|
|
integrity: pkg.dist.integrity().to_string(),
|
|
|
|
dependencies,
|
|
|
|
}
|
|
|
|
}
|