Skip to content

Commit ec73c3f

Browse files
author
David Freese
committed
Allow for the specification of the rustfmt path
In some situations, rustfmt can't be counted on to be in your PATH. In those cases, this adds a format_with(...) function to the builder to specify the path to rustfmt, which is then passed into the new format_with function. A small dance is done here with fmt(...) and format_with(...) to preserve the public API of the crate, so this, if I understand correctly, should be an API-compatible change. I'm not 100% this would be the way I would want to go, but I wanted to put it up for discussion. There are more roundabout approaches I could take to format each file in our build system, but I had deferred from doing that previously. The relevant section of a public draft of the implementation I had put together is here: https://github.com/bazelbuild/rules_rust/pull/479/files#r583439508 Also open to suggestions as to where this should be tested. I didn't see, but may have missed, a good place for it.
1 parent 61555ff commit ec73c3f

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

tonic-build/src/lib.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,14 +158,22 @@ pub trait Method {
158158
#[cfg(feature = "rustfmt")]
159159
#[cfg_attr(docsrs, doc(cfg(feature = "rustfmt")))]
160160
pub fn fmt(out_dir: &str) {
161+
// Since fmt is public, preserve the API of fmt by dispatching to a helper function that is
162+
// called within the crate.
163+
format_with("rustfmt", &out_dir);
164+
}
165+
166+
#[cfg(feature = "rustfmt")]
167+
#[cfg_attr(docsrs, doc(cfg(feature = "rustfmt")))]
168+
fn format_with(rustfmt_path: impl AsRef<std::ffi::OsStr>, out_dir: &str) {
161169
let dir = std::fs::read_dir(out_dir).unwrap();
162170

163171
for entry in dir {
164172
let file = entry.unwrap().file_name().into_string().unwrap();
165173
if !file.ends_with(".rs") {
166174
continue;
167175
}
168-
let result = Command::new("rustfmt")
176+
let result = Command::new(rustfmt_path.as_ref())
169177
.arg("--emit")
170178
.arg("files")
171179
.arg("--edition")

tonic-build/src/prost.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ pub fn configure() -> Builder {
2121
compile_well_known_types: false,
2222
#[cfg(feature = "rustfmt")]
2323
format: true,
24+
rustfmt_path: None,
2425
emit_package: true,
2526
}
2627
}
@@ -210,6 +211,8 @@ pub struct Builder {
210211
out_dir: Option<PathBuf>,
211212
#[cfg(feature = "rustfmt")]
212213
format: bool,
214+
#[cfg(feature = "rustfmt")]
215+
rustfmt_path: Option<PathBuf>,
213216
}
214217

215218
impl Builder {
@@ -239,6 +242,13 @@ impl Builder {
239242
self
240243
}
241244

245+
/// The path that should be used to find rustfmt, if format is enabled.
246+
#[cfg(feature = "rustfmt")]
247+
pub fn format_with(mut self, path: impl AsRef<Path>) -> Self {
248+
self.rustfmt_path = Some(path.as_ref().to_path_buf());
249+
self
250+
}
251+
242252
/// Set the output directory to generate code to.
243253
///
244254
/// Defaults to the `OUT_DIR` environment variable.
@@ -331,6 +341,11 @@ impl Builder {
331341

332342
#[cfg(feature = "rustfmt")]
333343
let format = self.format;
344+
#[cfg(feature = "rustfmt")]
345+
let rustfmt_path = self
346+
.rustfmt_path
347+
.clone()
348+
.unwrap_or_else(|| "rustfmt".into());
334349

335350
config.out_dir(out_dir.clone());
336351
if let Some(path) = self.file_descriptor_set_path.as_ref() {
@@ -355,7 +370,10 @@ impl Builder {
355370
#[cfg(feature = "rustfmt")]
356371
{
357372
if format {
358-
super::fmt(out_dir.to_str().expect("Expected utf8 out_dir"));
373+
super::format_with(
374+
rustfmt_path,
375+
out_dir.to_str().expect("Expected utf8 out_dir"),
376+
);
359377
}
360378
}
361379

0 commit comments

Comments
 (0)