Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Report more detailed semver errors #12924

Merged
merged 1 commit into from Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion crates/xtask-bump-check/Cargo.toml
Expand Up @@ -11,5 +11,6 @@ cargo.workspace = true
cargo-util.workspace = true
clap.workspace = true
git2.workspace = true
tracing.workspace = true
semver.workspace = true
tracing-subscriber.workspace = true
tracing.workspace = true
3 changes: 1 addition & 2 deletions crates/xtask-bump-check/src/xtask.rs
Expand Up @@ -24,7 +24,6 @@ use cargo::core::Workspace;
use cargo::sources::source::QueryKind;
use cargo::util::cache_lock::CacheLockMode;
use cargo::util::command_prelude::*;
use cargo::util::ToSemver;
use cargo::CargoResult;
use cargo_util::ProcessBuilder;

Expand Down Expand Up @@ -277,7 +276,7 @@ fn beta_and_stable_branch(repo: &git2::Repository) -> CargoResult<[git2::Branch<
tracing::trace!("branch `{name}` is not in the format of `<remote>/rust-<semver>`");
continue;
};
let Ok(version) = version.to_semver() else {
let Ok(version) = version.parse::<semver::Version>() else {
tracing::trace!("branch `{name}` is not a valid semver: `{version}`");
continue;
};
Expand Down
3 changes: 1 addition & 2 deletions src/bin/cargo/commands/install.rs
Expand Up @@ -6,7 +6,6 @@ use anyhow::format_err;
use cargo::core::{GitReference, SourceId, Workspace};
use cargo::ops;
use cargo::util::IntoUrl;
use cargo::util::ToSemver;
use cargo::util::VersionReqExt;
use cargo::CargoResult;
use itertools::Itertools;
Expand Down Expand Up @@ -264,7 +263,7 @@ fn parse_semver_flag(v: &str) -> CargoResult<VersionReq> {
),
}
} else {
match v.to_semver() {
match v.trim().parse() {
Ok(v) => Ok(VersionReq::exact(&v)),
Err(e) => {
let mut msg = e.to_string();
Expand Down
10 changes: 5 additions & 5 deletions src/cargo/core/package_id.rs
Expand Up @@ -12,7 +12,7 @@ use serde::ser;

use crate::core::SourceId;
use crate::util::interning::InternedString;
use crate::util::{CargoResult, ToSemver};
use crate::util::CargoResult;

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

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

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

impl PackageId {
pub fn new<T: ToSemver>(
pub fn new(
name: impl Into<InternedString>,
version: T,
version: &str,
sid: SourceId,
) -> CargoResult<PackageId> {
let v = version.to_semver()?;
let v = version.parse()?;
Ok(PackageId::pure(name.into(), v, sid))
}

Expand Down
2 changes: 1 addition & 1 deletion src/cargo/sources/registry/index.rs
Expand Up @@ -935,7 +935,7 @@ impl IndexSummary {
} = serde_json::from_slice(line)?;
let v = v.unwrap_or(1);
tracing::trace!("json parsed registry {}/{}", name, vers);
let pkgid = PackageId::new(name, &vers, source_id)?;
let pkgid = PackageId::pure(name.into(), vers.clone(), source_id);
let deps = deps
.into_iter()
.map(|dep| dep.into_dep(source_id))
Expand Down
2 changes: 0 additions & 2 deletions src/cargo/util/mod.rs
Expand Up @@ -23,7 +23,6 @@ pub use self::queue::Queue;
pub use self::restricted_names::validate_package_name;
pub use self::rustc::Rustc;
pub use self::semver_ext::{OptVersionReq, PartialVersion, RustVersion, VersionExt, VersionReqExt};
pub use self::to_semver::ToSemver;
pub use self::vcs::{existing_vcs_repo, FossilRepo, GitRepo, HgRepo, PijulRepo};
pub use self::workspace::{
add_path_args, path_args, print_available_benches, print_available_binaries,
Expand Down Expand Up @@ -62,7 +61,6 @@ pub mod restricted_names;
pub mod rustc;
mod semver_ext;
pub mod style;
pub mod to_semver;
pub mod toml;
pub mod toml_mut;
mod vcs;
Expand Down
36 changes: 0 additions & 36 deletions src/cargo/util/to_semver.rs

This file was deleted.

10 changes: 3 additions & 7 deletions src/cargo/util/toml/mod.rs
Expand Up @@ -536,7 +536,7 @@ impl schema::TomlManifest {
version
.clone()
.unwrap_or_else(|| semver::Version::new(0, 0, 0)),
)?;
);

let edition = if let Some(edition) = package.edition.clone() {
let edition: Edition = edition
Expand Down Expand Up @@ -1596,12 +1596,8 @@ impl schema::InheritableFields {
}

impl schema::TomlPackage {
pub fn to_package_id(
&self,
source_id: SourceId,
version: semver::Version,
) -> CargoResult<PackageId> {
PackageId::new(&self.name, version, source_id)
pub fn to_package_id(&self, source_id: SourceId, version: semver::Version) -> PackageId {
PackageId::pure(self.name.as_str().into(), version, source_id)
}
}

Expand Down
2 changes: 1 addition & 1 deletion tests/testsuite/install_upgrade.rs
Expand Up @@ -230,7 +230,7 @@ fn ambiguous_version_no_longer_allowed() {
cargo_process("install foo --version=1.0")
.with_stderr(
"\
[ERROR] invalid value '1.0' for '--version <VERSION>': cannot parse '1.0' as a SemVer version
[ERROR] invalid value '1.0' for '--version <VERSION>': unexpected end of input while parsing minor version number

tip: if you want to specify SemVer range, add an explicit qualifier, like '^1.0'

Expand Down
1 change: 0 additions & 1 deletion triagebot.toml
Expand Up @@ -201,7 +201,6 @@ trigger_files = ["src/cargo/util/auth/"]
trigger_files = [
"crates/semver-check",
"src/cargo/util/semver_ext.rs",
"src/cargo/util/to_semver.rs",
]

[autolabel."A-source-replacement"]
Expand Down