diff --git a/CHANGELOG.md b/CHANGELOG.md index 6d34b5c..94f9858 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,10 @@ - Added a `dist-url` config setting, allowing the download host to be customized. +#### 🐞 Fixes + +- Fixed `.nvmrc` and `.node-version` parsing when they contain comments. + ## 0.10.1 #### 🚀 Updates diff --git a/crates/node/src/proto.rs b/crates/node/src/proto.rs index dc24bd1..658dab1 100644 --- a/crates/node/src/proto.rs +++ b/crates/node/src/proto.rs @@ -58,7 +58,16 @@ pub fn parse_version_file( } } } else { - version = Some(UnresolvedVersionSpec::parse(input.content)?); + for line in input.content.lines() { + let line = line.trim(); + + if line.is_empty() || line.starts_with('#') { + continue; + } else { + version = Some(UnresolvedVersionSpec::parse(line)?); + break; + } + } } Ok(Json(ParseVersionFileOutput { version })) diff --git a/crates/node/tests/versions_test.rs b/crates/node/tests/versions_test.rs index 92b6aab..dfc5710 100644 --- a/crates/node/tests/versions_test.rs +++ b/crates/node/tests/versions_test.rs @@ -80,3 +80,67 @@ fn parses_volta() { } ); } + +#[test] +fn parses_nvmrc() { + let sandbox = create_empty_proto_sandbox(); + let plugin = sandbox.create_plugin("node-test"); + + assert_eq!( + plugin.parse_version_file(ParseVersionFileInput { + content: "~20".into(), + file: ".nvmrc".into(), + }), + ParseVersionFileOutput { + version: Some(UnresolvedVersionSpec::parse("~20").unwrap()), + } + ); +} + +#[test] +fn parses_nvmrc_with_comment() { + let sandbox = create_empty_proto_sandbox(); + let plugin = sandbox.create_plugin("node-test"); + + assert_eq!( + plugin.parse_version_file(ParseVersionFileInput { + content: "# comment\n^20.1".into(), + file: ".nvmrc".into(), + }), + ParseVersionFileOutput { + version: Some(UnresolvedVersionSpec::parse("^20.1").unwrap()), + } + ); +} + +#[test] +fn parses_node_version() { + let sandbox = create_empty_proto_sandbox(); + let plugin = sandbox.create_plugin("node-test"); + + assert_eq!( + plugin.parse_version_file(ParseVersionFileInput { + content: "~20".into(), + file: ".node-version".into(), + }), + ParseVersionFileOutput { + version: Some(UnresolvedVersionSpec::parse("~20").unwrap()), + } + ); +} + +#[test] +fn parses_node_version_with_comment() { + let sandbox = create_empty_proto_sandbox(); + let plugin = sandbox.create_plugin("node-test"); + + assert_eq!( + plugin.parse_version_file(ParseVersionFileInput { + content: "# comment\n^20.1".into(), + file: ".node-version".into(), + }), + ParseVersionFileOutput { + version: Some(UnresolvedVersionSpec::parse("^20.1").unwrap()), + } + ); +}