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 2024-03-26 21:08:12 -05:00 committed by GitHub
parent b9d60276b7
commit 1b03bed20a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 43 additions and 5 deletions

1
Cargo.lock generated
View file

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

View file

@ -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"] }

View file

@ -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(