Skip to content

Commit 0755364

Browse files
committed
Port some resolve messages.
1 parent 5e77adf commit 0755364

File tree

3 files changed

+100
-89
lines changed

3 files changed

+100
-89
lines changed

src/cargo/ops/resolve.rs

Lines changed: 38 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ use crate::util::CanonicalUrl;
7878
use crate::util::cache_lock::CacheLockMode;
7979
use crate::util::context::FeatureUnification;
8080
use crate::util::errors::CargoResult;
81+
use annotate_snippets::Group;
82+
use annotate_snippets::Level;
8183
use anyhow::Context as _;
8284
use cargo_util::paths;
8385
use cargo_util_schemas::core::PartialVersion;
@@ -217,16 +219,19 @@ pub fn resolve_ws_with_opts<'gctx>(
217219
unused_fields.push("`default-features`")
218220
}
219221
if !unused_fields.is_empty() {
220-
let mut shell = ws.gctx().shell();
221-
shell.warn(format!(
222-
"unused field in replacement for `{}`: {}",
223-
dep.package_name(),
224-
unused_fields.join(", ")
225-
))?;
226-
shell.note(format!(
227-
"configure {} in the `dependencies` entry",
228-
unused_fields.join(", ")
229-
))?;
222+
ws.gctx().shell().print_report(
223+
&[Level::WARNING
224+
.secondary_title(format!(
225+
"unused field in replacement for `{}`: {}",
226+
dep.package_name(),
227+
unused_fields.join(", ")
228+
))
229+
.element(Level::NOTE.message(format!(
230+
"configure {} in the `dependencies` entry",
231+
unused_fields.join(", ")
232+
)))],
233+
false,
234+
)?;
230235
}
231236
}
232237

