Skip to content
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

Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 23 additions & 0 deletions tonic-build/src/prost.rs
Expand Up @@ -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(),
Copy link
Contributor Author

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

  1. right, and
  2. necessary

Setting it to true may constitute a breaking change if people are using tonic_build outside of build scripts.
We could also set it to false in this version of tonic_build and make it true or automatically detected in the next major release.

Copy link
Member

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.

Copy link
Contributor Author

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.

Copy link
Member

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.

}
}

Expand Down Expand Up @@ -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>,
}
Expand Down Expand Up @@ -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.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this the only place I need to document this option?

Copy link
Member

Choose a reason for hiding this comment

The 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,
Expand Down Expand Up @@ -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)?;
Expand Down