Skip to content

Commit 7c16959

Browse files
authored
feat(prost-build): emit rerun commands (#1140)
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 d505b18 commit 7c16959

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
@@ -911,6 +911,7 @@ impl Config {
911911
) -> Result<FileDescriptorSet> {
912912
let tmp;
913913
let file_descriptor_set_path = if let Some(path) = &self.file_descriptor_set_path {
914+
println!("cargo:rerun-if-changed={}", path.display());
914915
path.clone()
915916
} else {
916917
if self.skip_protoc_run {
@@ -932,6 +933,7 @@ impl Config {
932933
cmd.arg("-o").arg(&file_descriptor_set_path);
933934

934935
for include in includes {
936+
println!("cargo:rerun-if-changed={}", include.as_ref().display());
935937
if include.as_ref().exists() {
936938
cmd.arg("-I").arg(include.as_ref());
937939
} else {
@@ -953,6 +955,7 @@ impl Config {
953955
}
954956

955957
for proto in protos {
958+
println!("cargo:rerun-if-changed={}", proto.as_ref().display());
956959
cmd.arg(proto.as_ref());
957960
}
958961

@@ -1023,12 +1026,6 @@ impl Config {
10231026
protos: &[impl AsRef<Path>],
10241027
includes: &[impl AsRef<Path>],
10251028
) -> Result<()> {
1026-
// TODO: This should probably emit 'rerun-if-changed=PATH' directives for cargo, however
1027-
// according to [1] if any are output then those paths replace the default crate root,
1028-
// which is undesirable. Figure out how to do it in an additive way; perhaps gcc-rs has
1029-
// this figured out.
1030-
// [1]: http://doc.crates.io/build-script.html#outputs-of-the-build-script
1031-
10321029
let file_descriptor_set = self.load_fds(protos, includes)?;
10331030

10341031
self.compile_fds(file_descriptor_set)
@@ -1249,13 +1246,15 @@ pub fn error_message_protoc_not_found() -> String {
12491246

12501247
/// Returns the path to the `protoc` binary.
12511248
pub fn protoc_from_env() -> PathBuf {
1249+
println!("cargo:rerun-if-env-changed=PROTOC");
12521250
env::var_os("PROTOC")
12531251
.map(PathBuf::from)
12541252
.unwrap_or(PathBuf::from("protoc"))
12551253
}
12561254

12571255
/// Returns the path to the Protobuf include directory.
12581256
pub fn protoc_include_from_env() -> Option<PathBuf> {
1257+
println!("cargo:rerun-if-env-changed=PROTOC_INCLUDE");
12591258
let protoc_include: PathBuf = env::var_os("PROTOC_INCLUDE")?.into();
12601259

12611260
if !protoc_include.exists() {

0 commit comments

Comments
 (0)