Add better error handling for wasm-opt and prevent file truncation on error (#4313)

* Add better error handling for wasm-opt and prevent file truncation on error

* clippy

---------

Co-authored-by: Jonathan Kelley <jkelleyrtp@gmail.com>
This commit is contained in:
Moritz HedtkeandJonathan Kelley authored and GitHub committed 2025-06-23 17:20:25 -07:00
1 parent d262aba8bb
commit f40f7a5aeb
1 file changed
+24 -2
+24 -2
View File
@@ -1,6 +1,7 @@
use anyhow::{anyhow, Context}; use anyhow::{anyhow, Context};
use flate2::read::GzDecoder; use flate2::read::GzDecoder;
use tar::Archive; use tar::Archive;
use tempfile::NamedTempFile;
use tokio::fs; use tokio::fs;
use crate::config::WasmOptLevel; use crate::config::WasmOptLevel;
@@ -24,6 +25,7 @@ pub async fn optimize(input_path: &Path, output_path: &Path, cfg: &WasmOptConfig
struct WasmOpt { struct WasmOpt {
path: PathBuf, path: PathBuf,
input_path: PathBuf, input_path: PathBuf,
temporary_output_path: NamedTempFile,
output_path: PathBuf, output_path: PathBuf,
cfg: WasmOptConfig, cfg: WasmOptConfig,
} }
@@ -38,6 +40,7 @@ impl WasmOpt {
Ok(Self { Ok(Self {
path, path,
input_path: input_path.to_path_buf(), input_path: input_path.to_path_buf(),
temporary_output_path: tempfile::NamedTempFile::new()?,
output_path: output_path.to_path_buf(), output_path: output_path.to_path_buf(),
cfg: cfg.clone(), cfg: cfg.clone(),
}) })
@@ -80,12 +83,20 @@ impl WasmOpt {
WasmOptLevel::Four => "-O4", WasmOptLevel::Four => "-O4",
}; };
tracing::debug!(
"Running wasm-opt: {} {} {} -o {} {}",
self.path.to_string_lossy(),
self.input_path.to_string_lossy(),
level,
self.temporary_output_path.path().to_string_lossy(),
args.join(" ")
);
let mut command = tokio::process::Command::new(&self.path); let mut command = tokio::process::Command::new(&self.path);
command command
.arg(&self.input_path) .arg(&self.input_path)
.arg(level) .arg(level)
.arg("-o") .arg("-o")
.arg(&self.output_path) .arg(self.temporary_output_path.path())
.args(args); .args(args);
command command
} }
@@ -96,7 +107,18 @@ impl WasmOpt {
if !res.status.success() { if !res.status.success() {
let err = String::from_utf8_lossy(&res.stderr); let err = String::from_utf8_lossy(&res.stderr);
tracing::error!("wasm-opt failed with status code {}: {}", res.status, err); tracing::error!(
"wasm-opt failed with status code {}\nstderr: {}\nstdout: {}",
res.status,
err,
String::from_utf8_lossy(&res.stdout)
);
// A failing wasm-opt execution may leave behind an empty file so copy the original file instead.
if self.input_path != self.output_path {
std::fs::copy(&self.input_path, &self.output_path).unwrap();
}
} else {
std::fs::copy(self.temporary_output_path.path(), &self.output_path).unwrap();
} }
Ok(()) Ok(())