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

Feature request: Add features using crate+feat1+feat2 syntax. #592

Closed
kurtbuilds opened this issue Jan 28, 2022 · 3 comments · Fixed by #609
Closed

Feature request: Add features using crate+feat1+feat2 syntax. #592

kurtbuilds opened this issue Jan 28, 2022 · 3 comments · Fixed by #609

Comments

@kurtbuilds
Copy link

kurtbuilds commented Jan 28, 2022

Currently, users can specify features with the --features option, like so:

cargo add serde --features derive

I think it's much easier as an end user to remember a syntax like the following for adding features:

cargo add serde+derive

This supports multiple features, as in:

cargo add serde+derive+rc

This syntax has the additional benefit that users can associate features on a per crate basis, enabling invocations like this:

cargo add serde+derive serde_json

I implemented this in a fork: https://github.com/kurtbuilds/cargo-edit But it needs to be updated, as there are code conflicts now.

Is there openness to merging/re-implementing this feature into mainline?

It should be entirely backwards compatible, since the --feature can continue to be supported, and no packages have + in their name.

I know there's the cargo-feature tool, but it seems like a worse approach to have a separate invocation cargo feature ... when adding features vs adding packages.

@epage
Copy link
Collaborator

epage commented Jan 28, 2022

Its an intriguing idea, in particular because it allows you to add multiple crates (cargo add serde+derive serde_json). This could be a more barebones approach to #383.

One question I have is how to tell when a + is in a path or is for adding features. So let's look at the full syntax

{<path> | <name>[:<path>] } [+<feature>]...

We could do the equivalent of rsplit_once('/') and assume + in the last path component are features.

And one concern is if we are getting too magical / custom in syntax. We are working on finalizing the CLI for cargo which gives us less room for experimentation. We could put this behind an unstable feature flag.

@kurtbuilds
Copy link
Author

kurtbuilds commented Jan 28, 2022

Its an intriguing idea, in particular because it allows you to add multiple crates (cargo add serde+derive serde_json). This could be a more barebones approach to #383.

One question I have is how to tell when a + is in a path or is for adding features. So let's look at the full syntax

{<path> | <name>[:<path>] } [+<feature>]...

We could do the equivalent of rsplit_once('/') and assume + in the last path component are features.

Yeah, I think your solution makes sense. Crates are forbidden to have + in the name, so any + there is either user error, or if this was implemented, would be a feature.

It would prevent something like cargo add ormx+sqlx/sqlite (where sqlite is a feature of the sqlx dependency of ormx). That seems esoteric enough to not be a blocker, as the user can fall back to cargo add ormx --features sqlx/sqlite.

Double checking, the current syntax doesn't allow for +<feature> right? It's <name | name:path> --features <feature>? That's an interesting idea which avoids the issue you brought up about filepaths. cargo add serde +derive +rc serde_json could be interpreted to apply those features only to the immediately preceding crate. It avoids the pathing problem you bring up, is backwards compatible (crates can't have + in the name), unambiguous, and I think equally readable (perhaps even a tad more so).

And one concern is if we are getting too magical / custom in syntax. We are working on finalizing the CLI for cargo which gives us less room for experimentation. We could put this behind an unstable feature flag.

That makes sense. If we get agreement on the syntax, whether with the space separator or the rsplit_once approach, it can be feature flagged if there's lingering concerns about possibilities for conflict.

@epage
Copy link
Collaborator

epage commented Jan 30, 2022

btw I posted on the internals thread for reviewing cargo-adds CLI about this proposal to solicit more feedback.

epage added a commit to epage/cargo-edit that referenced this issue Feb 1, 2022
Under `-Zinline-add`, we now support a `cargo feature`-like syntax of
`+feature`.  This allows `cargo add serde +derive serde_json`.

Benefits
- We become less reliant on modality
- Single-line getting started
- Shorter syntax

Fixes killercup#592
@epage epage closed this as completed in #609 Feb 1, 2022
epage added a commit that referenced this issue Feb 1, 2022
Under `-Zinline-add`, we now support a `cargo feature`-like syntax of
`+feature`.  This allows `cargo add serde +derive serde_json`.

Benefits
- We become less reliant on modality
- Single-line getting started
- Shorter syntax

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

Successfully merging a pull request may close this issue.

2 participants