Repository: ashleygwilliams/wasm-pack
Pull request #150 (2018-06-05 14:08:33)

check that crate type includes cdylib
Checks that crate-type includes "cdylib" -- fails with an error if it does not.

Closes https://github.com/ashleygwilliams/wasm-pack/issues/140

thank you for holding the rust workshop at jsconf this past weekend @ashleygwilliams! 😄  



Patch:
diff --git a/Cargo.toml b/Cargo.toml
index 8a73e82..d3544d5 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -6,7 +6,6 @@ authors = ["Ashley Williams <ashley666ashley@gmail.com>"]
 repository = "https://github.com/ashleygwilliams/wasm-pack.git"
 license = "MIT/Apache-2.0"
 readme = "README.md"
-categories = ["wasm"]
 
 [dependencies]
 console = "0.6.1"
diff --git a/src/command.rs b/src/command.rs
index 4f6023d..ae6f743 100644
--- a/src/command.rs
+++ b/src/command.rs
@@ -211,6 +211,19 @@ fn init(
     #[cfg(target_os = "windows")]
     info!(&log, "Copied readme from crate to {}\\pkg.", &crate_path);
 
+    info!(&log, "Checking the crate type from the manifest...");
+    manifest::check_crate_type(&crate_path)?;
+    #[cfg(not(target_os = "windows"))]
+    info!(
+        &log,
+        "Checked crate type from the manifest at {}/Cargo.toml.", &crate_path
+    );
+    #[cfg(target_os = "windows")]
+    info!(
+        &log,
+        "Checked crate type from the manifest at {}\\Cargo.toml.", &crate_path
+    );
+
     info!(&log, "Installing wasm-bindgen-cli...");
     bindgen::cargo_install_wasm_bindgen()?;
     info!(&log, "Installing wasm-bindgen-cli was successful.");
diff --git a/src/error.rs b/src/error.rs
index 7d84d7c..0bd2ba6 100644
--- a/src/error.rs
+++ b/src/error.rs
@@ -15,6 +15,8 @@ pub enum Error {
     SerdeToml(#[cause] toml::de::Error),
     #[fail(display = "{}. stderr:\n\n{}", message, stderr)]
     Cli { message: String, stderr: String },
+    #[fail(display = "{}", message)]
+    CrateConfig { message: String },
 }
 
 impl Error {
@@ -25,6 +27,12 @@ impl Error {
         })
     }
 
+    pub fn crate_config(message: &str) -> Result<(), Self> {
+        Err(Error::CrateConfig {
+            message: message.to_string(),
+        })
+    }
+
     pub fn error_type(&self) -> String {
         match self {
             Error::Io(_) => "There was an I/O error. Details:\n\n",
@@ -34,6 +42,9 @@ impl Error {
                 message: _,
                 stderr: _,
             } => "There was an error while calling another CLI tool. Details:\n\n",
+            Error::CrateConfig { message: _ } => {
+                "There was a crate configuration error. Details:\n\n"
+            }
         }.to_string()
     }
 }
diff --git a/src/manifest.rs b/src/manifest.rs
index 8662eea..297e995 100644
--- a/src/manifest.rs
+++ b/src/manifest.rs
@@ -11,6 +11,7 @@ use PBAR;
 #[derive(Deserialize)]
 struct CargoManifest {
     package: CargoPackage,
+    lib: Option<CargoLib>,
 }
 
 #[derive(Deserialize)]
@@ -23,6 +24,12 @@ struct CargoPackage {
     repository: Option<String>,
 }
 
+#[derive(Deserialize)]
+struct CargoLib {
+    #[serde(rename = "crate-type")]
+    crate_type: Option<Vec<String>>,
+}
+
 #[derive(Serialize)]
 struct NpmPackage {
     name: String,
@@ -136,3 +143,20 @@ pub fn write_package_json(
 pub fn get_crate_name(path: &str) -> Result<String, Error> {
     Ok(read_cargo_toml(path)?.package.name)
 }
+
+fn has_cdylib(path: &str) -> Result<bool, Error> {
+    Ok(read_cargo_toml(path)?.lib.map_or(false, |lib| {
+        lib.crate_type
+            .map_or(false, |types| types.iter().any(|s| s == "cdylib"))
+    }))
+}
+
+pub fn check_crate_type(path: &str) -> Result<(), Error> {
+    if !has_cdylib(path)? {
+        Error::crate_config(
+            "crate-type must include cdylib to compile to wasm32-unknown-unknown. Add the following to your Cargo.toml file:\n\n[lib]\ncrate-type = [\"cdylib\"]"
+        )
+    } else {
+        Ok(())
+    }
+}


Test patch:
diff --git a/tests/fixtures/bad-cargo-toml/Cargo.toml b/tests/fixtures/bad-cargo-toml/Cargo.toml
index 95110a2..c73a430 100644
--- a/tests/fixtures/bad-cargo-toml/Cargo.toml
+++ b/tests/fixtures/bad-cargo-toml/Cargo.toml
@@ -3,5 +3,8 @@ name = "bad-cargo-toml"
 version = "0.1.0"
 authors = ["Michael Gattozzi <mgattozzi@gmail.com>"]
 
+[lib]
+crate-type = ["foo"]
+
 [dependencies]
 wasm-bindgen = "0.2"
diff --git a/tests/manifest/main.rs b/tests/manifest/main.rs
index 995273a..dc2183c 100644
--- a/tests/manifest/main.rs
+++ b/tests/manifest/main.rs
@@ -25,6 +25,21 @@ fn it_gets_the_crate_name_provided_path() {
     );
 }
 
+#[test]
+fn it_checks_has_cdylib_default_path() {
+    assert!(manifest::check_crate_type(".").is_err());
+}
+
+#[test]
+fn it_checks_has_cdylib_provided_path() {
+    assert!(manifest::check_crate_type("tests/fixtures/js-hello-world").is_ok());
+}
+
+#[test]
+fn it_checks_has_cdylib_wrong_crate_type() {
+    assert!(manifest::check_crate_type("tests/fixtures/bad-cargo-toml").is_err());
+}
+
 #[test]
 fn it_creates_a_package_json_default_path() {
     let path = ".".to_string();