Warn about and ignore invalid crate-types in manifest
authorGeorge Hilliard <gh403@msstate.edu>
Sat, 15 Aug 2015 19:49:09 +0000 (14:49 -0500)
committerGeorge Hilliard <gh403@msstate.edu>
Wed, 19 Aug 2015 01:47:44 +0000 (20:47 -0500)
src/cargo/core/manifest.rs
src/cargo/util/toml.rs
tests/test_bad_config.rs

index 9b7907cc7de8d72010626dcc0ca4d4e54ce4895b..cc09b2d120ed157b5eeec921e64491aacab8085c 100644 (file)
@@ -79,7 +79,7 @@ impl LibKind {
             "rlib" => Ok(LibKind::Rlib),
             "dylib" => Ok(LibKind::Dylib),
             "staticlib" => Ok(LibKind::StaticLib),
-            _ => Err(human(format!("{} was not one of lib|rlib|dylib|staticlib",
+            _ => Err(human(format!("crate-type \"{}\" was not one of lib|rlib|dylib|staticlib",
                                    string)))
         }
     }
index 2b488beb83c0113e438679e27e4ece6abdf8618f..f3a16517c07900101830b31b75a54f5c167aeb60 100644 (file)
@@ -369,6 +369,7 @@ impl TomlManifest {
                        config: &Config)
         -> CargoResult<(Manifest, Vec<PathBuf>)> {
         let mut nested_paths = vec!();
+        let mut warnings = vec!();
 
         let project = self.project.as_ref().or_else(|| self.package.as_ref());
         let project = try!(project.chain_error(|| {
@@ -473,7 +474,8 @@ impl TomlManifest {
                                 &examples,
                                 &tests,
                                 &benches,
-                                &metadata);
+                                &metadata,
+                                &mut warnings);
 
         if targets.is_empty() {
             debug!("manifest has no build targets");
@@ -550,6 +552,9 @@ impl TomlManifest {
             manifest.add_warning(format!("warning: only one of `license` or \
                                                    `license-file` is necessary"));
         }
+        for warning in warnings {
+            manifest.add_warning(warning.clone());
+        }
 
         Ok((manifest, nested_paths))
     }
@@ -749,7 +754,8 @@ fn normalize(lib: &Option<TomlLibTarget>,
              examples: &[TomlExampleTarget],
              tests: &[TomlTestTarget],
              benches: &[TomlBenchTarget],
-             metadata: &Metadata) -> Vec<Target> {
+             metadata: &Metadata,
+             warnings: &mut Vec<String>) -> Vec<Target> {
     fn configure(toml: &TomlTarget, target: &mut Target) {
         let t2 = target.clone();
         target.set_tested(toml.test.unwrap_or(t2.tested()))
@@ -760,19 +766,32 @@ fn normalize(lib: &Option<TomlLibTarget>,
               .set_for_host(toml.plugin.unwrap_or(t2.for_host()));
     }
 
-    fn lib_target(dst: &mut Vec<Target>, l: &TomlLibTarget,
-                  metadata: &Metadata) {
-        let path = l.path.clone().unwrap_or_else(|| {
+    fn lib_target(dst: &mut Vec<Target>,
+                  l: &TomlLibTarget,
+                  metadata: &Metadata,
+                  warnings: &mut Vec<String>) {
+        let path = l.path.clone().unwrap_or(
             PathValue::Path(Path::new("src").join(&format!("{}.rs", l.name())))
-        });
-        let crate_types = l.crate_type.clone().and_then(|kinds| {
-            kinds.iter().map(|s| LibKind::from_str(s))
-                 .collect::<CargoResult<_>>().ok()
-        }).unwrap_or_else(|| {
-            vec![if l.plugin == Some(true) {LibKind::Dylib} else {LibKind::Lib}]
-        });
-
-        let mut target = Target::lib_target(&l.name(), crate_types.clone(),
+        );
+        let crate_types = match l.crate_type.clone() {
+            Some(kinds) => {
+                // For now, merely warn about invalid crate types.
+                // In the future, it might be nice to make them errors.
+                kinds.iter().filter_map(|s| {
+                    let kind = LibKind::from_str(s);
+                    if let Err(ref error) = kind {
+                        warnings.push(format!("warning: {}", error))
+                    }
+                    kind.ok()
+                }).collect()
+            }
+            None => {
+                vec![ if l.plugin == Some(true) {LibKind::Dylib}
+                      else {LibKind::Lib} ]
+            }
+        };
+
+        let mut target = Target::lib_target(&l.name(), crate_types,
                                             &path.to_path(),
                                             metadata.clone());
         configure(l, &mut target);
@@ -855,7 +874,7 @@ fn normalize(lib: &Option<TomlLibTarget>,
     let mut ret = Vec::new();
 
     if let Some(ref lib) = *lib {
-        lib_target(&mut ret, lib, metadata);
+        lib_target(&mut ret, lib, metadata, warnings);
         bin_targets(&mut ret, bins,
                     &mut |bin| Path::new("src").join("bin")
                                    .join(&format!("{}.rs", bin.name())));
index cc4008e5ba850c65bc1431fb31ad159b11712a04..069783906a36e90349ef974d8faaba45fb59d322 100644 (file)
@@ -239,3 +239,22 @@ Caused by:
   [7] 'file:///' is not a valid local file URI
 "));
 });
+
+test!(bad_crate_type {
+    let foo = project("foo")
+    .file("Cargo.toml", r#"
+        [package]
+        name = "foo"
+        version = "0.0.0"
+        authors = []
+
+        [lib]
+        crate-type = ["bad_type", "rlib"]
+    "#)
+    .file("src/lib.rs", "");
+
+    assert_that(foo.cargo_process("build").arg("-v"),
+                execs().with_status(0).with_stderr("\
+warning: crate-type \"bad_type\" was not one of lib|rlib|dylib|staticlib
+"));
+});