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
This commit is contained in:
Evan Almloff authored and GitHub committed 2024-03-26 19:08:12 -07:00
1 parent b9d60276b7
commit 1b03bed20a
3 files changed
+43 -5

No files matched your search

Generated
+1
View File
@@ -2063,6 +2063,7 @@ dependencies = [
"tracing-subscriber", "tracing-subscriber",
"walkdir", "walkdir",
"wasm-bindgen-cli-support", "wasm-bindgen-cli-support",
"wasm-bindgen-shared",
"zip", "zip",
] ]
+1
View File
@@ -13,6 +13,7 @@ keywords = ["react", "gui", "cli", "dioxus", "wasm"]
clap = { version = "4.2", features = ["derive", "cargo"] } clap = { version = "4.2", features = ["derive", "cargo"] }
thiserror = { workspace = true } thiserror = { workspace = true }
wasm-bindgen-cli-support = "0.2" wasm-bindgen-cli-support = "0.2"
wasm-bindgen-shared = "0.2"
colored = "2.0.0" colored = "2.0.0"
dioxus-cli-config = { workspace = true, features = ["cli"] } dioxus-cli-config = { workspace = true, features = ["cli"] }
fern = { version = "0.6.0", features = ["colored"] } fern = { version = "0.6.0", features = ["colored"] }
+41 -5
View File
@@ -168,12 +168,12 @@ pub fn build_web(
.with_extension("wasm"); .with_extension("wasm");
tracing::info!("Running wasm-bindgen"); 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 // [3] Bindgen the final binary for use easy linking
let mut bindgen_builder = Bindgen::new(); let mut bindgen_builder = Bindgen::new();
bindgen_builder bindgen_builder
.input_path(input_path) .input_path(&input_path)
.web(true) .web(true)
.unwrap() .unwrap()
.debug(true) .debug(true)
@@ -184,10 +184,15 @@ pub fn build_web(
.out_name(&dioxus_config.application.name) .out_name(&dioxus_config.application.name)
.generate(&bindgen_outdir) .generate(&bindgen_outdir)
.unwrap(); .unwrap();
}); };
let bindgen_result = panic::catch_unwind(run_wasm_bindgen);
if bindgen_result.is_err() { // WASM bindgen requires the exact version of the bindgen schema to match the version the CLI was built with
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())); // 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 // 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 /// Note: `rust_flags` argument is only used for the fullstack platform
/// (server). /// (server).
pub fn build_desktop( pub fn build_desktop(