2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2022-09-13 11:59:01 -04:00
|
|
|
|
2023-04-06 18:46:44 -04:00
|
|
|
use std::path::Path;
|
|
|
|
use std::path::PathBuf;
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
2024-06-02 21:39:13 -04:00
|
|
|
use cache::RegistryInfoDownloader;
|
2024-06-03 17:17:08 -04:00
|
|
|
use cache::TarballCache;
|
2022-09-22 11:17:02 -04:00
|
|
|
use deno_ast::ModuleSpecifier;
|
2023-10-02 17:53:55 -04:00
|
|
|
use deno_core::anyhow::Context;
|
2022-09-22 11:17:02 -04:00
|
|
|
use deno_core::error::AnyError;
|
2022-09-28 13:04:16 -04:00
|
|
|
use deno_core::serde_json;
|
2024-05-23 17:26:23 -04:00
|
|
|
use deno_npm::npm_rc::ResolvedNpmRc;
|
2024-06-05 11:04:16 -04:00
|
|
|
use deno_npm::registry::NpmPackageInfo;
|
2023-09-30 12:06:38 -04:00
|
|
|
use deno_npm::registry::NpmRegistryApi;
|
2023-04-06 18:46:44 -04:00
|
|
|
use deno_npm::resolution::NpmResolutionSnapshot;
|
2023-04-06 21:41:19 -04:00
|
|
|
use deno_npm::resolution::PackageReqNotFoundError;
|
2023-10-02 17:53:55 -04:00
|
|
|
use deno_npm::resolution::ValidSerializedNpmResolutionSnapshot;
|
2023-04-06 18:46:44 -04:00
|
|
|
use deno_npm::NpmPackageId;
|
2023-09-30 12:06:38 -04:00
|
|
|
use deno_npm::NpmResolutionPackage;
|
2023-05-17 17:38:50 -04:00
|
|
|
use deno_npm::NpmSystemInfo;
|
2023-05-10 20:06:59 -04:00
|
|
|
use deno_runtime::deno_fs::FileSystem;
|
2023-01-10 08:35:44 -05:00
|
|
|
use deno_runtime::deno_node::NodePermissions;
|
2024-07-25 19:08:14 -04:00
|
|
|
use deno_runtime::deno_node::NodeRequireResolver;
|
|
|
|
use deno_runtime::deno_node::NpmProcessStateProvider;
|
2023-08-21 05:53:52 -04:00
|
|
|
use deno_semver::package::PackageNv;
|
|
|
|
use deno_semver::package::PackageReq;
|
2024-07-25 19:08:14 -04:00
|
|
|
use node_resolver::errors::PackageFolderResolveError;
|
|
|
|
use node_resolver::errors::PackageFolderResolveIoError;
|
|
|
|
use node_resolver::NpmResolver;
|
2024-06-11 08:55:12 -04:00
|
|
|
use resolution::AddPkgReqsResult;
|
2022-09-13 11:59:01 -04:00
|
|
|
|
2024-06-28 20:18:21 -04:00
|
|
|
use crate::args::CliLockfile;
|
2024-07-09 23:06:08 -04:00
|
|
|
use crate::args::LifecycleScriptsConfig;
|
2024-09-04 10:00:44 -04:00
|
|
|
use crate::args::NpmInstallDepsProvider;
|
2023-10-02 17:53:55 -04:00
|
|
|
use crate::args::NpmProcessState;
|
2023-10-25 14:39:00 -04:00
|
|
|
use crate::args::NpmProcessStateKind;
|
2023-10-03 19:05:06 -04:00
|
|
|
use crate::cache::FastInsecureHasher;
|
2024-06-03 17:17:08 -04:00
|
|
|
use crate::http_util::HttpClientProvider;
|
2023-05-10 20:06:59 -04:00
|
|
|
use crate::util::fs::canonicalize_path_maybe_not_exists_with_fs;
|
2023-10-02 17:53:55 -04:00
|
|
|
use crate::util::progress_bar::ProgressBar;
|
2024-06-11 08:55:12 -04:00
|
|
|
use crate::util::sync::AtomicFlag;
|
2023-10-02 17:53:55 -04:00
|
|
|
|
|
|
|
use self::cache::NpmCache;
|
|
|
|
use self::registry::CliNpmRegistryApi;
|
|
|
|
use self::resolution::NpmResolution;
|
|
|
|
use self::resolvers::create_npm_fs_resolver;
|
2024-09-04 10:00:44 -04:00
|
|
|
pub use self::resolvers::normalize_pkg_name_for_node_modules_deno_folder;
|
2023-10-02 17:53:55 -04:00
|
|
|
use self::resolvers::NpmPackageFsResolver;
|
2022-09-13 11:59:01 -04:00
|
|
|
|
2023-09-30 12:06:38 -04:00
|
|
|
use super::CliNpmResolver;
|
|
|
|
use super::InnerCliNpmResolverRef;
|
2023-10-02 17:53:55 -04:00
|
|
|
use super::NpmCacheDir;
|
2023-09-30 12:06:38 -04:00
|
|
|
|
2023-10-02 17:53:55 -04:00
|
|
|
mod cache;
|
|
|
|
mod registry;
|
2023-09-30 12:06:38 -04:00
|
|
|
mod resolution;
|
|
|
|
mod resolvers;
|
2023-10-02 17:53:55 -04:00
|
|
|
|
|
|
|
pub enum CliNpmResolverManagedSnapshotOption {
|
2024-06-28 20:18:21 -04:00
|
|
|
ResolveFromLockfile(Arc<CliLockfile>),
|
2023-10-02 17:53:55 -04:00
|
|
|
Specified(Option<ValidSerializedNpmResolutionSnapshot>),
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct CliNpmResolverManagedCreateOptions {
|
|
|
|
pub snapshot: CliNpmResolverManagedSnapshotOption,
|
2024-06-28 20:18:21 -04:00
|
|
|
pub maybe_lockfile: Option<Arc<CliLockfile>>,
|
2023-10-02 17:53:55 -04:00
|
|
|
pub fs: Arc<dyn deno_runtime::deno_fs::FileSystem>,
|
2024-06-03 17:17:08 -04:00
|
|
|
pub http_client_provider: Arc<crate::http_util::HttpClientProvider>,
|
2023-10-02 17:53:55 -04:00
|
|
|
pub npm_global_cache_dir: PathBuf,
|
|
|
|
pub cache_setting: crate::args::CacheSetting,
|
|
|
|
pub text_only_progress_bar: crate::util::progress_bar::ProgressBar,
|
|
|
|
pub maybe_node_modules_path: Option<PathBuf>,
|
|
|
|
pub npm_system_info: NpmSystemInfo,
|
2024-09-04 10:00:44 -04:00
|
|
|
pub npm_install_deps_provider: Arc<NpmInstallDepsProvider>,
|
2024-05-23 17:26:23 -04:00
|
|
|
pub npmrc: Arc<ResolvedNpmRc>,
|
2024-07-09 23:06:08 -04:00
|
|
|
pub lifecycle_scripts: LifecycleScriptsConfig,
|
2023-10-02 17:53:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn create_managed_npm_resolver_for_lsp(
|
|
|
|
options: CliNpmResolverManagedCreateOptions,
|
|
|
|
) -> Arc<dyn CliNpmResolver> {
|
|
|
|
let npm_cache = create_cache(&options);
|
|
|
|
let npm_api = create_api(&options, npm_cache.clone());
|
2024-06-06 18:37:41 -04:00
|
|
|
// spawn due to the lsp's `Send` requirement
|
|
|
|
deno_core::unsync::spawn(async move {
|
|
|
|
let snapshot = match resolve_snapshot(&npm_api, options.snapshot).await {
|
|
|
|
Ok(snapshot) => snapshot,
|
|
|
|
Err(err) => {
|
|
|
|
log::warn!("failed to resolve snapshot: {}", err);
|
|
|
|
None
|
|
|
|
}
|
|
|
|
};
|
|
|
|
create_inner(
|
|
|
|
options.fs,
|
|
|
|
options.http_client_provider,
|
|
|
|
options.maybe_lockfile,
|
|
|
|
npm_api,
|
|
|
|
npm_cache,
|
|
|
|
options.npmrc,
|
2024-09-04 10:00:44 -04:00
|
|
|
options.npm_install_deps_provider,
|
2024-06-06 18:37:41 -04:00
|
|
|
options.text_only_progress_bar,
|
|
|
|
options.maybe_node_modules_path,
|
|
|
|
options.npm_system_info,
|
|
|
|
snapshot,
|
2024-07-09 23:06:08 -04:00
|
|
|
options.lifecycle_scripts,
|
2024-06-06 18:37:41 -04:00
|
|
|
)
|
|
|
|
})
|
|
|
|
.await
|
|
|
|
.unwrap()
|
2023-10-02 17:53:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn create_managed_npm_resolver(
|
|
|
|
options: CliNpmResolverManagedCreateOptions,
|
|
|
|
) -> Result<Arc<dyn CliNpmResolver>, AnyError> {
|
|
|
|
let npm_cache = create_cache(&options);
|
|
|
|
let npm_api = create_api(&options, npm_cache.clone());
|
|
|
|
let snapshot = resolve_snapshot(&npm_api, options.snapshot).await?;
|
|
|
|
Ok(create_inner(
|
|
|
|
options.fs,
|
2024-06-03 17:17:08 -04:00
|
|
|
options.http_client_provider,
|
2024-06-02 21:39:13 -04:00
|
|
|
options.maybe_lockfile,
|
|
|
|
npm_api,
|
|
|
|
npm_cache,
|
|
|
|
options.npmrc,
|
2024-09-04 10:00:44 -04:00
|
|
|
options.npm_install_deps_provider,
|
2023-10-02 17:53:55 -04:00
|
|
|
options.text_only_progress_bar,
|
|
|
|
options.maybe_node_modules_path,
|
|
|
|
options.npm_system_info,
|
2024-06-02 21:39:13 -04:00
|
|
|
snapshot,
|
2024-07-09 23:06:08 -04:00
|
|
|
options.lifecycle_scripts,
|
2023-10-02 17:53:55 -04:00
|
|
|
))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[allow(clippy::too_many_arguments)]
|
|
|
|
fn create_inner(
|
|
|
|
fs: Arc<dyn deno_runtime::deno_fs::FileSystem>,
|
2024-06-03 17:17:08 -04:00
|
|
|
http_client_provider: Arc<HttpClientProvider>,
|
2024-06-28 20:18:21 -04:00
|
|
|
maybe_lockfile: Option<Arc<CliLockfile>>,
|
2024-06-02 21:39:13 -04:00
|
|
|
npm_api: Arc<CliNpmRegistryApi>,
|
|
|
|
npm_cache: Arc<NpmCache>,
|
|
|
|
npm_rc: Arc<ResolvedNpmRc>,
|
2024-09-04 10:00:44 -04:00
|
|
|
npm_install_deps_provider: Arc<NpmInstallDepsProvider>,
|
2023-10-02 17:53:55 -04:00
|
|
|
text_only_progress_bar: crate::util::progress_bar::ProgressBar,
|
|
|
|
node_modules_dir_path: Option<PathBuf>,
|
|
|
|
npm_system_info: NpmSystemInfo,
|
2024-06-02 21:39:13 -04:00
|
|
|
snapshot: Option<ValidSerializedNpmResolutionSnapshot>,
|
2024-07-09 23:06:08 -04:00
|
|
|
lifecycle_scripts: LifecycleScriptsConfig,
|
2023-10-02 17:53:55 -04:00
|
|
|
) -> Arc<dyn CliNpmResolver> {
|
|
|
|
let resolution = Arc::new(NpmResolution::from_serialized(
|
|
|
|
npm_api.clone(),
|
|
|
|
snapshot,
|
|
|
|
maybe_lockfile.clone(),
|
|
|
|
));
|
2024-06-03 17:17:08 -04:00
|
|
|
let tarball_cache = Arc::new(TarballCache::new(
|
|
|
|
npm_cache.clone(),
|
|
|
|
fs.clone(),
|
|
|
|
http_client_provider.clone(),
|
|
|
|
npm_rc.clone(),
|
|
|
|
text_only_progress_bar.clone(),
|
|
|
|
));
|
2024-06-02 21:39:13 -04:00
|
|
|
let fs_resolver = create_npm_fs_resolver(
|
2023-10-02 17:53:55 -04:00
|
|
|
fs.clone(),
|
|
|
|
npm_cache.clone(),
|
2024-09-04 10:00:44 -04:00
|
|
|
&npm_install_deps_provider,
|
2023-10-02 17:53:55 -04:00
|
|
|
&text_only_progress_bar,
|
|
|
|
resolution.clone(),
|
2024-06-03 17:17:08 -04:00
|
|
|
tarball_cache.clone(),
|
2023-10-02 17:53:55 -04:00
|
|
|
node_modules_dir_path,
|
|
|
|
npm_system_info.clone(),
|
2024-07-09 23:06:08 -04:00
|
|
|
lifecycle_scripts.clone(),
|
2023-10-02 17:53:55 -04:00
|
|
|
);
|
|
|
|
Arc::new(ManagedCliNpmResolver::new(
|
|
|
|
fs,
|
2024-06-02 21:39:13 -04:00
|
|
|
fs_resolver,
|
2023-10-02 17:53:55 -04:00
|
|
|
maybe_lockfile,
|
2024-06-02 21:39:13 -04:00
|
|
|
npm_api,
|
|
|
|
npm_cache,
|
2024-09-04 10:00:44 -04:00
|
|
|
npm_install_deps_provider,
|
2024-06-02 21:39:13 -04:00
|
|
|
resolution,
|
2024-06-03 17:17:08 -04:00
|
|
|
tarball_cache,
|
2023-10-02 17:53:55 -04:00
|
|
|
text_only_progress_bar,
|
|
|
|
npm_system_info,
|
2024-07-09 23:06:08 -04:00
|
|
|
lifecycle_scripts,
|
2023-10-02 17:53:55 -04:00
|
|
|
))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn create_cache(options: &CliNpmResolverManagedCreateOptions) -> Arc<NpmCache> {
|
|
|
|
Arc::new(NpmCache::new(
|
2024-05-23 17:26:23 -04:00
|
|
|
NpmCacheDir::new(
|
|
|
|
options.npm_global_cache_dir.clone(),
|
|
|
|
options.npmrc.get_all_known_registries_urls(),
|
|
|
|
),
|
2023-10-02 17:53:55 -04:00
|
|
|
options.cache_setting.clone(),
|
2024-05-23 17:26:23 -04:00
|
|
|
options.npmrc.clone(),
|
2023-10-02 17:53:55 -04:00
|
|
|
))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn create_api(
|
|
|
|
options: &CliNpmResolverManagedCreateOptions,
|
|
|
|
npm_cache: Arc<NpmCache>,
|
|
|
|
) -> Arc<CliNpmRegistryApi> {
|
|
|
|
Arc::new(CliNpmRegistryApi::new(
|
|
|
|
npm_cache.clone(),
|
2024-06-05 11:04:16 -04:00
|
|
|
Arc::new(RegistryInfoDownloader::new(
|
2024-06-02 21:39:13 -04:00
|
|
|
npm_cache,
|
2024-06-03 17:17:08 -04:00
|
|
|
options.http_client_provider.clone(),
|
2024-06-02 21:39:13 -04:00
|
|
|
options.npmrc.clone(),
|
|
|
|
options.text_only_progress_bar.clone(),
|
2024-06-05 11:04:16 -04:00
|
|
|
)),
|
2023-10-02 17:53:55 -04:00
|
|
|
))
|
|
|
|
}
|
2023-05-19 18:39:27 -04:00
|
|
|
|
2023-10-02 17:53:55 -04:00
|
|
|
async fn resolve_snapshot(
|
|
|
|
api: &CliNpmRegistryApi,
|
|
|
|
snapshot: CliNpmResolverManagedSnapshotOption,
|
|
|
|
) -> Result<Option<ValidSerializedNpmResolutionSnapshot>, AnyError> {
|
|
|
|
match snapshot {
|
|
|
|
CliNpmResolverManagedSnapshotOption::ResolveFromLockfile(lockfile) => {
|
2024-06-28 20:18:21 -04:00
|
|
|
if !lockfile.overwrite() {
|
2023-10-02 17:53:55 -04:00
|
|
|
let snapshot = snapshot_from_lockfile(lockfile.clone(), api)
|
|
|
|
.await
|
|
|
|
.with_context(|| {
|
2024-06-28 20:18:21 -04:00
|
|
|
format!("failed reading lockfile '{}'", lockfile.filename.display())
|
2023-10-02 17:53:55 -04:00
|
|
|
})?;
|
|
|
|
Ok(Some(snapshot))
|
|
|
|
} else {
|
|
|
|
Ok(None)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CliNpmResolverManagedSnapshotOption::Specified(snapshot) => Ok(snapshot),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn snapshot_from_lockfile(
|
2024-06-28 20:18:21 -04:00
|
|
|
lockfile: Arc<CliLockfile>,
|
2023-10-02 17:53:55 -04:00
|
|
|
api: &dyn NpmRegistryApi,
|
|
|
|
) -> Result<ValidSerializedNpmResolutionSnapshot, AnyError> {
|
2024-01-22 16:31:12 -05:00
|
|
|
let (incomplete_snapshot, skip_integrity_check) = {
|
2023-10-02 17:53:55 -04:00
|
|
|
let lock = lockfile.lock();
|
2024-01-22 16:31:12 -05:00
|
|
|
(
|
|
|
|
deno_npm::resolution::incomplete_snapshot_from_lockfile(&lock)?,
|
|
|
|
lock.overwrite,
|
|
|
|
)
|
2023-10-02 17:53:55 -04:00
|
|
|
};
|
2024-01-22 16:31:12 -05:00
|
|
|
let snapshot = deno_npm::resolution::snapshot_from_lockfile(
|
|
|
|
deno_npm::resolution::SnapshotFromLockfileParams {
|
|
|
|
incomplete_snapshot,
|
|
|
|
api,
|
|
|
|
skip_integrity_check,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.await?;
|
2023-10-02 17:53:55 -04:00
|
|
|
Ok(snapshot)
|
2022-09-28 13:04:16 -04:00
|
|
|
}
|
|
|
|
|
2023-09-29 09:26:25 -04:00
|
|
|
/// An npm resolver where the resolution is managed by Deno rather than
|
|
|
|
/// the user bringing their own node_modules (BYONM) on the file system.
|
|
|
|
pub struct ManagedCliNpmResolver {
|
2023-05-10 20:06:59 -04:00
|
|
|
fs: Arc<dyn FileSystem>,
|
2023-02-22 14:15:25 -05:00
|
|
|
fs_resolver: Arc<dyn NpmPackageFsResolver>,
|
2024-06-28 20:18:21 -04:00
|
|
|
maybe_lockfile: Option<Arc<CliLockfile>>,
|
2024-06-02 21:39:13 -04:00
|
|
|
npm_api: Arc<CliNpmRegistryApi>,
|
|
|
|
npm_cache: Arc<NpmCache>,
|
2024-09-04 10:00:44 -04:00
|
|
|
npm_install_deps_provider: Arc<NpmInstallDepsProvider>,
|
2024-06-02 21:39:13 -04:00
|
|
|
resolution: Arc<NpmResolution>,
|
2024-06-03 17:17:08 -04:00
|
|
|
tarball_cache: Arc<TarballCache>,
|
2024-06-02 21:39:13 -04:00
|
|
|
text_only_progress_bar: ProgressBar,
|
|
|
|
npm_system_info: NpmSystemInfo,
|
2024-06-11 08:55:12 -04:00
|
|
|
top_level_install_flag: AtomicFlag,
|
2024-07-09 23:06:08 -04:00
|
|
|
lifecycle_scripts: LifecycleScriptsConfig,
|
2022-10-21 11:20:18 -04:00
|
|
|
}
|
|
|
|
|
2023-09-29 09:26:25 -04:00
|
|
|
impl std::fmt::Debug for ManagedCliNpmResolver {
|
2022-10-21 11:20:18 -04:00
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
2023-09-29 09:26:25 -04:00
|
|
|
f.debug_struct("ManagedNpmResolver")
|
2023-10-02 17:53:55 -04:00
|
|
|
.field("<omitted>", &"<omitted>")
|
2022-10-21 11:20:18 -04:00
|
|
|
.finish()
|
|
|
|
}
|
2022-09-13 11:59:01 -04:00
|
|
|
}
|
|
|
|
|
2023-09-29 09:26:25 -04:00
|
|
|
impl ManagedCliNpmResolver {
|
2023-10-02 17:53:55 -04:00
|
|
|
#[allow(clippy::too_many_arguments)]
|
2023-03-12 23:32:59 -04:00
|
|
|
pub fn new(
|
2023-05-10 20:06:59 -04:00
|
|
|
fs: Arc<dyn FileSystem>,
|
2023-03-12 23:32:59 -04:00
|
|
|
fs_resolver: Arc<dyn NpmPackageFsResolver>,
|
2024-06-28 20:18:21 -04:00
|
|
|
maybe_lockfile: Option<Arc<CliLockfile>>,
|
2024-06-02 21:39:13 -04:00
|
|
|
npm_api: Arc<CliNpmRegistryApi>,
|
|
|
|
npm_cache: Arc<NpmCache>,
|
2024-09-04 10:00:44 -04:00
|
|
|
npm_install_deps_provider: Arc<NpmInstallDepsProvider>,
|
2024-06-02 21:39:13 -04:00
|
|
|
resolution: Arc<NpmResolution>,
|
2024-06-03 17:17:08 -04:00
|
|
|
tarball_cache: Arc<TarballCache>,
|
2024-06-02 21:39:13 -04:00
|
|
|
text_only_progress_bar: ProgressBar,
|
2023-10-02 17:53:55 -04:00
|
|
|
npm_system_info: NpmSystemInfo,
|
2024-07-09 23:06:08 -04:00
|
|
|
lifecycle_scripts: LifecycleScriptsConfig,
|
2022-09-13 11:59:01 -04:00
|
|
|
) -> Self {
|
|
|
|
Self {
|
2023-05-10 20:06:59 -04:00
|
|
|
fs,
|
2023-02-22 14:15:25 -05:00
|
|
|
fs_resolver,
|
2023-01-24 09:05:54 -05:00
|
|
|
maybe_lockfile,
|
2024-06-02 21:39:13 -04:00
|
|
|
npm_api,
|
|
|
|
npm_cache,
|
2024-09-04 10:00:44 -04:00
|
|
|
npm_install_deps_provider,
|
2024-06-02 21:39:13 -04:00
|
|
|
text_only_progress_bar,
|
|
|
|
resolution,
|
2024-06-03 17:17:08 -04:00
|
|
|
tarball_cache,
|
2023-10-02 17:53:55 -04:00
|
|
|
npm_system_info,
|
2024-06-11 08:55:12 -04:00
|
|
|
top_level_install_flag: Default::default(),
|
2024-07-09 23:06:08 -04:00
|
|
|
lifecycle_scripts,
|
2022-09-13 11:59:01 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-10 20:06:59 -04:00
|
|
|
pub fn resolve_pkg_folder_from_pkg_id(
|
2023-02-22 14:15:25 -05:00
|
|
|
&self,
|
2023-02-22 22:45:35 -05:00
|
|
|
pkg_id: &NpmPackageId,
|
2022-09-22 11:17:02 -04:00
|
|
|
) -> Result<PathBuf, AnyError> {
|
2023-04-14 16:22:33 -04:00
|
|
|
let path = self.fs_resolver.package_folder(pkg_id)?;
|
2023-11-29 09:32:23 -05:00
|
|
|
let path =
|
|
|
|
canonicalize_path_maybe_not_exists_with_fs(&path, self.fs.as_ref())?;
|
2022-12-05 20:09:31 -05:00
|
|
|
log::debug!(
|
|
|
|
"Resolved package folder of {} to {}",
|
2023-02-22 22:45:35 -05:00
|
|
|
pkg_id.as_serialized(),
|
2022-12-05 20:09:31 -05:00
|
|
|
path.display()
|
|
|
|
);
|
2022-09-22 11:17:02 -04:00
|
|
|
Ok(path)
|
2022-09-13 11:59:01 -04:00
|
|
|
}
|
|
|
|
|
2024-08-20 10:11:43 -04:00
|
|
|
/// Resolves the package id from the provided specifier.
|
2023-09-28 16:43:45 -04:00
|
|
|
pub fn resolve_pkg_id_from_specifier(
|
2023-07-01 21:07:57 -04:00
|
|
|
&self,
|
|
|
|
specifier: &ModuleSpecifier,
|
2023-07-17 14:00:44 -04:00
|
|
|
) -> Result<Option<NpmPackageId>, AnyError> {
|
|
|
|
let Some(cache_folder_id) = self
|
2023-07-01 21:07:57 -04:00
|
|
|
.fs_resolver
|
2023-08-27 00:04:12 -04:00
|
|
|
.resolve_package_cache_folder_id_from_specifier(specifier)?
|
|
|
|
else {
|
|
|
|
return Ok(None);
|
|
|
|
};
|
2023-07-17 14:00:44 -04:00
|
|
|
Ok(Some(
|
2023-07-01 21:07:57 -04:00
|
|
|
self
|
|
|
|
.resolution
|
|
|
|
.resolve_pkg_id_from_pkg_cache_folder_id(&cache_folder_id)?,
|
2023-07-17 14:00:44 -04:00
|
|
|
))
|
2023-07-01 21:07:57 -04:00
|
|
|
}
|
|
|
|
|
2023-09-30 12:06:38 -04:00
|
|
|
pub fn resolve_pkg_reqs_from_pkg_id(
|
|
|
|
&self,
|
|
|
|
id: &NpmPackageId,
|
|
|
|
) -> Vec<PackageReq> {
|
|
|
|
self.resolution.resolve_pkg_reqs_from_pkg_id(id)
|
|
|
|
}
|
|
|
|
|
2022-10-28 16:19:55 -04:00
|
|
|
/// Attempts to get the package size in bytes.
|
|
|
|
pub fn package_size(
|
|
|
|
&self,
|
2023-02-21 12:03:48 -05:00
|
|
|
package_id: &NpmPackageId,
|
2022-10-28 16:19:55 -04:00
|
|
|
) -> Result<u64, AnyError> {
|
2023-04-14 16:22:33 -04:00
|
|
|
let package_folder = self.fs_resolver.package_folder(package_id)?;
|
|
|
|
Ok(crate::util::fs::dir_size(&package_folder)?)
|
2022-10-28 16:19:55 -04:00
|
|
|
}
|
|
|
|
|
2023-09-30 12:06:38 -04:00
|
|
|
pub fn all_system_packages(
|
|
|
|
&self,
|
|
|
|
system_info: &NpmSystemInfo,
|
|
|
|
) -> Vec<NpmResolutionPackage> {
|
|
|
|
self.resolution.all_system_packages(system_info)
|
|
|
|
}
|
|
|
|
|
2023-10-03 19:05:06 -04:00
|
|
|
/// Checks if the provided package req's folder is cached.
|
|
|
|
pub fn is_pkg_req_folder_cached(&self, req: &PackageReq) -> bool {
|
|
|
|
self
|
|
|
|
.resolve_pkg_id_from_pkg_req(req)
|
|
|
|
.ok()
|
|
|
|
.and_then(|id| self.fs_resolver.package_folder(&id).ok())
|
|
|
|
.map(|folder| folder.exists())
|
|
|
|
.unwrap_or(false)
|
|
|
|
}
|
|
|
|
|
2022-10-21 11:20:18 -04:00
|
|
|
/// Adds package requirements to the resolver and ensures everything is setup.
|
2024-07-02 18:00:16 -04:00
|
|
|
/// This includes setting up the `node_modules` directory, if applicable.
|
2022-09-13 11:59:01 -04:00
|
|
|
pub async fn add_package_reqs(
|
|
|
|
&self,
|
2023-08-21 05:53:52 -04:00
|
|
|
packages: &[PackageReq],
|
2022-09-13 11:59:01 -04:00
|
|
|
) -> Result<(), AnyError> {
|
2024-07-02 18:00:16 -04:00
|
|
|
self
|
|
|
|
.add_package_reqs_raw(packages)
|
|
|
|
.await
|
|
|
|
.dependencies_result
|
2024-06-11 08:55:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn add_package_reqs_raw(
|
|
|
|
&self,
|
|
|
|
packages: &[PackageReq],
|
|
|
|
) -> AddPkgReqsResult {
|
2022-10-21 11:20:18 -04:00
|
|
|
if packages.is_empty() {
|
2024-06-11 08:55:12 -04:00
|
|
|
return AddPkgReqsResult {
|
|
|
|
dependencies_result: Ok(()),
|
|
|
|
results: vec![],
|
|
|
|
};
|
2022-10-21 11:20:18 -04:00
|
|
|
}
|
2022-09-13 11:59:01 -04:00
|
|
|
|
2024-06-11 08:55:12 -04:00
|
|
|
let mut result = self.resolution.add_package_reqs(packages).await;
|
2024-07-02 18:00:16 -04:00
|
|
|
|
|
|
|
if result.dependencies_result.is_ok() {
|
|
|
|
if let Some(lockfile) = self.maybe_lockfile.as_ref() {
|
|
|
|
result.dependencies_result = lockfile.error_if_changed();
|
|
|
|
}
|
|
|
|
}
|
2024-06-11 08:55:12 -04:00
|
|
|
if result.dependencies_result.is_ok() {
|
2024-08-28 14:17:47 -04:00
|
|
|
result.dependencies_result = self.cache_packages().await;
|
2024-06-11 08:55:12 -04:00
|
|
|
}
|
2022-10-25 12:20:07 -04:00
|
|
|
|
2024-06-11 08:55:12 -04:00
|
|
|
result
|
2022-09-13 11:59:01 -04:00
|
|
|
}
|
2022-09-28 13:04:16 -04:00
|
|
|
|
2022-10-21 11:20:18 -04:00
|
|
|
/// Sets package requirements to the resolver, removing old requirements and adding new ones.
|
2022-12-19 20:22:17 -05:00
|
|
|
///
|
|
|
|
/// This will retrieve and resolve package information, but not cache any package files.
|
2022-10-21 11:20:18 -04:00
|
|
|
pub async fn set_package_reqs(
|
|
|
|
&self,
|
2023-08-21 05:53:52 -04:00
|
|
|
packages: &[PackageReq],
|
2022-10-21 11:20:18 -04:00
|
|
|
) -> Result<(), AnyError> {
|
2023-02-22 14:15:25 -05:00
|
|
|
self.resolution.set_package_reqs(packages).await
|
2022-10-21 11:20:18 -04:00
|
|
|
}
|
|
|
|
|
2022-10-28 16:19:55 -04:00
|
|
|
pub fn snapshot(&self) -> NpmResolutionSnapshot {
|
2023-02-22 14:15:25 -05:00
|
|
|
self.resolution.snapshot()
|
2022-10-28 16:19:55 -04:00
|
|
|
}
|
|
|
|
|
2023-10-02 17:53:55 -04:00
|
|
|
pub fn serialized_valid_snapshot_for_system(
|
|
|
|
&self,
|
|
|
|
system_info: &NpmSystemInfo,
|
|
|
|
) -> ValidSerializedNpmResolutionSnapshot {
|
|
|
|
self
|
|
|
|
.resolution
|
|
|
|
.serialized_valid_snapshot_for_system(system_info)
|
|
|
|
}
|
|
|
|
|
2023-01-24 09:05:54 -05:00
|
|
|
pub async fn inject_synthetic_types_node_package(
|
|
|
|
&self,
|
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
// add and ensure this isn't added to the lockfile
|
2024-06-11 08:55:12 -04:00
|
|
|
self
|
|
|
|
.add_package_reqs(&[PackageReq::from_str("@types/node").unwrap()])
|
|
|
|
.await?;
|
2023-02-22 14:15:25 -05:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2023-01-24 09:05:54 -05:00
|
|
|
|
2023-10-02 17:53:55 -04:00
|
|
|
pub async fn cache_packages(&self) -> Result<(), AnyError> {
|
2024-06-03 17:17:08 -04:00
|
|
|
self.fs_resolver.cache_packages().await
|
2023-01-24 09:05:54 -05:00
|
|
|
}
|
2023-09-29 09:26:25 -04:00
|
|
|
|
2023-10-03 19:05:06 -04:00
|
|
|
pub fn resolve_pkg_folder_from_deno_module(
|
|
|
|
&self,
|
|
|
|
nv: &PackageNv,
|
|
|
|
) -> Result<PathBuf, AnyError> {
|
|
|
|
let pkg_id = self.resolution.resolve_pkg_id_from_deno_module(nv)?;
|
|
|
|
self.resolve_pkg_folder_from_pkg_id(&pkg_id)
|
|
|
|
}
|
|
|
|
|
2023-09-29 09:26:25 -04:00
|
|
|
fn resolve_pkg_id_from_pkg_req(
|
|
|
|
&self,
|
|
|
|
req: &PackageReq,
|
|
|
|
) -> Result<NpmPackageId, PackageReqNotFoundError> {
|
|
|
|
self.resolution.resolve_pkg_id_from_pkg_req(req)
|
|
|
|
}
|
2023-09-30 12:06:38 -04:00
|
|
|
|
2024-07-02 18:00:16 -04:00
|
|
|
/// Ensures that the top level `package.json` dependencies are installed.
|
|
|
|
/// This may set up the `node_modules` directory.
|
|
|
|
///
|
|
|
|
/// Returns `true` if any changes (such as caching packages) were made.
|
|
|
|
/// If this returns `false`, `node_modules` has _not_ been set up.
|
2023-09-30 12:06:38 -04:00
|
|
|
pub async fn ensure_top_level_package_json_install(
|
|
|
|
&self,
|
2024-07-02 18:00:16 -04:00
|
|
|
) -> Result<bool, AnyError> {
|
2024-06-11 08:55:12 -04:00
|
|
|
if !self.top_level_install_flag.raise() {
|
2024-07-02 18:00:16 -04:00
|
|
|
return Ok(false); // already did this
|
2024-06-11 08:55:12 -04:00
|
|
|
}
|
2024-09-04 10:00:44 -04:00
|
|
|
let pkg_json_remote_pkgs = self.npm_install_deps_provider.remote_pkgs();
|
2024-07-15 15:08:51 -04:00
|
|
|
if pkg_json_remote_pkgs.is_empty() {
|
2024-07-03 20:54:33 -04:00
|
|
|
return Ok(false);
|
|
|
|
}
|
|
|
|
|
2024-06-11 08:55:12 -04:00
|
|
|
// check if something needs resolving before bothering to load all
|
|
|
|
// the package information (which is slow)
|
2024-07-15 15:08:51 -04:00
|
|
|
if pkg_json_remote_pkgs.iter().all(|pkg| {
|
|
|
|
self
|
|
|
|
.resolution
|
|
|
|
.resolve_pkg_id_from_pkg_req(&pkg.req)
|
|
|
|
.is_ok()
|
|
|
|
}) {
|
2024-06-11 08:55:12 -04:00
|
|
|
log::debug!(
|
|
|
|
"All package.json deps resolvable. Skipping top level install."
|
|
|
|
);
|
2024-07-02 18:00:16 -04:00
|
|
|
return Ok(false); // everything is already resolvable
|
2024-06-11 08:55:12 -04:00
|
|
|
}
|
|
|
|
|
2024-07-15 15:08:51 -04:00
|
|
|
let pkg_reqs = pkg_json_remote_pkgs
|
|
|
|
.iter()
|
|
|
|
.map(|pkg| pkg.req.clone())
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
self.add_package_reqs(&pkg_reqs).await.map(|_| true)
|
2023-09-30 12:06:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn cache_package_info(
|
|
|
|
&self,
|
|
|
|
package_name: &str,
|
2024-06-05 11:04:16 -04:00
|
|
|
) -> Result<Arc<NpmPackageInfo>, AnyError> {
|
2023-09-30 12:06:38 -04:00
|
|
|
// this will internally cache the package information
|
|
|
|
self
|
2024-06-02 21:39:13 -04:00
|
|
|
.npm_api
|
2023-09-30 12:06:38 -04:00
|
|
|
.package_info(package_name)
|
|
|
|
.await
|
|
|
|
.map_err(|err| err.into())
|
|
|
|
}
|
2023-10-02 17:53:55 -04:00
|
|
|
|
2024-05-23 17:26:23 -04:00
|
|
|
pub fn global_cache_root_folder(&self) -> PathBuf {
|
2024-06-02 21:39:13 -04:00
|
|
|
self.npm_cache.root_folder()
|
2023-10-02 17:53:55 -04:00
|
|
|
}
|
2022-09-13 11:59:01 -04:00
|
|
|
}
|
|
|
|
|
2024-07-15 15:11:09 -04:00
|
|
|
fn npm_process_state(
|
|
|
|
snapshot: ValidSerializedNpmResolutionSnapshot,
|
|
|
|
node_modules_path: Option<&Path>,
|
|
|
|
) -> String {
|
|
|
|
serde_json::to_string(&NpmProcessState {
|
|
|
|
kind: NpmProcessStateKind::Snapshot(snapshot.into_serialized()),
|
|
|
|
local_node_modules_path: node_modules_path
|
|
|
|
.map(|p| p.to_string_lossy().to_string()),
|
|
|
|
})
|
|
|
|
.unwrap()
|
|
|
|
}
|
|
|
|
|
2023-09-29 09:26:25 -04:00
|
|
|
impl NpmResolver for ManagedCliNpmResolver {
|
2022-09-13 11:59:01 -04:00
|
|
|
fn resolve_package_folder_from_package(
|
|
|
|
&self,
|
2023-04-21 21:02:46 -04:00
|
|
|
name: &str,
|
|
|
|
referrer: &ModuleSpecifier,
|
2024-07-09 12:15:03 -04:00
|
|
|
) -> Result<PathBuf, PackageFolderResolveError> {
|
2023-04-21 21:02:46 -04:00
|
|
|
let path = self
|
|
|
|
.fs_resolver
|
2024-06-08 20:05:28 -04:00
|
|
|
.resolve_package_folder_from_package(name, referrer)?;
|
2024-04-11 13:18:19 -04:00
|
|
|
let path =
|
2024-07-09 12:15:03 -04:00
|
|
|
canonicalize_path_maybe_not_exists_with_fs(&path, self.fs.as_ref())
|
2024-07-23 20:22:24 -04:00
|
|
|
.map_err(|err| PackageFolderResolveIoError {
|
2024-07-09 12:15:03 -04:00
|
|
|
package_name: name.to_string(),
|
|
|
|
referrer: referrer.clone(),
|
|
|
|
source: err,
|
|
|
|
})?;
|
2023-04-21 21:02:46 -04:00
|
|
|
log::debug!("Resolved {} from {} to {}", name, referrer, path.display());
|
|
|
|
Ok(path)
|
2022-09-13 11:59:01 -04:00
|
|
|
}
|
|
|
|
|
2023-04-21 21:02:46 -04:00
|
|
|
fn in_npm_package(&self, specifier: &ModuleSpecifier) -> bool {
|
2023-09-26 16:42:39 -04:00
|
|
|
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())
|
2022-09-13 11:59:01 -04:00
|
|
|
}
|
2024-07-25 19:08:14 -04:00
|
|
|
}
|
2022-09-13 11:59:01 -04:00
|
|
|
|
2024-07-25 19:08:14 -04:00
|
|
|
impl NodeRequireResolver for ManagedCliNpmResolver {
|
2023-01-10 08:35:44 -05:00
|
|
|
fn ensure_read_permission(
|
|
|
|
&self,
|
2024-06-06 23:37:53 -04:00
|
|
|
permissions: &mut dyn NodePermissions,
|
2023-01-10 08:35:44 -05:00
|
|
|
path: &Path,
|
|
|
|
) -> Result<(), AnyError> {
|
2023-04-21 21:02:46 -04:00
|
|
|
self.fs_resolver.ensure_read_permission(permissions, path)
|
2022-09-13 11:59:01 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-25 19:08:14 -04:00
|
|
|
impl NpmProcessStateProvider for ManagedCliNpmResolver {
|
|
|
|
fn get_npm_process_state(&self) -> String {
|
|
|
|
npm_process_state(
|
|
|
|
self.resolution.serialized_valid_snapshot(),
|
|
|
|
self.fs_resolver.node_modules_path().map(|p| p.as_path()),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-29 09:26:25 -04:00
|
|
|
impl CliNpmResolver for ManagedCliNpmResolver {
|
|
|
|
fn into_npm_resolver(self: Arc<Self>) -> Arc<dyn NpmResolver> {
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2024-07-25 19:08:14 -04:00
|
|
|
fn into_require_resolver(self: Arc<Self>) -> Arc<dyn NodeRequireResolver> {
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
fn into_process_state_provider(
|
|
|
|
self: Arc<Self>,
|
|
|
|
) -> Arc<dyn NpmProcessStateProvider> {
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2023-10-02 17:53:55 -04:00
|
|
|
fn clone_snapshotted(&self) -> Arc<dyn CliNpmResolver> {
|
|
|
|
// create a new snapshotted npm resolution and resolver
|
|
|
|
let npm_resolution = Arc::new(NpmResolution::new(
|
2024-06-02 21:39:13 -04:00
|
|
|
self.npm_api.clone(),
|
2023-10-02 17:53:55 -04:00
|
|
|
self.resolution.snapshot(),
|
|
|
|
self.maybe_lockfile.clone(),
|
|
|
|
));
|
|
|
|
|
|
|
|
Arc::new(ManagedCliNpmResolver::new(
|
|
|
|
self.fs.clone(),
|
|
|
|
create_npm_fs_resolver(
|
|
|
|
self.fs.clone(),
|
2024-06-02 21:39:13 -04:00
|
|
|
self.npm_cache.clone(),
|
2024-09-04 10:00:44 -04:00
|
|
|
&self.npm_install_deps_provider,
|
2024-06-02 21:39:13 -04:00
|
|
|
&self.text_only_progress_bar,
|
|
|
|
npm_resolution.clone(),
|
2024-06-03 17:17:08 -04:00
|
|
|
self.tarball_cache.clone(),
|
2023-11-29 09:32:23 -05:00
|
|
|
self.root_node_modules_path().map(ToOwned::to_owned),
|
2023-10-02 17:53:55 -04:00
|
|
|
self.npm_system_info.clone(),
|
2024-07-09 23:06:08 -04:00
|
|
|
self.lifecycle_scripts.clone(),
|
2023-10-02 17:53:55 -04:00
|
|
|
),
|
|
|
|
self.maybe_lockfile.clone(),
|
2024-06-02 21:39:13 -04:00
|
|
|
self.npm_api.clone(),
|
|
|
|
self.npm_cache.clone(),
|
2024-09-04 10:00:44 -04:00
|
|
|
self.npm_install_deps_provider.clone(),
|
2024-06-02 21:39:13 -04:00
|
|
|
npm_resolution,
|
2024-06-03 17:17:08 -04:00
|
|
|
self.tarball_cache.clone(),
|
2024-06-02 21:39:13 -04:00
|
|
|
self.text_only_progress_bar.clone(),
|
2023-10-02 17:53:55 -04:00
|
|
|
self.npm_system_info.clone(),
|
2024-07-09 23:06:08 -04:00
|
|
|
self.lifecycle_scripts.clone(),
|
2023-10-02 17:53:55 -04:00
|
|
|
))
|
|
|
|
}
|
|
|
|
|
2023-09-29 09:26:25 -04:00
|
|
|
fn as_inner(&self) -> InnerCliNpmResolverRef {
|
|
|
|
InnerCliNpmResolverRef::Managed(self)
|
|
|
|
}
|
|
|
|
|
2023-11-29 09:32:23 -05:00
|
|
|
fn root_node_modules_path(&self) -> Option<&PathBuf> {
|
2023-09-29 09:26:25 -04:00
|
|
|
self.fs_resolver.node_modules_path()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn resolve_pkg_folder_from_deno_module_req(
|
|
|
|
&self,
|
|
|
|
req: &PackageReq,
|
2023-10-03 19:05:06 -04:00
|
|
|
_referrer: &ModuleSpecifier,
|
2023-09-29 09:26:25 -04:00
|
|
|
) -> Result<PathBuf, AnyError> {
|
|
|
|
let pkg_id = self.resolve_pkg_id_from_pkg_req(req)?;
|
|
|
|
self.resolve_pkg_folder_from_pkg_id(&pkg_id)
|
|
|
|
}
|
|
|
|
|
2023-10-03 19:05:06 -04:00
|
|
|
fn check_state_hash(&self) -> Option<u64> {
|
|
|
|
// We could go further and check all the individual
|
|
|
|
// npm packages, but that's probably overkill.
|
|
|
|
let mut package_reqs = self
|
|
|
|
.resolution
|
|
|
|
.package_reqs()
|
|
|
|
.into_iter()
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
package_reqs.sort_by(|a, b| a.0.cmp(&b.0)); // determinism
|
2024-05-29 14:38:18 -04:00
|
|
|
let mut hasher = FastInsecureHasher::new_without_deno_version();
|
2024-04-15 18:58:04 -04:00
|
|
|
// ensure the cache gets busted when turning nodeModulesDir on or off
|
|
|
|
// as this could cause changes in resolution
|
|
|
|
hasher.write_hashable(self.fs_resolver.node_modules_path().is_some());
|
2023-10-03 19:05:06 -04:00
|
|
|
for (pkg_req, pkg_nv) in package_reqs {
|
|
|
|
hasher.write_hashable(&pkg_req);
|
|
|
|
hasher.write_hashable(&pkg_nv);
|
|
|
|
}
|
|
|
|
Some(hasher.finish())
|
2023-09-29 09:26:25 -04:00
|
|
|
}
|
|
|
|
}
|