Skip to content

Commit 2d99bdd

Browse files
committed
fix: Report more detailed semver errors
For `cargo install` we'll now show a more specific parse error for semver, much like other parts of cargo. This came out of my work on #12801. I was looking at what might be appropriate to put in a `cargo-util-semver` crate and realized we have the `ToSemver` trait that exists but doesn't do much, so I dropped it.
1 parent a9f6393 commit 2d99bdd

File tree

11 files changed

+15
-58
lines changed

11 files changed

+15
-58
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/xtask-bump-check/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@ cargo.workspace = true
1111
cargo-util.workspace = true
1212
clap.workspace = true
1313
git2.workspace = true
14-
tracing.workspace = true
14+
semver.workspace = true
1515
tracing-subscriber.workspace = true
16+
tracing.workspace = true

crates/xtask-bump-check/src/xtask.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ use cargo::core::Workspace;
2424
use cargo::sources::source::QueryKind;
2525
use cargo::util::cache_lock::CacheLockMode;
2626
use cargo::util::command_prelude::*;
27-
use cargo::util::ToSemver;
2827
use cargo::CargoResult;
2928
use cargo_util::ProcessBuilder;
3029

@@ -277,7 +276,7 @@ fn beta_and_stable_branch(repo: &git2::Repository) -> CargoResult<[git2::Branch<
277276
tracing::trace!("branch `{name}` is not in the format of `<remote>/rust-<semver>`");
278277
continue;
279278
};
280-
let Ok(version) = version.to_semver() else {
279+
let Ok(version) = version.parse::<semver::Version>() else {
281280
tracing::trace!("branch `{name}` is not a valid semver: `{version}`");
282281
continue;
283282
};

src/bin/cargo/commands/install.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use anyhow::format_err;
66
use cargo::core::{GitReference, SourceId, Workspace};
77
use cargo::ops;
88
use cargo::util::IntoUrl;
9-
use cargo::util::ToSemver;
109
use cargo::util::VersionReqExt;
1110
use cargo::CargoResult;
1211
use itertools::Itertools;
@@ -264,7 +263,7 @@ fn parse_semver_flag(v: &str) -> CargoResult<VersionReq> {
264263
),
265264
}
266265
} else {
267-
match v.to_semver() {
266+
match v.trim().parse() {
268267
Ok(v) => Ok(VersionReq::exact(&v)),
269268
Err(e) => {
270269
let mut msg = e.to_string();

src/cargo/core/package_id.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use serde::ser;
1212

1313
use crate::core::SourceId;
1414
use crate::util::interning::InternedString;
15-
use crate::util::{CargoResult, ToSemver};
15+
use crate::util::CargoResult;
1616

1717
static PACKAGE_ID_CACHE: OnceLock<Mutex<HashSet<&'static PackageIdInner>>> = OnceLock::new();
1818

@@ -82,7 +82,7 @@ impl<'de> de::Deserialize<'de> for PackageId {
8282
let (field, rest) = rest
8383
.split_once(' ')
8484
.ok_or_else(|| de::Error::custom("invalid serialized PackageId"))?;
85-
let version = field.to_semver().map_err(de::Error::custom)?;
85+
let version = field.parse().map_err(de::Error::custom)?;
8686

8787
let url =
8888
strip_parens(rest).ok_or_else(|| de::Error::custom("invalid serialized PackageId"))?;
@@ -123,12 +123,12 @@ impl Hash for PackageId {
123123
}
124124

125125
impl PackageId {
126-
pub fn new<T: ToSemver>(
126+
pub fn new(
127127
name: impl Into<InternedString>,
128-
version: T,
128+
version: &str,
129129
sid: SourceId,
130130
) -> CargoResult<PackageId> {
131-
let v = version.to_semver()?;
131+
let v = version.parse()?;
132132
Ok(PackageId::pure(name.into(), v, sid))
133133
}
134134

src/cargo/sources/registry/index.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -935,7 +935,7 @@ impl IndexSummary {
935935
} = serde_json::from_slice(line)?;
936936
let v = v.unwrap_or(1);
937937
tracing::trace!("json parsed registry {}/{}", name, vers);
938-
let pkgid = PackageId::new(name, &vers, source_id)?;
938+
let pkgid = PackageId::pure(name.into(), vers.clone(), source_id);
939939
let deps = deps
940940
.into_iter()
941941
.map(|dep| dep.into_dep(source_id))

src/cargo/util/mod.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ pub use self::queue::Queue;
2323
pub use self::restricted_names::validate_package_name;
2424
pub use self::rustc::Rustc;
2525
pub use self::semver_ext::{OptVersionReq, PartialVersion, RustVersion, VersionExt, VersionReqExt};
26-
pub use self::to_semver::ToSemver;
2726
pub use self::vcs::{existing_vcs_repo, FossilRepo, GitRepo, HgRepo, PijulRepo};
2827
pub use self::workspace::{
2928
add_path_args, path_args, print_available_benches, print_available_binaries,
@@ -62,7 +61,6 @@ pub mod restricted_names;
6261
pub mod rustc;
6362
mod semver_ext;
6463
pub mod style;
65-
pub mod to_semver;
6664
pub mod toml;
6765
pub mod toml_mut;
6866
mod vcs;

src/cargo/util/to_semver.rs

Lines changed: 0 additions & 36 deletions
This file was deleted.

src/cargo/util/toml/mod.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,7 @@ impl schema::TomlManifest {
536536
version
537537
.clone()
538538
.unwrap_or_else(|| semver::Version::new(0, 0, 0)),
539-
)?;
539+
);
540540

541541
let edition = if let Some(edition) = package.edition.clone() {
542542
let edition: Edition = edition
@@ -1596,12 +1596,8 @@ impl schema::InheritableFields {
15961596
}
15971597

15981598
impl schema::TomlPackage {
1599-
pub fn to_package_id(
1600-
&self,
1601-
source_id: SourceId,
1602-
version: semver::Version,
1603-
) -> CargoResult<PackageId> {
1604-
PackageId::new(&self.name, version, source_id)
1599+
pub fn to_package_id(&self, source_id: SourceId, version: semver::Version) -> PackageId {
1600+
PackageId::pure(self.name.as_str().into(), version, source_id)
16051601
}
16061602
}
16071603

tests/testsuite/install_upgrade.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ fn ambiguous_version_no_longer_allowed() {
230230
cargo_process("install foo --version=1.0")
231231
.with_stderr(
232232
"\
233-
[ERROR] invalid value '1.0' for '--version <VERSION>': cannot parse '1.0' as a SemVer version
233+
[ERROR] invalid value '1.0' for '--version <VERSION>': unexpected end of input while parsing minor version number
234234
235235
tip: if you want to specify SemVer range, add an explicit qualifier, like '^1.0'
236236

triagebot.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,6 @@ trigger_files = ["src/cargo/util/auth/"]
201201
trigger_files = [
202202
"crates/semver-check",
203203
"src/cargo/util/semver_ext.rs",
204-
"src/cargo/util/to_semver.rs",
205204
]
206205

207206
[autolabel."A-source-replacement"]

0 commit comments

Comments
 (0)