Skip to content

Commit 36bc1d1

Browse files
authored
More warning conversions (#16126)
Yet another part of #15944. I saw some inconsistent formatting for links: some are surrounded in "<>" but others aren't. Is there a policy for that?
2 parents 7a5192f + 0755364 commit 36bc1d1

File tree

13 files changed

+322
-270
lines changed

13 files changed

+322
-270
lines changed

src/bin/cargo/cli.rs

Lines changed: 43 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use annotate_snippets::Level;
12
use anyhow::{Context as _, anyhow};
23
use cargo::core::{CliUnstable, features};
34
use cargo::util::context::TermConfig;
@@ -318,26 +319,40 @@ To pass the arguments to the subcommand, remove `--`",
318319
// a hard error.
319320
if super::builtin_aliases_execs(cmd).is_none() {
320321
if let Some(path) = super::find_external_subcommand(gctx, cmd) {
321-
gctx.shell().warn(format!(
322-
"\
323-
user-defined alias `{}` is shadowing an external subcommand found at: `{}`
324-
This was previously accepted but is being phased out; it will become a hard error in a future release.
325-
For more information, see issue #10049 <https://github.com/rust-lang/cargo/issues/10049>.",
326-
cmd,
327-
path.display(),
328-
))?;
322+
gctx.shell().print_report(
323+
&[
324+
Level::WARNING.secondary_title(format!(
325+
"user-defined alias `{}` is shadowing an external subcommand found at `{}`",
326+
cmd,
327+
path.display()
328+
)).element(
329+
Level::NOTE.message(
330+
"this was previously accepted but will become a hard error in the future; \
331+
see <https://github.com/rust-lang/cargo/issues/10049>"
332+
)
333+
)
334+
],
335+
false,
336+
)?;
329337
}
330338
}
331339
if commands::run::is_manifest_command(cmd) {
332340
if gctx.cli_unstable().script {
333341
return Ok((args, GlobalArgs::default()));
334342
} else {
335-
gctx.shell().warn(format_args!(
336-
"\
337-
user-defined alias `{cmd}` has the appearance of a manifest-command
338-
This was previously accepted but will be phased out when `-Zscript` is stabilized.
339-
For more information, see issue #12207 <https://github.com/rust-lang/cargo/issues/12207>."
340-
))?;
343+
gctx.shell().print_report(
344+
&[
345+
Level::WARNING.secondary_title(
346+
format!("user-defined alias `{cmd}` has the appearance of a manifest-command")
347+
).element(
348+
Level::NOTE.message(
349+
"this was previously accepted but will be phased out when `-Zscript` is stabilized; \
350+
see <https://github.com/rust-lang/cargo/issues/12207>"
351+
)
352+
)
353+
],
354+
false
355+
)?;
341356
}
342357
}
343358

@@ -477,12 +492,20 @@ impl Exec {
477492
Self::Manifest(cmd) => {
478493
let ext_path = super::find_external_subcommand(gctx, &cmd);
479494
if !gctx.cli_unstable().script && ext_path.is_some() {
480-
gctx.shell().warn(format_args!(
481-
"\
482-
external subcommand `{cmd}` has the appearance of a manifest-command
483-
This was previously accepted but will be phased out when `-Zscript` is stabilized.
484-
For more information, see issue #12207 <https://github.com/rust-lang/cargo/issues/12207>.",
485-
))?;
495+
gctx.shell().print_report(
496+
&[
497+
Level::WARNING.secondary_title(
498+
format!("external subcommand `{cmd}` has the appearance of a manifest-command")
499+
).element(
500+
Level::NOTE.message(
501+
"this was previously accepted but will be phased out when `-Zscript` is stabilized; \
502+
see <https://github.com/rust-lang/cargo/issues/12207>"
503+
)
504+
)
505+
],
506+
false
507+
)?;
508+
486509
Self::External(cmd).exec(gctx, subcommand_args)
487510
} else {
488511
let ext_args: Vec<OsString> = subcommand_args

src/cargo/core/compiler/build_runner/mod.rs

Lines changed: 52 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use crate::core::compiler::compilation::{self, UnitOutput};
99
use crate::core::compiler::{self, Unit, artifact};
1010
use crate::util::cache_lock::CacheLockMode;
1111
use crate::util::errors::CargoResult;
12+
use annotate_snippets::{Level, Message};
1213
use anyhow::{Context as _, bail};
1314
use cargo_util::paths;
1415
use filetime::FileTime;
@@ -515,58 +516,56 @@ impl<'a, 'gctx> BuildRunner<'a, 'gctx> {
515516
#[tracing::instrument(skip_all)]
516517
fn check_collisions(&self) -> CargoResult<()> {
517518
let mut output_collisions = HashMap::new();
518-
let describe_collision = |unit: &Unit, other_unit: &Unit, path: &PathBuf| -> String {
519+
let describe_collision = |unit: &Unit, other_unit: &Unit| -> String {
519520
format!(
520-
"The {} target `{}` in package `{}` has the same output \
521-
filename as the {} target `{}` in package `{}`.\n\
522-
Colliding filename is: {}\n",
521+
"the {} target `{}` in package `{}` has the same output filename as the {} target `{}` in package `{}`",
523522
unit.target.kind().description(),
524523
unit.target.name(),
525524
unit.pkg.package_id(),
526525
other_unit.target.kind().description(),
527526
other_unit.target.name(),
528527
other_unit.pkg.package_id(),
529-
path.display()
530528
)
531529
};
532-
let suggestion = "Consider changing their names to be unique or compiling them separately.\n\
533-
This may become a hard error in the future; see \
534-
<https://github.com/rust-lang/cargo/issues/6313>.";
535-
let rustdoc_suggestion = "This is a known bug where multiple crates with the same name use\n\
536-
the same path; see <https://github.com/rust-lang/cargo/issues/6313>.";
530+
let suggestion = [
531+
Level::NOTE.message("this may become a hard error in the future; see <https://github.com/rust-lang/cargo/issues/6313>"),
532+
Level::HELP.message("consider changing their names to be unique or compiling them separately")
533+
];
534+
let rustdoc_suggestion = [
535+
Level::NOTE.message("this is a known bug where multiple crates with the same name use the same path; see <https://github.com/rust-lang/cargo/issues/6313>")
536+
];
537537
let report_collision = |unit: &Unit,
538538
other_unit: &Unit,
539539
path: &PathBuf,
540-
suggestion: &str|
540+
messages: &[Message<'_>]|
541541
-> CargoResult<()> {
542542
if unit.target.name() == other_unit.target.name() {
543-
self.bcx.gctx.shell().warn(format!(
544-
"output filename collision.\n\
545-
{}\
546-
The targets should have unique names.\n\
547-
{}",
548-
describe_collision(unit, other_unit, path),
549-
suggestion
550-
))
543+
self.bcx.gctx.shell().print_report(
544+
&[Level::WARNING
545+
.secondary_title(format!("output filename collision at {}", path.display()))
546+
.elements(
547+
[Level::NOTE.message(describe_collision(unit, other_unit))]
548+
.into_iter()
549+
.chain(messages.iter().cloned()),
550+
)],
551+
false,
552+
)
551553
} else {
552-
self.bcx.gctx.shell().warn(format!(
553-
"output filename collision.\n\
554-
{}\
555-
The output filenames should be unique.\n\
556-
{}\n\
557-
If this looks unexpected, it may be a bug in Cargo. Please file a bug report at\n\
558-
https://github.com/rust-lang/cargo/issues/ with as much information as you\n\
559-
can provide.\n\
560-
cargo {} running on `{}` target `{}`\n\
561-
First unit: {:?}\n\
562-
Second unit: {:?}",
563-
describe_collision(unit, other_unit, path),
564-
suggestion,
565-
crate::version(),
566-
self.bcx.host_triple(),
567-
self.bcx.target_data.short_name(&unit.kind),
568-
unit,
569-
other_unit))
554+
self.bcx.gctx.shell().print_report(
555+
&[Level::WARNING
556+
.secondary_title(format!("output filename collision at {}", path.display()))
557+
.elements([
558+
Level::NOTE.message(describe_collision(unit, other_unit)),
559+
Level::NOTE.message("if this looks unexpected, it may be a bug in Cargo. Please file a bug \
560+
report at https://github.com/rust-lang/cargo/issues/ with as much information as you \
561+
can provide."),
562+
Level::NOTE.message(format!("cargo {} running on `{}` target `{}`",
563+
crate::version(), self.bcx.host_triple(), self.bcx.target_data.short_name(&unit.kind))),
564+
Level::NOTE.message(format!("first unit: {unit:?}")),
565+
Level::NOTE.message(format!("second unit: {other_unit:?}")),
566+
])],
567+
false,
568+
)
570569
}
571570
};
572571

@@ -623,26 +622,31 @@ impl<'a, 'gctx> BuildRunner<'a, 'gctx> {
623622
if unit.mode.is_doc() {
624623
// See https://github.com/rust-lang/rust/issues/56169
625624
// and https://github.com/rust-lang/rust/issues/61378
626-
report_collision(unit, other_unit, &output.path, rustdoc_suggestion)?;
625+
report_collision(unit, other_unit, &output.path, &rustdoc_suggestion)?;
627626
} else {
628-
report_collision(unit, other_unit, &output.path, suggestion)?;
627+
report_collision(unit, other_unit, &output.path, &suggestion)?;
629628
}
630629
}
631630
if let Some(hardlink) = output.hardlink.as_ref() {
632631
if let Some(other_unit) = output_collisions.insert(hardlink.clone(), unit) {
633-
report_collision(unit, other_unit, hardlink, suggestion)?;
632+
report_collision(unit, other_unit, hardlink, &suggestion)?;
634633
}
635634
}
636635
if let Some(ref export_path) = output.export_path {
637636
if let Some(other_unit) = output_collisions.insert(export_path.clone(), unit) {
638-
self.bcx.gctx.shell().warn(format!(
639-
"`--artifact-dir` filename collision.\n\
640-
{}\
641-
The exported filenames should be unique.\n\
642-
{}",
643-
describe_collision(unit, other_unit, export_path),
644-
suggestion
645-
))?;
637+
self.bcx.gctx.shell().print_report(
638+
&[Level::WARNING
639+
.secondary_title(format!(
640+
"`--artifact-dir` filename collision at {}",
641+
export_path.display()
642+
))
643+
.elements(
644+
[Level::NOTE.message(describe_collision(unit, other_unit))]
645+
.into_iter()
646+
.chain(suggestion.iter().cloned()),
647+
)],
648+
false,
649+
)?;
646650
}
647651
}
648652
}

src/cargo/core/compiler/mod.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1717,15 +1717,16 @@ fn build_deps_args(
17171717
if let Some(dep) = deps.iter().find(|dep| {
17181718
!dep.unit.mode.is_doc() && dep.unit.target.is_lib() && !dep.unit.artifact.is_true()
17191719
}) {
1720-
bcx.gctx.shell().warn(format!(
1721-
"The package `{}` \
1722-
provides no linkable target. The compiler might raise an error while compiling \
1723-
`{}`. Consider adding 'dylib' or 'rlib' to key `crate-type` in `{}`'s \
1724-
Cargo.toml. This warning might turn into a hard error in the future.",
1725-
dep.unit.target.crate_name(),
1726-
unit.target.crate_name(),
1727-
dep.unit.target.crate_name()
1728-
))?;
1720+
let dep_name = dep.unit.target.crate_name();
1721+
let name = unit.target.crate_name();
1722+
bcx.gctx.shell().print_report(&[
1723+
Level::WARNING.secondary_title(format!("the package `{dep_name}` provides no linkable target"))
1724+
.elements([
1725+
Level::NOTE.message(format!("this might cause `{name}` to fail compilation")),
1726+
Level::NOTE.message("this warning might turn into a hard error in the future"),
1727+
Level::HELP.message(format!("consider adding 'dylib' or 'rlib' to key 'crate-type' in `{dep_name}`'s Cargo.toml"))
1728+
])
1729+
], false)?;
17291730
}
17301731
}
17311732

src/cargo/ops/registry/publish.rs

Lines changed: 52 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use std::io::Seek;
1111
use std::io::SeekFrom;
1212
use std::time::Duration;
1313

14+
use annotate_snippets::Level;
1415
use anyhow::Context as _;
1516
use anyhow::bail;
1617
use cargo_credential::Operation;
@@ -108,12 +109,16 @@ pub fn publish(ws: &Workspace<'_>, opts: &PublishOpts<'_>) -> CargoResult<()> {
108109
if allow_unpublishable {
109110
let n = unpublishable.len();
110111
let plural = if n == 1 { "" } else { "s" };
111-
ws.gctx().shell().warn(format_args!(
112-
"nothing to publish, but found {n} unpublishable package{plural}"
113-
))?;
114-
ws.gctx().shell().note(format_args!(
115-
"to publish packages, set `package.publish` to `true` or a non-empty list"
116-
))?;
112+
ws.gctx().shell().print_report(
113+
&[Level::WARNING
114+
.secondary_title(format!(
115+
"nothing to publish, but found {n} unpublishable package{plural}"
116+
))
117+
.element(Level::HELP.message(
118+
"to publish packages, set `package.publish` to `true` or a non-empty list",
119+
))],
120+
false,
121+
)?;
117122
return Ok(());
118123
} else {
119124
unreachable!("must have at least one publishable package");
@@ -323,18 +328,23 @@ pub fn publish(ws: &Workspace<'_>, opts: &PublishOpts<'_>) -> CargoResult<()> {
323328
)?;
324329
} else {
325330
let short_pkg_descriptions = package_list(to_confirm.iter().copied(), "or");
326-
opts.gctx.shell().warn(format!(
327-
"timed out waiting for {short_pkg_descriptions} to be available in {source_description}",
328-
))?;
329-
opts.gctx.shell().note(format!(
330-
"the registry may have a backlog that is delaying making the \
331-
{crate} available. The {crate} should be available soon.",
332-
crate = if to_confirm.len() == 1 {
333-
"crate"
334-
} else {
335-
"crates"
336-
}
337-
))?;
331+
let krate = if to_confirm.len() == 1 {
332+
"crate"
333+
} else {
334+
"crates"
335+
};
336+
opts.gctx.shell().print_report(
337+
&[Level::WARNING
338+
.secondary_title(format!(
339+
"timed out waiting for {short_pkg_descriptions} \
340+
to be available in {source_description}",
341+
))
342+
.element(Level::NOTE.message(format!(
343+
"the registry may have a backlog that is delaying making the \
344+
{krate} available. The {krate} should be available soon.",
345+
)))],
346+
false,
347+
)?;
338348
}
339349
confirmed
340350
} else {
@@ -676,25 +686,38 @@ fn transmit(
676686

677687
if !warnings.invalid_categories.is_empty() {
678688
let msg = format!(
679-
"the following are not valid category slugs and were \
680-
ignored: {}. Please see https://crates.io/category_slugs \
681-
for the list of all category slugs. \
682-
",
689+
"the following are not valid category slugs and were ignored: {}",
683690
warnings.invalid_categories.join(", ")
684691
);
685-
gctx.shell().warn(&msg)?;
692+
gctx.shell().print_report(
693+
&[Level::WARNING
694+
.secondary_title(msg)
695+
.element(Level::HELP.message(
696+
"please see <https://crates.io/category_slugs> for the list of all category slugs",
697+
))],
698+
false,
699+
)?;
686700
}
687701

688702
if !warnings.invalid_badges.is_empty() {
689703
let msg = format!(
690-
"the following are not valid badges and were ignored: {}. \
691-
Either the badge type specified is unknown or a required \
692-
attribute is missing. Please see \
693-
https://doc.rust-lang.org/cargo/reference/manifest.html#package-metadata \
694-
for valid badge types and their required attributes.",
704+
"the following are not valid badges and were ignored: {}",
695705
warnings.invalid_badges.join(", ")
696706
);
697-
gctx.shell().warn(&msg)?;
707+
gctx.shell().print_report(
708+
&[Level::WARNING.secondary_title(msg).elements([
709+
Level::NOTE.message(
710+
"either the badge type specified is unknown or a required \
711+
attribute is missing",
712+
),
713+
Level::HELP.message(
714+
"please see \
715+
<https://doc.rust-lang.org/cargo/reference/manifest.html#package-metadata> \
716+
for valid badge types and their required attributes",
717+
),
718+
])],
719+
false,
720+
)?;
698721
}
699722

700723
if !warnings.other.is_empty() {

0 commit comments

Comments
 (0)