Skip to content

Commit 9f8adff

Browse files
committed
Auto merge of #13754 - epage:resolve-type, r=weihanglo
feat(resolve): Tell the user the style of resovle done ### What does this PR try to resolve? This is to help with #9930 Example changes: ```diff -[LOCKING] 4 packages +[LOCKING] 4 packages to latest compatible version -[LOCKING] 2 packages +[LOCKING] 2 packages to latest Rust 1.60.0 compatible versions -[LOCKING] 2 packages +[LOCKING] 2 packages to earliest compatible versions ``` Benefits - The package count is of "added" packages and this makes that more logically clear - This gives users transparency into what is happening, especially with - what rust-version is use - the transition to this feature in the new edition - whether the planned config was applied or not (as I don't want it to require an MSRV bump) - Will make it easier in tests to show what changed - Provides more motiviation to show this message in `cargo update` and `cargo install` (that will be explored in a follow up PR) This does come at the cost of more verbose output but hopefully not too verbose. This is why I left off other factors, like avoid-dev-deps. ### How should we test and review this PR? ### Additional information
2 parents 624233b + 1876326 commit 9f8adff

File tree

166 files changed

+613
-541
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

166 files changed

+613
-541
lines changed

src/cargo/ops/cargo_generate_lockfile.rs

Lines changed: 68 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ pub fn generate_lockfile(ws: &Workspace<'_>) -> CargoResult<()> {
3737
true,
3838
)?;
3939
ops::write_pkg_lockfile(ws, &mut resolve)?;
40-
print_lockfile_changes(ws.gctx(), previous_resolve, &resolve, &mut registry)?;
40+
print_lockfile_changes(ws, previous_resolve, &resolve, &mut registry)?;
4141
Ok(())
4242
}
4343

@@ -170,7 +170,7 @@ pub fn update_lockfile(ws: &Workspace<'_>, opts: &UpdateOptions<'_>) -> CargoRes
170170
true,
171171
)?;
172172

