From 02a1c891a626bc22656d064f2a31e5e141b7c3bc Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Thu, 15 Jun 2023 22:32:16 +1000 Subject: [PATCH] Don't require DESCRIPTORS_BIN in an external workspace Consumers may not require it --- rslib/proto/build.rs | 5 ++--- rslib/proto/rust.rs | 16 +++++++++------- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/rslib/proto/build.rs b/rslib/proto/build.rs index f4141084a..5d88a1b15 100644 --- a/rslib/proto/build.rs +++ b/rslib/proto/build.rs @@ -9,13 +9,12 @@ pub mod utils; use std::env; use std::path::PathBuf; -use anyhow::Context; use anyhow::Result; fn main() -> Result<()> { - let descriptors_path = PathBuf::from(env::var("DESCRIPTORS_BIN").context("DESCRIPTORS_BIN")?); + let descriptors_path = env::var("DESCRIPTORS_BIN").ok().map(PathBuf::from); - let pool = rust::write_backend_proto_rs(&descriptors_path)?; + let pool = rust::write_backend_proto_rs(descriptors_path)?; python::write_python_interface(&pool)?; ts::write_ts_interface(&pool)?; diff --git a/rslib/proto/rust.rs b/rslib/proto/rust.rs index e4011c63b..03d8a0f15 100644 --- a/rslib/proto/rust.rs +++ b/rslib/proto/rust.rs @@ -14,16 +14,11 @@ use anyhow::Result; use prost_build::ServiceGenerator; use prost_reflect::DescriptorPool; -pub fn write_backend_proto_rs(descriptors_path: &Path) -> Result { +pub fn write_backend_proto_rs(descriptors_path: Option) -> Result { set_protoc_path(); let proto_dir = PathBuf::from("../../proto"); let paths = gather_proto_paths(&proto_dir)?; let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap()); - create_dir_all( - descriptors_path - .parent() - .context("missing parent of descriptor")?, - )?; let tmp_descriptors = out_dir.join("descriptors.tmp"); prost_build::Config::new() .out_dir(&out_dir) @@ -55,7 +50,14 @@ pub fn write_backend_proto_rs(descriptors_path: &Path) -> Result .context("prost build")?; let descriptors = read_file(&tmp_descriptors)?; - write_file_if_changed(descriptors_path, &descriptors)?; + if let Some(descriptors_path) = descriptors_path { + create_dir_all( + descriptors_path + .parent() + .context("missing parent of descriptor")?, + )?; + write_file_if_changed(descriptors_path, &descriptors)?; + } write_service_index(&out_dir, descriptors) }