Skip to content

Commit cdccb41

Browse files
committed
feat(prost-build): emit rerun commands
Inform `cargo` about the files and env vars used by `prost-build`. Then `cargo` can better determine when to rebuild a project. - Emit `rerun-if-changed` for each proto file specified - Emit `rerun-if-changed` for each include directory specified - Emit `rerun-if-changed` if `file_descriptor_set_path` is set - Emit `rerun-if-env-changed` for `PROTOC` and `PROTOC_INCLUDE` https://doc.rust-lang.org/cargo/reference/build-scripts.html#rerun-if-changed BREAKING CHANGE: Previously `cargo` assumed it had to rerun `build.rs` if any files in the project changed. `prost-build` will now emit `rerun` commands, which means only the explicitly marked files cause a rerun. If your `build.rs` is dependent on any other file paths than those given to `prost-build`, then your `build.rs` needs to emit `rerun` commands as well.
1 parent 86f87a2 commit cdccb41

File tree

1 file changed

+5
-6
lines changed

1 file changed

+5
-6
lines changed

prost-build/src/config.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -889,6 +889,7 @@ impl Config {
889889
) -> Result<FileDescriptorSet> {
890890
let tmp;
891891
let file_descriptor_set_path = if let Some(path) = &self.file_descriptor_set_path {
892+
println!("cargo:rerun-if-changed={}", path.display());
892893
path.clone()
893894
} else {
894895
if self.skip_protoc_run {
@@ -909,6 +910,7 @@ impl Config {
909910
.arg(&file_descriptor_set_path);
910911

911912
for include in includes {
913+
println!("cargo:rerun-if-changed={}", include.as_ref().display());
912914
if include.as_ref().exists() {
913915
cmd.arg("-I").arg(include.as_ref());
914916
} else {
@@ -930,6 +932,7 @@ impl Config {
930932
}
931933

932934
for proto in protos {
935+
println!("cargo:rerun-if-changed={}", proto.as_ref().display());
933936
cmd.arg(proto.as_ref());
934937
}
935938

@@ -1000,12 +1003,6 @@ impl Config {
10001003
protos: &[impl AsRef<Path>],
10011004
includes: &[impl AsRef<Path>],
10021005
) -> Result<()> {
1003-
// TODO: This should probably emit 'rerun-if-changed=PATH' directives for cargo, however
1004-
// according to [1] if any are output then those paths replace the default crate root,
1005-
// which is undesirable. Figure out how to do it in an additive way; perhaps gcc-rs has
1006-
// this figured out.
1007-
// [1]: http://doc.crates.io/build-script.html#outputs-of-the-build-script
1008-
10091006
let file_descriptor_set = self.load_fds(protos, includes)?;
10101007

10111008
self.compile_fds(file_descriptor_set)
@@ -1224,13 +1221,15 @@ pub fn error_message_protoc_not_found() -> String {
12241221

12251222
/// Returns the path to the `protoc` binary.
12261223
pub fn protoc_from_env() -> PathBuf {
1224+
println!("cargo:rerun-if-env-changed=PROTOC");
12271225
env::var_os("PROTOC")
12281226
.map(PathBuf::from)
12291227
.unwrap_or(PathBuf::from("protoc"))
12301228
}
12311229

12321230
/// Returns the path to the Protobuf include directory.
12331231
pub fn protoc_include_from_env() -> Option<PathBuf> {
1232+
println!("cargo:rerun-if-env-changed=PROTOC_INCLUDE");
12341233
let protoc_include: PathBuf = env::var_os("PROTOC_INCLUDE")?.into();
12351234

12361235
if !protoc_include.exists() {

0 commit comments

Comments
 (0)