Skip to content

Commit

Permalink
Merge pull request #1567 from iNViTiON/main
Browse files Browse the repository at this point in the history
Fetch Native Windows on ARM Node builds for Node 20+
  • Loading branch information
charlespierce committed May 13, 2024
2 parents 7e0c08e + be55375 commit 08219c5
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 12 deletions.
15 changes: 12 additions & 3 deletions crates/volta-core/src/tool/node/metadata.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use std::collections::HashSet;

use super::NODE_DISTRO_IDENTIFIER;
#[cfg(all(target_os = "macos", target_arch = "aarch64"))]
#[cfg(any(
all(target_os = "macos", target_arch = "aarch64"),
all(target_os = "windows", target_arch = "aarch64")
))]
use super::NODE_DISTRO_IDENTIFIER_FALLBACK;
use crate::version::{option_version_serde, version_serde};
use node_semver::Version;
Expand Down Expand Up @@ -39,7 +42,10 @@ impl From<RawNodeIndex> for NodeIndex {
.0
.into_iter()
.filter_map(|entry| {
#[cfg(not(all(target_os = "macos", target_arch = "aarch64")))]
#[cfg(not(any(
all(target_os = "macos", target_arch = "aarch64"),
all(target_os = "windows", target_arch = "aarch64")
)))]
if entry.npm.is_some() && entry.files.contains(NODE_DISTRO_IDENTIFIER) {
Some(NodeEntry {
version: entry.version,
Expand All @@ -49,7 +55,10 @@ impl From<RawNodeIndex> for NodeIndex {
None
}

#[cfg(all(target_os = "macos", target_arch = "aarch64"))]
#[cfg(any(
all(target_os = "macos", target_arch = "aarch64"),
all(target_os = "windows", target_arch = "aarch64")
))]
if entry.npm.is_some()
&& (entry.files.contains(NODE_DISTRO_IDENTIFIER)
|| entry.files.contains(NODE_DISTRO_IDENTIFIER_FALLBACK))
Expand Down
78 changes: 69 additions & 9 deletions crates/volta-core/src/tool/node/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,23 @@ cfg_if! {
pub const NODE_DISTRO_EXTENSION: &str = "zip";
/// The file identifier in the Node index `files` array
pub const NODE_DISTRO_IDENTIFIER: &str = "win-x64-zip";
} else if #[cfg(all(target_os = "windows", target_arch = "aarch64"))] {
/// The OS component of a Node distro filename
pub const NODE_DISTRO_OS: &str = "win";
/// The architecture component of a Node distro filename
pub const NODE_DISTRO_ARCH: &str = "arm64";
/// The extension for Node distro files
pub const NODE_DISTRO_EXTENSION: &str = "zip";
/// The file identifier in the Node index `files` array
pub const NODE_DISTRO_IDENTIFIER: &str = "win-arm64-zip";

// NOTE: Node support for pre-built ARM64 binaries on Windows was added in major version 20
// For versions prior to that, we need to fall back on the x64 binaries via emulator

/// The fallback architecture component of a Node distro filename
pub const NODE_DISTRO_ARCH_FALLBACK: &str = "x64";
/// The fallback file identifier in the Node index `files` array
pub const NODE_DISTRO_IDENTIFIER_FALLBACK: &str = "win-x64-zip";
} else if #[cfg(all(target_os = "macos", target_arch = "x86_64"))] {
/// The OS component of a Node distro filename
pub const NODE_DISTRO_OS: &str = "darwin";
Expand Down Expand Up @@ -129,7 +146,10 @@ impl Node {
Node { version }
}

#[cfg(not(all(target_os = "macos", target_arch = "aarch64")))]
#[cfg(not(any(
all(target_os = "macos", target_arch = "aarch64"),
all(target_os = "windows", target_arch = "aarch64")
)))]
pub fn archive_basename(version: &Version) -> String {
format!("node-v{}-{}-{}", version, NODE_DISTRO_OS, NODE_DISTRO_ARCH)
}
Expand All @@ -150,6 +170,22 @@ impl Node {
)
}

#[cfg(all(target_os = "windows", target_arch = "aarch64"))]
pub fn archive_basename(version: &Version) -> String {
// Note: Node began shipping pre-built binaries for Windows ARM with Major version 20
// Prior to that, we need to fall back on the x64 binaries
format!(
"node-v{}-{}-{}",
version,
NODE_DISTRO_OS,
if version.major >= 20 {
NODE_DISTRO_ARCH
} else {
NODE_DISTRO_ARCH_FALLBACK
}
)
}

pub fn archive_filename(version: &Version) -> String {
format!(
"{}.{}",
Expand Down Expand Up @@ -259,17 +295,17 @@ mod tests {
#[test]
fn test_node_archive_basename() {
assert_eq!(
Node::archive_basename(&Version::parse("16.2.3").unwrap()),
format!("node-v16.2.3-{}-{}", NODE_DISTRO_OS, NODE_DISTRO_ARCH)
Node::archive_basename(&Version::parse("20.2.3").unwrap()),
format!("node-v20.2.3-{}-{}", NODE_DISTRO_OS, NODE_DISTRO_ARCH)
);
}

#[test]
fn test_node_archive_filename() {
assert_eq!(
Node::archive_filename(&Version::parse("16.2.3").unwrap()),
Node::archive_filename(&Version::parse("20.2.3").unwrap()),
format!(
"node-v16.2.3-{}-{}.{}",
"node-v20.2.3-{}-{}.{}",
NODE_DISTRO_OS, NODE_DISTRO_ARCH, NODE_DISTRO_EXTENSION
)
);
Expand All @@ -279,9 +315,21 @@ mod tests {
#[cfg(all(target_os = "macos", target_arch = "aarch64"))]
fn test_fallback_node_archive_basename() {
assert_eq!(
Node::archive_basename(&Version::parse("1.2.3").unwrap()),
Node::archive_basename(&Version::parse("15.2.3").unwrap()),
format!(
"node-v1.2.3-{}-{}",
"node-v15.2.3-{}-{}",
NODE_DISTRO_OS, NODE_DISTRO_ARCH_FALLBACK
)
);
}

#[test]
#[cfg(all(target_os = "windows", target_arch = "aarch64"))]
fn test_fallback_node_archive_basename() {
assert_eq!(
Node::archive_basename(&Version::parse("19.2.3").unwrap()),
format!(
"node-v19.2.3-{}-{}",
NODE_DISTRO_OS, NODE_DISTRO_ARCH_FALLBACK
)
);
Expand All @@ -291,9 +339,21 @@ mod tests {
#[cfg(all(target_os = "macos", target_arch = "aarch64"))]
fn test_fallback_node_archive_filename() {
assert_eq!(
Node::archive_filename(&Version::parse("1.2.3").unwrap()),
Node::archive_filename(&Version::parse("15.2.3").unwrap()),
format!(
"node-v15.2.3-{}-{}.{}",
NODE_DISTRO_OS, NODE_DISTRO_ARCH_FALLBACK, NODE_DISTRO_EXTENSION
)
);
}

#[test]
#[cfg(all(target_os = "windows", target_arch = "aarch64"))]
fn test_fallback_node_archive_filename() {
assert_eq!(
Node::archive_filename(&Version::parse("19.2.3").unwrap()),
format!(
"node-v1.2.3-{}-{}.{}",
"node-v19.2.3-{}-{}.{}",
NODE_DISTRO_OS, NODE_DISTRO_ARCH_FALLBACK, NODE_DISTRO_EXTENSION
)
);
Expand Down

0 comments on commit 08219c5

Please sign in to comment.