-
Notifications
You must be signed in to change notification settings - Fork 723
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
Adds support for running rustfmt on generated bindings #905
Conversation
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.
This looks great! Just a couple things before we merge:
-
The builder has a method
command_line_flags
that (re)constructs equivalent command line flags to the builder's configuration. This is really useful for debugging with C-Reduce when things go wrong. We need to add support for these new flags to that method. (Also, if we forgot about this for--impl Debug
then we should do a new PR for that too). -
A few nitpicks below.
Thanks again! :)
src/lib.rs
Outdated
/// Set the absolute path to the rustfmt configuration file, if None, the standard rustfmt | ||
/// options are used. | ||
pub fn format_configuration_file(mut self, path: Option<PathBuf>) -> Self { | ||
self.options.format_configuration_file = path; |
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.
This should probably imply format_bindings
is true
. That is, calling this method should also set format_bindings
to true
.
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.
^ This comment does not appear to be addressed.
src/lib.rs
Outdated
cmd.args(&["--config-path", path]); | ||
} | ||
|
||
if let Err(e) = cmd.arg(file).status() { |
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.
Note that status()
only returns the child processes exit status, it doesn't check that the exit status is success. Additionally, I think it makes sense to propagate any errors, since (unlike if rustfmt
is just missing) we don't know if the bindings are still in an OK state if this fails: maybe only half of the formatted bindings got written to the file, and the other half didn't?
let status = cmd.arg(file).status()?;
if status.success() {
Ok(())
} else {
Err(io::Error::new(io::ErrorKind::Other, "rustfmt exited with a failure status: {}", status))
}
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.
As far as I know, rustfmt does not write only the half. The problem with the status code is that if rustfmt can not format (because line too long or whatever), it ends with status != 0. I also can not return an error here, because write_to_file returns a io::Result. Chain_error would be nice here.
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.
he problem with the status code is that if rustfmt can not format (because line too long or whatever), it ends with status != 0.
Yeah. Ideally we would have some way of distinguishing between "real" failures and "warnings". I guess we should just add a comment to this effect here and move on with our lives.
cc @nrc: you may find this ^ useful for informing rustfmt development maybe
I also can not return an error here, because write_to_file returns a io::Result.
io::Error
allows for custom errors (like in the above snippet) and the std::process
APIs also return io::Result
s, so I don't understand the problem. I must not be following something. But I guess it doesn't matter since we aren't changing these things.
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.
Ohh... I totally missed that Command returns a io::Result...
src/lib.rs
Outdated
@@ -1086,6 +1110,13 @@ pub struct BindgenOptions { | |||
|
|||
/// Whether to prepend the enum name to bitfield or constant variants. | |||
pub prepend_enum_name: bool, | |||
|
|||
/// Whether rustfmt should format the generated bindings. | |||
pub format_bindings: bool, |
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.
Super nitpicky (sorry!), but I'd prefer rustfmt_bindings
and --rustfmt-bindings
(and similar for the config files).
https://github.com/bkchr/rust-bindgen/blob/35aaee1d21a19cefd37c388f6889c4961dafcb75/src/lib.rs#L457 isn't that your first point? If yes, I also done that for derive-debug :) |
Ah yes. Somehow I missed that :-P |
Addressed all issues :) |
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.
👍 thanks!
@bors-servo r+ |
📌 Commit 7fe3f0c has been approved by |
Adds support for running rustfmt on generated bindings This patch enables bindgen to run rustfmt on generated bindings. Rustfmt is used from the global PATH. Two new command-line arguments are added: 1. --format-bindings: Enables running rustfmt 2. --format-configuration-file: The configuration file for rustfmt (not required). Fixes: #900
💔 Test failed - status-travis |
I had rustfmt-bindgens enabled by default and then the test failed.. I changed it to disabled by default, is also probably better. |
Now all tests are green :) |
☔ The latest upstream changes (presumably #892) made this pull request unmergeable. Please resolve the merge conflicts. |
Rebased. |
Ah, yes! It should be disabled by default, sorry for not catching that in review! |
src/lib.rs
Outdated
@@ -1183,6 +1214,8 @@ impl Default for BindgenOptions { | |||
objc_extern_crate: false, | |||
enable_mangling: true, | |||
prepend_enum_name: true, | |||
format_bindings: true, |
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.
And this is defaulting to true
, but you just said you changed it to default to false
(which it should) -- did you forget to push some local changes?
@bkchr looks like a couple pieces of feedback got missed, let me know when you push fixes, thanks! |
This patch enables bindgen to run rustfmt on generated bindings. Rustfmt is used from the global PATH. Two new command-line arguments are added: 1. --format-bindings: Enables running rustfmt 2. --format-configuration-file: The configuration file for rustfmt (not required).
The --rustfmt-configuration-file command-line argument automatically activates --rustfmt-bindings.
@fitzgen ohh sorry... I worked across different pc's... And I did not pull before rebasing.. Now it should be as intended^^ |
@bkchr no problem, thanks for the PR :) @bors-servo r+ |
📌 Commit 27dd628 has been approved by |
Adds support for running rustfmt on generated bindings This patch enables bindgen to run rustfmt on generated bindings. Rustfmt is used from the global PATH. Two new command-line arguments are added: 1. --format-bindings: Enables running rustfmt 2. --format-configuration-file: The configuration file for rustfmt (not required). Fixes: #900
☀️ Test successful - status-travis |
This patch enables bindgen to run rustfmt on generated bindings. Rustfmt is used
from the global PATH. Two new command-line arguments are added:
Fixes: #900