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

Working with features #194

Closed
wants to merge 22 commits into from
Closed

Working with features #194

wants to merge 22 commits into from

Conversation

alanpoon
Copy link

cc #193
Build Status

Copy link
Collaborator

@ordian ordian left a comment

Choose a reason for hiding this comment

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

Thank you for contributing to cargo-edit, @alanpoon!

Comments:

  1. I've left some inline comments.
  2. We had a discussion in Support for working with features #193 about the format of output. Ideally, we want to extract the information about available features from the crate's Cargo.toml and print a nicely formatted feature list, but that can be done in a separate PR.

README.md Outdated
@@ -71,7 +71,8 @@ Specify what crate to add:
`cargo add bitflags@0.3.2`.
--git <uri> Specify a git repository to download the crate from.
--path <uri> Specify the path the crate should be loaded from.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think we should follow cargo in describing the feature flag:

$ cargo build --help 2>&1 | grep -e "--features"
    --features FEATURES          Space-separated list of features to also build

(and maybe replace also build with add)

.travis.yml Outdated
@@ -44,4 +44,4 @@ env:
- secure: XXYE8x015gOSJ+DD9FeCdEKQTeU6mZ5j47Nh1ncZTkdN6+RnrN/DxAwccvh9u6t8/V6WN868AHIvTc0nA031mqwnh6vcQDr3Ak8ydJCTM8KFgwsuESokSZYkbiiQd/FWTBri/P9H5jejCw3aSFQZUVeCpu2Zxp0N9/Jn3ky7NVknBOVdz21jRC/KivBkpbvOCj8iEtGZuYSQu3Gpq1wbuZaqDdgg6JVvrYy7LbUNGe0rHMjCcyuiutqp6R8zIQoC7p2kmoHkXrTaDKQrpmSi+uLJJE7KqML9tFFnvjvmgroRYDCk0gc/fr/GunikvUkcczBBSsVXWj66EpriQxwdEnI1uhNtKUOGALIAMiLz2k0rbLl1wFLOvFlhEPhDGvt+mw+h2BIxoyy9mwVioYym7V8pksw0UuCuLuyAp1Rc89iT6/N3HfMFPZeTBeudktUb5ijHgJyobVwakxKXgKqELbrhUhmzBf8fwtmPH+Lnganyrz4qcKBnuUl2fSp6MIaGIeSZZwigggV8QSXw9u/1c32RhQs47ZDRJX2lzEzVyAVZ0liyn32hWFI6Ctur4jNV7LWtOcDqdYZZfqyZOL/HehNFtDoJfrYsp/KZvmBipmOkrNsKCKEAdvUoeTDXo4ME31tjGtiPbHIdRuBJJqt38gm1AKyoVJKp2KFGDsI0djA=
branches:
except:
- staging.tmp
Copy link
Collaborator

Choose a reason for hiding this comment

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

Could you revert this change?

}


