Skip to content

Commit db9e6d1

Browse files
committed
address PR feedback
1 parent 5f06a33 commit db9e6d1

File tree

7 files changed

+51
-51
lines changed

7 files changed

+51
-51
lines changed

crates/volta-core/src/error/kind.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1379,9 +1379,9 @@ Please ensure you have permissions to edit your environment variables."
13791379
),
13801380
ErrorKind::Yarn2NotSupported => write!(
13811381
f,
1382-
"Yarn@2 is not recommended for use, and not supported by Volta.
1382+
"Yarn version 2 is not recommended for use, and not supported by Volta.
13831383
1384-
Please use Yarn@3 instead."
1384+
Please use version 3 or greater instead."
13851385
),
13861386
ErrorKind::YarnLatestFetchError { from_url } => write!(
13871387
f,

crates/volta-core/src/tool/npm/fetch.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ fn determine_remote_url(version: &Version, hooks: Option<&ToolHooks<Npm>>) -> Fa
129129
let distro_file_name = Npm::archive_filename(&version_str);
130130
hook.resolve(version, &distro_file_name)
131131
}
132-
_ => Ok(public_registry_package("npm", "npm", &version_str)),
132+
_ => Ok(public_registry_package("npm", &version_str)),
133133
}
134134
}
135135

crates/volta-core/src/tool/npm/resolve.rs

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
11
//! Provides resolution of npm Version requirements into specific versions
22
33
use super::super::registry::{
4-
public_registry_index, PackageDetails, PackageIndex, RawPackageMetadata,
5-
NPM_ABBREVIATED_ACCEPT_HEADER,
4+
fetch_public_index, public_registry_index, PackageDetails, PackageIndex,
65
};
7-
use super::super::registry_fetch_error;
8-
use crate::error::{Context, ErrorKind, Fallible};
6+
use crate::error::{ErrorKind, Fallible};
97
use crate::hook::ToolHooks;
108
use crate::session::Session;
11-
use crate::style::progress_spinner;
129
use crate::tool::Npm;
1310
use crate::version::{VersionSpec, VersionTag};
14-
use attohttpc::header::ACCEPT;
15-
use attohttpc::Response;
1611
use log::debug;
1712
use semver::{Version, VersionReq};
1813

@@ -41,16 +36,7 @@ fn fetch_npm_index(hooks: Option<&ToolHooks<Npm>>) -> Fallible<(String, PackageI
4136
_ => public_registry_index("npm"),
4237
};
4338

44-
let spinner = progress_spinner(format!("Fetching public registry: {}", url));
45-
let metadata: RawPackageMetadata = attohttpc::get(&url)
46-
.header(ACCEPT, NPM_ABBREVIATED_ACCEPT_HEADER)
47-
.send()
48-
.and_then(Response::error_for_status)
49-
.and_then(Response::json)
50-
.with_context(registry_fetch_error("npm", &url))?;
51-
52-
spinner.finish_and_clear();
53-
Ok((url, metadata.into()))
39+
fetch_public_index(url, "Npm")
5440
}
5541

5642
fn resolve_tag(tag: &str, hooks: Option<&ToolHooks<Npm>>) -> Fallible<Version> {

crates/volta-core/src/tool/registry.rs

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
use std::collections::HashMap;
22
use std::path::{Path, PathBuf};
33

4+
use super::registry_fetch_error;
45
use crate::error::{Context, ErrorKind, Fallible};
56
use crate::fs::read_dir_eager;
7+
use crate::style::progress_spinner;
68
use crate::version::{hashmap_version_serde, version_serde};
9+
use attohttpc::header::ACCEPT;
10+
use attohttpc::Response;
711
use cfg_if::cfg_if;
812
use semver::Version;
913
use serde::Deserialize;
@@ -30,13 +34,36 @@ cfg_if! {
3034
}
3135
}
3236

