New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(build): Add option to emit rerun-if-changed instructions #1021
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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<OsString>, | ||
pub(crate) include_file: Option<PathBuf>, | ||
pub(crate) emit_rerun_if_changed: bool, | ||
|
||
out_dir: Option<PathBuf>, | ||
} | ||
|
@@ -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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this the only place I need to document this option? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah I think this is fine, I would also add info about the default setting that if you're in a build script this will be enabled and outside of a build script it won't |
||
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)?; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if run-time detection of whether the code is run as a build script is
Setting it to
true
may constitute a breaking change if people are usingtonic_build
outside of build scripts.We could also set it to
false
in this version of tonic_build and make ittrue
or automatically detected in the next major release.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think its fine to set this for a build script. Do you know what other projects with similar features use? I don't mind auto enabling this in this release since I don't really think its a breaking style change etc.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I skimmed through the cargo reference but did not find anything else. I also failed to find results for how to detect Rust code is being run from a build script through Google. I hope this does a good enough job.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, it seems fine to me.