let dependency = if !crate_name_is_url_or_path(&self.arg_crate) {
let dependency = Dependency::new(&self.arg_crate);
let dependency = Dependency::new(&self.arg_crate);
Copy link
Collaborator

Choose a reason for hiding this comment

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

This formatting seems odd (why 8 whitespaces?).

@@ -14,6 +14,7 @@ pub struct Dependency {
pub name: String,
optional: bool,
source: DependencySource,
features: Vec<String>,
Copy link
Collaborator

Choose a reason for hiding this comment

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

We need to distinguish an empty feature list from the default feature list (not specifying the feature flag).

Copy link
Author

Choose a reason for hiding this comment

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

Hi Ordian,
Thanks for your suggestion. "cargo list" or "cargo read-manifest" can only read the local cargo.toml manifest file. When you add a dependency to your project, you don't save a copy of the manifest file of the dependencies. Without the manifest file of the dependency, there is no way of returning the list of the available features. I think this enhancement is only useful for experienced users who would check out the available features themselves. I am only motivated in using cargo-edit because Cargo does not support Target specific features. rust-lang/cargo#1197
With cargo-edit being able to specify features, I can use continuous integration to build various target builds with dependencies with various features.

Copy link
Collaborator

Choose a reason for hiding this comment

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

  1. We download Cargo.toml locally for git dependencies in cargo-edit (and download json for crates.io dependencies), so a proper way to extract feature list would be to create a function get_features_from_manifest similar to get_name_from_manifest. But that can be done in a separate PR.
  2. The point of the comment is different: if user of cargo-edit does not specify --features when adding a dependency, he probably wants a default feature list, not the empty one. Btw, current CI failure is relevant.

@@ -95,7 +105,13 @@ impl Dependency {
if self.optional {
data.get_or_insert("optional", optional);
}

if !self.features.is_empty() {
let mut to_array = toml_edit::Array::default();
Copy link
Collaborator

Choose a reason for hiding this comment

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

toml_edit::Value implements FromIterator, so we can simplify this:

use std::iter::FromIterator;

let features = toml_edit::Value::from_iter(self.features.iter().cloned());
data.get_or_insert("features", features);

but we need to specify feature list only if user specified it.

// dependency present afterwards
let toml = get_toml(&manifest);
let val = &toml["dependencies"]["cargo-edit"]["features"];
let result = if let toml_edit::Item::Value(toml_edit::Value::Array(ref arr_z)) = *val {
Copy link
Collaborator

@ordian ordian Jan 25, 2018

Choose a reason for hiding this comment

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

I haven't tried this, but it should work:

let val = toml["dependencies"]["cargo-edit"]["features"][0].as_str();
assert_eq!(val, Some("jui"));

@ordian
Copy link
Collaborator

ordian commented Jan 31, 2018

Thanks for the changes. Do you think we should also add a no-default-features flag for consistency?

cc @killercup

README.md Outdated
@@ -71,7 +71,8 @@ Specify what crate to add:
`cargo add bitflags@0.3.2`.
--git <uri> Specify a git repository to download the crate from.
--path <uri> Specify the path the crate should be loaded from.

--features <FEATURES> Space-separated list of features to add

Copy link

Choose a reason for hiding this comment

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

stray whitespace here

@qmx
Copy link

qmx commented Jan 31, 2018

this works as expected, we might want to change the user output though

❯ cargo add cursive --features="termion-backend"   
      Adding cursive v0.7.5 to features termion-backend dependencies

@ordian
Copy link
Collaborator

ordian commented Feb 1, 2018

Thanks @qmx, good catch! But we may change the output to the form mentioned in #193 in the future PR.

@alanpoon, could you add an output test similar to this one, fix the ordering of features in the output and add a flag no-default-features? Also could you please revert all unrelated changes, like formatting and travis?

Copy link
Collaborator

@ordian ordian left a comment

Choose a reason for hiding this comment

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

Could you also please rebase and squash this PR down to a single commit when you're ready? Thanks!

@@ -97,6 +98,9 @@ fn print_msg(dep: &Dependency, section: &[String], optional: bool) -> Result<()>
if optional {
write!(output, " optional")?;
}
if let Some(f) = features {
write!(output, " features {}", f)?
Copy link
Collaborator

Choose a reason for hiding this comment

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

Could you place this output after line 109 (and change writeln to write), so that the output would be

... to dependencies with features: jui

rather than

to features jui dependencies

@bjgill bjgill mentioned this pull request Feb 16, 2018
@derekdreery
Copy link

Friendly ping!

This would be a nice feature to have. @alanpoon if you like I could adopt the PR?

@alanpoon
Copy link
Author

Hi derekdreery,
Sure, do I need to do anything?

@polarathene
Copy link

I'd also like to see this PR get merged, it's been a while since it was submitted and thus getting stale with conflicts. The changes that were requested were done? Can it be reviewed again and if valid then re-based and merged?

@ordian
Copy link
Collaborator

ordian commented Dec 1, 2019

Master has changed substantially, this PR needs rebasing and then I'll review it.

@alsuren alsuren mentioned this pull request Mar 26, 2020
5 tasks
alsuren added a commit to alsuren/cargo-edit that referenced this pull request Mar 28, 2020
Credit to @alanpoon for the original implementation
of this feature. This patch is a rebase of the original
PR (killercup#194) with minor tidying up.

Also contains some unrelated clippy and fmt fixes for
rust 1.41.0.
ordian pushed a commit that referenced this pull request Mar 30, 2020
* add --features='nose mouth'

Credit to @alanpoon for the original implementation
of this feature. This patch is a rebase of the original
PR (#194) with minor tidying up.

Also contains some unrelated clippy and fmt fixes for
rust 1.41.0.

* mention cargo-feature; update README

* forbid specifying multiple crates with --features

* fixup! forbid specifying multiple crates with --features
@ordian
Copy link
Collaborator

ordian commented Sep 13, 2021

I believe this was superceded by #395

@ordian ordian closed this Sep 13, 2021
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

5 participants