From bc8a0e6e68547cf07a246b8b6c886de155dc8282 Mon Sep 17 00:00:00 2001 From: Nathan Whitaker <17734409+nathanwhit@users.noreply.github.com> Date: Fri, 28 Jun 2024 17:18:21 -0700 Subject: refactor(cli): Create wrapper around `deno_lockfile::Lockfile` (#24366) As suggested in https://github.com/denoland/deno/pull/24355#discussion_r1657875422. I wasn't able to hide the mutex stuff as much as I'd like (ended up just adding an escape hatch `inner()` method that locks the inner mutex), because you can't return references to the inner fields through a mutex. This is mostly motivated by the frozen lockfile changes --- cli/npm/managed/mod.rs | 22 +++++++++------------- cli/npm/managed/resolution.rs | 19 +++++++++---------- 2 files changed, 18 insertions(+), 23 deletions(-) (limited to 'cli/npm') diff --git a/cli/npm/managed/mod.rs b/cli/npm/managed/mod.rs index c086235c3..f0fc0f7f7 100644 --- a/cli/npm/managed/mod.rs +++ b/cli/npm/managed/mod.rs @@ -9,7 +9,6 @@ use cache::TarballCache; use deno_ast::ModuleSpecifier; use deno_core::anyhow::Context; use deno_core::error::AnyError; -use deno_core::parking_lot::Mutex; use deno_core::serde_json; use deno_npm::npm_rc::ResolvedNpmRc; use deno_npm::registry::NpmPackageInfo; @@ -27,7 +26,7 @@ use deno_semver::package::PackageNv; use deno_semver::package::PackageReq; use resolution::AddPkgReqsResult; -use crate::args::Lockfile; +use crate::args::CliLockfile; use crate::args::NpmProcessState; use crate::args::NpmProcessStateKind; use crate::args::PackageJsonDepsProvider; @@ -53,13 +52,13 @@ mod resolution; mod resolvers; pub enum CliNpmResolverManagedSnapshotOption { - ResolveFromLockfile(Arc>), + ResolveFromLockfile(Arc), Specified(Option), } pub struct CliNpmResolverManagedCreateOptions { pub snapshot: CliNpmResolverManagedSnapshotOption, - pub maybe_lockfile: Option>>, + pub maybe_lockfile: Option>, pub fs: Arc, pub http_client_provider: Arc, pub npm_global_cache_dir: PathBuf, @@ -128,7 +127,7 @@ pub async fn create_managed_npm_resolver( fn create_inner( fs: Arc, http_client_provider: Arc, - maybe_lockfile: Option>>, + maybe_lockfile: Option>, npm_api: Arc, npm_cache: Arc, npm_rc: Arc, @@ -205,14 +204,11 @@ async fn resolve_snapshot( ) -> Result, AnyError> { match snapshot { CliNpmResolverManagedSnapshotOption::ResolveFromLockfile(lockfile) => { - if !lockfile.lock().overwrite { + if !lockfile.overwrite() { let snapshot = snapshot_from_lockfile(lockfile.clone(), api) .await .with_context(|| { - format!( - "failed reading lockfile '{}'", - lockfile.lock().filename.display() - ) + format!("failed reading lockfile '{}'", lockfile.filename.display()) })?; Ok(Some(snapshot)) } else { @@ -224,7 +220,7 @@ async fn resolve_snapshot( } async fn snapshot_from_lockfile( - lockfile: Arc>, + lockfile: Arc, api: &dyn NpmRegistryApi, ) -> Result { let (incomplete_snapshot, skip_integrity_check) = { @@ -250,7 +246,7 @@ async fn snapshot_from_lockfile( pub struct ManagedCliNpmResolver { fs: Arc, fs_resolver: Arc, - maybe_lockfile: Option>>, + maybe_lockfile: Option>, npm_api: Arc, npm_cache: Arc, package_json_deps_provider: Arc, @@ -274,7 +270,7 @@ impl ManagedCliNpmResolver { pub fn new( fs: Arc, fs_resolver: Arc, - maybe_lockfile: Option>>, + maybe_lockfile: Option>, npm_api: Arc, npm_cache: Arc, package_json_deps_provider: Arc, diff --git a/cli/npm/managed/resolution.rs b/cli/npm/managed/resolution.rs index c1d31325d..3d13e1735 100644 --- a/cli/npm/managed/resolution.rs +++ b/cli/npm/managed/resolution.rs @@ -5,7 +5,6 @@ use std::collections::HashSet; use std::sync::Arc; use deno_core::error::AnyError; -use deno_core::parking_lot::Mutex; use deno_lockfile::NpmPackageDependencyLockfileInfo; use deno_lockfile::NpmPackageLockfileInfo; use deno_npm::registry::NpmRegistryApi; @@ -27,7 +26,7 @@ use deno_semver::package::PackageNv; use deno_semver::package::PackageReq; use deno_semver::VersionReq; -use crate::args::Lockfile; +use crate::args::CliLockfile; use crate::util::sync::SyncReadAsyncWriteLock; use super::CliNpmRegistryApi; @@ -50,7 +49,7 @@ pub struct AddPkgReqsResult { pub struct NpmResolution { api: Arc, snapshot: SyncReadAsyncWriteLock, - maybe_lockfile: Option>>, + maybe_lockfile: Option>, } impl std::fmt::Debug for NpmResolution { @@ -66,7 +65,7 @@ impl NpmResolution { pub fn from_serialized( api: Arc, initial_snapshot: Option, - maybe_lockfile: Option>>, + maybe_lockfile: Option>, ) -> Self { let snapshot = NpmResolutionSnapshot::new(initial_snapshot.unwrap_or_default()); @@ -76,7 +75,7 @@ impl NpmResolution { pub fn new( api: Arc, initial_snapshot: NpmResolutionSnapshot, - maybe_lockfile: Option>>, + maybe_lockfile: Option>, ) -> Self { Self { api, @@ -262,7 +261,7 @@ impl NpmResolution { async fn add_package_reqs_to_snapshot( api: &CliNpmRegistryApi, package_reqs: &[PackageReq], - maybe_lockfile: Option>>, + maybe_lockfile: Option>, get_new_snapshot: impl Fn() -> NpmResolutionSnapshot, ) -> deno_npm::resolution::AddPkgReqsResult { let snapshot = get_new_snapshot(); @@ -301,9 +300,8 @@ async fn add_package_reqs_to_snapshot( }; if let Ok(snapshot) = &result.dep_graph_result { - if let Some(lockfile_mutex) = maybe_lockfile { - let mut lockfile = lockfile_mutex.lock(); - populate_lockfile_from_snapshot(&mut lockfile, snapshot); + if let Some(lockfile) = maybe_lockfile { + populate_lockfile_from_snapshot(&lockfile, snapshot); } } @@ -326,9 +324,10 @@ fn get_npm_pending_resolver( } fn populate_lockfile_from_snapshot( - lockfile: &mut Lockfile, + lockfile: &CliLockfile, snapshot: &NpmResolutionSnapshot, ) { + let mut lockfile = lockfile.lock(); for (package_req, nv) in snapshot.package_reqs() { lockfile.insert_package_specifier( format!("npm:{}", package_req), -- cgit v1.2.3