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

Adds support for running rustfmt on generated bindings #905

Merged
merged 5 commits into from Aug 14, 2017

Conversation

bkchr
Copy link
Contributor

@bkchr bkchr commented Aug 11, 2017

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

Copy link
Member

@fitzgen fitzgen left a 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:

  1. 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).

  2. 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;
Copy link
Member

@fitzgen fitzgen Aug 11, 2017

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.

Copy link
Member

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() {
Copy link
Member

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))
}

Copy link
Contributor Author

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.

Copy link
Member

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::Results, 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.

Copy link
Contributor Author

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,
Copy link
Member

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).

@bkchr
Copy link
Contributor Author

bkchr commented Aug 11, 2017

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 :)

@fitzgen
Copy link
Member

fitzgen commented Aug 11, 2017

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

@bkchr
Copy link
Contributor Author

bkchr commented Aug 11, 2017

Addressed all issues :)

Copy link
Member

@fitzgen fitzgen left a comment

Choose a reason for hiding this comment

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

👍 thanks!

@fitzgen
Copy link
Member

fitzgen commented Aug 11, 2017

@bors-servo r+

@bors-servo
Copy link

📌 Commit 7fe3f0c has been approved by fitzgen

@bors-servo
Copy link

⌛ Testing commit 7fe3f0c with merge 99d188f...

bors-servo pushed a commit that referenced this pull request Aug 11, 2017
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
@bors-servo
Copy link

💔 Test failed - status-travis

@bkchr
Copy link
Contributor Author

bkchr commented Aug 12, 2017

I had rustfmt-bindgens enabled by default and then the test failed.. I changed it to disabled by default, is also probably better.

@bkchr
Copy link
Contributor Author

bkchr commented Aug 12, 2017

Now all tests are green :)

@bors-servo
Copy link

☔ The latest upstream changes (presumably #892) made this pull request unmergeable. Please resolve the merge conflicts.

@bkchr
Copy link
Contributor Author

bkchr commented Aug 14, 2017

Rebased.

@fitzgen
Copy link
Member

fitzgen commented Aug 14, 2017

I changed it to disabled by default, is also probably better.

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,
Copy link
Member

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?

@fitzgen
Copy link
Member

fitzgen commented Aug 14, 2017

@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.
@bkchr
Copy link
Contributor Author

bkchr commented Aug 14, 2017

@fitzgen ohh sorry... I worked across different pc's... And I did not pull before rebasing.. Now it should be as intended^^

@fitzgen
Copy link
Member

fitzgen commented Aug 14, 2017

@bkchr no problem, thanks for the PR :)

@bors-servo r+

@bors-servo
Copy link

📌 Commit 27dd628 has been approved by fitzgen

@bors-servo
Copy link

⌛ Testing commit 27dd628 with merge 5ca0569...

bors-servo pushed a commit that referenced this pull request Aug 14, 2017
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
@bors-servo
Copy link

☀️ Test successful - status-travis
Approved by: fitzgen
Pushing 5ca0569 to master...

@bors-servo bors-servo merged commit 27dd628 into rust-lang:master Aug 14, 2017
@bkchr bkchr deleted the rustfmt branch August 14, 2017 17:48
@rukai rukai mentioned this pull request Apr 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants