From a8e8c9db3a1ff5ce51a02f93d6f285901dff7391 Mon Sep 17 00:00:00 2001 From: Lucas Kent Date: Fri, 8 Mar 2024 20:07:33 +1100 Subject: [PATCH] Remove which dependency. (#962) --- prost-build/Cargo.toml | 1 - prost-build/src/lib.rs | 32 +++++++++++++++++++------------- 2 files changed, 19 insertions(+), 14 deletions(-) diff --git a/prost-build/Cargo.toml b/prost-build/Cargo.toml index 221cddd0c..eb0faa369 100644 --- a/prost-build/Cargo.toml +++ b/prost-build/Cargo.toml @@ -31,7 +31,6 @@ prost-types = { version = "0.12.3", path = "../prost-types", default-features = tempfile = "3" once_cell = "1.17.1" regex = { version = "1.8.1", default-features = false, features = ["std", "unicode-bool"] } -which = "4" prettyplease = { version = "0.2", optional = true } syn = { version = "2", features = ["full"], optional = true } diff --git a/prost-build/src/lib.rs b/prost-build/src/lib.rs index 07a7083a8..992efd4c8 100644 --- a/prost-build/src/lib.rs +++ b/prost-build/src/lib.rs @@ -1125,12 +1125,17 @@ impl Config { debug!("Running: {:?}", cmd); - let output = cmd.output().map_err(|error| { - Error::new( - error.kind(), - format!("failed to invoke protoc (hint: https://docs.rs/prost-build/#sourcing-protoc): (path: {:?}): {}", &protoc, error), - ) - })?; + let output = match cmd.output() { + Err(err) if ErrorKind::NotFound == err.kind() => return Err(Error::new( + err.kind(), + error_message_protoc_not_found() + )), + Err(err) => return Err(Error::new( + err.kind(), + format!("failed to invoke protoc (hint: https://docs.rs/prost-build/#sourcing-protoc): (path: {:?}): {}", &protoc, err), + )), + Ok(output) => output, + }; if !output.status.success() { return Err(Error::new( @@ -1505,6 +1510,12 @@ pub fn compile_fds(fds: FileDescriptorSet) -> Result<()> { /// Returns the path to the `protoc` binary. pub fn protoc_from_env() -> PathBuf { + env::var_os("PROTOC") + .map(PathBuf::from) + .unwrap_or(PathBuf::from("protoc")) +} + +pub fn error_message_protoc_not_found() -> String { let os_specific_hint = if cfg!(target_os = "macos") { "You could try running `brew install protobuf` or downloading it from https://github.com/protocolbuffers/protobuf/releases" } else if cfg!(target_os = "linux") { @@ -1517,18 +1528,13 @@ pub fn protoc_from_env() -> PathBuf { this knowledge. If `protoc` is installed and this crate had trouble finding it, you can set the `PROTOC` environment variable with the specific path to your installed `protoc` binary."; - let msg = format!( + format!( "{}{} For more information: https://docs.rs/prost-build/#sourcing-protoc ", error_msg, os_specific_hint - ); - - env::var_os("PROTOC") - .map(PathBuf::from) - .or_else(|| which::which("protoc").ok()) - .expect(&msg) + ) } /// Returns the path to the Protobuf include directory.