Skip to content

Commit 91b8682

Browse files
committed
better error handling in capnpc
Runs `capnp --version` before doing anything else, to verify that that the `capnp` executable exists.
1 parent fd6fa0a commit 91b8682

File tree

1 file changed

+32
-8
lines changed

1 file changed

+32
-8
lines changed

capnpc/src/lib.rs

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,15 @@ impl CompilerCommand {
186186
self
187187
}
188188

189+
/// Internal function for starting to build a capnp command.
190+
fn new_command(&self) -> ::std::process::Command {
191+
if let Some(executable) = &self.executable_path {
192+
::std::process::Command::new(executable)
193+
} else {
194+
::std::process::Command::new("capnp")
195+
}
196+
}
197+
189198
/// Sets the default parent module. This indicates the scope in your crate where you will
190199
/// add a module containing the generated code. For example, if you set this option to
191200
/// `vec!["foo".into(), "bar".into()]`, and you are generating code for `baz.capnp`, then your crate
@@ -222,11 +231,28 @@ impl CompilerCommand {
222231
/// Runs the command.
223232
/// Returns an error if `OUT_DIR` or a custom output directory was not set, or if `capnp compile` fails.
224233
pub fn run(&mut self) -> ::capnp::Result<()> {
225-
let mut command = if let Some(executable) = &self.executable_path {
226-
::std::process::Command::new(executable)
227-
} else {
228-
::std::process::Command::new("capnp")
229-
};
234+
match self.new_command().arg("--version").output() {
235+
Err(error) => {
236+
return Err(::capnp::Error::failed(format!(
237+
"Failed to execute `capnp --version`: {error}. \
238+
Please verify that version 0.5.2 or higher of the capnp executable \
239+
is installed on your system. See https://capnproto.org/install.html"
240+
)))
241+
}
242+
Ok(output) => {
243+
if !output.status.success() {
244+
return Err(::capnp::Error::failed(format!(
245+
"`capnp --version` returned an error: {:?}. \
246+
Please verify that version 0.5.2 or higher of the capnp executable \
247+
is installed on your system. See https://capnproto.org/install.html",
248+
output.status
249+
)));
250+
}
251+
// TODO Parse the version string?
252+
}
253+
}
254+
255+
let mut command = self.new_command();
230256

231257
command.arg("compile").arg("-o").arg("-");
232258

@@ -288,9 +314,7 @@ impl CompilerCommand {
288314

289315
run_command(command, code_generation_command).map_err(|error| {
290316
::capnp::Error::failed(format!(
291-
"Error while trying to execute `capnp compile`: {error}. \
292-
Please verify that version 0.5.2 or higher of the capnp executable \
293-
is installed on your system. See https://capnproto.org/install.html"
317+
"Error while trying to execute `capnp compile`: {error}."
294318
))
295319
})
296320
}

0 commit comments

Comments
 (0)