]> git.proxmox.com Git - cargo.git/commitdiff
Improve error message for wrong target names
authorhi-rustin <rustin.liu@gmail.com>
Wed, 17 Aug 2022 13:43:27 +0000 (21:43 +0800)
committerhi-rustin <rustin.liu@gmail.com>
Fri, 19 Aug 2022 02:53:09 +0000 (10:53 +0800)
src/cargo/ops/cargo_compile.rs

index ced55770ef8072e5df12332e4a37f1575212a0e8..9cca2ee3099282c5aada391cf27984319d654d9f 100644 (file)
@@ -23,6 +23,7 @@
 //!       repeats until the queue is empty.
 
 use std::collections::{BTreeSet, HashMap, HashSet};
+use std::fmt::Write;
 use std::hash::{Hash, Hasher};
 use std::sync::Arc;
 
@@ -1526,19 +1527,40 @@ fn find_named_targets<'a>(
     };
 
     if proposals.is_empty() {
-        let targets = packages.iter().flat_map(|pkg| {
-            pkg.targets()
-                .iter()
-                .filter(|target| is_expected_kind(target))
-        });
-        let suggestion = closest_msg(target_name, targets, |t| t.name());
-        anyhow::bail!(
-            "no {} target {} `{}`{}",
-            target_desc,
-            if is_glob { "matches pattern" } else { "named" },
-            target_name,
-            suggestion
-        );
+        let targets = packages
+            .iter()
+            .flat_map(|pkg| {
+                pkg.targets()
+                    .iter()
+                    .filter(|target| is_expected_kind(target))
+            })
+            .collect::<Vec<_>>();
+        let suggestion = closest_msg(target_name, targets.iter(), |t| t.name());
+        if !suggestion.is_empty() {
+            anyhow::bail!(
+                "no {} target {} `{}`{}",
+                target_desc,
+                if is_glob { "matches pattern" } else { "named" },
+                target_name,
+                suggestion
+            );
+        } else {
+            let mut msg = String::new();
+            writeln!(
+                msg,
+                "no {} target {} `{}`.",
+                target_desc,
+                if is_glob { "matches pattern" } else { "named" },
+                target_name,
+            )?;
+            if !targets.is_empty() {
+                writeln!(msg, "Available {} targets:", target_desc)?;
+                for target in targets {
+                    writeln!(msg, "    {}", target.name())?;
+                }
+            }
+            anyhow::bail!(msg);
+        }
     }
     Ok(proposals)
 }