Skip to content

Commit

Permalink
In case of --breaking, don't touch the non-breaking deps.
Browse files Browse the repository at this point in the history
  • Loading branch information
torhovland committed May 30, 2024
1 parent 8afd52b commit 01dbc05
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/bin/cargo/commands/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ pub fn exec(gctx: &mut GlobalContext, args: &ArgMatches) -> CliResult {
};

let upgrades = ops::update_manifests(&mut ws, &update_opts)?;
ops::update_lockfile(&ws, &update_opts)?;
ops::update_lockfile(&ws, &update_opts, &upgrades)?;
ops::write_manifests(&ws, &update_opts, &upgrades)?;

Ok(())
Expand Down
38 changes: 27 additions & 11 deletions src/cargo/ops/cargo_update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ pub fn generate_lockfile(ws: &Workspace<'_>) -> CargoResult<()> {
Ok(())
}

pub fn update_lockfile(ws: &Workspace<'_>, opts: &UpdateOptions<'_>) -> CargoResult<()> {
pub fn update_lockfile(
ws: &Workspace<'_>,
opts: &UpdateOptions<'_>,
upgrades: &HashMap<String, Version>,
) -> CargoResult<()> {
if opts.recursive && opts.precise.is_some() {
anyhow::bail!("cannot specify both recursive and precise simultaneously")
}
Expand Down Expand Up @@ -161,7 +165,12 @@ pub fn update_lockfile(ws: &Workspace<'_>, opts: &UpdateOptions<'_>) -> CargoRes
.filter(|s| !s.is_registry())
.collect();

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

let mut resolve = ops::resolve_with_previous(
&mut registry,
Expand Down Expand Up @@ -238,7 +247,7 @@ pub fn update_manifests(
.summary()
.clone()
.map_dependencies(|dependency| {
let req = if let OptVersionReq::Req(current) = dependency.version_req() {
if let OptVersionReq::Req(current) = dependency.version_req() {
let query = crate::core::dependency::Dependency::parse(
dependency.package_name(),
None,
Expand Down Expand Up @@ -269,6 +278,13 @@ pub fn update_manifests(

if let Some(latest) = latest.clone() {
if !current.matches(&latest) {
debug!(
"upgrading {} from {} to {}",
dependency.package_name(),
current,
latest
);

opts.gctx
.shell()
.status_with_color(
Expand All @@ -284,17 +300,17 @@ pub fn update_manifests(
.unwrap();

upgrades.insert(dependency.package_name().to_string(), latest.clone());

let req =
OptVersionReq::Req(VersionReq::parse(&latest.to_string()).unwrap());
let mut dep = dependency.clone();
dep.set_version_req(req);
return dep;
}
}
}

OptVersionReq::Req(VersionReq::parse(&latest.unwrap().to_string()).unwrap())
} else {
dependency.version_req().clone()
};

let mut dep = dependency.clone();
dep.set_version_req(req);
dep
dependency.clone()
});

let summary = member.manifest_mut().summary_mut();
Expand Down
13 changes: 8 additions & 5 deletions tests/testsuite/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1661,7 +1661,7 @@ fn update_with_missing_feature() {
}

#[cargo_test]
fn update_compatible() {
fn update_nonbreaking() {
Package::new("compatible", "1.0.0").publish();
Package::new("incompatible", "1.0.0").publish();

Expand Down Expand Up @@ -1701,7 +1701,7 @@ fn update_compatible() {
}

#[cargo_test]
fn update_incompatible() {
fn update_breaking() {
Package::new("compatible", "1.0.0").publish();
Package::new("incompatible", "1.0.0").publish();

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

p.cargo("update --breaking")
// .env("CARGO_LOG", "cargo::ops=trace,cargo::util::toml=debug")
// .env(
// "CARGO_LOG",
// "cargo::ops=trace,cargo::util::toml=debug,cargo::core::resolver=debug",
// )
.with_stderr(
"\
[UPDATING] `[..]` index
[UPGRADING] incompatible ^1.0 -> v2.0.0
[LOCKING] 2 packages to latest compatible versions
[UPDATING] compatible v1.0.0 -> v1.0.1
[LOCKING] 1 package to latest compatible version
[UPDATING] incompatible v1.0.0 -> v2.0.0
[NOTE] pass `--verbose` to see 1 unchanged dependencies behind latest
",
)
.run();
Expand Down

0 comments on commit 01dbc05

Please sign in to comment.