From 41f618a1df6bb8c66d7968ac64456139b9f4c197 Mon Sep 17 00:00:00 2001 From: David Sherret Date: Wed, 17 May 2023 17:38:50 -0400 Subject: fix(npm): improved optional dependency support (#19135) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Note: If the package information has already been cached, then this requires running with `--reload` or for the registry information to be fetched some other way (ex. the cache busting). Closes #15544 --------- Co-authored-by: Bartek IwaƄczuk --- cli/args/lockfile.rs | 20 +++++++++++++++----- cli/args/mod.rs | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 5 deletions(-) (limited to 'cli/args') diff --git a/cli/args/lockfile.rs b/cli/args/lockfile.rs index 8cb21781a..aa7e51fa1 100644 --- a/cli/args/lockfile.rs +++ b/cli/args/lockfile.rs @@ -107,8 +107,12 @@ pub async fn snapshot_from_lockfile( packages.push(SerializedNpmResolutionSnapshotPackage { pkg_id, - dist: Default::default(), // temporarily empty dependencies, + // temporarily empty + os: Default::default(), + cpu: Default::default(), + dist: Default::default(), + optional_dependencies: Default::default(), }); } (root_packages, packages) @@ -131,11 +135,17 @@ pub async fn snapshot_from_lockfile( })) }; let mut version_infos = get_version_infos(); - let mut i = 0; while let Some(result) = version_infos.next().await { - packages[i].dist = match result { - Ok(version_info) => version_info.dist, + match result { + Ok(version_info) => { + let mut package = &mut packages[i]; + package.dist = version_info.dist; + package.cpu = version_info.cpu; + package.os = version_info.os; + package.optional_dependencies = + version_info.optional_dependencies.into_keys().collect(); + } Err(err) => { if api.mark_force_reload() { // reset and try again @@ -146,7 +156,7 @@ pub async fn snapshot_from_lockfile( return Err(err); } } - }; + } i += 1; } diff --git a/cli/args/mod.rs b/cli/args/mod.rs index 31035fdd0..53dad9cae 100644 --- a/cli/args/mod.rs +++ b/cli/args/mod.rs @@ -13,6 +13,7 @@ use self::package_json::PackageJsonDeps; use ::import_map::ImportMap; use deno_core::resolve_url_or_path; use deno_npm::resolution::ValidSerializedNpmResolutionSnapshot; +use deno_npm::NpmSystemInfo; use deno_runtime::deno_tls::RootCertStoreProvider; use deno_semver::npm::NpmPackageReqReference; use indexmap::IndexMap; @@ -688,6 +689,42 @@ impl CliOptions { } } + pub fn npm_system_info(&self) -> NpmSystemInfo { + match self.sub_command() { + DenoSubcommand::Compile(CompileFlags { + target: Some(target), + .. + }) => { + // the values of NpmSystemInfo align with the possible values for the + // `arch` and `platform` fields of Node.js' `process` global: + // https://nodejs.org/api/process.html + match target.as_str() { + "aarch64-apple-darwin" => NpmSystemInfo { + os: "darwin".to_string(), + cpu: "arm64".to_string(), + }, + "x86_64-apple-darwin" => NpmSystemInfo { + os: "darwin".to_string(), + cpu: "x64".to_string(), + }, + "x86_64-unknown-linux-gnu" => NpmSystemInfo { + os: "linux".to_string(), + cpu: "x64".to_string(), + }, + "x86_64-pc-windows-msvc" => NpmSystemInfo { + os: "win32".to_string(), + cpu: "x64".to_string(), + }, + value => { + log::warn!("Not implemented NPM system info for target '{value}'. Using current system default. This may impact NPM "); + NpmSystemInfo::default() + } + } + } + _ => NpmSystemInfo::default(), + } + } + pub fn resolve_deno_dir(&self) -> Result { Ok(DenoDir::new(self.maybe_custom_root())?) } -- cgit v1.2.3