Skip to content

Commit a3aed62

Browse files
chore(openshell-core): discover proto files in build script (#1122)
Walk ../../proto recursively instead of maintaining a hard-coded list. Sort paths for deterministic codegen and keep cargo:rerun-if-changed scoped to the proto tree. Signed-off-by: ddurst <267424412+ddurst-nvidia@users.noreply.github.com>
1 parent 55f0e37 commit a3aed62

1 file changed

Lines changed: 23 additions & 13 deletions

File tree

crates/openshell-core/build.rs

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
// SPDX-License-Identifier: Apache-2.0
33

44
use std::env;
5+
use std::path::{Path, PathBuf};
6+
7+
const PROTO_REL: &str = "../../proto";
58

69
fn main() -> Result<(), Box<dyn std::error::Error>> {
710
// --- Git-derived version ---
@@ -17,6 +20,8 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
1720
}
1821

1922
// --- Protobuf compilation ---
23+
// Re-run when anything under proto/ changes (including newly added .proto files).
24+
println!("cargo:rerun-if-changed={PROTO_REL}");
2025
// Use bundled protoc from protobuf-src. The system protoc (from apt-get)
2126
// does not bundle the well-known type includes (google/protobuf/struct.proto
2227
// etc.), so we must use protobuf-src which ships both the binary and the
@@ -28,26 +33,31 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
2833
env::set_var("PROTOC", protobuf_src::protoc());
2934
}
3035

31-
let proto_files = [
32-
"../../proto/openshell.proto",
33-
"../../proto/datamodel.proto",
34-
"../../proto/sandbox.proto",
35-
"../../proto/compute_driver.proto",
36-
"../../proto/inference.proto",
37-
"../../proto/test.proto",
38-
];
36+
let manifest_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR")?);
37+
let proto_root = manifest_dir.join(PROTO_REL);
38+
39+
let mut proto_files = Vec::new();
40+
collect_proto_files(&proto_root, &mut proto_files)?;
41+
proto_files.sort();
3942

4043
// Configure tonic-build
4144
tonic_build::configure()
4245
.build_server(true)
4346
.build_client(true)
44-
.compile_protos(&proto_files, &["../../proto"])?;
47+
.compile_protos(&proto_files, &[proto_root.as_path()])?;
4548

46-
// Tell cargo to rerun if the proto file changes
47-
for proto_file in proto_files {
48-
println!("cargo:rerun-if-changed={proto_file}");
49-
}
49+
Ok(())
50+
}
5051

52+
fn collect_proto_files(dir: &Path, out: &mut Vec<PathBuf>) -> std::io::Result<()> {
53+
for entry in std::fs::read_dir(dir)? {
54+
let path = entry?.path();
55+
if path.is_dir() {
56+
collect_proto_files(&path, out)?;
57+
} else if path.extension().is_some_and(|ext| ext == "proto") {
58+
out.push(path);
59+
}
60+
}
5161
Ok(())
5262
}
5363

0 commit comments

Comments
 (0)