fix(npm): do not "npm install" when npm specifier happens to match package.json entry (#18660)

This commit is contained in:
David Sherret 2023-04-11 18:10:51 -04:00 committed by GitHub
parent 805214626f
commit 8820f6e922
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 92 additions and 41 deletions

View file

@ -1,6 +1,5 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
use std::sync::atomic::AtomicBool;
use std::sync::Arc;
use deno_core::error::AnyError;
@ -9,13 +8,14 @@ use deno_core::futures::StreamExt;
use deno_npm::registry::NpmRegistryApi;
use crate::args::package_json::PackageJsonDeps;
use crate::util::sync::AtomicFlag;
use super::NpmRegistry;
use super::NpmResolution;
#[derive(Debug)]
struct PackageJsonDepsInstallerInner {
has_installed: AtomicBool,
has_installed_flag: AtomicFlag,
npm_registry_api: NpmRegistry,
npm_resolution: NpmResolution,
package_deps: PackageJsonDeps,
@ -33,7 +33,7 @@ impl PackageJsonDepsInstaller {
) -> Self {
Self(deps.map(|package_deps| {
Arc::new(PackageJsonDepsInstallerInner {
has_installed: AtomicBool::new(false),
has_installed_flag: Default::default(),
npm_registry_api,
npm_resolution,
package_deps,
@ -45,30 +45,15 @@ impl PackageJsonDepsInstaller {
self.0.as_ref().map(|inner| &inner.package_deps)
}
/// Gets if the package.json has the specified package name.
pub fn has_package_name(&self, name: &str) -> bool {
if let Some(package_deps) = self.package_deps() {
// ensure this looks at the package name and not the
// bare specifiers (do not look at the keys!)
package_deps
.values()
.filter_map(|r| r.as_ref().ok())
.any(|v| v.name == name)
} else {
false
}
}
/// Installs the top level dependencies in the package.json file
/// without going through and resolving the descendant dependencies yet.
pub async fn ensure_top_level_install(&self) -> Result<(), AnyError> {
use std::sync::atomic::Ordering;
let inner = match &self.0 {
Some(inner) => inner,
None => return Ok(()),
};
if inner.has_installed.swap(true, Ordering::SeqCst) {
if !inner.has_installed_flag.raise() {
return Ok(()); // already installed by something else
}

View file

@ -5,8 +5,6 @@ use std::collections::HashSet;
use std::fs;
use std::io::ErrorKind;
use std::path::PathBuf;
use std::sync::atomic::AtomicBool;
use std::sync::atomic::Ordering;
use std::sync::Arc;
use async_trait::async_trait;
@ -31,6 +29,7 @@ use crate::cache::CACHE_PERM;
use crate::http_util::HttpClient;
use crate::util::fs::atomic_write_file;
use crate::util::progress_bar::ProgressBar;
use crate::util::sync::AtomicFlag;
use super::cache::should_sync_download;
use super::cache::NpmCache;
@ -70,7 +69,7 @@ impl NpmRegistry {
Self(Some(Arc::new(NpmRegistryApiInner {
base_url,
cache,
force_reload: AtomicBool::new(false),
force_reload_flag: Default::default(),
mem_cache: Default::default(),
previously_reloaded_packages: Default::default(),
http_client,
@ -109,7 +108,7 @@ impl NpmRegistry {
if matches!(self.inner().cache.cache_setting(), CacheSetting::Only) {
return false;
}
!self.inner().force_reload.swap(true, Ordering::SeqCst)
self.inner().force_reload_flag.raise()
}
fn inner(&self) -> &Arc<NpmRegistryApiInner> {
@ -160,7 +159,7 @@ enum CacheItem {
struct NpmRegistryApiInner {
base_url: Url,
cache: NpmCache,
force_reload: AtomicBool,
force_reload_flag: AtomicFlag,
mem_cache: Mutex<HashMap<String, CacheItem>>,
previously_reloaded_packages: Mutex<HashSet<String>>,
http_client: HttpClient,
@ -230,7 +229,7 @@ impl NpmRegistryApiInner {
}
fn force_reload(&self) -> bool {
self.force_reload.load(Ordering::SeqCst)
self.force_reload_flag.is_raised()
}
fn load_file_cached_package_info(