From ec2efa0e0fd7bf0141035340972b40684603b3aa Mon Sep 17 00:00:00 2001 From: Mick van Gelderen Date: Fri, 17 Jun 2022 23:00:06 +0200 Subject: [PATCH 1/3] tonic-build: Add option to emit cargo:rerun-if-changed instructions Currently, tonic-build does not emit cargo:rerun-if-changed instructions. This will cause the build script to be rerun if any file changes in the package. This commit implements an option on the Builder called emit_rerun_if_changed that, when enabled, will emit the cargo:rerun-if-changed instructions for all of the proto file paths and include directories. Fixes: #1020 Refs: #1019 --- tonic-build/src/prost.rs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/tonic-build/src/prost.rs b/tonic-build/src/prost.rs index 779414dd9..8a4248edc 100644 --- a/tonic-build/src/prost.rs +++ b/tonic-build/src/prost.rs @@ -27,6 +27,7 @@ pub fn configure() -> Builder { emit_package: true, protoc_args: Vec::new(), include_file: None, + emit_rerun_if_changed: std::env::var_os("CARGO").is_some(), } } @@ -224,6 +225,7 @@ pub struct Builder { pub(crate) compile_well_known_types: bool, pub(crate) protoc_args: Vec, pub(crate) include_file: Option, + pub(crate) emit_rerun_if_changed: bool, out_dir: Option, } @@ -368,6 +370,14 @@ impl Builder { self } + /// Enable or disable emitting the rerun-if-changed cargo instruction + /// + /// If set, emits rerun-if-changed instructions so that Cargo understands when to rerun the build script. + pub fn emit_rerun_if_changed(mut self, enable: bool) -> Self { + self.emit_rerun_if_changed = enable; + self + } + /// Compile the .proto files and execute code generation. pub fn compile( self, @@ -415,6 +425,19 @@ impl Builder { config.protoc_arg(arg); } + if self.emit_rerun_if_changed { + for path in protos.iter() { + println!("cargo:rerun-if-changed={}", path.as_ref().display()) + } + + for path in includes.iter() { + // Cargo will watch the **entire** directory recursively. If we + // could figure out which files are imported by our protos we + // could specify only those files instead. + println!("cargo:rerun-if-changed={}", path.as_ref().display()) + } + } + config.service_generator(self.service_generator()); config.compile_protos(protos, includes)?; From f774465068b59e2e38d0b3c8730e9dc1f3012926 Mon Sep 17 00:00:00 2001 From: Mick van Gelderen Date: Mon, 20 Jun 2022 19:15:27 +0200 Subject: [PATCH 2/3] tonic-build: Document default value for emit_rerun_if_changed --- tonic-build/src/prost.rs | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/tonic-build/src/prost.rs b/tonic-build/src/prost.rs index 8a4248edc..9a25d0698 100644 --- a/tonic-build/src/prost.rs +++ b/tonic-build/src/prost.rs @@ -370,9 +370,19 @@ impl Builder { self } - /// Enable or disable emitting the rerun-if-changed cargo instruction + /// Enable or disable emitting + /// [`cargo:rerun-if-changed=PATH`](https://doc.rust-lang.org/cargo/reference/build-scripts.html#rerun-if-changed) + /// instructions for Cargo. /// - /// If set, emits rerun-if-changed instructions so that Cargo understands when to rerun the build script. + /// If set, writes instructions to `stdout` for Cargo so that it understands + /// when to rerun the build script. By default, this setting is enabled if + /// the `CARGO` environment variable is set. The `CARGO` environment + /// variable is set by Cargo for build scripts. Therefore, this setting + /// should be enabled automatically when run from a build script. However, + /// the method of detection is not completely reliable since the `CARGO` + /// environment variable can have been set by anything else. If writing the + /// instructions to `stdout` is undesireable, you can disable this setting + /// explicitly. pub fn emit_rerun_if_changed(mut self, enable: bool) -> Self { self.emit_rerun_if_changed = enable; self From 8e64c55f269ddeaef6de93ed5782ac64e44e651c Mon Sep 17 00:00:00 2001 From: Mick van Gelderen Date: Mon, 20 Jun 2022 23:04:03 +0200 Subject: [PATCH 3/3] Format --- tonic-build/src/prost.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tonic-build/src/prost.rs b/tonic-build/src/prost.rs index 9a25d0698..b6b5bf121 100644 --- a/tonic-build/src/prost.rs +++ b/tonic-build/src/prost.rs @@ -373,7 +373,7 @@ impl Builder { /// Enable or disable emitting /// [`cargo:rerun-if-changed=PATH`](https://doc.rust-lang.org/cargo/reference/build-scripts.html#rerun-if-changed) /// instructions for Cargo. - /// + /// /// If set, writes instructions to `stdout` for Cargo so that it understands /// when to rerun the build script. By default, this setting is enabled if /// the `CARGO` environment variable is set. The `CARGO` environment