cargo icon indicating copy to clipboard operation
cargo copied to clipboard

Support a shorthand for specifying features when `cargo add`ing multiple crates

Open kotx opened this issue 3 years ago • 11 comments

Problem

cargo add can be a bit annoying when specifying features for multiple crates. For example: cargo add serde sqlx --features serde/derive sqlx/postgres fails with error: invalid character `/` in dependency name: `sqlx/postgres`, characters must be Unicode XID characters (numbers, `-`, `_`, or most letters)

It's necessary to use one of

$ cargo add serde --features serde/derive sqlx --features sqlx/postgres
$ cargo add serde -F serde/derive sqlx -F sqlx/postgres
$ cargo add serde sqlx -F serde/derive,sqlx/postgres
$ cargo add serde sqlx -F "serde/derive sqlx/postgres"

But those methods are also cumbersome in having to specify the crate name multiple times

Proposed Solution

It would be useful to have a shorthand for specifying features, whether

  • Like pip extra's: cargo add serde[derive] sqlx[postgres,sqlite]
    • If we're daring maybe even cargo add sqlx[-default,postgres,sqlite]
  • Like cargo feature or the cargo-edit version of cargo add: cargo add serde +derive sqlx +postgres +sqlite
  • Allowing bare feature names when unambigious: cargo add serde -F derive sqlx -F postgres -F sqlite

Notes

No response

kotx avatar Jul 01 '22 03:07 kotx

It's necessary to use cargo add serde --features serde/derive sqlx --features sqlx/postgres (or in short cargo add serde -F serde/derive sqlx -F sqlx/postgres), but both methods are also cumbersome.

You can also do

$ cargo add serde sqlx -F "serde/derive sqlx/postgres"
$ cargo add serde sqlx -F serde/derive,sqlx/postgres

A space in argument values needs to be quoted but , is supported as an alternative separator.

epage avatar Jul 01 '22 03:07 epage

For background, before dep/feature was allowed with add, an alternative syntax was proposed, see https://github.com/killercup/cargo-edit/issues/592. For more history on it see

  • https://internals.rust-lang.org/t/feedback-on-cargo-add-before-its-merged/16024
  • https://github.com/rust-lang/cargo/pull/10472
  • https://rust-lang.zulipchat.com/#narrow/stream/246057-t-cargo/topic/Stablizing.20cargo-add

In summary, the main reason we removed it was cargo add was going to be insta-stable due to having higher precedence than cargo-edit and we wanted the initial release to be more conservative, so we only focused on --features (and added the -F shorthand) which is consistent with the rest of cargo.

epage avatar Jul 01 '22 04:07 epage

You can also do

$ cargo add serde sqlx -F "serde/derive sqlx/postgres"
$ cargo add serde sqlx -F serde/derive,sqlx/postgres

A space in argument values needs to be quoted but , is supported as an alternative separator.

@epage That sounds better, but it's still not possible(?) to specify both crate name and feature in one go, i.e. the crate name is still required twice. Would like a way to specify a crate with features just so it's faster, since I create new crates quite often.

In summary, the main reason we removed it was cargo add was going to be insta-stable due to having higher precedence than cargo-edit and we wanted the initial release to be more conservative, so we only focused on --features (and added the -F shorthand) which is consistent with the rest of cargo.

This makes sense, but I want to ask, is the new functionality proposed here or the +feature syntax possible for a future release or is a decision still pending?

kotx avatar Jul 01 '22 04:07 kotx

@epage That sounds better, but it's still not possible(?) to specify both crate name and feature in one go, i.e. the crate name is still required twice. Would like a way to specify a crate with features just so it's faster, since I create new crates quite often. ... is the new functionality proposed here or the +feature syntax possible for a future release or is a decision still pending?

Still pending. We have not brought the subject up again.

For myself, I would want to a better idea of the scope of people who are wanting to do these more involved cargo add commands. My assumption is that in the majority of cases, it'll be copy/paste from documentation and a minority of times it will be by users. If the scope is small enough, it might not be worth providing a shorthand. Some of this will require time of cargo add being out there and being used.

epage avatar Jul 01 '22 04:07 epage

@kotx do you mind if I rename / reword this a bit to generalize this to about a shorter feature syntax with python and cargo feature syntax be possible solutions for that more general problem?

epage avatar Jul 01 '22 14:07 epage

@kotx do you mind if I rename / reword this a bit to generalize this to about a shorter feature syntax with python and cargo feature syntax be possible solutions for that more general problem?

@epage Go ahead!

kotx avatar Jul 01 '22 16:07 kotx

I implemented the cargo add crate1 +feat1 +feat2 crate2 syntax in a fork.

https://github.com/kurtbuilds/cargo

If there's interest, I can create a PR to merge.

kurtbuilds avatar Sep 04 '22 15:09 kurtbuilds

We are not blocked on implementation (this previously existed in the cargo-add version). See my previous comment

is the new functionality proposed here or the +feature syntax possible for a future release or is a decision still pending?

Still pending. We have not brought the subject up again.

For myself, I would want to a better idea of the scope of people who are wanting to do these more involved cargo add commands.

epage avatar Sep 05 '22 12:09 epage

serde/derive shortcut seems quite nice, since that's the Cargo's internal syntax too, so the syntax should be recognizable. It wouldn't cause confusion when people try to use the CLI syntax in their TOML syntax.

Multiple features could be specified by repeating the crate name (again same as [features]) or just left out for -F to handle.

kornelski avatar Sep 07 '22 14:09 kornelski

Is this ticket still relevant? I'm able to install packages and features like this:

cargo add thiserror tokio argon2 axum-extra tracing tracing-subscriber serde secrecy \
    -F 'serde/derive' -F 'secrecy/serde' -F 'tracing-subscriber/env-filter' -F 'tokio/full' \
    -F argon2/std -F axum-extra/cookie -F axum-extra/cookie-signed

It would be nice to provide an example on the cargo add page for multiple crates with multiple features

woile avatar Aug 26 '23 13:08 woile

Is this ticket still relevant? I'm able to install packages and features like this:

This issue is about having a short hand

It would be nice to provide an example on the cargo add page for multiple crates with multiple features

We do have cargo add serde serde_json -F serde/derive. The question from there is how many cases should we support. The existing examples shows how to do it and that it is using existing cargo syntax.

epage avatar Dec 07 '23 15:12 epage