From a4f760e5d53086d989f28dc9d1d0adcc22fe37c4 Mon Sep 17 00:00:00 2001 From: Thomas Gibson-Robinson Date: Thu, 12 Jan 2023 12:01:20 +0000 Subject: [PATCH] fix: follow cargo best practice in build.rs script Using env! inside the build.rs script makes it difficult to build this package with other build tools like bazel which sandbox execution, since the source directory might be at different paths between when the build.rs file is compiled, and when the resulting build script is executed. Cargo recommends that CARGO_MANIFEST_DIR is read at runtime instead: https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-sets-for-build-scripts The above links also includes the promise that Cargo will set the cwd to the crate's directory. This patch uses this to find the descriptors.bin file. --- pbjson-types/build.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/pbjson-types/build.rs b/pbjson-types/build.rs index 99bebcc..84b1627 100644 --- a/pbjson-types/build.rs +++ b/pbjson-types/build.rs @@ -1,15 +1,13 @@ //! Compiles Protocol Buffers and FlatBuffers schema definitions into //! native Rust types. -use std::env; use std::path::PathBuf; type Error = Box; type Result = std::result::Result; fn main() -> Result<()> { - let root = PathBuf::from(env!("CARGO_MANIFEST_DIR")); - let descriptor_path = root.join("descriptors.bin"); + let descriptor_path: PathBuf = "descriptors.bin".into(); println!("cargo:rerun-if-changed={}", descriptor_path.display()); let mut config = prost_build::Config::new();