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

Adding feature-flags to cargo publish and cargo package #6453

Merged
merged 5 commits into from
Jan 10, 2019

Conversation

spacekookie
Copy link
Member

This change adds the --features and --all-features flags to the
above mentioned commands. This is to allow publishing of crates that
have certain feature requirements for compilation, without having to
rely on --no-verify which isn't good practise.

This PR adds two new fields features and all_features to both the
PackageOpts and PublishOpts and also adds the argument options
to the CLI commands.

Merging this feature will allow people to publish crates on crates.io
that require some feature flag to compile (or all?) without needing
to rely on not checking the package before uploading, potentially
resulting in less packaging errors and broken packages.

@rust-highfive
Copy link

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @Eh2406 (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

Please see the contribution instructions for more information.

spacekookie added a commit to spacekookie/cargo-release that referenced this pull request Dec 17, 2018
This PR adds `features` and `all_features` as both flags to the CLI
as well as the `release.toml` configuration. This change allows you
to tell `cargo-release` to verify the package with certain features
enabled, in order to avoid needing to use `no-verify` for crates
that depend on certain features being enabled (or simply any
feature) before they can be uploaded.

This change is dependent on rust-lang/cargo#6453, which adds this
feature to the base `cargo publish` command.

The motivation behind this PR is the same as the above mentioned
cargo PR: reducing the amount of packaging errors that can happen
due to people not being able to verify their code because it has
some feature requirements for compilation, ultimately resulting in
a more healthy package ecosystem.
@Eh2406
Copy link
Contributor

Eh2406 commented Dec 17, 2018

This makes sense to me, but I am not comfortable adding a insta stable argument on my own.
@alexcrichton do you have an opinion on adding this?
@nrc how does this work with the feature freeze?

@ehuss
Copy link
Contributor

ehuss commented Dec 17, 2018

I'm not sure I understand the use case here. If you publish a package like this, and I depend on it, it will also fail to compile? Can you explain more about why you need it?

As an aside, this is missing --no-default-features and tests.

@spacekookie
Copy link
Member Author

@ehuss it would fail to compile with an error like "please provide either feature a, b or c".

That's wanted behaviour because maybe it it's not obvious which feature should be used by default and an explicit choice is the most desirable.

The crate should still be publishable though, thus this change.

I will look into writing some tests for this later though

@alexcrichton
Copy link
Member

I think this is fine to add, even as insta stable, personally. This is following the clear precedent of other subcommands, and while I don't necessarily personally think it's good for packages to require having a feature activated, that's not really up to us to pass a judgment on!

I'd be ok r+'ing this with some tests!

@bors
Copy link
Collaborator

bors commented Dec 20, 2018

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

This change adds the `--features` and `--all-features` flags to the
above mentioned commands. This is to allow publishing of crates that
have certain feature requirements for compilation, without having to
rely on `--no-verify` which isn't good practise.

This PR adds two new fields `features` and `all_features` to both the
`PackageOpts` and `PublishOpts` and also adds the argument options
to the CLI commands.

Merging this feature will allow people to publish crates on crates.io
that require some feature flag to compile (or all?) without needing
to rely on not checking the package before uploading, potentially
resulting in less packaging errors and broken packages.
@spacekookie
Copy link
Member Author

ping @alexcrichton, there's tests now and they pass 🎉

Copy link
Contributor

@ehuss ehuss left a comment

Choose a reason for hiding this comment

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

Also, this still needs to add support for --no-default-features.

@@ -26,6 +26,9 @@ pub struct PackageOpts<'cfg> {
pub verify: bool,
pub jobs: Option<u32>,
pub target: Option<String>,
pub registry: Option<String>,
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is registry added if it is never used?

Copy link
Member Author

Choose a reason for hiding this comment

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

Good catch!

I copied the registry field over from somewhere else and then implemented the init without realising it was actually never used 😅

@@ -23,8 +23,7 @@ fn simple() {
license = "MIT"
description = "foo"
"#,
)
.file("src/main.rs", "fn main() {}")
).file("src/main.rs", "fn main() {}")
Copy link
Contributor

Choose a reason for hiding this comment

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

All these formatting changes need to be removed.

.file(
"Cargo.toml",
r#"
cargo-features = ["alternative-registries"]
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do these tests have this cargo-feature?

fn main() {}",
).build();

p.cargo("publish --registry alternative -Zunstable-options --features required")
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure why these tests are set up this way with alternative registries. You can publish to a local (fake) registry. See simple for an example.

Copy link
Member Author

Choose a reason for hiding this comment

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

Hey, so I finally got the chance to look at this again.

I think the reason why most of the other tests are using the alternative registry is so that they can omit options from the Cargo.toml, which is technically not valid (anymore?).

Without an alternative registry I'll need to provide an author which is fine by itself, just means that it would be the only test in the file to provide one.

I opted for "do it the same way all the other tests work" over trying to be clever.

Do you think that's okay? Otherwise I'll happily change the test and just add more fields to the manifest as are needed.

Copy link
Contributor

Choose a reason for hiding this comment

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

The nearby tests that are using alternative registries are doing so to test the publish field which requires that feature.

I'm not sure I understand what you mean by the author field, since there is one in the test. Here's the style I'm thinking of:

