]> git.proxmox.com Git - cargo.git/commitdiff
Fix #2997: error message when version not found could be improved
authorAndrew Watts <a@andwur.com>
Mon, 17 Apr 2017 12:24:56 +0000 (21:54 +0930)
committerAndrew Watts <a@andwur.com>
Mon, 17 Apr 2017 12:41:11 +0000 (22:11 +0930)
This changes the error message when a package *is* found but there's no
matching version to be a little more helpful.

Old: "no matching package named `...`"
New: "no matching version `...` found for package `...`"

src/cargo/core/resolver/mod.rs
tests/build.rs
tests/registry.rs

index 40bb734db5b06d6fdc91dd9084ca18f9a73c9f89..896b280469e6fea9c04dd02ef1cb518d4ae779bc 100644 (file)
@@ -643,14 +643,6 @@ fn activation_error(cx: &Context,
     // Note that we re-query the registry with a new dependency that
     // allows any version so we can give some nicer error reporting
     // which indicates a few versions that were actually found.
-    let msg = format!("no matching package named `{}` found \
-                       (required by `{}`)\n\
-                       location searched: {}\n\
-                       version required: {}",
-                      dep.name(), parent.name(),
-                      dep.source_id(),
-                      dep.version_req());
-    let mut msg = msg;
     let all_req = semver::VersionReq::parse("*").unwrap();
     let new_dep = dep.clone_inner().set_version_req(all_req).into_dependency();
     let mut candidates = match registry.query(&new_dep) {
@@ -660,27 +652,50 @@ fn activation_error(cx: &Context,
     candidates.sort_by(|a, b| {
         b.version().cmp(a.version())
     });
-    if !candidates.is_empty() {
-        msg.push_str("\nversions found: ");
-        for (i, c) in candidates.iter().take(3).enumerate() {
-            if i != 0 { msg.push_str(", "); }
-            msg.push_str(&c.version().to_string());
-        }
-        if candidates.len() > 3 {
-            msg.push_str(", ...");
+
+    let msg = if !candidates.is_empty() {
+        let versions = {
+            let mut versions = candidates.iter().take(3).map(|cand| {
+                cand.version().to_string()
+            }).collect::<Vec<_>>();
+
+            if candidates.len() > 3 {
+                versions.push("...".into());
+            }
+
+            versions.join(", ")
+        };
+
+        let mut msg = format!("no matching version `{}` found for package `{}` \
+                               (required by `{}`)\n\
+                               location searched: {}\n\
+                               versions found: {}",
+                              dep.version_req(),
+                              dep.name(),
+                              parent.name(),
+                              dep.source_id(),
+                              versions);
+
+        // If we have a path dependency with a locked version, then this may
+        // indicate that we updated a sub-package and forgot to run `cargo
+        // update`. In this case try to print a helpful error!
+        if dep.source_id().is_path()
+           && dep.version_req().to_string().starts_with("=") {
+            msg.push_str("\nconsider running `cargo update` to update \
+                          a path dependency's locked version");
         }
-    }
 
-    // If we have a path dependency with a locked version, then this may
-    // indicate that we updated a sub-package and forgot to run `cargo
-    // update`. In this case try to print a helpful error!
-    if dep.source_id().is_path() &&
-       dep.version_req().to_string().starts_with("=") &&
-       !candidates.is_empty() {
-        msg.push_str("\nconsider running `cargo update` to update \
-                      a path dependency's locked version");
+        msg
+    } else {
+        format!("no matching package named `{}` found \
+                 (required by `{}`)\n\
+                 location searched: {}\n\
+                 version required: {}",
+                dep.name(), parent.name(),
+                dep.source_id(),
+                dep.version_req())
+    };
 
-    }
     human(msg)
 }
 
index 1eea86b5cb2c26c7e94963e6d7570a7d518c6f4b..eccccbe6cf90af7dbf2b8265e14555795dc322cb 100644 (file)
@@ -760,9 +760,8 @@ fn compile_path_dep_then_change_version() {
 
     assert_that(p.cargo("build"),
                 execs().with_status(101).with_stderr("\
-[ERROR] no matching package named `bar` found (required by `foo`)
+[ERROR] no matching version `= 0.0.1` found for package `bar` (required by `foo`)
 location searched: [..]
-version required: = 0.0.1
 versions found: 0.0.2
 consider running `cargo update` to update a path dependency's locked version
 "));
index d99415264d5b38582bb047f59cbe453774ed1e3f..b336827abbc4f3345f25af3690a61b87742f85f1 100644 (file)
@@ -134,9 +134,8 @@ fn wrong_version() {
 
     assert_that(p.cargo("build"),
                 execs().with_status(101).with_stderr_contains("\
-[ERROR] no matching package named `foo` found (required by `foo`)
+[ERROR] no matching version `>= 1.0.0` found for package `foo` (required by `foo`)
 location searched: registry [..]
-version required: >= 1.0.0
 versions found: 0.0.2, 0.0.1
 "));
 
@@ -145,9 +144,8 @@ versions found: 0.0.2, 0.0.1
 
     assert_that(p.cargo("build"),
                 execs().with_status(101).with_stderr_contains("\
-[ERROR] no matching package named `foo` found (required by `foo`)
+[ERROR] no matching version `>= 1.0.0` found for package `foo` (required by `foo`)
 location searched: registry [..]
-version required: >= 1.0.0
 versions found: 0.0.4, 0.0.3, 0.0.2, ...
 "));
 }
@@ -399,9 +397,8 @@ fn relying_on_a_yank_is_bad() {
 
     assert_that(p.cargo("build"),
                 execs().with_status(101).with_stderr_contains("\
-[ERROR] no matching package named `baz` found (required by `bar`)
+[ERROR] no matching version `= 0.0.2` found for package `baz` (required by `bar`)
 location searched: registry [..]
-version required: = 0.0.2
 versions found: 0.0.1
 "));
 }