Skip to content

Commit

Permalink
fix: follow cargo best practice in build.rs script (#90)
Browse files Browse the repository at this point in the history
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.

Co-authored-by: Raphael Taylor-Davies <[email protected]>
  • Loading branch information
tomgr and tustvold authored Sep 17, 2023
1 parent 688c8b4 commit 2889fe4
Showing 1 changed file with 1 addition and 3 deletions.
4 changes: 1 addition & 3 deletions pbjson-types/build.rs
Original file line number Diff line number Diff line change
@@ -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<dyn std::error::Error>;
type Result<T, E = Error> = std::result::Result<T, E>;

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();
Expand Down

0 comments on commit 2889fe4

Please sign in to comment.