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

Add a feature to make which dependency optional #1615

Merged
merged 7 commits into from
Sep 17, 2019
Merged

Add a feature to make which dependency optional #1615

merged 7 commits into from
Sep 17, 2019

Conversation

lopopolo
Copy link
Contributor

@lopopolo lopopolo commented Sep 5, 2019

This PR adds a new feature, enabled by default, called which-rustfmt. When enabled, this feature enables using the which crate to search for the rustfmt binary. which is now an optional dependency. Disabling this feature trims some dependencies in the case that bindings are generated at build time and never checked in.

This PR changes the API of rustfmt_path to return Result<Option<Cow<PathBuf>>>.

Ok(None) is returned in the case where which is disabled and no rustfmt command is
supplied explicitly either via configuration or env variable.

Downstream code checks for the presence of None to directly return the source without
emitting an error.

Fixes GH-1614.

I named the feature which-rustfmt instead of which because the feature is tied to the functionality, not the crate. If this functionality for some reason requires additional dependencies in the future, they can be added to this feature and excluded in a backwards compatible way. This is also the suggested way to name features by the cargo documentation.

@emilio I have rustfmt enabled by default on save in my editor and there was quite a lot of diff to ignore to make this PR minimal. Can we either rustfmt bindgen and add a step to CI to enforce code is formatted or add a rustfmt.toml so the formatting is preserved?

This feature controls whether bindgen will use the which crate to detect
the rustfmt binary. This makes which an optional dependency.

which-rustfmt is a default feature which makes this change backward compatible.
This commit changes the API of rustfmt_path to return Result<Option<Cow<PathBuf>>>.

Ok(None) is returned in the case where which is disabled and no rustfmt command is
supplied explicitly either via configuration or env variable.

Downstream code checks for the presence of None to directly return the source without
emitting an error.
Copy link
Contributor

@emilio emilio left a comment

Choose a reason for hiding this comment

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

Thanks, this looks pretty good :)

I think it could be simpler, and it'd be great to tweak .travis.yml to also at least build with this configuration.

Thank you!

@@ -54,7 +54,7 @@ lazy_static = "1"
peeking_take_while = "0.1.2"
quote = { version = "1", default-features = false }
regex = "1.0"
which = ">=1.0, <3.0"
which = { version = ">=1.0, <3.0", optional = true }
Copy link
Contributor

Choose a reason for hiding this comment

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

What about just leaving this change, then the feature will be automatically called which (you get a feature for each optional dependency).

@@ -70,9 +70,11 @@ optional = true
version = "0.4"

[features]
default = ["logging", "clap"]
default = ["logging", "clap", "which-rustfmt"]
Copy link
Contributor

Choose a reason for hiding this comment

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

So then you replace which-rustfmt by which here...

logging = ["env_logger", "log"]
static = []
# Dynamically discover a `rustfmt` binary using the `which` crate
which-rustfmt = ["which"]
Copy link
Contributor

Choose a reason for hiding this comment

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

And remove this line.

@@ -32,6 +32,7 @@ extern crate quote;
extern crate proc_macro2;
extern crate regex;
extern crate shlex;
#[cfg(feature = "which-rustfmt")]
Copy link
Contributor

Choose a reason for hiding this comment

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

And s/which-rustfmt/which here.

Err(e) => Err(io::Error::new(io::ErrorKind::Other, format!("{}", e))),
}
#[cfg(not(feature = "which-rustfmt"))]
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd just return an error here, like Err(io::Error::new(io::ErrorKind::Other, "which wasn't enabled, and no rustfmt binary specified"))), then the patch becomes much simpler, wdyt?

The error is not fatal so you'd still generate bindings.

@lopopolo lopopolo mentioned this pull request Sep 15, 2019
@lopopolo
Copy link
Contributor Author

Hi @emilio I'm a bit confused. This PR is approved but you've left feedback. Who is responsible for pushing this forward?

Your feedback on the name of the feature seems to ignore my justification for my decision from the PR description and the recommendation from the cargo docs. I'm happy to make this change but the Cargo.toml also has a logging feature which follows this pattern. Which is the right way in bindgen?

I named the feature which-rustfmt instead of which because the feature is tied to the functionality, not the crate. If this functionality for some reason requires additional dependencies in the future, they can be added to this feature and excluded in a backwards compatible way. This is also the suggested way to name features by the cargo documentation.

I found the codebase more difficult to contribute to than it should be because my reasonable editor configuration makes massive diffs when formatting on save. I've filed GH-1618 to address this.

@emilio
Copy link
Contributor

emilio commented Sep 16, 2019

Hi @emilio I'm a bit confused. This PR is approved but you've left feedback. Who is responsible for pushing this forward?

I expected you to address the comments before I merge it, though if you don't have the cycles I'm happy to fix it.

I found the codebase more difficult to contribute to than it should be because my reasonable editor configuration makes massive diffs when formatting on save. I've filed GH-1618 to address this.

That's fair, sorry about it. I'd be happy to do that, but probably worth waiting until this is merged so you don't have to rebase conflicts.

@emilio
Copy link
Contributor

emilio commented Sep 16, 2019

I named the feature which-rustfmt instead of which because the feature is tied to the functionality, not the crate. If this functionality for some reason requires additional dependencies in the future, they can be added to this feature and excluded in a backwards compatible way. This is also the suggested way to name features by the cargo documentation.

Ah, sorry, had missed this... Let's add build this on .travis.yml.

@lopopolo
Copy link
Contributor Author

@emilio (pending Travis passing) I think this is ready to be looked at again

Copy link
Contributor

@emilio emilio left a comment

Choose a reason for hiding this comment

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

Looks great, thanks!

@emilio emilio merged commit 3062841 into rust-lang:master Sep 17, 2019
@lopopolo
Copy link
Contributor Author

Thanks for merging @emilio 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add a cargo feature to disable rustfmt
3 participants