From 89b01197725e4b30747df14f8ccdb6d53ab20119 Mon Sep 17 00:00:00 2001 From: Lin Yihai Date: Mon, 15 Jul 2024 15:25:12 +0800 Subject: [PATCH 1/2] test: Add test about update --breaking on prerelease --- src/cargo/util/toml_mut/upgrade.rs | 5 ++ tests/testsuite/update.rs | 94 ++++++++++++++++++++++++++++++ 2 files changed, 99 insertions(+) diff --git a/src/cargo/util/toml_mut/upgrade.rs b/src/cargo/util/toml_mut/upgrade.rs index 13d5563450c..698fcab654c 100644 --- a/src/cargo/util/toml_mut/upgrade.rs +++ b/src/cargo/util/toml_mut/upgrade.rs @@ -217,5 +217,10 @@ mod test { assert_req_bump("1.1.1", "=1.0.0", "=1.1.1"); assert_req_bump("2.0.0", "=1.0.0", "=2.0.0"); } + + #[test] + fn caret_prerelease() { + assert_req_bump("1.7.0", "2.0.0-beta.21", "1.7.0"); + } } } diff --git a/tests/testsuite/update.rs b/tests/testsuite/update.rs index f27fd68881a..3b5e1162b63 100644 --- a/tests/testsuite/update.rs +++ b/tests/testsuite/update.rs @@ -2619,3 +2619,97 @@ fn update_breaking_mixed_pinning_renaming() { "#]], ); } + +#[cargo_test] +fn update_breaking_pre_release_downgrade() { + Package::new("bar", "2.0.0-beta.21").publish(); + + let p = project() + .file( + "Cargo.toml", + r#" + [package] + name = "foo" + version = "0.0.1" + edition = "2015" + authors = [] + + [dependencies] + bar = "2.0.0-beta.21" + "#, + ) + .file("src/lib.rs", "") + .build(); + + p.cargo("generate-lockfile").run(); + + // The purpose of this test is + // to demonstrate that `update --breaking` will not try to downgrade to the latest stable version (1.7.0), + // but will rather keep the latest pre-release (2.0.0-beta.21). + Package::new("bar", "1.7.0").publish(); + p.cargo("update -Zunstable-options --breaking bar") + .masquerade_as_nightly_cargo(&["update-breaking"]) + .with_stderr_data(str![[r#" +[UPDATING] `dummy-registry` index +[UPGRADING] bar ^2.0.0-beta.21 -> ^1.7.0 +[LOCKING] 1 package to latest compatible version +[DOWNGRADING] bar v2.0.0-beta.21 -> v1.7.0 + +"#]]) + .run(); +} + +#[cargo_test] +fn update_breaking_pre_release_upgrade() { + Package::new("bar", "2.0.0-beta.21").publish(); + + let p = project() + .file( + "Cargo.toml", + r#" + [package] + name = "foo" + version = "0.0.1" + edition = "2015" + authors = [] + + [dependencies] + bar = "2.0.0-beta.21" + "#, + ) + .file("src/lib.rs", "") + .build(); + + p.cargo("generate-lockfile").run(); + + // TODO: `2.0.0-beta.21` can be upgraded to `2.0.0-beta.22` + Package::new("bar", "2.0.0-beta.22").publish(); + p.cargo("update -Zunstable-options --breaking bar") + .masquerade_as_nightly_cargo(&["update-breaking"]) + .with_stderr_data(str![[r#" +[UPDATING] `dummy-registry` index + +"#]]) + .run(); + // TODO: `2.0.0-beta.21` can be upgraded to `2.0.0` + Package::new("bar", "2.0.0").publish(); + p.cargo("update -Zunstable-options --breaking bar") + .masquerade_as_nightly_cargo(&["update-breaking"]) + .with_stderr_data(str![[r#" +[UPDATING] `dummy-registry` index + +"#]]) + .run(); + + Package::new("bar", "3.0.0").publish(); + p.cargo("update -Zunstable-options --breaking bar") + .masquerade_as_nightly_cargo(&["update-breaking"]) + .with_stderr_data(str![[r#" +[UPDATING] `dummy-registry` index +[UPGRADING] bar ^2.0.0-beta.21 -> ^3.0.0 +[LOCKING] 1 package to latest compatible version +[UPDATING] bar v2.0.0-beta.21 -> v3.0.0 + +"#]]) + .run(); +} From 9e2701aedb683ee2a53f65528ac4982205bef5b9 Mon Sep 17 00:00:00 2001 From: Lin Yihai Date: Wed, 17 Jul 2024 09:47:58 +0800 Subject: [PATCH 2/2] fix: Don't downgrade with `update --breaking` if VersionReq is prerelease --- src/cargo/util/toml_mut/upgrade.rs | 48 ++++++++++++++++++++++++++---- tests/testsuite/update.rs | 3 -- 2 files changed, 43 insertions(+), 8 deletions(-) diff --git a/src/cargo/util/toml_mut/upgrade.rs b/src/cargo/util/toml_mut/upgrade.rs index 698fcab654c..b81f51ec5e3 100644 --- a/src/cargo/util/toml_mut/upgrade.rs +++ b/src/cargo/util/toml_mut/upgrade.rs @@ -15,12 +15,16 @@ pub(crate) fn upgrade_requirement( // Empty matches everything, no-change. Ok(None) } else { - let comparators: CargoResult> = raw_req + let comparators: Vec<_> = raw_req .comparators .into_iter() + // Don't downgrade if pre-release was used, see https://github.com/rust-lang/cargo/issues/14178 and https://github.com/rust-lang/cargo/issues/13290. + .filter(|p| p.pre.is_empty() || matches_greater(p, version)) .map(|p| set_comparator(p, version)) - .collect(); - let comparators = comparators?; + .collect::>()?; + if comparators.is_empty() { + return Ok(None); + } let new_req = semver::VersionReq { comparators }; let mut new_req_text = new_req.to_string(); if new_req_text.starts_with('^') && !req.starts_with('^') { @@ -74,6 +78,33 @@ fn set_comparator( } } +// See https://github.com/dtolnay/semver/blob/69efd3cc770ead273a06ad1788477b3092996d29/src/eval.rs#L64-L88 +fn matches_greater(cmp: &semver::Comparator, ver: &semver::Version) -> bool { + if ver.major != cmp.major { + return ver.major > cmp.major; + } + + match cmp.minor { + None => return false, + Some(minor) => { + if ver.minor != minor { + return ver.minor > minor; + } + } + } + + match cmp.patch { + None => return false, + Some(patch) => { + if ver.patch != patch { + return ver.patch > patch; + } + } + } + + ver.pre > cmp.pre +} + fn assign_partial_req( version: &semver::Version, mut pred: semver::Comparator, @@ -219,8 +250,15 @@ mod test { } #[test] - fn caret_prerelease() { - assert_req_bump("1.7.0", "2.0.0-beta.21", "1.7.0"); + fn greater_prerelease() { + assert_req_bump("1.7.0", "2.0.0-beta.21", None); + assert_req_bump("1.7.0", "=2.0.0-beta.21", None); + assert_req_bump("1.7.0", "~2.0.0-beta.21", None); + assert_req_bump("2.0.0-beta.20", "2.0.0-beta.21", None); + assert_req_bump("2.0.0-beta.21", "2.0.0-beta.21", None); + assert_req_bump("2.0.0-beta.22", "2.0.0-beta.21", "2.0.0-beta.22"); + assert_req_bump("2.0.0", "2.0.0-beta.21", "2.0.0"); + assert_req_bump("3.0.0", "2.0.0-beta.21", "3.0.0"); } } } diff --git a/tests/testsuite/update.rs b/tests/testsuite/update.rs index 3b5e1162b63..0b66cb38afd 100644 --- a/tests/testsuite/update.rs +++ b/tests/testsuite/update.rs @@ -2651,9 +2651,6 @@ fn update_breaking_pre_release_downgrade() { .masquerade_as_nightly_cargo(&["update-breaking"]) .with_stderr_data(str![[r#" [UPDATING] `dummy-registry` index -[UPGRADING] bar ^2.0.0-beta.21 -> ^1.7.0 -[LOCKING] 1 package to latest compatible version -[DOWNGRADING] bar v2.0.0-beta.21 -> v1.7.0 "#]]) .run();