Skip to content

Commit 01dbc05

Browse files
committed
In case of --breaking, don't touch the non-breaking deps.
1 parent 8afd52b commit 01dbc05

3 files changed

Lines changed: 36 additions & 17 deletions

File tree

src/bin/cargo/commands/update.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ pub fn exec(gctx: &mut GlobalContext, args: &ArgMatches) -> CliResult {
100100
};
101101

102102
let upgrades = ops::update_manifests(&mut ws, &update_opts)?;
103-
ops::update_lockfile(&ws, &update_opts)?;
103+
ops::update_lockfile(&ws, &update_opts, &upgrades)?;
104104
ops::write_manifests(&ws, &update_opts, &upgrades)?;
105105

106106
Ok(())

src/cargo/ops/cargo_update.rs

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,11 @@ pub fn generate_lockfile(ws: &Workspace<'_>) -> CargoResult<()> {
4545
Ok(())
4646
}
4747

48-
pub fn update_lockfile(ws: &Workspace<'_>, opts: &UpdateOptions<'_>) -> CargoResult<()> {
48+
pub fn update_lockfile(
49+
ws: &Workspace<'_>,
50+
opts: &UpdateOptions<'_>,
51+
upgrades: &HashMap<String, Version>,
52+
) -> CargoResult<()> {
4953
if opts.recursive && opts.precise.is_some() {
5054
anyhow::bail!("cannot specify both recursive and precise simultaneously")
5155
}
@@ -161,7 +165,12 @@ pub fn update_lockfile(ws: &Workspace<'_>, opts: &UpdateOptions<'_>) -> CargoRes
161165
.filter(|s| !s.is_registry())
162166
.collect();
163167

164-
let keep = |p: &PackageId| !to_avoid_sources.contains(&p.source_id()) && !to_avoid.contains(p);
168+
let keep = |p: &PackageId| {
169+
(!to_avoid_sources.contains(&p.source_id()) && !to_avoid.contains(p))
170+
// In case of `--breaking`, we want to keep all packages unchanged that
171+
// didn't get upgraded.
172+
|| (opts.breaking && !upgrades.contains_key(&p.name().to_string()))
173+
};
165174

166175
let mut resolve = ops::resolve_with_previous(
167176
&mut registry,
@@ -238,7 +247,7 @@ pub fn update_manifests(
238247
.summary()
239248
.clone()
240249
.map_dependencies(|dependency| {
241-
let req = if let OptVersionReq::Req(current) = dependency.version_req() {
250+
if let OptVersionReq::Req(current) = dependency.version_req() {
242251
let query = crate::core::dependency::Dependency::parse(
243252
dependency.package_name(),
244253
None,
@@ -269,6 +278,13 @@ pub fn update_manifests(
269278

270279
if let Some(latest) = latest.clone() {
271280
if !current.matches(&latest) {
281+
debug!(
282+
"upgrading {} from {} to {}",
283+
dependency.package_name(),
284+
current,
285+
latest
286+
);
287+
272288
opts.gctx
273289
.shell()
274290
.status_with_color(
@@ -284,17 +300,17 @@ pub fn update_manifests(
284300
.unwrap();
285301

286302
upgrades.insert(dependency.package_name().to_string(), latest.clone());
303+
304+
let req =
305+
OptVersionReq::Req(VersionReq::parse(&latest.to_string()).unwrap());
306+
let mut dep = dependency.clone();
307+
dep.set_version_req(req);
308+
return dep;
287309
}
288310
}
311+
}
289312

290-
OptVersionReq::Req(VersionReq::parse(&latest.unwrap().to_string()).unwrap())
291-
} else {
292-
dependency.version_req().clone()
293-
};
294-
295-
let mut dep = dependency.clone();
296-
dep.set_version_req(req);
297-
dep
313+
dependency.clone()
298314
});
299315

300316
let summary = member.manifest_mut().summary_mut();

tests/testsuite/update.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1661,7 +1661,7 @@ fn update_with_missing_feature() {
16611661
}
16621662

16631663
#[cargo_test]
1664-
fn update_compatible() {
1664+
fn update_nonbreaking() {
16651665
Package::new("compatible", "1.0.0").publish();
16661666
Package::new("incompatible", "1.0.0").publish();
16671667

@@ -1701,7 +1701,7 @@ fn update_compatible() {
17011701
}
17021702

17031703
#[cargo_test]
1704-
fn update_incompatible() {
1704+
fn update_breaking() {
17051705
Package::new("compatible", "1.0.0").publish();
17061706
Package::new("incompatible", "1.0.0").publish();
17071707

@@ -1729,14 +1729,17 @@ incompatible = "1.0" # This line gets partially rewritten
17291729
Package::new("incompatible", "2.0.0").publish();
17301730

17311731
p.cargo("update --breaking")
1732-
// .env("CARGO_LOG", "cargo::ops=trace,cargo::util::toml=debug")
1732+
// .env(
1733+
// "CARGO_LOG",
1734+
// "cargo::ops=trace,cargo::util::toml=debug,cargo::core::resolver=debug",
1735+
// )
17331736
.with_stderr(
17341737
"\
17351738
[UPDATING] `[..]` index
17361739
[UPGRADING] incompatible ^1.0 -> v2.0.0
1737-
[LOCKING] 2 packages to latest compatible versions
1738-
[UPDATING] compatible v1.0.0 -> v1.0.1
1740+
[LOCKING] 1 package to latest compatible version
17391741
[UPDATING] incompatible v1.0.0 -> v2.0.0
1742+
[NOTE] pass `--verbose` to see 1 unchanged dependencies behind latest
17401743
",
17411744
)
17421745
.run();

0 commit comments

Comments
 (0)