diff --git a/tests/testsuite/publish.rs b/tests/testsuite/publish.rs
index dca4e92c..7b3dce40 100644
--- a/tests/testsuite/publish.rs
+++ b/tests/testsuite/publish.rs
@@ -812,15 +812,12 @@ fn publish_with_select_features() {
         .file(
             "Cargo.toml",
             r#"
-            cargo-features = ["alternative-registries"]
-
             [project]
             name = "foo"
             version = "0.0.1"
             authors = []
             license = "MIT"
             description = "foo"
-            publish = []

             [features]
             required = []
@@ -829,20 +826,15 @@ fn publish_with_select_features() {
         )
         .file(
             "src/main.rs",
-            "#[cfg(not(required))]
+            "#[cfg(not(feature=\"required\"))]
              compile_error!(\"This crate requires `required` feature!\");
              fn main() {}",
         ).build();

-    p.cargo("publish --registry alternative -Zunstable-options --features required")
-        .masquerade_as_nightly_cargo()
-        .with_status(101)
-        .with_stderr(
-            "\
-[ERROR] some crates cannot be published.
-`foo` is marked as unpublishable
-",
-        ).run();
+    p.cargo("publish --features required --index")
+        .arg(publish::registry().to_string())
+        .with_stderr_contains("[UPLOADING] foo v0.0.1 ([CWD])")
+        .run();
 }

 #[test]

Copy link
Member Author

Choose a reason for hiding this comment

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

Hmm...so the problem with authors = [] was that it was complaining about it being empty. But I just realised I missed your point about the --index flag which seems to fix this issue.

@spacekookie
Copy link
Member Author

Should be good now, @Eh2406 please let me know if there's anything else I've missed

@Eh2406
Copy link
Contributor

Eh2406 commented Jan 9, 2019

I will give others a chance to speak up, but looks good to me.

@alexcrichton
Copy link
Member

@bors: r+

@bors
Copy link
Collaborator

bors commented Jan 10, 2019

📌 Commit b29e379 has been approved by alexcrichton

@bors
Copy link
Collaborator

bors commented Jan 10, 2019

⌛ Testing commit b29e379 with merge c9fa7db...

bors added a commit that referenced this pull request Jan 10, 2019
Adding feature-flags to `cargo publish` and `cargo package`

This change adds the `--features` and `--all-features` flags to the
above mentioned commands. This is to allow publishing of crates that
have certain feature requirements for compilation, without having to
rely on `--no-verify` which isn't good practise.

This PR adds two new fields `features` and `all_features` to both the
`PackageOpts` and `PublishOpts` and also adds the argument options
to the CLI commands.

Merging this feature will allow people to publish crates on crates.io
that require some feature flag to compile (or all?) without needing
to rely on not checking the package before uploading, potentially
resulting in less packaging errors and broken packages.
@bors
Copy link
Collaborator

bors commented Jan 10, 2019

☀️ Test successful - checks-travis, status-appveyor
Approved by: alexcrichton
Pushing c9fa7db to master...

@bors bors merged commit b29e379 into rust-lang:master Jan 10, 2019
@therealprof
Copy link

@alexcrichton There's a very prominent usecase in embedded where we want to provide hardware specific abstractions for multiple devices and the user needs to select the target hardware. Neither does it make sense to allow not selecting hardware (in some cases the crate wouldn't even compile then because of lower level dependencies) nor would it make sense to provide a default.

Up to now the common workarounds were:

I'm very psyched about this landing! 🎉

@spacekookie
Copy link
Member Author

@therealprof I think diesel-rs/diesel has so far only been publishable with --no-verify.

I'm glad it's having impact in use cases I hadn't even considered originally 🙂

@spacekookie spacekookie deleted the publish-featured branch January 11, 2019 16:13
bors added a commit to rust-lang/rust that referenced this pull request Jan 14, 2019
Update cargo

13 commits in 34320d212dca8cd27d06ce93c16c6151f46fcf2e..2b4a5f1f0bb6e13759e88ea9512527b0beba154f
2019-01-03 19:12:38 +0000 to 2019-01-12 04:13:12 +0000
- Add test for publish with [patch] + cleanup. (rust-lang/cargo#6544)
- Fix clippy warning (rust-lang/cargo#6546)
- Revert "Workaround by using yesterday's nightly" (rust-lang/cargo#6540)
- Adding feature-flags to `cargo publish` and `cargo package` (rust-lang/cargo#6453)
- Fix the Travis CI badge (rust-lang/cargo#6530)
- Add helpful text for Windows exceptions like Unix (rust-lang/cargo#6532)
- Report fix bugs to Rust instead of Cargo (rust-lang/cargo#6531)
- --{example,bin,bench,test} with no argument now lists all available targets (rust-lang/cargo#6505)
- Rebuild on mid build file modification (rust-lang/cargo#6484)
- Derive Clone for TomlDependency (rust-lang/cargo#6527)
- publish: rework the crates.io detection logic. (rust-lang/cargo#6525)
- avoid duplicates in ignore files (rust-lang/cargo#6521)
- Rustflags in metadata (rust-lang/cargo#6503)

r? @alexcrichton
bors added a commit that referenced this pull request Jan 15, 2019
Add documentation for new package/publish feature flags.

Added in #6453.
spacekookie added a commit to spacekookie/cargo-release that referenced this pull request Jan 29, 2019
This PR adds `features` and `all_features` as both flags to the CLI
as well as the `release.toml` configuration. This change allows you
to tell `cargo-release` to verify the package with certain features
enabled, in order to avoid needing to use `no-verify` for crates
that depend on certain features being enabled (or simply any
feature) before they can be uploaded.

This change is dependent on rust-lang/cargo#6453, which adds this
feature to the base `cargo publish` command.

The motivation behind this PR is the same as the above mentioned
cargo PR: reducing the amount of packaging errors that can happen
due to people not being able to verify their code because it has
some feature requirements for compilation, ultimately resulting in
a more healthy package ecosystem.
@ehuss ehuss added this to the 1.33.0 milestone Feb 6, 2022
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.

7 participants