2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2022-09-22 11:17:02 -04:00
|
|
|
|
|
|
|
//! Code for local node_modules resolution.
|
|
|
|
|
2024-05-23 19:43:38 -04:00
|
|
|
mod bin_entries;
|
|
|
|
|
2022-11-16 13:44:31 -05:00
|
|
|
use std::borrow::Cow;
|
2024-06-06 18:37:41 -04:00
|
|
|
use std::cell::RefCell;
|
2023-05-24 15:04:21 -04:00
|
|
|
use std::cmp::Ordering;
|
2024-07-16 16:30:28 -04:00
|
|
|
use std::collections::hash_map::Entry;
|
2024-07-09 14:04:21 -04:00
|
|
|
use std::collections::BTreeMap;
|
2023-05-24 15:04:21 -04:00
|
|
|
use std::collections::HashMap;
|
2022-09-22 11:17:02 -04:00
|
|
|
use std::fs;
|
|
|
|
use std::path::Path;
|
|
|
|
use std::path::PathBuf;
|
2024-06-06 18:37:41 -04:00
|
|
|
use std::rc::Rc;
|
2023-04-14 16:22:33 -04:00
|
|
|
use std::sync::Arc;
|
2022-09-22 11:17:02 -04:00
|
|
|
|
2024-07-09 23:06:08 -04:00
|
|
|
use crate::args::LifecycleScriptsConfig;
|
|
|
|
use crate::args::PackagesAllowedScripts;
|
2023-02-22 14:15:25 -05:00
|
|
|
use async_trait::async_trait;
|
2022-09-22 11:17:02 -04:00
|
|
|
use deno_ast::ModuleSpecifier;
|
2024-07-09 23:06:08 -04:00
|
|
|
use deno_core::anyhow;
|
2022-09-22 11:17:02 -04:00
|
|
|
use deno_core::anyhow::Context;
|
|
|
|
use deno_core::error::AnyError;
|
2024-06-02 21:39:13 -04:00
|
|
|
use deno_core::futures::stream::FuturesUnordered;
|
|
|
|
use deno_core::futures::StreamExt;
|
2022-09-22 11:17:02 -04:00
|
|
|
use deno_core::url::Url;
|
2023-04-06 18:46:44 -04:00
|
|
|
use deno_npm::resolution::NpmResolutionSnapshot;
|
|
|
|
use deno_npm::NpmPackageCacheFolderId;
|
|
|
|
use deno_npm::NpmPackageId;
|
2023-05-24 15:04:21 -04:00
|
|
|
use deno_npm::NpmResolutionPackage;
|
2023-05-17 17:38:50 -04:00
|
|
|
use deno_npm::NpmSystemInfo;
|
2023-05-05 12:44:24 -04:00
|
|
|
use deno_runtime::deno_fs;
|
2024-07-09 12:15:03 -04:00
|
|
|
use deno_runtime::deno_node::errors::PackageFolderResolveError;
|
2024-07-23 20:22:24 -04:00
|
|
|
use deno_runtime::deno_node::errors::PackageFolderResolveIoError;
|
|
|
|
use deno_runtime::deno_node::errors::PackageNotFoundError;
|
|
|
|
use deno_runtime::deno_node::errors::ReferrerNotFoundError;
|
2023-01-10 08:35:44 -05:00
|
|
|
use deno_runtime::deno_node::NodePermissions;
|
2023-08-21 05:53:52 -04:00
|
|
|
use deno_semver::package::PackageNv;
|
2023-07-10 13:42:47 -04:00
|
|
|
use serde::Deserialize;
|
|
|
|
use serde::Serialize;
|
2022-09-22 11:17:02 -04:00
|
|
|
|
2024-07-09 12:15:03 -04:00
|
|
|
use crate::args::PackageJsonInstallDepsProvider;
|
|
|
|
use crate::cache::CACHE_PERM;
|
|
|
|
use crate::npm::cache_dir::mixed_case_package_name_decode;
|
2023-10-02 17:53:55 -04:00
|
|
|
use crate::npm::cache_dir::mixed_case_package_name_encode;
|
2024-07-09 12:15:03 -04:00
|
|
|
use crate::util::fs::atomic_write_file_with_retries;
|
|
|
|
use crate::util::fs::canonicalize_path_maybe_not_exists_with_fs;
|
|
|
|
use crate::util::fs::clone_dir_recursive;
|
|
|
|
use crate::util::fs::symlink_dir;
|
|
|
|
use crate::util::fs::LaxSingleProcessFsFlag;
|
|
|
|
use crate::util::progress_bar::ProgressBar;
|
|
|
|
use crate::util::progress_bar::ProgressMessagePrompt;
|
2022-09-22 11:17:02 -04:00
|
|
|
|
2023-10-02 17:53:55 -04:00
|
|
|
use super::super::cache::NpmCache;
|
2024-06-02 21:39:13 -04:00
|
|
|
use super::super::cache::TarballCache;
|
2023-09-30 12:06:38 -04:00
|
|
|
use super::super::resolution::NpmResolution;
|
2023-02-22 14:15:25 -05:00
|
|
|
use super::common::NpmPackageFsResolver;
|
2023-06-09 14:41:18 -04:00
|
|
|
use super::common::RegistryReadPermissionChecker;
|
2022-09-22 11:17:02 -04:00
|
|
|
|
|
|
|
/// Resolver that creates a local node_modules directory
|
|
|
|
/// and resolves packages from it.
|
2023-04-14 16:22:33 -04:00
|
|
|
#[derive(Debug)]
|
2022-09-22 11:17:02 -04:00
|
|
|
pub struct LocalNpmPackageResolver {
|
2023-04-14 16:22:33 -04:00
|
|
|
cache: Arc<NpmCache>,
|
2024-06-02 21:39:13 -04:00
|
|
|
fs: Arc<dyn deno_fs::FileSystem>,
|
2024-07-03 20:54:33 -04:00
|
|
|
pkg_json_deps_provider: Arc<PackageJsonInstallDepsProvider>,
|
2023-03-13 14:18:29 -04:00
|
|
|
progress_bar: ProgressBar,
|
2023-04-14 16:22:33 -04:00
|
|
|
resolution: Arc<NpmResolution>,
|
2024-06-02 21:39:13 -04:00
|
|
|
tarball_cache: Arc<TarballCache>,
|
2022-09-22 11:17:02 -04:00
|
|
|
root_node_modules_path: PathBuf,
|
2023-02-23 10:58:10 -05:00
|
|
|
root_node_modules_url: Url,
|
2023-05-17 17:38:50 -04:00
|
|
|
system_info: NpmSystemInfo,
|
2023-06-09 14:41:18 -04:00
|
|
|
registry_read_permission_checker: RegistryReadPermissionChecker,
|
2024-07-09 23:06:08 -04:00
|
|
|
lifecycle_scripts: LifecycleScriptsConfig,
|
2022-09-22 11:17:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl LocalNpmPackageResolver {
|
2024-07-03 20:54:33 -04:00
|
|
|
#[allow(clippy::too_many_arguments)]
|
2022-09-22 11:17:02 -04:00
|
|
|
pub fn new(
|
2023-04-14 16:22:33 -04:00
|
|
|
cache: Arc<NpmCache>,
|
2024-06-02 21:39:13 -04:00
|
|
|
fs: Arc<dyn deno_fs::FileSystem>,
|
2024-07-03 20:54:33 -04:00
|
|
|
pkg_json_deps_provider: Arc<PackageJsonInstallDepsProvider>,
|
2023-03-13 14:18:29 -04:00
|
|
|
progress_bar: ProgressBar,
|
2023-04-14 16:22:33 -04:00
|
|
|
resolution: Arc<NpmResolution>,
|
2024-06-02 21:39:13 -04:00
|
|
|
tarball_cache: Arc<TarballCache>,
|
|
|
|
node_modules_folder: PathBuf,
|
2023-05-17 17:38:50 -04:00
|
|
|
system_info: NpmSystemInfo,
|
2024-07-09 23:06:08 -04:00
|
|
|
lifecycle_scripts: LifecycleScriptsConfig,
|
2022-09-22 11:17:02 -04:00
|
|
|
) -> Self {
|
|
|
|
Self {
|
|
|
|
cache,
|
2024-06-02 21:39:13 -04:00
|
|
|
fs: fs.clone(),
|
2024-07-03 20:54:33 -04:00
|
|
|
pkg_json_deps_provider,
|
2023-03-13 14:18:29 -04:00
|
|
|
progress_bar,
|
2022-09-22 11:17:02 -04:00
|
|
|
resolution,
|
2024-06-02 21:39:13 -04:00
|
|
|
tarball_cache,
|
2023-06-09 14:41:18 -04:00
|
|
|
registry_read_permission_checker: RegistryReadPermissionChecker::new(
|
|
|
|
fs,
|
2024-06-02 21:39:13 -04:00
|
|
|
node_modules_folder.clone(),
|
2023-06-09 14:41:18 -04:00
|
|
|
),
|
2024-06-02 21:39:13 -04:00
|
|
|
root_node_modules_url: Url::from_directory_path(&node_modules_folder)
|
|
|
|
.unwrap(),
|
|
|
|
root_node_modules_path: node_modules_folder,
|
|
|
|
system_info,
|
2024-07-09 23:06:08 -04:00
|
|
|
lifecycle_scripts,
|
2022-09-22 11:17:02 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn resolve_package_root(&self, path: &Path) -> PathBuf {
|
|
|
|
let mut last_found = path;
|
|
|
|
loop {
|
|
|
|
let parent = last_found.parent().unwrap();
|
|
|
|
if parent.file_name().unwrap() == "node_modules" {
|
|
|
|
return last_found.to_path_buf();
|
|
|
|
} else {
|
|
|
|
last_found = parent;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn resolve_folder_for_specifier(
|
|
|
|
&self,
|
|
|
|
specifier: &ModuleSpecifier,
|
2024-07-09 12:15:03 -04:00
|
|
|
) -> Result<Option<PathBuf>, std::io::Error> {
|
2023-08-27 00:04:12 -04:00
|
|
|
let Some(relative_url) =
|
|
|
|
self.root_node_modules_url.make_relative(specifier)
|
|
|
|
else {
|
2023-07-17 14:00:44 -04:00
|
|
|
return Ok(None);
|
|
|
|
};
|
2022-09-22 11:17:02 -04:00
|
|
|
if relative_url.starts_with("../") {
|
2023-07-17 14:00:44 -04:00
|
|
|
return Ok(None);
|
2022-09-22 11:17:02 -04:00
|
|
|
}
|
|
|
|
// it's within the directory, so use it
|
2023-07-17 14:00:44 -04:00
|
|
|
let Some(path) = specifier.to_file_path().ok() else {
|
|
|
|
return Ok(None);
|
|
|
|
};
|
|
|
|
// Canonicalize the path so it's not pointing to the symlinked directory
|
|
|
|
// in `node_modules` directory of the referrer.
|
2023-11-29 09:32:23 -05:00
|
|
|
canonicalize_path_maybe_not_exists_with_fs(&path, self.fs.as_ref())
|
|
|
|
.map(Some)
|
2022-09-22 11:17:02 -04:00
|
|
|
}
|
2024-05-23 17:26:23 -04:00
|
|
|
|
|
|
|
fn resolve_package_folder_from_specifier(
|
|
|
|
&self,
|
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
) -> Result<Option<PathBuf>, AnyError> {
|
|
|
|
let Some(local_path) = self.resolve_folder_for_specifier(specifier)? else {
|
|
|
|
return Ok(None);
|
|
|
|
};
|
|
|
|
let package_root_path = self.resolve_package_root(&local_path);
|
|
|
|
Ok(Some(package_root_path))
|
|
|
|
}
|
2023-04-14 16:22:33 -04:00
|
|
|
}
|
2022-11-08 14:17:24 -05:00
|
|
|
|
2024-06-06 18:37:41 -04:00
|
|
|
#[async_trait(?Send)]
|
2023-04-14 16:22:33 -04:00
|
|
|
impl NpmPackageFsResolver for LocalNpmPackageResolver {
|
|
|
|
fn root_dir_url(&self) -> &Url {
|
|
|
|
&self.root_node_modules_url
|
|
|
|
}
|
|
|
|
|
2023-11-29 09:32:23 -05:00
|
|
|
fn node_modules_path(&self) -> Option<&PathBuf> {
|
|
|
|
Some(&self.root_node_modules_path)
|
2023-04-14 16:22:33 -04:00
|
|
|
}
|
|
|
|
|
2024-07-09 12:15:03 -04:00
|
|
|
fn maybe_package_folder(&self, id: &NpmPackageId) -> Option<PathBuf> {
|
|
|
|
let cache_folder_id = self
|
|
|
|
.resolution
|
|
|
|
.resolve_pkg_cache_folder_id_from_pkg_id(id)?;
|
|
|
|
// package is stored at:
|
|
|
|
// node_modules/.deno/<package_cache_folder_id_folder_name>/node_modules/<package_name>
|
|
|
|
Some(
|
|
|
|
self
|
|
|
|
.root_node_modules_path
|
|
|
|
.join(".deno")
|
|
|
|
.join(get_package_folder_id_folder_name(&cache_folder_id))
|
|
|
|
.join("node_modules")
|
|
|
|
.join(&cache_folder_id.nv.name),
|
|
|
|
)
|
2022-11-08 14:17:24 -05:00
|
|
|
}
|
2022-09-22 11:17:02 -04:00
|
|
|
|
|
|
|
fn resolve_package_folder_from_package(
|
|
|
|
&self,
|
|
|
|
name: &str,
|
|
|
|
referrer: &ModuleSpecifier,
|
2024-07-09 12:15:03 -04:00
|
|
|
) -> Result<PathBuf, PackageFolderResolveError> {
|
|
|
|
let maybe_local_path = self
|
|
|
|
.resolve_folder_for_specifier(referrer)
|
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,
|
|
|
|
})?;
|
|
|
|
let Some(local_path) = maybe_local_path else {
|
|
|
|
return Err(
|
2024-07-23 20:22:24 -04:00
|
|
|
ReferrerNotFoundError {
|
2024-07-09 12:15:03 -04:00
|
|
|
referrer: referrer.clone(),
|
|
|
|
referrer_extra: None,
|
|
|
|
}
|
|
|
|
.into(),
|
|
|
|
);
|
2023-07-17 14:00:44 -04:00
|
|
|
};
|
2022-09-22 11:17:02 -04:00
|
|
|
let package_root_path = self.resolve_package_root(&local_path);
|
|
|
|
let mut current_folder = package_root_path.as_path();
|
2024-04-25 10:13:55 -04:00
|
|
|
while let Some(parent_folder) = current_folder.parent() {
|
|
|
|
current_folder = parent_folder;
|
2023-05-24 15:04:21 -04:00
|
|
|
let node_modules_folder = if current_folder.ends_with("node_modules") {
|
|
|
|
Cow::Borrowed(current_folder)
|
|
|
|
} else {
|
|
|
|
Cow::Owned(current_folder.join("node_modules"))
|
|
|
|
};
|
2022-10-21 11:20:18 -04:00
|
|
|
|
2023-10-25 14:39:00 -04:00
|
|
|
let sub_dir = join_package_name(&node_modules_folder, name);
|
|
|
|
if self.fs.is_dir_sync(&sub_dir) {
|
|
|
|
return Ok(sub_dir);
|
|
|
|
}
|
|
|
|
|
2022-09-22 11:17:02 -04:00
|
|
|
if current_folder == self.root_node_modules_path {
|
2024-04-25 10:13:55 -04:00
|
|
|
break;
|
2022-09-22 11:17:02 -04:00
|
|
|
}
|
|
|
|
}
|
2024-04-25 10:13:55 -04:00
|
|
|
|
2024-07-09 12:15:03 -04:00
|
|
|
Err(
|
2024-07-23 20:22:24 -04:00
|
|
|
PackageNotFoundError {
|
2024-07-09 12:15:03 -04:00
|
|
|
package_name: name.to_string(),
|
|
|
|
referrer: referrer.clone(),
|
|
|
|
referrer_extra: None,
|
|
|
|
}
|
|
|
|
.into(),
|
|
|
|
)
|
2022-09-22 11:17:02 -04:00
|
|
|
}
|
|
|
|
|
2023-07-01 21:07:57 -04:00
|
|
|
fn resolve_package_cache_folder_id_from_specifier(
|
|
|
|
&self,
|
|
|
|
specifier: &ModuleSpecifier,
|
2023-07-17 14:00:44 -04:00
|
|
|
) -> Result<Option<NpmPackageCacheFolderId>, AnyError> {
|
2023-08-27 00:04:12 -04:00
|
|
|
let Some(folder_path) =
|
|
|
|
self.resolve_package_folder_from_specifier(specifier)?
|
|
|
|
else {
|
2023-07-17 14:00:44 -04:00
|
|
|
return Ok(None);
|
|
|
|
};
|
2023-07-01 21:07:57 -04:00
|
|
|
let folder_name = folder_path.parent().unwrap().to_string_lossy();
|
2023-07-17 14:00:44 -04:00
|
|
|
Ok(get_package_folder_id_from_folder_name(&folder_name))
|
2023-07-01 21:07:57 -04:00
|
|
|
}
|
|
|
|
|
2024-06-03 17:17:08 -04:00
|
|
|
async fn cache_packages(&self) -> Result<(), AnyError> {
|
2023-03-13 14:18:29 -04:00
|
|
|
sync_resolution_with_fs(
|
|
|
|
&self.resolution.snapshot(),
|
|
|
|
&self.cache,
|
2024-07-03 20:54:33 -04:00
|
|
|
&self.pkg_json_deps_provider,
|
2023-03-13 14:18:29 -04:00
|
|
|
&self.progress_bar,
|
2024-06-02 21:39:13 -04:00
|
|
|
&self.tarball_cache,
|
2023-03-13 14:18:29 -04:00
|
|
|
&self.root_node_modules_path,
|
2023-05-17 17:38:50 -04:00
|
|
|
&self.system_info,
|
2024-07-09 23:06:08 -04:00
|
|
|
&self.lifecycle_scripts,
|
2023-03-13 14:18:29 -04:00
|
|
|
)
|
|
|
|
.await
|
2022-09-22 11:17:02 -04:00
|
|
|
}
|
|
|
|
|
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-06-09 14:41:18 -04:00
|
|
|
self
|
|
|
|
.registry_read_permission_checker
|
|
|
|
.ensure_registry_read_permission(permissions, path)
|
2022-09-22 11:17:02 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-09 23:06:08 -04:00
|
|
|
// take in all (non copy) packages from snapshot,
|
|
|
|
// and resolve the set of available binaries to create
|
|
|
|
// custom commands available to the task runner
|
|
|
|
fn resolve_baseline_custom_commands(
|
|
|
|
snapshot: &NpmResolutionSnapshot,
|
|
|
|
packages: &[NpmResolutionPackage],
|
|
|
|
local_registry_dir: &Path,
|
|
|
|
) -> Result<crate::task_runner::TaskCustomCommands, AnyError> {
|
|
|
|
let mut custom_commands = crate::task_runner::TaskCustomCommands::new();
|
|
|
|
custom_commands
|
|
|
|
.insert("npx".to_string(), Rc::new(crate::task_runner::NpxCommand));
|
|
|
|
|
|
|
|
custom_commands
|
|
|
|
.insert("npm".to_string(), Rc::new(crate::task_runner::NpmCommand));
|
|
|
|
|
2024-07-19 14:59:04 -04:00
|
|
|
custom_commands
|
|
|
|
.insert("node".to_string(), Rc::new(crate::task_runner::NodeCommand));
|
2024-07-09 23:06:08 -04:00
|
|
|
|
|
|
|
custom_commands.insert(
|
|
|
|
"node-gyp".to_string(),
|
|
|
|
Rc::new(crate::task_runner::NodeGypCommand),
|
|
|
|
);
|
|
|
|
|
|
|
|
// TODO: this recreates the bin entries which could be redoing some work, but the ones
|
|
|
|
// we compute earlier in `sync_resolution_with_fs` may not be exhaustive (because we skip
|
|
|
|
// doing it for packages that are set up already.
|
|
|
|
// realistically, scripts won't be run very often so it probably isn't too big of an issue.
|
|
|
|
resolve_custom_commands_from_packages(
|
|
|
|
custom_commands,
|
|
|
|
snapshot,
|
|
|
|
packages,
|
|
|
|
local_registry_dir,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// resolves the custom commands from an iterator of packages
|
|
|
|
// and adds them to the existing custom commands.
|
|
|
|
// note that this will overwrite any existing custom commands
|
|
|
|
fn resolve_custom_commands_from_packages<
|
|
|
|
'a,
|
|
|
|
P: IntoIterator<Item = &'a NpmResolutionPackage>,
|
|
|
|
>(
|
|
|
|
mut commands: crate::task_runner::TaskCustomCommands,
|
|
|
|
snapshot: &'a NpmResolutionSnapshot,
|
|
|
|
packages: P,
|
|
|
|
local_registry_dir: &Path,
|
|
|
|
) -> Result<crate::task_runner::TaskCustomCommands, AnyError> {
|
|
|
|
let mut bin_entries = bin_entries::BinEntries::new();
|
|
|
|
for package in packages {
|
|
|
|
let package_path =
|
|
|
|
local_node_modules_package_path(local_registry_dir, package);
|
|
|
|
|
|
|
|
if package.bin.is_some() {
|
|
|
|
bin_entries.add(package.clone(), package_path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let bins = bin_entries.into_bin_files(snapshot);
|
|
|
|
for (bin_name, script_path) in bins {
|
|
|
|
commands.insert(
|
|
|
|
bin_name.clone(),
|
|
|
|
Rc::new(crate::task_runner::NodeModulesFileRunCommand {
|
|
|
|
command_name: bin_name,
|
|
|
|
path: script_path,
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(commands)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn local_node_modules_package_path(
|
|
|
|
local_registry_dir: &Path,
|
|
|
|
package: &NpmResolutionPackage,
|
|
|
|
) -> PathBuf {
|
|
|
|
local_registry_dir
|
|
|
|
.join(get_package_folder_id_folder_name(
|
|
|
|
&package.get_package_cache_folder_id(),
|
|
|
|
))
|
|
|
|
.join("node_modules")
|
|
|
|
.join(&package.id.nv.name)
|
|
|
|
}
|
|
|
|
|
|
|
|
// resolves the custom commands from the dependencies of a package
|
|
|
|
// and adds them to the existing custom commands.
|
|
|
|
// note that this will overwrite any existing custom commands.
|
|
|
|
fn resolve_custom_commands_from_deps(
|
|
|
|
baseline: crate::task_runner::TaskCustomCommands,
|
|
|
|
package: &NpmResolutionPackage,
|
|
|
|
snapshot: &NpmResolutionSnapshot,
|
|
|
|
local_registry_dir: &Path,
|
|
|
|
) -> Result<crate::task_runner::TaskCustomCommands, AnyError> {
|
|
|
|
resolve_custom_commands_from_packages(
|
|
|
|
baseline,
|
|
|
|
snapshot,
|
|
|
|
package
|
|
|
|
.dependencies
|
|
|
|
.values()
|
|
|
|
.map(|id| snapshot.package_from_id(id).unwrap()),
|
|
|
|
local_registry_dir,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn can_run_scripts(
|
|
|
|
allow_scripts: &PackagesAllowedScripts,
|
|
|
|
package_nv: &PackageNv,
|
|
|
|
) -> bool {
|
|
|
|
match allow_scripts {
|
|
|
|
PackagesAllowedScripts::All => true,
|
|
|
|
// TODO: make this more correct
|
|
|
|
PackagesAllowedScripts::Some(allow_list) => allow_list.iter().any(|s| {
|
|
|
|
let s = s.strip_prefix("npm:").unwrap_or(s);
|
|
|
|
s == package_nv.name || s == package_nv.to_string()
|
|
|
|
}),
|
|
|
|
PackagesAllowedScripts::None => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-11 14:39:45 -04:00
|
|
|
// npm defaults to running `node-gyp rebuild` if there is a `binding.gyp` file
|
|
|
|
// but it always fails if the package excludes the `binding.gyp` file when they publish.
|
|
|
|
// (for example, `fsevents` hits this)
|
|
|
|
fn is_broken_default_install_script(script: &str, package_path: &Path) -> bool {
|
|
|
|
script == "node-gyp rebuild" && !package_path.join("binding.gyp").exists()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn has_lifecycle_scripts(
|
|
|
|
package: &NpmResolutionPackage,
|
|
|
|
package_path: &Path,
|
|
|
|
) -> bool {
|
|
|
|
if let Some(install) = package.scripts.get("install") {
|
|
|
|
// default script
|
|
|
|
if !is_broken_default_install_script(install, package_path) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2024-07-09 23:06:08 -04:00
|
|
|
package.scripts.contains_key("preinstall")
|
|
|
|
|| package.scripts.contains_key("postinstall")
|
|
|
|
}
|
|
|
|
|
2022-09-22 11:17:02 -04:00
|
|
|
/// Creates a pnpm style folder structure.
|
2024-07-09 23:06:08 -04:00
|
|
|
#[allow(clippy::too_many_arguments)]
|
2022-09-23 17:35:48 -04:00
|
|
|
async fn sync_resolution_with_fs(
|
2022-09-22 11:17:02 -04:00
|
|
|
snapshot: &NpmResolutionSnapshot,
|
2023-04-14 16:22:33 -04:00
|
|
|
cache: &Arc<NpmCache>,
|
2024-07-03 20:54:33 -04:00
|
|
|
pkg_json_deps_provider: &PackageJsonInstallDepsProvider,
|
2023-03-13 14:18:29 -04:00
|
|
|
progress_bar: &ProgressBar,
|
2024-06-02 21:39:13 -04:00
|
|
|
tarball_cache: &Arc<TarballCache>,
|
2022-09-22 11:17:02 -04:00
|
|
|
root_node_modules_dir_path: &Path,
|
2023-05-17 17:38:50 -04:00
|
|
|
system_info: &NpmSystemInfo,
|
2024-07-09 23:06:08 -04:00
|
|
|
lifecycle_scripts: &LifecycleScriptsConfig,
|
2022-09-22 11:17:02 -04:00
|
|
|
) -> Result<(), AnyError> {
|
2024-07-03 20:54:33 -04:00
|
|
|
if snapshot.is_empty() && pkg_json_deps_provider.workspace_pkgs().is_empty() {
|
2023-03-13 10:03:19 -04:00
|
|
|
return Ok(()); // don't create the directory
|
|
|
|
}
|
|
|
|
|
2022-09-22 11:17:02 -04:00
|
|
|
let deno_local_registry_dir = root_node_modules_dir_path.join(".deno");
|
2023-05-24 15:04:21 -04:00
|
|
|
let deno_node_modules_dir = deno_local_registry_dir.join("node_modules");
|
|
|
|
fs::create_dir_all(&deno_node_modules_dir).with_context(|| {
|
2022-09-22 11:17:02 -04:00
|
|
|
format!("Creating '{}'", deno_local_registry_dir.display())
|
|
|
|
})?;
|
2024-05-23 19:43:38 -04:00
|
|
|
let bin_node_modules_dir_path = root_node_modules_dir_path.join(".bin");
|
|
|
|
fs::create_dir_all(&bin_node_modules_dir_path).with_context(|| {
|
|
|
|
format!("Creating '{}'", bin_node_modules_dir_path.display())
|
|
|
|
})?;
|
2022-09-22 11:17:02 -04:00
|
|
|
|
2023-03-08 10:13:13 -05:00
|
|
|
let single_process_lock = LaxSingleProcessFsFlag::lock(
|
|
|
|
deno_local_registry_dir.join(".deno.lock"),
|
|
|
|
// similar message used by cargo build
|
|
|
|
"waiting for file lock on node_modules directory",
|
|
|
|
)
|
|
|
|
.await;
|
|
|
|
|
2023-07-10 13:42:47 -04:00
|
|
|
// load this after we get the directory lock
|
|
|
|
let mut setup_cache =
|
|
|
|
SetupCache::load(deno_local_registry_dir.join(".setup-cache.bin"));
|
|
|
|
|
2023-03-13 14:18:29 -04:00
|
|
|
let pb_clear_guard = progress_bar.clear_guard(); // prevent flickering
|
|
|
|
|
2022-09-22 11:17:02 -04:00
|
|
|
// 1. Write all the packages out the .deno directory.
|
|
|
|
//
|
|
|
|
// Copy (hardlink in future) <global_registry_cache>/<package_id>/ to
|
2022-11-08 14:17:24 -05:00
|
|
|
// node_modules/.deno/<package_folder_id_folder_name>/node_modules/<package_name>
|
2023-09-14 13:51:28 -04:00
|
|
|
let package_partitions =
|
2023-05-17 17:38:50 -04:00
|
|
|
snapshot.all_system_packages_partitioned(system_info);
|
2024-06-02 21:39:13 -04:00
|
|
|
let mut cache_futures = FuturesUnordered::new();
|
2023-05-24 15:04:21 -04:00
|
|
|
let mut newest_packages_by_name: HashMap<&String, &NpmResolutionPackage> =
|
|
|
|
HashMap::with_capacity(package_partitions.packages.len());
|
2024-06-06 18:37:41 -04:00
|
|
|
let bin_entries = Rc::new(RefCell::new(bin_entries::BinEntries::new()));
|
2024-07-09 23:06:08 -04:00
|
|
|
let mut packages_with_scripts = Vec::with_capacity(2);
|
|
|
|
let mut packages_with_scripts_not_run = Vec::new();
|
2022-11-08 14:17:24 -05:00
|
|
|
for package in &package_partitions.packages {
|
2023-05-24 15:04:21 -04:00
|
|
|
if let Some(current_pkg) =
|
2023-05-24 16:23:10 -04:00
|
|
|
newest_packages_by_name.get_mut(&package.id.nv.name)
|
2023-05-24 15:04:21 -04:00
|
|
|
{
|
2023-05-24 16:23:10 -04:00
|
|
|
if current_pkg.id.nv.cmp(&package.id.nv) == Ordering::Less {
|
2023-05-24 15:04:21 -04:00
|
|
|
*current_pkg = package;
|
|
|
|
}
|
|
|
|
} else {
|
2023-05-24 16:23:10 -04:00
|
|
|
newest_packages_by_name.insert(&package.id.nv.name, package);
|
2023-05-24 15:04:21 -04:00
|
|
|
};
|
|
|
|
|
2023-07-10 13:42:47 -04:00
|
|
|
let package_folder_name =
|
2022-11-08 14:17:24 -05:00
|
|
|
get_package_folder_id_folder_name(&package.get_package_cache_folder_id());
|
2023-07-10 13:42:47 -04:00
|
|
|
let folder_path = deno_local_registry_dir.join(&package_folder_name);
|
2022-11-08 14:17:24 -05:00
|
|
|
let initialized_file = folder_path.join(".initialized");
|
2022-11-27 13:25:08 -05:00
|
|
|
if !cache
|
|
|
|
.cache_setting()
|
2023-05-24 16:23:10 -04:00
|
|
|
.should_use_for_npm_package(&package.id.nv.name)
|
2022-11-08 14:17:24 -05:00
|
|
|
|| !initialized_file.exists()
|
|
|
|
{
|
2023-07-10 13:42:47 -04:00
|
|
|
// cache bust the dep from the dep setup cache so the symlinks
|
|
|
|
// are forced to be recreated
|
|
|
|
setup_cache.remove_dep(&package_folder_name);
|
|
|
|
|
2024-07-09 23:06:08 -04:00
|
|
|
let folder_path = folder_path.clone();
|
2024-05-29 20:45:22 -04:00
|
|
|
let bin_entries_to_setup = bin_entries.clone();
|
2024-06-02 21:39:13 -04:00
|
|
|
cache_futures.push(async move {
|
|
|
|
tarball_cache
|
2024-06-03 17:17:08 -04:00
|
|
|
.ensure_package(&package.id.nv, &package.dist)
|
2024-06-02 21:39:13 -04:00
|
|
|
.await?;
|
|
|
|
let pb_guard = progress_bar.update_with_prompt(
|
2023-03-13 14:18:29 -04:00
|
|
|
ProgressMessagePrompt::Initialize,
|
2023-05-24 16:23:10 -04:00
|
|
|
&package.id.nv.to_string(),
|
2023-03-13 14:18:29 -04:00
|
|
|
);
|
2022-09-23 17:35:48 -04:00
|
|
|
let sub_node_modules = folder_path.join("node_modules");
|
|
|
|
let package_path =
|
2023-05-24 16:23:10 -04:00
|
|
|
join_package_name(&sub_node_modules, &package.id.nv.name);
|
2024-06-02 21:39:13 -04:00
|
|
|
let cache_folder = cache.package_folder_for_nv(&package.id.nv);
|
2024-05-28 17:17:36 -04:00
|
|
|
|
|
|
|
deno_core::unsync::spawn_blocking({
|
|
|
|
let package_path = package_path.clone();
|
|
|
|
move || {
|
|
|
|
clone_dir_recursive(&cache_folder, &package_path)?;
|
|
|
|
// write out a file that indicates this folder has been initialized
|
|
|
|
fs::write(initialized_file, "")?;
|
|
|
|
|
|
|
|
Ok::<_, AnyError>(())
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.await??;
|
2024-05-23 19:43:38 -04:00
|
|
|
|
|
|
|
if package.bin.is_some() {
|
|
|
|
bin_entries_to_setup
|
2024-06-06 18:37:41 -04:00
|
|
|
.borrow_mut()
|
2024-05-29 20:45:22 -04:00
|
|
|
.add(package.clone(), package_path);
|
2024-05-23 19:43:38 -04:00
|
|
|
}
|
|
|
|
|
2023-03-13 14:18:29 -04:00
|
|
|
// finally stop showing the progress bar
|
|
|
|
drop(pb_guard); // explicit for clarity
|
2024-06-02 21:39:13 -04:00
|
|
|
Ok::<_, AnyError>(())
|
2022-09-23 17:35:48 -04:00
|
|
|
});
|
2022-09-22 11:17:02 -04:00
|
|
|
}
|
2024-07-09 23:06:08 -04:00
|
|
|
|
2024-07-11 14:39:45 -04:00
|
|
|
let sub_node_modules = folder_path.join("node_modules");
|
|
|
|
let package_path =
|
|
|
|
join_package_name(&sub_node_modules, &package.id.nv.name);
|
|
|
|
if has_lifecycle_scripts(package, &package_path) {
|
2024-07-09 23:06:08 -04:00
|
|
|
let scripts_run = folder_path.join(".scripts-run");
|
2024-07-11 12:41:42 -04:00
|
|
|
let has_warned = folder_path.join(".scripts-warned");
|
2024-07-09 23:06:08 -04:00
|
|
|
if can_run_scripts(&lifecycle_scripts.allowed, &package.id.nv) {
|
|
|
|
if !scripts_run.exists() {
|
|
|
|
packages_with_scripts.push((
|
|
|
|
package.clone(),
|
|
|
|
package_path,
|
|
|
|
scripts_run,
|
|
|
|
));
|
|
|
|
}
|
2024-07-11 12:41:42 -04:00
|
|
|
} else if !scripts_run.exists() && !has_warned.exists() {
|
|
|
|
packages_with_scripts_not_run.push((has_warned, package.id.nv.clone()));
|
2024-07-09 23:06:08 -04:00
|
|
|
}
|
|
|
|
}
|
2022-09-22 11:17:02 -04:00
|
|
|
}
|
|
|
|
|
2024-06-02 21:39:13 -04:00
|
|
|
while let Some(result) = cache_futures.next().await {
|
|
|
|
result?; // surface the first error
|
2022-09-23 17:35:48 -04:00
|
|
|
}
|
|
|
|
|
2022-11-08 14:17:24 -05:00
|
|
|
// 2. Create any "copy" packages, which are used for peer dependencies
|
|
|
|
for package in &package_partitions.copy_packages {
|
|
|
|
let package_cache_folder_id = package.get_package_cache_folder_id();
|
|
|
|
let destination_path = deno_local_registry_dir
|
2022-12-17 17:20:15 -05:00
|
|
|
.join(get_package_folder_id_folder_name(&package_cache_folder_id));
|
2022-11-08 14:17:24 -05:00
|
|
|
let initialized_file = destination_path.join(".initialized");
|
|
|
|
if !initialized_file.exists() {
|
|
|
|
let sub_node_modules = destination_path.join("node_modules");
|
2023-02-21 12:03:48 -05:00
|
|
|
let package_path =
|
2023-05-24 16:23:10 -04:00
|
|
|
join_package_name(&sub_node_modules, &package.id.nv.name);
|
2024-05-28 14:59:17 -04:00
|
|
|
|
2022-11-08 14:17:24 -05:00
|
|
|
let source_path = join_package_name(
|
|
|
|
&deno_local_registry_dir
|
2022-12-17 17:20:15 -05:00
|
|
|
.join(get_package_folder_id_folder_name(
|
2022-11-08 14:17:24 -05:00
|
|
|
&package_cache_folder_id.with_no_count(),
|
|
|
|
))
|
|
|
|
.join("node_modules"),
|
2023-05-24 16:23:10 -04:00
|
|
|
&package.id.nv.name,
|
2022-11-08 14:17:24 -05:00
|
|
|
);
|
2024-05-28 17:17:36 -04:00
|
|
|
|
2024-05-28 14:59:17 -04:00
|
|
|
clone_dir_recursive(&source_path, &package_path)?;
|
2022-11-08 14:17:24 -05:00
|
|
|
// write out a file that indicates this folder has been initialized
|
|
|
|
fs::write(initialized_file, "")?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 3. Symlink all the dependencies into the .deno directory.
|
2022-09-22 11:17:02 -04:00
|
|
|
//
|
|
|
|
// Symlink node_modules/.deno/<package_id>/node_modules/<dep_name> to
|
|
|
|
// node_modules/.deno/<dep_id>/node_modules/<dep_package_name>
|
2023-05-24 16:23:10 -04:00
|
|
|
for package in package_partitions.iter_all() {
|
2023-07-10 13:42:47 -04:00
|
|
|
let package_folder_name =
|
|
|
|
get_package_folder_id_folder_name(&package.get_package_cache_folder_id());
|
2022-09-22 11:17:02 -04:00
|
|
|
let sub_node_modules = deno_local_registry_dir
|
2023-07-10 13:42:47 -04:00
|
|
|
.join(&package_folder_name)
|
2022-09-22 11:17:02 -04:00
|
|
|
.join("node_modules");
|
2023-07-10 13:42:47 -04:00
|
|
|
let mut dep_setup_cache = setup_cache.with_dep(&package_folder_name);
|
2022-09-22 11:17:02 -04:00
|
|
|
for (name, dep_id) in &package.dependencies {
|
2023-12-06 14:24:00 -05:00
|
|
|
let dep = snapshot.package_from_id(dep_id).unwrap();
|
|
|
|
if package.optional_dependencies.contains(name)
|
|
|
|
&& !dep.system.matches_system(system_info)
|
|
|
|
{
|
|
|
|
continue; // this isn't a dependency for the current system
|
|
|
|
}
|
|
|
|
let dep_cache_folder_id = dep.get_package_cache_folder_id();
|
2022-11-08 14:17:24 -05:00
|
|
|
let dep_folder_name =
|
|
|
|
get_package_folder_id_folder_name(&dep_cache_folder_id);
|
2023-07-10 13:42:47 -04:00
|
|
|
if dep_setup_cache.insert(name, &dep_folder_name) {
|
|
|
|
let dep_folder_path = join_package_name(
|
|
|
|
&deno_local_registry_dir
|
|
|
|
.join(dep_folder_name)
|
|
|
|
.join("node_modules"),
|
|
|
|
&dep_id.nv.name,
|
|
|
|
);
|
|
|
|
symlink_package_dir(
|
|
|
|
&dep_folder_path,
|
|
|
|
&join_package_name(&sub_node_modules, name),
|
|
|
|
)?;
|
|
|
|
}
|
2022-09-22 11:17:02 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-16 16:30:28 -04:00
|
|
|
let mut found_names: HashMap<&String, &PackageNv> = HashMap::new();
|
|
|
|
|
|
|
|
// 4. Create symlinks for package json dependencies
|
|
|
|
{
|
|
|
|
for remote in pkg_json_deps_provider.remote_pkgs() {
|
|
|
|
let Some(remote_id) = snapshot
|
|
|
|
.resolve_best_package_id(&remote.req.name, &remote.req.version_req)
|
|
|
|
else {
|
|
|
|
continue; // skip, package not found
|
|
|
|
};
|
|
|
|
let remote_pkg = snapshot.package_from_id(&remote_id).unwrap();
|
|
|
|
let alias_clashes = remote.req.name != remote.alias
|
|
|
|
&& newest_packages_by_name.contains_key(&remote.alias);
|
|
|
|
let install_in_child = {
|
|
|
|
// we'll install in the child if the alias is taken by another package, or
|
|
|
|
// if there's already a package with the same name but different version
|
|
|
|
// linked into the root
|
|
|
|
match found_names.entry(&remote.alias) {
|
|
|
|
Entry::Occupied(nv) => {
|
|
|
|
alias_clashes
|
|
|
|
|| remote.req.name != nv.get().name // alias to a different package (in case of duplicate aliases)
|
|
|
|
|| !remote.req.version_req.matches(&nv.get().version) // incompatible version
|
|
|
|
}
|
|
|
|
Entry::Vacant(entry) => {
|
|
|
|
entry.insert(&remote_pkg.id.nv);
|
|
|
|
alias_clashes
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
let target_folder_name = get_package_folder_id_folder_name(
|
|
|
|
&remote_pkg.get_package_cache_folder_id(),
|
|
|
|
);
|
|
|
|
let local_registry_package_path = join_package_name(
|
|
|
|
&deno_local_registry_dir
|
|
|
|
.join(&target_folder_name)
|
|
|
|
.join("node_modules"),
|
|
|
|
&remote_pkg.id.nv.name,
|
|
|
|
);
|
|
|
|
if install_in_child {
|
|
|
|
// symlink the dep into the package's child node_modules folder
|
|
|
|
let dest_path =
|
|
|
|
remote.base_dir.join("node_modules").join(&remote.alias);
|
|
|
|
|
|
|
|
symlink_package_dir(&local_registry_package_path, &dest_path)?;
|
|
|
|
} else {
|
|
|
|
// symlink the package into `node_modules/<alias>`
|
|
|
|
if setup_cache
|
|
|
|
.insert_root_symlink(&remote_pkg.id.nv.name, &target_folder_name)
|
|
|
|
{
|
|
|
|
symlink_package_dir(
|
|
|
|
&local_registry_package_path,
|
|
|
|
&join_package_name(root_node_modules_dir_path, &remote.alias),
|
|
|
|
)?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 5. Create symlinks for the remaining top level packages in the node_modules folder.
|
|
|
|
// (These may be present if they are not in the package.json dependencies, such as )
|
|
|
|
// Symlink node_modules/.deno/<package_id>/node_modules/<package_name> to
|
|
|
|
// node_modules/<package_name>
|
|
|
|
let mut ids = snapshot
|
|
|
|
.top_level_packages()
|
|
|
|
.filter(|f| !found_names.contains_key(&f.nv.name))
|
|
|
|
.collect::<Vec<_>>();
|
2023-04-24 17:08:11 -04:00
|
|
|
ids.sort_by(|a, b| b.cmp(a)); // create determinism and only include the latest version
|
|
|
|
for id in ids {
|
2024-07-16 16:30:28 -04:00
|
|
|
match found_names.entry(&id.nv.name) {
|
|
|
|
Entry::Occupied(_) => {
|
|
|
|
continue; // skip, already handled
|
|
|
|
}
|
|
|
|
Entry::Vacant(entry) => {
|
|
|
|
entry.insert(&id.nv);
|
|
|
|
}
|
2023-05-24 15:04:21 -04:00
|
|
|
}
|
2023-04-06 18:46:44 -04:00
|
|
|
let package = snapshot.package_from_id(id).unwrap();
|
2023-07-10 13:42:47 -04:00
|
|
|
let target_folder_name =
|
|
|
|
get_package_folder_id_folder_name(&package.get_package_cache_folder_id());
|
|
|
|
if setup_cache.insert_root_symlink(&id.nv.name, &target_folder_name) {
|
|
|
|
let local_registry_package_path = join_package_name(
|
|
|
|
&deno_local_registry_dir
|
|
|
|
.join(target_folder_name)
|
|
|
|
.join("node_modules"),
|
|
|
|
&id.nv.name,
|
|
|
|
);
|
2022-09-22 11:17:02 -04:00
|
|
|
|
2023-07-10 13:42:47 -04:00
|
|
|
symlink_package_dir(
|
|
|
|
&local_registry_package_path,
|
|
|
|
&join_package_name(root_node_modules_dir_path, &id.nv.name),
|
|
|
|
)?;
|
|
|
|
}
|
2023-05-24 15:04:21 -04:00
|
|
|
}
|
|
|
|
|
2024-07-16 16:30:28 -04:00
|
|
|
// 6. Create a node_modules/.deno/node_modules/<package-name> directory with
|
2023-05-24 15:04:21 -04:00
|
|
|
// the remaining packages
|
|
|
|
for package in newest_packages_by_name.values() {
|
2024-07-16 16:30:28 -04:00
|
|
|
match found_names.entry(&package.id.nv.name) {
|
|
|
|
Entry::Occupied(_) => {
|
|
|
|
continue; // skip, already handled
|
|
|
|
}
|
|
|
|
Entry::Vacant(entry) => {
|
|
|
|
entry.insert(&package.id.nv);
|
|
|
|
}
|
2023-05-24 15:04:21 -04:00
|
|
|
}
|
|
|
|
|
2023-07-10 13:42:47 -04:00
|
|
|
let target_folder_name =
|
|
|
|
get_package_folder_id_folder_name(&package.get_package_cache_folder_id());
|
|
|
|
if setup_cache.insert_deno_symlink(&package.id.nv.name, &target_folder_name)
|
|
|
|
{
|
|
|
|
let local_registry_package_path = join_package_name(
|
|
|
|
&deno_local_registry_dir
|
|
|
|
.join(target_folder_name)
|
|
|
|
.join("node_modules"),
|
|
|
|
&package.id.nv.name,
|
|
|
|
);
|
2023-05-24 15:04:21 -04:00
|
|
|
|
2023-07-10 13:42:47 -04:00
|
|
|
symlink_package_dir(
|
|
|
|
&local_registry_package_path,
|
|
|
|
&join_package_name(&deno_node_modules_dir, &package.id.nv.name),
|
|
|
|
)?;
|
|
|
|
}
|
2022-09-22 11:17:02 -04:00
|
|
|
}
|
|
|
|
|
2024-07-16 16:30:28 -04:00
|
|
|
// 7. Set up `node_modules/.bin` entries for packages that need it.
|
2024-05-23 19:43:38 -04:00
|
|
|
{
|
2024-06-06 18:37:41 -04:00
|
|
|
let bin_entries = std::mem::take(&mut *bin_entries.borrow_mut());
|
2024-05-29 20:45:22 -04:00
|
|
|
bin_entries.finish(snapshot, &bin_node_modules_dir_path)?;
|
2024-05-23 19:43:38 -04:00
|
|
|
}
|
|
|
|
|
2024-07-16 16:30:28 -04:00
|
|
|
// 8. Create symlinks for the workspace packages
|
2024-07-03 20:54:33 -04:00
|
|
|
{
|
|
|
|
// todo(#24419): this is not exactly correct because it should
|
|
|
|
// install correctly for a workspace (potentially in sub directories),
|
|
|
|
// but this is good enough for a first pass
|
|
|
|
for workspace in pkg_json_deps_provider.workspace_pkgs() {
|
|
|
|
symlink_package_dir(
|
2024-07-15 15:08:51 -04:00
|
|
|
&workspace.target_dir,
|
2024-07-03 20:54:33 -04:00
|
|
|
&root_node_modules_dir_path.join(&workspace.alias),
|
|
|
|
)?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-09 23:06:08 -04:00
|
|
|
if !packages_with_scripts.is_empty() {
|
|
|
|
// get custom commands for each bin available in the node_modules dir (essentially
|
|
|
|
// the scripts that are in `node_modules/.bin`)
|
|
|
|
let base = resolve_baseline_custom_commands(
|
|
|
|
snapshot,
|
|
|
|
&package_partitions.packages,
|
|
|
|
&deno_local_registry_dir,
|
|
|
|
)?;
|
|
|
|
let init_cwd = lifecycle_scripts.initial_cwd.as_deref().unwrap();
|
2024-07-15 15:11:09 -04:00
|
|
|
let process_state = crate::npm::managed::npm_process_state(
|
|
|
|
snapshot.as_valid_serialized(),
|
|
|
|
Some(root_node_modules_dir_path),
|
|
|
|
);
|
2024-07-09 23:06:08 -04:00
|
|
|
|
2024-07-15 15:11:09 -04:00
|
|
|
let mut env_vars = crate::task_runner::real_env_vars();
|
|
|
|
env_vars.insert(
|
|
|
|
crate::args::NPM_RESOLUTION_STATE_ENV_VAR_NAME.to_string(),
|
|
|
|
process_state,
|
|
|
|
);
|
2024-07-09 23:06:08 -04:00
|
|
|
for (package, package_path, scripts_run_path) in packages_with_scripts {
|
|
|
|
// add custom commands for binaries from the package's dependencies. this will take precedence over the
|
|
|
|
// baseline commands, so if the package relies on a bin that conflicts with one higher in the dependency tree, the
|
|
|
|
// correct bin will be used.
|
|
|
|
let custom_commands = resolve_custom_commands_from_deps(
|
|
|
|
base.clone(),
|
|
|
|
&package,
|
|
|
|
snapshot,
|
|
|
|
&deno_local_registry_dir,
|
|
|
|
)?;
|
|
|
|
for script_name in ["preinstall", "install", "postinstall"] {
|
|
|
|
if let Some(script) = package.scripts.get(script_name) {
|
2024-07-11 14:39:45 -04:00
|
|
|
if script_name == "install"
|
|
|
|
&& is_broken_default_install_script(script, &package_path)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2024-07-09 23:06:08 -04:00
|
|
|
let exit_code =
|
|
|
|
crate::task_runner::run_task(crate::task_runner::RunTaskOptions {
|
|
|
|
task_name: script_name,
|
|
|
|
script,
|
|
|
|
cwd: &package_path,
|
2024-07-15 15:11:09 -04:00
|
|
|
env_vars: env_vars.clone(),
|
2024-07-09 23:06:08 -04:00
|
|
|
custom_commands: custom_commands.clone(),
|
|
|
|
init_cwd,
|
|
|
|
argv: &[],
|
|
|
|
root_node_modules_dir: Some(root_node_modules_dir_path),
|
|
|
|
})
|
|
|
|
.await?;
|
|
|
|
if exit_code != 0 {
|
|
|
|
anyhow::bail!(
|
|
|
|
"script '{}' in '{}' failed with exit code {}",
|
|
|
|
script_name,
|
|
|
|
package.id.nv,
|
|
|
|
exit_code,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fs::write(scripts_run_path, "")?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !packages_with_scripts_not_run.is_empty() {
|
|
|
|
let (maybe_install, maybe_install_example) = if *crate::args::DENO_FUTURE {
|
|
|
|
(
|
|
|
|
" or `deno install`",
|
|
|
|
" or `deno install --allow-scripts=pkg1,pkg2`",
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
("", "")
|
|
|
|
};
|
|
|
|
let packages = packages_with_scripts_not_run
|
|
|
|
.iter()
|
2024-07-11 12:41:42 -04:00
|
|
|
.map(|(_, p)| format!("npm:{p}"))
|
2024-07-09 23:06:08 -04:00
|
|
|
.collect::<Vec<_>>()
|
|
|
|
.join(", ");
|
|
|
|
log::warn!("{}: Packages contained npm lifecycle scripts (preinstall/install/postinstall) that were not executed.
|
|
|
|
This may cause the packages to not work correctly. To run them, use the `--allow-scripts` flag with `deno cache`{maybe_install}
|
|
|
|
(e.g. `deno cache --allow-scripts=pkg1,pkg2 <entrypoint>`{maybe_install_example}):\n {packages}", crate::colors::yellow("warning"));
|
2024-07-11 12:41:42 -04:00
|
|
|
for (scripts_warned_path, _) in packages_with_scripts_not_run {
|
|
|
|
let _ignore_err = fs::write(scripts_warned_path, "");
|
|
|
|
}
|
2024-07-09 23:06:08 -04:00
|
|
|
}
|
|
|
|
|
2023-07-10 13:42:47 -04:00
|
|
|
setup_cache.save();
|
2023-03-08 10:13:13 -05:00
|
|
|
drop(single_process_lock);
|
2023-03-13 14:18:29 -04:00
|
|
|
drop(pb_clear_guard);
|
2023-03-08 10:13:13 -05:00
|
|
|
|
2022-09-22 11:17:02 -04:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2024-07-09 14:04:21 -04:00
|
|
|
// Uses BTreeMap to preserve the ordering of the elements in memory, to ensure
|
|
|
|
// the file generated from this datastructure is deterministic.
|
|
|
|
// See: https://github.com/denoland/deno/issues/24479
|
2023-07-10 13:42:47 -04:00
|
|
|
/// Represents a dependency at `node_modules/.deno/<package_id>/`
|
|
|
|
struct SetupCacheDep<'a> {
|
2024-07-09 14:04:21 -04:00
|
|
|
previous: Option<&'a BTreeMap<String, String>>,
|
|
|
|
current: &'a mut BTreeMap<String, String>,
|
2023-07-10 13:42:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> SetupCacheDep<'a> {
|
|
|
|
pub fn insert(&mut self, name: &str, target_folder_name: &str) -> bool {
|
|
|
|
self
|
|
|
|
.current
|
|
|
|
.insert(name.to_string(), target_folder_name.to_string());
|
|
|
|
if let Some(previous_target) = self.previous.and_then(|p| p.get(name)) {
|
|
|
|
previous_target != target_folder_name
|
|
|
|
} else {
|
|
|
|
true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-09 14:04:21 -04:00
|
|
|
// Uses BTreeMap to preserve the ordering of the elements in memory, to ensure
|
|
|
|
// the file generated from this datastructure is deterministic.
|
|
|
|
// See: https://github.com/denoland/deno/issues/24479
|
2023-07-10 13:42:47 -04:00
|
|
|
#[derive(Debug, Default, Serialize, Deserialize, PartialEq, Eq)]
|
|
|
|
struct SetupCacheData {
|
2024-07-09 14:04:21 -04:00
|
|
|
root_symlinks: BTreeMap<String, String>,
|
|
|
|
deno_symlinks: BTreeMap<String, String>,
|
|
|
|
dep_symlinks: BTreeMap<String, BTreeMap<String, String>>,
|
2023-07-10 13:42:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// It is very slow to try to re-setup the symlinks each time, so this will
|
|
|
|
/// cache what we've setup on the last run and only update what is necessary.
|
|
|
|
/// Obviously this could lead to issues if the cache gets out of date with the
|
|
|
|
/// file system, such as if the user manually deletes a symlink.
|
|
|
|
struct SetupCache {
|
|
|
|
file_path: PathBuf,
|
|
|
|
previous: Option<SetupCacheData>,
|
|
|
|
current: SetupCacheData,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl SetupCache {
|
|
|
|
pub fn load(file_path: PathBuf) -> Self {
|
|
|
|
let previous = std::fs::read(&file_path)
|
|
|
|
.ok()
|
|
|
|
.and_then(|data| bincode::deserialize(&data).ok());
|
|
|
|
Self {
|
|
|
|
file_path,
|
|
|
|
previous,
|
|
|
|
current: Default::default(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn save(&self) -> bool {
|
|
|
|
if let Some(previous) = &self.previous {
|
|
|
|
if previous == &self.current {
|
|
|
|
return false; // nothing to save
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bincode::serialize(&self.current).ok().and_then(|data| {
|
2024-06-07 13:06:17 -04:00
|
|
|
atomic_write_file_with_retries(&self.file_path, data, CACHE_PERM).ok()
|
2023-07-10 13:42:47 -04:00
|
|
|
});
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Inserts and checks for the existence of a root symlink
|
|
|
|
/// at `node_modules/<package_name>` pointing to
|
|
|
|
/// `node_modules/.deno/<package_id>/`
|
|
|
|
pub fn insert_root_symlink(
|
|
|
|
&mut self,
|
|
|
|
name: &str,
|
|
|
|
target_folder_name: &str,
|
|
|
|
) -> bool {
|
|
|
|
self
|
|
|
|
.current
|
|
|
|
.root_symlinks
|
|
|
|
.insert(name.to_string(), target_folder_name.to_string());
|
|
|
|
if let Some(previous_target) = self
|
|
|
|
.previous
|
|
|
|
.as_ref()
|
|
|
|
.and_then(|p| p.root_symlinks.get(name))
|
|
|
|
{
|
|
|
|
previous_target != target_folder_name
|
|
|
|
} else {
|
|
|
|
true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Inserts and checks for the existence of a symlink at
|
|
|
|
/// `node_modules/.deno/node_modules/<package_name>` pointing to
|
|
|
|
/// `node_modules/.deno/<package_id>/`
|
|
|
|
pub fn insert_deno_symlink(
|
|
|
|
&mut self,
|
|
|
|
name: &str,
|
|
|
|
target_folder_name: &str,
|
|
|
|
) -> bool {
|
|
|
|
self
|
|
|
|
.current
|
|
|
|
.deno_symlinks
|
|
|
|
.insert(name.to_string(), target_folder_name.to_string());
|
|
|
|
if let Some(previous_target) = self
|
|
|
|
.previous
|
|
|
|
.as_ref()
|
|
|
|
.and_then(|p| p.deno_symlinks.get(name))
|
|
|
|
{
|
|
|
|
previous_target != target_folder_name
|
|
|
|
} else {
|
|
|
|
true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn remove_dep(&mut self, parent_name: &str) {
|
|
|
|
if let Some(previous) = &mut self.previous {
|
|
|
|
previous.dep_symlinks.remove(parent_name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn with_dep(&mut self, parent_name: &str) -> SetupCacheDep<'_> {
|
|
|
|
SetupCacheDep {
|
|
|
|
previous: self
|
|
|
|
.previous
|
|
|
|
.as_ref()
|
|
|
|
.and_then(|p| p.dep_symlinks.get(parent_name)),
|
|
|
|
current: self
|
|
|
|
.current
|
|
|
|
.dep_symlinks
|
|
|
|
.entry(parent_name.to_string())
|
|
|
|
.or_default(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-21 12:03:48 -05:00
|
|
|
fn get_package_folder_id_folder_name(
|
|
|
|
folder_id: &NpmPackageCacheFolderId,
|
|
|
|
) -> String {
|
|
|
|
let copy_str = if folder_id.copy_index == 0 {
|
2022-11-08 14:17:24 -05:00
|
|
|
"".to_string()
|
|
|
|
} else {
|
2023-02-21 12:03:48 -05:00
|
|
|
format!("_{}", folder_id.copy_index)
|
2022-11-08 14:17:24 -05:00
|
|
|
};
|
2023-02-21 12:03:48 -05:00
|
|
|
let nv = &folder_id.nv;
|
|
|
|
let name = if nv.name.to_lowercase() == nv.name {
|
|
|
|
Cow::Borrowed(&nv.name)
|
2022-11-16 13:44:31 -05:00
|
|
|
} else {
|
2023-02-21 12:03:48 -05:00
|
|
|
Cow::Owned(format!("_{}", mixed_case_package_name_encode(&nv.name)))
|
2022-11-16 13:44:31 -05:00
|
|
|
};
|
2023-02-21 12:03:48 -05:00
|
|
|
format!("{}@{}{}", name, nv.version, copy_str).replace('/', "+")
|
2022-11-08 14:17:24 -05:00
|
|
|
}
|
|
|
|
|
2023-07-01 21:07:57 -04:00
|
|
|
fn get_package_folder_id_from_folder_name(
|
|
|
|
folder_name: &str,
|
|
|
|
) -> Option<NpmPackageCacheFolderId> {
|
|
|
|
let folder_name = folder_name.replace('+', "/");
|
|
|
|
let (name, ending) = folder_name.rsplit_once('@')?;
|
|
|
|
let name = if let Some(encoded_name) = name.strip_prefix('_') {
|
|
|
|
mixed_case_package_name_decode(encoded_name)?
|
|
|
|
} else {
|
|
|
|
name.to_string()
|
|
|
|
};
|
|
|
|
let (raw_version, copy_index) = match ending.split_once('_') {
|
|
|
|
Some((raw_version, copy_index)) => {
|
|
|
|
let copy_index = copy_index.parse::<u8>().ok()?;
|
|
|
|
(raw_version, copy_index)
|
|
|
|
}
|
|
|
|
None => (ending, 0),
|
|
|
|
};
|
|
|
|
let version = deno_semver::Version::parse_from_npm(raw_version).ok()?;
|
|
|
|
Some(NpmPackageCacheFolderId {
|
2023-08-21 05:53:52 -04:00
|
|
|
nv: PackageNv { name, version },
|
2023-07-01 21:07:57 -04:00
|
|
|
copy_index,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-09-22 11:17:02 -04:00
|
|
|
fn symlink_package_dir(
|
|
|
|
old_path: &Path,
|
|
|
|
new_path: &Path,
|
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
let new_parent = new_path.parent().unwrap();
|
|
|
|
if new_parent.file_name().unwrap() != "node_modules" {
|
|
|
|
// create the parent folder that will contain the symlink
|
|
|
|
fs::create_dir_all(new_parent)
|
|
|
|
.with_context(|| format!("Creating '{}'", new_parent.display()))?;
|
|
|
|
}
|
|
|
|
|
|
|
|
// need to delete the previous symlink before creating a new one
|
|
|
|
let _ignore = fs::remove_dir_all(new_path);
|
2022-09-27 18:02:35 -04:00
|
|
|
|
|
|
|
#[cfg(windows)]
|
|
|
|
return junction_or_symlink_dir(old_path, new_path);
|
|
|
|
#[cfg(not(windows))]
|
2022-11-28 17:28:54 -05:00
|
|
|
symlink_dir(old_path, new_path)
|
2022-09-22 11:17:02 -04:00
|
|
|
}
|
|
|
|
|
2022-09-27 18:02:35 -04:00
|
|
|
#[cfg(windows)]
|
|
|
|
fn junction_or_symlink_dir(
|
|
|
|
old_path: &Path,
|
|
|
|
new_path: &Path,
|
|
|
|
) -> Result<(), AnyError> {
|
2024-07-09 12:15:03 -04:00
|
|
|
use deno_core::anyhow::bail;
|
2022-09-27 18:02:35 -04:00
|
|
|
// Use junctions because they're supported on ntfs file systems without
|
|
|
|
// needing to elevate privileges on Windows
|
2022-11-28 17:28:54 -05:00
|
|
|
|
2022-09-27 18:02:35 -04:00
|
|
|
match junction::create(old_path, new_path) {
|
|
|
|
Ok(()) => Ok(()),
|
|
|
|
Err(junction_err) => {
|
|
|
|
if cfg!(debug) {
|
|
|
|
// When running the tests, junctions should be created, but if not then
|
|
|
|
// surface this error.
|
|
|
|
log::warn!("Error creating junction. {:#}", junction_err);
|
|
|
|
}
|
|
|
|
|
2022-11-28 17:28:54 -05:00
|
|
|
match symlink_dir(old_path, new_path) {
|
2022-09-27 18:02:35 -04:00
|
|
|
Ok(()) => Ok(()),
|
|
|
|
Err(symlink_err) => bail!(
|
|
|
|
concat!(
|
|
|
|
"Failed creating junction and fallback symlink in node_modules folder.\n\n",
|
|
|
|
"{:#}\n\n{:#}",
|
|
|
|
),
|
|
|
|
junction_err,
|
|
|
|
symlink_err,
|
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-22 11:17:02 -04:00
|
|
|
fn join_package_name(path: &Path, package_name: &str) -> PathBuf {
|
|
|
|
let mut path = path.to_path_buf();
|
|
|
|
// ensure backslashes are used on windows
|
|
|
|
for part in package_name.split('/') {
|
|
|
|
path = path.join(part);
|
|
|
|
}
|
|
|
|
path
|
|
|
|
}
|
2023-07-01 21:07:57 -04:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use deno_npm::NpmPackageCacheFolderId;
|
2023-08-21 05:53:52 -04:00
|
|
|
use deno_semver::package::PackageNv;
|
2023-07-10 13:42:47 -04:00
|
|
|
use test_util::TempDir;
|
2023-07-01 21:07:57 -04:00
|
|
|
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_get_package_folder_id_folder_name() {
|
|
|
|
let cases = vec![
|
|
|
|
(
|
|
|
|
NpmPackageCacheFolderId {
|
2023-08-21 05:53:52 -04:00
|
|
|
nv: PackageNv::from_str("@types/foo@1.2.3").unwrap(),
|
2023-07-01 21:07:57 -04:00
|
|
|
copy_index: 1,
|
|
|
|
},
|
|
|
|
"@types+foo@1.2.3_1".to_string(),
|
|
|
|
),
|
|
|
|
(
|
|
|
|
NpmPackageCacheFolderId {
|
2023-08-21 05:53:52 -04:00
|
|
|
nv: PackageNv::from_str("JSON@3.2.1").unwrap(),
|
2023-07-01 21:07:57 -04:00
|
|
|
copy_index: 0,
|
|
|
|
},
|
|
|
|
"_jjju6tq@3.2.1".to_string(),
|
|
|
|
),
|
|
|
|
];
|
|
|
|
for (input, output) in cases {
|
|
|
|
assert_eq!(get_package_folder_id_folder_name(&input), output);
|
|
|
|
let folder_id = get_package_folder_id_from_folder_name(&output).unwrap();
|
|
|
|
assert_eq!(folder_id, input);
|
|
|
|
}
|
|
|
|
}
|
2023-07-10 13:42:47 -04:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_setup_cache() {
|
|
|
|
let temp_dir = TempDir::new();
|
|
|
|
let cache_bin_path = temp_dir.path().join("cache.bin").to_path_buf();
|
|
|
|
let mut cache = SetupCache::load(cache_bin_path.clone());
|
|
|
|
assert!(cache.insert_deno_symlink("package-a", "package-a@1.0.0"));
|
|
|
|
assert!(cache.insert_root_symlink("package-a", "package-a@1.0.0"));
|
|
|
|
assert!(cache
|
|
|
|
.with_dep("package-a")
|
|
|
|
.insert("package-b", "package-b@1.0.0"));
|
|
|
|
assert!(cache.save());
|
|
|
|
|
|
|
|
let mut cache = SetupCache::load(cache_bin_path.clone());
|
|
|
|
assert!(!cache.insert_deno_symlink("package-a", "package-a@1.0.0"));
|
|
|
|
assert!(!cache.insert_root_symlink("package-a", "package-a@1.0.0"));
|
|
|
|
assert!(!cache
|
|
|
|
.with_dep("package-a")
|
|
|
|
.insert("package-b", "package-b@1.0.0"));
|
|
|
|
assert!(!cache.save());
|
|
|
|
assert!(cache.insert_root_symlink("package-b", "package-b@0.2.0"));
|
|
|
|
assert!(cache.save());
|
|
|
|
|
|
|
|
let mut cache = SetupCache::load(cache_bin_path);
|
|
|
|
cache.remove_dep("package-a");
|
|
|
|
assert!(cache
|
|
|
|
.with_dep("package-a")
|
|
|
|
.insert("package-b", "package-b@1.0.0"));
|
|
|
|
}
|
2023-07-01 21:07:57 -04:00
|
|
|
}
|