From 1b03bed20a5d5c27e3346f0470853758b8a23696 Mon Sep 17 00:00:00 2001 From: Evan Almloff Date: Tue, 26 Mar 2024 21:08:12 -0500 Subject: [PATCH] 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 --- Cargo.lock | 1 + packages/cli/Cargo.toml | 1 + packages/cli/src/builder.rs | 46 +++++++++++++++++++++++++++++++++---- 3 files changed, 43 insertions(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index b8131548e..79886e669 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2063,6 +2063,7 @@ dependencies = [ "tracing-subscriber", "walkdir", "wasm-bindgen-cli-support", + "wasm-bindgen-shared", "zip", ] diff --git a/packages/cli/Cargo.toml b/packages/cli/Cargo.toml index 659ed34c6..7a0938dd1 100644 --- a/packages/cli/Cargo.toml +++ b/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"] } diff --git a/packages/cli/src/builder.rs b/packages/cli/src/builder.rs index 1ccfc0136..07012f021 100644 --- a/packages/cli/src/builder.rs +++ b/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(); - }); + }; + let bindgen_result = panic::catch_unwind(run_wasm_bindgen); - 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())); + // 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(