Skip to content

Commit

Permalink
Remove which dependency. (#962)
Browse files Browse the repository at this point in the history
  • Loading branch information
rukai authored Mar 8, 2024
1 parent 3961682 commit a8e8c9d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
1 change: 0 additions & 1 deletion prost-build/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
32 changes: 19 additions & 13 deletions prost-build/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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") {
Expand All @@ -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.
Expand Down

0 comments on commit a8e8c9d

Please sign in to comment.