33-
// need package and filename for namespaced tools like @yarnpkg/cli-dist, which is located at
34-
// https://registry.npmjs.org/@yarnpkg/cli-dist/-/cli-dist-1.2.3.tgz
35-
pub fn public_registry_package(package: &str, filename: &str, version: &str) -> String {
37+
pub fn fetch_public_index(url: String, name: &str) -> Fallible<(String, PackageIndex)> {
38+
let spinner = progress_spinner(format!("Fetching public registry: {}", url));
39+
let metadata: RawPackageMetadata = attohttpc::get(&url)
40+
.header(ACCEPT, NPM_ABBREVIATED_ACCEPT_HEADER)
41+
.send()
42+
.and_then(Response::error_for_status)
43+
.and_then(Response::json)
44+
.with_context(registry_fetch_error(name, &url))?;
45+
46+
spinner.finish_and_clear();
47+
Ok((url, metadata.into()))
48+
}
49+
50+
pub fn public_registry_package(package: &str, version: &str) -> String {
3651
format!(
3752
"{}/-/{}-{}.tgz",
3853
public_registry_index(package),
39-
filename,
54+
package,
55+
version
56+
)
57+
}
58+
59+
// need package and filename for namespaced tools like @yarnpkg/cli-dist, which is located at
60+
// https://registry.npmjs.org/@yarnpkg/cli-dist/-/cli-dist-1.2.3.tgz
61+
pub fn scoped_public_registry_package(scope: &str, package: &str, version: &str) -> String {
62+
format!(
63+
"{}/{}/-/{}-{}.tgz",
64+
public_registry_index(scope),
65+
package,
66+
package,
4067
version
4168
)
4269
}

crates/volta-core/src/tool/yarn/fetch.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ use std::fs::File;
55
use std::path::Path;
66

77
use super::super::download_tool_error;
8-
use super::super::registry::{find_unpack_dir, public_registry_package};
8+
use super::super::registry::{
9+
find_unpack_dir, public_registry_package, scoped_public_registry_package,
10+
};
911
use crate::error::{Context, ErrorKind, Fallible};
1012
use crate::fs::{create_staging_dir, create_staging_file, rename, set_executable};
1113
use crate::hook::ToolHooks;
@@ -16,7 +18,7 @@ use crate::version::VersionSpec;
1618
use archive::{Archive, Tarball};
1719
use fs_utils::ensure_containing_dir_exists;
1820
use log::debug;
19-
use semver::{Version, VersionReq};
21+
use semver::Version;
2022

2123
pub fn fetch(version: &Version, hooks: Option<&ToolHooks<Yarn>>) -> Fallible<()> {
2224
let yarn_dir = volta_home()?.yarn_inventory_dir();
@@ -127,16 +129,14 @@ fn determine_remote_url(version: &Version, hooks: Option<&ToolHooks<Yarn>>) -> F
127129
hook.resolve(version, &distro_file_name)
128130
}
129131
_ => {
130-
let matches_yarn_berry = VersionReq::parse(">=2").unwrap();
131-
if env::var_os("VOLTA_FEATURE_YARN_3").is_some() && matches_yarn_berry.matches(version)
132-
{
133-
Ok(public_registry_package(
134-
"@yarnpkg/cli-dist",
132+
if env::var_os("VOLTA_FEATURE_YARN_3").is_some() && version.major >= 2 {
133+
Ok(scoped_public_registry_package(
134+
"@yarnpkg",
135135
"cli-dist",
136136
&version_str,
137137
))
138138
} else {
139-
Ok(public_registry_package("yarn", "yarn", &version_str))
139+
Ok(public_registry_package("yarn", &version_str))
140140
}
141141
}
142142
}

crates/volta-core/src/tool/yarn/resolve.rs

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
use std::env;
44

55
use super::super::registry::{
6-
public_registry_index, PackageDetails, PackageIndex, RawPackageMetadata,
7-
NPM_ABBREVIATED_ACCEPT_HEADER,
6+
fetch_public_index, public_registry_index, PackageDetails, PackageIndex,
87
};
98
use super::super::registry_fetch_error;
109
use super::metadata::{RawYarnIndex, YarnIndex};
@@ -14,7 +13,6 @@ use crate::session::Session;
1413
use crate::style::progress_spinner;
1514
use crate::tool::Yarn;
1615
use crate::version::{parse_version, VersionSpec, VersionTag};
17-
use attohttpc::header::ACCEPT;
1816
use attohttpc::Response;
1917
use log::debug;
2018
use semver::{Version, VersionReq};
@@ -73,27 +71,17 @@ fn resolve_semver(matching: VersionReq, hooks: Option<&ToolHooks<Yarn>>) -> Fall
7371

7472
fn fetch_yarn_index(package: &str) -> Fallible<(String, PackageIndex)> {
7573
let url = public_registry_index(package);
76-
let spinner = progress_spinner(format!("Fetching public registry: {}", url));
77-
let metadata: RawPackageMetadata = attohttpc::get(&url)
78-
.header(ACCEPT, NPM_ABBREVIATED_ACCEPT_HEADER)
79-
.send()
80-
.and_then(Response::error_for_status)
81-
.and_then(Response::json)
82-
.with_context(registry_fetch_error("Yarn", &url))?;
83-
84-
spinner.finish_and_clear();
85-
Ok((url, metadata.into()))
74+
fetch_public_index(url, "Yarn")
8675
}
8776

8877
fn resolve_custom_tag(tag: String) -> Fallible<Version> {
8978
if env::var_os("VOLTA_FEATURE_YARN_3").is_some() {
9079
// first try yarn2+, which uses "@yarnpkg/cli-dist" instead of "yarn"
9180
let (url, mut index) = fetch_yarn_index("@yarnpkg/cli-dist")?;
9281

93-
let matches_yarn_2 = VersionReq::parse("2.*").unwrap();
9482
if let Some(version) = index.tags.remove(&tag) {
9583
debug!("Found yarn@{} matching tag '{}' from {}", version, tag, url);
96-
if matches_yarn_2.matches(&version) {
84+
if version.major == 2 {
9785
return Err(ErrorKind::Yarn2NotSupported.into());
9886
}
9987
return Ok(version);
@@ -137,11 +125,10 @@ fn resolve_semver_from_registry(matching: VersionReq) -> Fallible<Version> {
137125
.filter(|PackageDetails { version, .. }| matching.matches(version))
138126
.collect();
139127

140-
if matching_entries.len() > 0 {
141-
let matches_yarn_3 = VersionReq::parse(">=3").unwrap();
128+
if !matching_entries.is_empty() {
142129
let details_opt = matching_entries
143130
.iter()
144-
.find(|PackageDetails { version, .. }| matches_yarn_3.matches(version));
131+
.find(|PackageDetails { version, .. }| version.major >= 3);
145132

146133
match details_opt {
147134
Some(details) => {

tests/acceptance/volta_pin.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ fn pin_yarn_2_is_error() {
425425
execs()
426426
.with_status(ExitCode::NoVersionMatch as i32)
427427
.with_stderr_contains(
428-
"[..]Yarn@2 is not recommended for use, and not supported by Volta[..]"
428+
"[..]Yarn version 2 is not recommended for use, and not supported by Volta[..]"
429429
)
430430
);
431431

0 commit comments

Comments
 (0)