From 182acad73c43ad50f8fb5768855773f9a62b589e Mon Sep 17 00:00:00 2001 From: Jake Runzer Date: Fri, 10 Jan 2025 18:23:04 -0500 Subject: [PATCH 1/2] select latest lts node version when find latest version that matches range --- src/providers/node/mod.rs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/providers/node/mod.rs b/src/providers/node/mod.rs index 6e7aed6d1..dbe53f2b2 100644 --- a/src/providers/node/mod.rs +++ b/src/providers/node/mod.rs @@ -634,9 +634,14 @@ fn parse_node_version_into_pkg(node_version: &str) -> String { eprintln!("Warning: node version {node_version} is not valid, using default node version {default_node_pkg_name}"); Range::parse(DEFAULT_NODE_VERSION.to_string()).unwrap() }); - let mut available_node_versions = AVAILABLE_NODE_VERSIONS.to_vec(); + let mut available_lts_node_versions = AVAILABLE_NODE_VERSIONS + .to_vec() + .iter() + .filter(|v| v % 2 == 0) + .collect::>(); + // use newest node version first - available_node_versions.sort_by(|a, b| b.cmp(a)); + available_lts_node_versions.sort_by(|a, b| b.cmp(a)); for version_number in available_node_versions { let version_range_string = format!("{version_number}.x.x"); let version_range: Range = version_range_string.parse().unwrap(); From e315b2af3e3a13b157287a738c23022580561c9c Mon Sep 17 00:00:00 2001 From: Jake Runzer Date: Fri, 10 Jan 2025 18:26:39 -0500 Subject: [PATCH 2/2] add test --- src/providers/node/mod.rs | 25 ++++++++++++++++--- ...nerate_plan_tests__node_pnpm_corepack.snap | 3 +-- .../generate_plan_tests__node_turborepo.snap | 3 +-- 3 files changed, 23 insertions(+), 8 deletions(-) diff --git a/src/providers/node/mod.rs b/src/providers/node/mod.rs index dbe53f2b2..e98fee186 100644 --- a/src/providers/node/mod.rs +++ b/src/providers/node/mod.rs @@ -635,18 +635,17 @@ fn parse_node_version_into_pkg(node_version: &str) -> String { Range::parse(DEFAULT_NODE_VERSION.to_string()).unwrap() }); let mut available_lts_node_versions = AVAILABLE_NODE_VERSIONS - .to_vec() .iter() - .filter(|v| v % 2 == 0) + .filter(|v| *v % 2 == 0) .collect::>(); // use newest node version first available_lts_node_versions.sort_by(|a, b| b.cmp(a)); - for version_number in available_node_versions { + for version_number in available_lts_node_versions { let version_range_string = format!("{version_number}.x.x"); let version_range: Range = version_range_string.parse().unwrap(); if version_range.allows_any(&range) { - return version_number_to_pkg(version_number); + return version_number_to_pkg(*version_number); } } default_node_pkg_name @@ -726,6 +725,24 @@ mod test { Ok(()) } + #[test] + fn test_latest_lts_version() -> Result<()> { + assert_eq!( + NodeProvider::get_nix_node_pkg( + &PackageJson { + name: Some(String::default()), + engines: Some(engines_node(">=18")), + ..Default::default() + }, + &App::new("examples/node")?, + &Environment::default() + )?, + Pkg::new(version_number_to_pkg(22).as_str()) + ); + + Ok(()) + } + #[test] fn test_simple_engine() -> Result<()> { assert_eq!( diff --git a/tests/snapshots/generate_plan_tests__node_pnpm_corepack.snap b/tests/snapshots/generate_plan_tests__node_pnpm_corepack.snap index c2629e25e..2e4c1a0b5 100644 --- a/tests/snapshots/generate_plan_tests__node_pnpm_corepack.snap +++ b/tests/snapshots/generate_plan_tests__node_pnpm_corepack.snap @@ -1,7 +1,6 @@ --- source: tests/generate_plan_tests.rs expression: plan -snapshot_kind: text --- { "providers": [], @@ -44,7 +43,7 @@ snapshot_kind: text "setup": { "name": "setup", "nixPkgs": [ - "nodejs_23", + "nodejs_22", "pnpm-9_x" ], "nixOverlays": [ diff --git a/tests/snapshots/generate_plan_tests__node_turborepo.snap b/tests/snapshots/generate_plan_tests__node_turborepo.snap index 4550bd278..3c634e673 100644 --- a/tests/snapshots/generate_plan_tests__node_turborepo.snap +++ b/tests/snapshots/generate_plan_tests__node_turborepo.snap @@ -1,7 +1,6 @@ --- source: tests/generate_plan_tests.rs expression: plan -snapshot_kind: text --- { "providers": [], @@ -45,7 +44,7 @@ snapshot_kind: text "setup": { "name": "setup", "nixPkgs": [ - "nodejs_23", + "nodejs_22", "npm-8_x" ], "nixOverlays": [