@@ -808,7 +813,7 @@ fn emit_warnings_of_unused_patches(
808813
resolve: &Resolve,
809814
registry: &PackageRegistry<'_>,
810815
) -> CargoResult<()> {
811-
const MESSAGE: &str = "was not used in the crate graph.";
816+
const MESSAGE: &str = "was not used in the crate graph";
812817

813818
// Patch package with the source URLs being patch
814819
let mut patch_pkgid_to_urls = HashMap::new();
@@ -832,8 +837,8 @@ fn emit_warnings_of_unused_patches(
832837

833838
let mut unemitted_unused_patches = Vec::new();
834839
for unused in resolve.unused_patches().iter() {
835-
// Show alternative source URLs if the source URLs being patch
836-
// cannot not be found in the crate graph.
840+
// Show alternative source URLs if the source URLs being patched
841+
// cannot be found in the crate graph.
837842
match (
838843
source_ids_grouped_by_pkg_name.get(&unused.name()),
839844
patch_pkgid_to_urls.get(unused),
@@ -843,32 +848,36 @@ fn emit_warnings_of_unused_patches(
843848
.iter()
844849
.all(|id| !patched_urls.contains(id.canonical_url())) =>
845850
{
846-
use std::fmt::Write;
847-
let mut msg = String::new();
848-
writeln!(msg, "Patch `{}` {}", unused, MESSAGE)?;
849-
write!(
850-
msg,
851-
"Perhaps you misspelled the source URL being patched.\n\
852-
Possible URLs for `[patch.<URL>]`:",
853-
)?;
854-
for id in ids.iter() {
855-
write!(msg, "\n {}", id.display_registry_name())?;
851+
let mut help = "perhaps you meant one of the following:".to_owned();
852+
for id in ids {
853+
help.push_str("\n\t");
854+
help.push_str(&id.display_registry_name());
856855
}
857-
ws.gctx().shell().warn(msg)?;
856+
ws.gctx().shell().print_report(
857+
&[Level::WARNING
858+
.secondary_title(format!("patch `{unused}` {MESSAGE}"))
859+
.element(Level::HELP.message(help))],
860+
false,
861+
)?;
858862
}
859863
_ => unemitted_unused_patches.push(unused),
860864
}
861865
}
862866

863867
// Show general help message.
864868
if !unemitted_unused_patches.is_empty() {
865-
let warnings: Vec<_> = unemitted_unused_patches
869+
let mut warnings: Vec<_> = unemitted_unused_patches
866870
.iter()
867-
.map(|pkgid| format!("Patch `{}` {}", pkgid, MESSAGE))
871+
.map(|pkgid| {
872+
Group::with_title(
873+
Level::WARNING.secondary_title(format!("patch `{pkgid}` {MESSAGE}")),
874+
)
875+
})
868876
.collect();
869-
ws.gctx()
870-
.shell()
871-
.warn(format!("{}\n{}", warnings.join("\n"), UNUSED_PATCH_WARNING))?;
877+
warnings.push(Group::with_title(
878+
Level::HELP.secondary_title(UNUSED_PATCH_WARNING),
879+
));
880+
ws.gctx().shell().print_report(&warnings, false)?;
872881
}
873882

874883
return Ok(());

tests/testsuite/patch.rs

Lines changed: 58 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -386,11 +386,11 @@ fn unused() {
386386
p.cargo("check")
387387
.with_stderr_data(str![[r#"
388388
[UPDATING] `dummy-registry` index
389-
[WARNING] Patch `bar v0.2.0 ([ROOT]/foo/bar)` was not used in the crate graph.
390-
Check that the patched package version and available features are compatible
391-
with the dependency requirements. If the patch has a different version from
392-
what is locked in the Cargo.lock file, run `cargo update` to use the new
393-
version. This may also occur with an optional dependency that is not enabled.
389+
[WARNING] patch `bar v0.2.0 ([ROOT]/foo/bar)` was not used in the crate graph
390+
[HELP] Check that the patched package version and available features are compatible
391+
with the dependency requirements. If the patch has a different version from
392+
what is locked in the Cargo.lock file, run `cargo update` to use the new
393+
version. This may also occur with an optional dependency that is not enabled.
394394
[LOCKING] 1 package to latest compatible version
395395
[ADDING] bar v0.1.0 (available: v0.2.0)
396396
[DOWNLOADING] crates ...
@@ -403,11 +403,11 @@ version. This may also occur with an optional dependency that is not enabled.
403403
.run();
404404
p.cargo("check")
405405
.with_stderr_data(str![[r#"
406-
[WARNING] Patch `bar v0.2.0 ([ROOT]/foo/bar)` was not used in the crate graph.
407-
Check that the patched package version and available features are compatible
408-
with the dependency requirements. If the patch has a different version from
409-
what is locked in the Cargo.lock file, run `cargo update` to use the new
410-
version. This may also occur with an optional dependency that is not enabled.
406+
[WARNING] patch `bar v0.2.0 ([ROOT]/foo/bar)` was not used in the crate graph
407+
[HELP] Check that the patched package version and available features are compatible
408+
with the dependency requirements. If the patch has a different version from
409+
what is locked in the Cargo.lock file, run `cargo update` to use the new
410+
version. This may also occur with an optional dependency that is not enabled.
411411
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
412412
413413
"#]])
@@ -459,15 +459,15 @@ fn unused_with_mismatch_source_being_patched() {
459459
p.cargo("check")
460460
.with_stderr_data(str![[r#"
461461
[UPDATING] `dummy-registry` index
462-
[WARNING] Patch `bar v0.2.0 ([ROOT]/foo/bar)` was not used in the crate graph.
463-
Perhaps you misspelled the source URL being patched.
464-
Possible URLs for `[patch.<URL>]`:
465-
crates-io
466-
[WARNING] Patch `bar v0.3.0 ([ROOT]/foo/baz)` was not used in the crate graph.
467-
Check that the patched package version and available features are compatible
468-
with the dependency requirements. If the patch has a different version from
469-
what is locked in the Cargo.lock file, run `cargo update` to use the new
470-
version. This may also occur with an optional dependency that is not enabled.
462+
[WARNING] patch `bar v0.2.0 ([ROOT]/foo/bar)` was not used in the crate graph
463+
|
464+
= [HELP] perhaps you meant one of the following:
465+
crates-io
466+
[WARNING] patch `bar v0.3.0 ([ROOT]/foo/baz)` was not used in the crate graph
467+
[HELP] Check that the patched package version and available features are compatible
468+
with the dependency requirements. If the patch has a different version from
469+
what is locked in the Cargo.lock file, run `cargo update` to use the new
470+
version. This may also occur with an optional dependency that is not enabled.
471471
[LOCKING] 1 package to latest compatible version
472472
[ADDING] bar v0.1.0 (available: v0.3.0)
473473
[DOWNLOADING] crates ...
@@ -562,11 +562,11 @@ fn unused_from_config() {
562562
p.cargo("check")
563563
.with_stderr_data(str![[r#"
564564
[UPDATING] `dummy-registry` index
565-
[WARNING] Patch `bar v0.2.0 ([ROOT]/foo/bar)` was not used in the crate graph.
566-
Check that the patched package version and available features are compatible
567-
with the dependency requirements. If the patch has a different version from
568-
what is locked in the Cargo.lock file, run `cargo update` to use the new
569-
version. This may also occur with an optional dependency that is not enabled.
565+
[WARNING] patch `bar v0.2.0 ([ROOT]/foo/bar)` was not used in the crate graph
566+
[HELP] Check that the patched package version and available features are compatible
567+
with the dependency requirements. If the patch has a different version from
568+
what is locked in the Cargo.lock file, run `cargo update` to use the new
569+
version. This may also occur with an optional dependency that is not enabled.
570570
[LOCKING] 1 package to latest compatible version
571571
[ADDING] bar v0.1.0 (available: v0.2.0)
572572
[DOWNLOADING] crates ...
@@ -579,11 +579,11 @@ version. This may also occur with an optional dependency that is not enabled.
579579
.run();
580580
p.cargo("check")
581581
.with_stderr_data(str![[r#"
582-
[WARNING] Patch `bar v0.2.0 ([ROOT]/foo/bar)` was not used in the crate graph.
583-
Check that the patched package version and available features are compatible
584-
with the dependency requirements. If the patch has a different version from
585-
what is locked in the Cargo.lock file, run `cargo update` to use the new
586-
version. This may also occur with an optional dependency that is not enabled.
582+
[WARNING] patch `bar v0.2.0 ([ROOT]/foo/bar)` was not used in the crate graph
583+
[HELP] Check that the patched package version and available features are compatible
584+
with the dependency requirements. If the patch has a different version from
585+
what is locked in the Cargo.lock file, run `cargo update` to use the new
586+
version. This may also occur with an optional dependency that is not enabled.
587587
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
588588
589589
"#]])
@@ -636,11 +636,11 @@ fn unused_git() {
636636
.with_stderr_data(str![[r#"
637637
[UPDATING] git repository `[ROOTURL]/override`
638638
[UPDATING] `dummy-registry` index
639-
[WARNING] Patch `bar v0.2.0 ([ROOTURL]/override#[..])` was not used in the crate graph.
640-
Check that the patched package version and available features are compatible
641-
with the dependency requirements. If the patch has a different version from
642-
what is locked in the Cargo.lock file, run `cargo update` to use the new
643-
version. This may also occur with an optional dependency that is not enabled.
639+
[WARNING] patch `bar v0.2.0 ([ROOTURL]/override#[..])` was not used in the crate graph
640+
[HELP] Check that the patched package version and available features are compatible
641+
with the dependency requirements. If the patch has a different version from
642+
what is locked in the Cargo.lock file, run `cargo update` to use the new
643+
version. This may also occur with an optional dependency that is not enabled.
644644
[LOCKING] 1 package to latest compatible version
645645
[ADDING] bar v0.1.0 (available: v0.2.0)
646646
[DOWNLOADING] crates ...
@@ -653,11 +653,11 @@ version. This may also occur with an optional dependency that is not enabled.
653653
.run();
654654
p.cargo("check")
655655
.with_stderr_data(str![[r#"
656-
[WARNING] Patch `bar v0.2.0 ([ROOTURL]/override#[..])` was not used in the crate graph.
657-
Check that the patched package version and available features are compatible
658-
with the dependency requirements. If the patch has a different version from
659-
what is locked in the Cargo.lock file, run `cargo update` to use the new
660-
version. This may also occur with an optional dependency that is not enabled.
656+
[WARNING] patch `bar v0.2.0 ([ROOTURL]/override#[..])` was not used in the crate graph
657+
[HELP] Check that the patched package version and available features are compatible
658+
with the dependency requirements. If the patch has a different version from
659+
what is locked in the Cargo.lock file, run `cargo update` to use the new
660+
version. This may also occur with an optional dependency that is not enabled.
661661
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
662662
663663
"#]])
@@ -870,22 +870,22 @@ fn add_ignored_patch() {
870870

871871
p.cargo("check")
872872
.with_stderr_data(str![[r#"
873-
[WARNING] Patch `bar v0.1.1 ([ROOT]/foo/bar)` was not used in the crate graph.
874-
Check that the patched package version and available features are compatible
875-
with the dependency requirements. If the patch has a different version from
876-
what is locked in the Cargo.lock file, run `cargo update` to use the new
877-
version. This may also occur with an optional dependency that is not enabled.
873+
[WARNING] patch `bar v0.1.1 ([ROOT]/foo/bar)` was not used in the crate graph
874+
[HELP] Check that the patched package version and available features are compatible
875+
with the dependency requirements. If the patch has a different version from
876+
what is locked in the Cargo.lock file, run `cargo update` to use the new
877+
version. This may also occur with an optional dependency that is not enabled.
878878
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
879879
880880
"#]])
881881
.run();
882882
p.cargo("check")
883883
.with_stderr_data(str![[r#"
884-
[WARNING] Patch `bar v0.1.1 ([ROOT]/foo/bar)` was not used in the crate graph.
885-
Check that the patched package version and available features are compatible
886-
with the dependency requirements. If the patch has a different version from
887-
what is locked in the Cargo.lock file, run `cargo update` to use the new
888-
version. This may also occur with an optional dependency that is not enabled.
884+
[WARNING] patch `bar v0.1.1 ([ROOT]/foo/bar)` was not used in the crate graph
885+
[HELP] Check that the patched package version and available features are compatible
886+
with the dependency requirements. If the patch has a different version from
887+
what is locked in the Cargo.lock file, run `cargo update` to use the new
888+
version. This may also occur with an optional dependency that is not enabled.
889889
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
890890
891891
"#]])
@@ -1884,10 +1884,10 @@ fn two_semver_compatible() {
18841884
p.cargo("check").run();
18851885
p.cargo("check")
18861886
.with_stderr_data(str![[r#"
1887-
[WARNING] Patch `bar v0.1.1 ([ROOTURL]/override#[..])` was not used in the crate graph.
1888-
Perhaps you misspelled the source URL being patched.
1889-
Possible URLs for `[patch.<URL>]`:
1890-
[ROOT]/foo/bar
1887+
[WARNING] patch `bar v0.1.1 ([ROOTURL]/override#[..])` was not used in the crate graph
1888+
|
1889+
= [HELP] perhaps you meant one of the following:
1890+
[ROOT]/foo/bar
18911891
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
18921892
18931893
"#]])
@@ -1940,10 +1940,10 @@ fn multipatch_select_big() {
19401940
p.cargo("check").run();
19411941
p.cargo("check")
19421942
.with_stderr_data(str![[r#"
1943-
[WARNING] Patch `bar v0.1.0 ([ROOTURL]/override#[..])` was not used in the crate graph.
1944-
Perhaps you misspelled the source URL being patched.
1945-
Possible URLs for `[patch.<URL>]`:
1946-
[ROOT]/foo/bar
1943+
[WARNING] patch `bar v0.1.0 ([ROOTURL]/override#[..])` was not used in the crate graph
1944+
|
1945+
= [HELP] perhaps you meant one of the following:
1946+
[ROOT]/foo/bar
19471947
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
19481948
19491949
"#]])
@@ -2064,7 +2064,7 @@ fn update_unused_new_version() {
20642064
p.cargo("check")
20652065
.with_stderr_data(str![[r#"
20662066
[UPDATING] `dummy-registry` index
2067-
[WARNING] Patch `bar v0.1.4 ([ROOT]/bar)` was not used in the crate graph.
2067+
[WARNING] patch `bar v0.1.4 ([ROOT]/bar)` was not used in the crate graph
20682068
...
20692069
"#]])
20702070
.run();

tests/testsuite/replace.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,8 @@ fn override_with_features() {
9595
[UPDATING] git repository `[ROOTURL]/override`
9696
[LOCKING] 2 packages to latest compatible versions
9797
[WARNING] unused field in replacement for `bar`: `features`
98-
[NOTE] configure `features` in the `dependencies` entry
98+
|
99+
= [NOTE] configure `features` in the `dependencies` entry
99100
[CHECKING] bar v0.1.0 ([ROOTURL]/override#[..])
100101
[CHECKING] foo v0.0.1 ([ROOT]/foo)
101102
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
@@ -145,7 +146,8 @@ fn override_with_setting_default_features() {
145146
[UPDATING] git repository `[ROOTURL]/override`
146147
[LOCKING] 2 packages to latest compatible versions
147148
[WARNING] unused field in replacement for `bar`: `features`, `default-features`
148-
[NOTE] configure `features`, `default-features` in the `dependencies` entry
149+
|
150+
= [NOTE] configure `features`, `default-features` in the `dependencies` entry
149151
[CHECKING] bar v0.1.0 ([ROOTURL]/override#[..])
150152
[CHECKING] foo v0.0.1 ([ROOT]/foo)
151153
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s

0 commit comments

Comments
 (0)