瀏覽代碼

Provide a better error message for wasm bindgen version mismatches (#2136)

* provide a better error message for wasm bindgen version mismatches

* fix clippy

* only rerun wasm bindgen if the first run fails

* remove old logs
Evan Almloff 1 年之前
父節點
當前提交
1b03bed20a
共有 3 個文件被更改,包括 44 次插入6 次删除
  1. 1 0
      Cargo.lock
  2. 1 0
      packages/cli/Cargo.toml
  3. 42 6
      packages/cli/src/builder.rs

+ 1 - 0
Cargo.lock

@@ -2063,6 +2063,7 @@ dependencies = [
  "tracing-subscriber",
  "walkdir",
  "wasm-bindgen-cli-support",
+ "wasm-bindgen-shared",
  "zip",
 ]
 

+ 1 - 0
packages/cli/Cargo.toml

@@ -13,6 +13,7 @@ keywords = ["react", "gui", "cli", "dioxus", "wasm"]
 clap = { version = "4.2", features = ["derive", "cargo"] }
 thiserror = { workspace = true }
 wasm-bindgen-cli-support = "0.2"
+wasm-bindgen-shared = "0.2"
 colored = "2.0.0"
 dioxus-cli-config = { workspace = true, features = ["cli"] }
 fern = { version = "0.6.0", features = ["colored"] }

+ 42 - 6
packages/cli/src/builder.rs

@@ -168,12 +168,12 @@ pub fn build_web(
         .with_extension("wasm");
 
     tracing::info!("Running wasm-bindgen");
-    let bindgen_result = panic::catch_unwind(move || {
+    let run_wasm_bindgen = || {
         // [3] Bindgen the final binary for use easy linking
         let mut bindgen_builder = Bindgen::new();
 
         bindgen_builder
-            .input_path(input_path)
+            .input_path(&input_path)
             .web(true)
             .unwrap()
             .debug(true)
@@ -184,10 +184,15 @@ pub fn build_web(
             .out_name(&dioxus_config.application.name)
             .generate(&bindgen_outdir)
             .unwrap();
-    });
-
-    if bindgen_result.is_err() {
-        return Err(Error::BuildFailed("Bindgen build failed! \nThis is probably due to the Bindgen version, dioxus-cli using `0.2.81` Bindgen crate.".to_string()));
+    };
+    let bindgen_result = panic::catch_unwind(run_wasm_bindgen);
+
+    // WASM bindgen requires the exact version of the bindgen schema to match the version the CLI was built with
+    // If we get an error, we can try to recover by pinning the user's wasm-bindgen version to the version we used
+    if let Err(err) = bindgen_result {
+        tracing::error!("Bindgen build failed: {:?}", err);
+        update_wasm_bindgen_version()?;
+        run_wasm_bindgen();
     }
 
     // check binaryen:wasm-opt tool
@@ -319,6 +324,37 @@ pub fn build_web(
     })
 }
 
+// Attempt to automatically recover from a bindgen failure by updating the wasm-bindgen version
+fn update_wasm_bindgen_version() -> Result<()> {
+    let cli_bindgen_version = wasm_bindgen_shared::version();
+    tracing::info!("Attempting to recover from bindgen failure by setting the wasm-bindgen version to {cli_bindgen_version}...");
+
+    let output = Command::new("cargo")
+        .args([
+            "update",
+            "-p",
+            "wasm-bindgen",
+            "--precise",
+            &cli_bindgen_version,
+        ])
+        .output();
+    let mut error_message = None;
+    if let Ok(output) = output {
+        if output.status.success() {
+            tracing::info!("Successfully updated wasm-bindgen to {cli_bindgen_version}");
+            return Ok(());
+        } else {
+            error_message = Some(output);
+        }
+    }
+
+    if let Some(output) = error_message {
+        tracing::error!("Failed to update wasm-bindgen: {:#?}", output);
+    }
+
+    Err(Error::BuildFailed(format!("WASM bindgen build failed!\nThis is probably due to the Bindgen version, dioxus-cli is using `{cli_bindgen_version}` which is not compatible with your crate.\nPlease reinstall the dioxus cli to fix this issue.\nYou can reinstall the dioxus cli by running `cargo install dioxus-cli --force` and then rebuild your project")))
+}
+
 /// Note: `rust_flags` argument is only used for the fullstack platform
 /// (server).
 pub fn build_desktop(