173-
print_lockfile_updates(opts.gctx, &previous_resolve, &resolve, &mut registry)?;
173+
print_lockfile_updates(ws, &previous_resolve, &resolve, &mut registry)?;
174174
if opts.dry_run {
175175
opts.gctx
176176
.shell()
@@ -186,21 +186,23 @@ pub fn update_lockfile(ws: &Workspace<'_>, opts: &UpdateOptions<'_>) -> CargoRes
186186
/// This would acquire the package-cache lock, as it may update the index to
187187
/// show users latest available versions.
188188
pub fn print_lockfile_changes(
189-
gctx: &GlobalContext,
189+
ws: &Workspace<'_>,
190190
previous_resolve: Option<&Resolve>,
191191
resolve: &Resolve,
192192
registry: &mut PackageRegistry<'_>,
193193
) -> CargoResult<()> {
194-
let _lock = gctx.acquire_package_cache_lock(CacheLockMode::DownloadExclusive)?;
194+
let _lock = ws
195+
.gctx()
196+
.acquire_package_cache_lock(CacheLockMode::DownloadExclusive)?;
195197
if let Some(previous_resolve) = previous_resolve {
196-
print_lockfile_sync(gctx, previous_resolve, resolve, registry)
198+
print_lockfile_sync(ws, previous_resolve, resolve, registry)
197199
} else {
198-
print_lockfile_generation(gctx, resolve, registry)
200+
print_lockfile_generation(ws, resolve, registry)
199201
}
200202
}
201203

202204
fn print_lockfile_generation(
203-
gctx: &GlobalContext,
205+
ws: &Workspace<'_>,
204206
resolve: &Resolve,
205207
registry: &mut PackageRegistry<'_>,
206208
) -> CargoResult<()> {
@@ -210,8 +212,7 @@ fn print_lockfile_generation(
210212
// just ourself, nothing worth reporting
211213
return Ok(());
212214
}
213-
gctx.shell()
214-
.status("Locking", format!("{num_pkgs} packages"))?;
215+
status_locking(ws, num_pkgs)?;
215216

216217
for diff in diff {
217218
fn format_latest(version: semver::Version) -> String {
@@ -245,7 +246,7 @@ fn print_lockfile_generation(
245246
};
246247

247248
if let Some(latest) = latest {
248-
gctx.shell().status_with_color(
249+
ws.gctx().shell().status_with_color(
249250
"Adding",
250251
format!("{package}{latest}"),
251252
&style::NOTE,
@@ -258,7 +259,7 @@ fn print_lockfile_generation(
258259
}
259260

260261
fn print_lockfile_sync(
261-
gctx: &GlobalContext,
262+
ws: &Workspace<'_>,
262263
previous_resolve: &Resolve,
263264
resolve: &Resolve,
264265
registry: &mut PackageRegistry<'_>,
@@ -268,9 +269,7 @@ fn print_lockfile_sync(
268269
if num_pkgs == 0 {
269270
return Ok(());
270271
}
271-
let plural = if num_pkgs == 1 { "" } else { "s" };
272-
gctx.shell()
273-
.status("Locking", format!("{num_pkgs} package{plural}"))?;
272+
status_locking(ws, num_pkgs)?;
274273

275274
for diff in diff {
276275
fn format_latest(version: semver::Version) -> String {
@@ -318,10 +317,12 @@ fn print_lockfile_sync(
318317
// This metadata is often stuff like git commit hashes, which are
319318
// not meaningfully ordered.
320319
if removed.version().cmp_precedence(added.version()) == Ordering::Greater {
321-
gctx.shell()
320+
ws.gctx()
321+
.shell()
322322
.status_with_color("Downgrading", msg, &style::WARN)?;
323323
} else {
324-
gctx.shell()
324+
ws.gctx()
325+
.shell()
325326
.status_with_color("Updating", msg, &style::GOOD)?;
326327
}
327328
} else {
@@ -339,7 +340,7 @@ fn print_lockfile_sync(
339340
}
340341
.unwrap_or_default();
341342

342-
gctx.shell().status_with_color(
343+
ws.gctx().shell().status_with_color(
343344
"Adding",
344345
format!("{package}{latest}"),
345346
&style::NOTE,
@@ -352,7 +353,7 @@ fn print_lockfile_sync(
352353
}
353354

354355
fn print_lockfile_updates(
355-
gctx: &GlobalContext,
356+
ws: &Workspace<'_>,
356357
previous_resolve: &Resolve,
357358
resolve: &Resolve,
358359
registry: &mut PackageRegistry<'_>,
@@ -404,16 +405,21 @@ fn print_lockfile_updates(
404405
// This metadata is often stuff like git commit hashes, which are
405406
// not meaningfully ordered.
406407
if removed.version().cmp_precedence(added.version()) == Ordering::Greater {
407-
gctx.shell()
408+
ws.gctx()
409+
.shell()
408410
.status_with_color("Downgrading", msg, &style::WARN)?;
409411
} else {
410-
gctx.shell()
412+
ws.gctx()
413+
.shell()
411414
.status_with_color("Updating", msg, &style::GOOD)?;
412415
}
413416
} else {
414417
for package in diff.removed.iter() {
415-
gctx.shell()
416-
.status_with_color("Removing", format!("{package}"), &style::ERROR)?;
418+
ws.gctx().shell().status_with_color(
419+
"Removing",
420+
format!("{package}"),
421+
&style::ERROR,
422+
)?;
417423
}
418424
for package in diff.added.iter() {
419425
let latest = if !possibilities.is_empty() {
@@ -429,7 +435,7 @@ fn print_lockfile_updates(
429435
}
430436
.unwrap_or_default();
431437

432-
gctx.shell().status_with_color(
438+
ws.gctx().shell().status_with_color(
433439
"Adding",
434440
format!("{package}{latest}"),
435441
&style::NOTE,
@@ -451,8 +457,8 @@ fn print_lockfile_updates(
451457

452458
if let Some(latest) = latest {
453459
unchanged_behind += 1;
454-
if gctx.shell().verbosity() == Verbosity::Verbose {
455-
gctx.shell().status_with_color(
460+
if ws.gctx().shell().verbosity() == Verbosity::Verbose {
461+
ws.gctx().shell().status_with_color(
456462
"Unchanged",
457463
format!("{package}{latest}"),
458464
&anstyle::Style::new().bold(),
@@ -462,13 +468,13 @@ fn print_lockfile_updates(
462468
}
463469
}
464470

465-
if gctx.shell().verbosity() == Verbosity::Verbose {
466-
gctx.shell().note(
471+
if ws.gctx().shell().verbosity() == Verbosity::Verbose {
472+
ws.gctx().shell().note(
467473
"to see how you depend on a package, run `cargo tree --invert --package <dep>@<ver>`",
468474
)?;
469475
} else {
470476
if 0 < unchanged_behind {
471-
gctx.shell().note(format!(
477+
ws.gctx().shell().note(format!(
472478
"pass `--verbose` to see {unchanged_behind} unchanged dependencies behind latest"
473479
))?;
474480
}
@@ -477,6 +483,40 @@ fn print_lockfile_updates(
477483
Ok(())
478484
}
479485

486+
fn status_locking(ws: &Workspace<'_>, num_pkgs: usize) -> CargoResult<()> {
487+
use std::fmt::Write as _;
488+
489+
let plural = if num_pkgs == 1 { "" } else { "s" };
490+
491+
let mut cfg = String::new();
492+
// Don't have a good way to describe `direct_minimal_versions` atm
493+
if !ws.gctx().cli_unstable().direct_minimal_versions {
494+
write!(&mut cfg, " to")?;
495+
if ws.gctx().cli_unstable().minimal_versions {
496+
write!(&mut cfg, " earliest")?;
497+
} else {
498+
write!(&mut cfg, " latest")?;
499+
}
500+
501+
if ws.resolve_honors_rust_version() {
502+
let rust_version = if let Some(ver) = ws.rust_version() {
503+
ver.clone().into_partial()
504+
} else {
505+
let rustc = ws.gctx().load_global_rustc(Some(ws))?;
506+
let rustc_version = rustc.version.clone().into();
507+
rustc_version
508+
};
509+
write!(&mut cfg, " Rust {rust_version}")?;
510+
}
511+
write!(&mut cfg, " compatible version{plural}")?;
512+
}
513+
514+
ws.gctx()
515+
.shell()
516+
.status("Locking", format!("{num_pkgs} package{plural}{cfg}"))?;
517+
Ok(())
518+
}
519+
480520
fn is_latest(candidate: &semver::Version, current: &semver::Version) -> bool {
481521
current < candidate
482522
// Only match pre-release if major.minor.patch are the same

src/cargo/ops/resolve.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ fn resolve_with_registry<'gctx>(
255255
false
256256
};
257257
if print {
258-
ops::print_lockfile_changes(ws.gctx(), prev.as_ref(), &resolve, registry)?;
258+
ops::print_lockfile_changes(ws, prev.as_ref(), &resolve, registry)?;
259259
}
260260
Ok(resolve)
261261
}

tests/testsuite/alt_registry.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ fn depend_on_alt_registry() {
3333
.with_stderr(
3434
"\
3535
[UPDATING] `alternative` index
36-
[LOCKING] 2 packages
36+
[LOCKING] 2 packages to latest compatible versions
3737
[DOWNLOADING] crates ...
3838
[DOWNLOADED] bar v0.0.1 (registry `alternative`)
3939
[CHECKING] bar v0.0.1 (registry `alternative`)
@@ -88,7 +88,7 @@ fn depend_on_alt_registry_depends_on_same_registry_no_index() {
8888
.with_stderr(
8989
"\
9090
[UPDATING] `alternative` index
91-
[LOCKING] 3 packages
91+
[LOCKING] 3 packages to latest compatible versions
9292
[DOWNLOADING] crates ...
9393
[DOWNLOADED] [..] v0.0.1 (registry `alternative`)
9494
[DOWNLOADED] [..] v0.0.1 (registry `alternative`)
@@ -132,7 +132,7 @@ fn depend_on_alt_registry_depends_on_same_registry() {
132132
.with_stderr(
133133
"\
134134
[UPDATING] `alternative` index
135-
[LOCKING] 3 packages
135+
[LOCKING] 3 packages to latest compatible versions
136136
[DOWNLOADING] crates ...
137137
[DOWNLOADED] [..] v0.0.1 (registry `alternative`)
138138
[DOWNLOADED] [..] v0.0.1 (registry `alternative`)
@@ -177,7 +177,7 @@ fn depend_on_alt_registry_depends_on_crates_io() {
177177
"\
178178
[UPDATING] `alternative` index
179179
[UPDATING] `dummy-registry` index
180-
[LOCKING] 3 packages
180+
[LOCKING] 3 packages to latest compatible versions
181181
[DOWNLOADING] crates ...
182182
[DOWNLOADED] baz v0.0.1 (registry `dummy-registry`)
183183
[DOWNLOADED] bar v0.0.1 (registry `alternative`)
@@ -217,7 +217,7 @@ fn registry_and_path_dep_works() {
217217
p.cargo("check")
218218
.with_stderr(
219219
"\
220-
[LOCKING] 2 packages
220+
[LOCKING] 2 packages to latest compatible versions
221221
[CHECKING] bar v0.0.1 ([CWD]/bar)
222222
[CHECKING] foo v0.0.1 ([CWD])
223223
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [..]s
@@ -421,7 +421,7 @@ fn alt_registry_and_crates_io_deps() {
421421
"\
422422
[UPDATING] `alternative` index
423423
[UPDATING] `dummy-registry` index
424-
[LOCKING] 3 packages
424+
[LOCKING] 3 packages to latest compatible versions
425425
[DOWNLOADING] crates ...
426426
[DOWNLOADED] crates_io_dep v0.0.1 (registry `dummy-registry`)
427427
[DOWNLOADED] alt_reg_dep v0.1.0 (registry `alternative`)
@@ -698,7 +698,7 @@ fn patch_alt_reg() {
698698
.with_stderr(
699699
"\
700700
[UPDATING] `alternative` index
701-
[LOCKING] 2 packages
701+
[LOCKING] 2 packages to latest compatible versions
702702
[CHECKING] bar v0.1.0 ([CWD]/bar)
703703
[CHECKING] foo v0.0.1 ([CWD])
704704
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [..]
@@ -791,7 +791,7 @@ fn no_api() {
791791
.with_stderr(
792792
"\
793793
[UPDATING] `alternative` index
794-
[LOCKING] 2 packages
794+
[LOCKING] 2 packages to latest compatible versions
795795
[DOWNLOADING] crates ...
796796
[DOWNLOADED] bar v0.0.1 (registry `alternative`)
797797
[CHECKING] bar v0.0.1 (registry `alternative`)
@@ -1354,7 +1354,7 @@ fn registries_index_relative_url() {
13541354
.with_stderr(
13551355
"\
13561356
[UPDATING] `relative` index
1357-
[LOCKING] 2 packages
1357+
[LOCKING] 2 packages to latest compatible versions
13581358
[DOWNLOADING] crates ...
13591359
[DOWNLOADED] bar v0.0.1 (registry `relative`)
13601360
[CHECKING] bar v0.0.1 (registry `relative`)

0 commit comments

Comments
 (0)