From 0dd256471f64c3a5cc21fabdb82985da15f1eb89 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Dr=C3=B6ge?= Date: Wed, 18 Dec 2024 10:21:34 +0200 Subject: [PATCH] gtk4-macros: Wait for blueprint-compiler process in all branches Otherwise clippy complains and zombie processes might stay around. Also make sure to never panic on normal errors for the same reason, and provide more useful error messages. --- gtk4-macros/src/blueprint.rs | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/gtk4-macros/src/blueprint.rs b/gtk4-macros/src/blueprint.rs index 7253878873d3..d704c66f6bc1 100644 --- a/gtk4-macros/src/blueprint.rs +++ b/gtk4-macros/src/blueprint.rs @@ -1,29 +1,33 @@ // Take a look at the license at the top of the repository in the LICENSE file. use std::{ - io::{Error, ErrorKind, Result, Write}, + io::Write, process::{Command, Stdio}, }; -pub(crate) fn compile_blueprint(blueprint: &[u8]) -> Result { +pub(crate) fn compile_blueprint(blueprint: &[u8]) -> Result { let mut compiler = Command::new("blueprint-compiler") .args(["compile", "-"]) .stdin(Stdio::piped()) .stdout(Stdio::piped()) .spawn() - .unwrap_or_else(|_| panic!("blueprint-compiler not found")); + .map_err(|e| format!("blueprint-compiler couldn't be spawned: {e}"))?; let mut stdin = compiler.stdin.take().unwrap(); - stdin.write_all(blueprint)?; + if let Err(e) = stdin.write_all(blueprint) { + let _ = compiler.wait(); + return Err(format!( + "Couldn't send blueprint to blueprint-compiler: {e}" + )); + } drop(stdin); let output = compiler .wait_with_output() - .unwrap_or_else(|e| panic!("blueprint-compiler process failed {e}")); + .map_err(|e| format!("blueprint-compiler process failed: {e}"))?; let buf = String::from_utf8(output.stdout).unwrap(); - if !buf.starts_with('<') { - return Err(Error::new(ErrorKind::Other, buf)); + return Err(format!("blueprint-compiler failed: {buf}")); } Ok(buf)