Skip to content

Feature Request: add suggestion for similarly named features with cargo add #15436

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

Open
Jujumba opened this issue Apr 18, 2025 · 8 comments · May be fixed by #15438
Open

Feature Request: add suggestion for similarly named features with cargo add #15436

Jujumba opened this issue Apr 18, 2025 · 8 comments · May be fixed by #15438
Assignees
Labels
C-feature-request Category: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted` Command-add S-accepted Status: Issue or feature is accepted, and has a team member available to help mentor or review

Comments

@Jujumba
Copy link

Jujumba commented Apr 18, 2025

Problem

When adding a crate with certain features and misspelling one of them, cargo doesn't suggest features with similar names. This may be confusing if a crate has lots of features. For example:

$ cargo add axum_extra --features typed-headers  # The actual feature is `typed-header`, see the last row of disabled features
    Updating crates.io index
warning: translating `axum_extra` to `axum-extra`
      Adding axum-extra v0.10.1 to dependencies
error: unrecognized feature for crate axum-extra: typed-headers
disabled features:
    __private_docs, async-read-body, async-stream, attachment, cookie
    cookie-key-expansion, cookie-private, cookie-signed, erased-json
    error-response, file-stream, form, json-deserializer, json-lines, multipart
    protobuf, query, scheme, typed-header, typed-routing
enabled features:
    tracing

Proposed Solution

Instead of just listing all feature flags, cargo may additionally search for similarly named features and suggest them, so that it looks like the following:

$ cargo add axum_extra --features typed-headers
    Updating crates.io index
warning: translating `axum_extra` to `axum-extra`
      Adding axum-extra v0.10.1 to dependencies
error: unrecognized feature for crate axum-extra: typed-headers
help: a feature with a similar name exists: `typed-header`
disabled features:
    __private_docs, async-read-body, async-stream, attachment, cookie
    cookie-key-expansion, cookie-private, cookie-signed, erased-json
    error-response, file-stream, form, json-deserializer, json-lines, multipart
    protobuf, query, scheme, typed-header, typed-routing
enabled features:
    tracing

The help output here is worded the same way the compiler does

Notes

No response

@Jujumba Jujumba added C-feature-request Category: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted` S-triage Status: This issue is waiting on initial triage. labels Apr 18, 2025
@Jujumba
Copy link
Author

Jujumba commented Apr 18, 2025

I'd also love to work on this if the issue gets accepted. Thanks!

@epage epage added Command-add S-accepted Status: Issue or feature is accepted, and has a team member available to help mentor or review and removed S-triage Status: This issue is waiting on initial triage. labels Apr 18, 2025
@epage
Copy link
Contributor

epage commented Apr 18, 2025

@epage
Copy link
Contributor

epage commented Apr 18, 2025

Also, be sure to read https://doc.crates.io/contrib/process/working-on-cargo.html#submitting-a-pull-request

@Jujumba
Copy link
Author

Jujumba commented Apr 18, 2025

@rustbot claim

@Jujumba
Copy link
Author

Jujumba commented Apr 18, 2025

The helper for generating the message is at https://github.com/rust-lang/cargo/blob/master/src/cargo/util/edit_distance.rs#L112

Do you think that edit_distance::closest_msg is appropriate? I think edit_distance::closest is a better choice since there may be several misspelled features, and adding a new line for each suggestion is a little lengthy.

@epage
Copy link
Contributor

epage commented Apr 18, 2025

My first thought is "what would rustc do?". In general, we are moving towards better aligning ourselves with rustc.

I feel like the scale for the number of misspelled features within one invocation is likely small (0-2) that it shouldn't be much of a problem in the common cases.

@Jujumba
Copy link
Author

Jujumba commented Apr 18, 2025

My first thought is "what would rustc do?". In general, we are moving towards better aligning ourselves with rustc.

I feel like the scale for the number of misspelled features within one invocation is likely small (0-2) that it shouldn't be much of a problem in the common cases.

Alright, thanks!

I've implemented the feature though one thing lacks: coloring of the help: ... section. How could I get the help part of the message colored?

@epage
Copy link
Contributor

epage commented Apr 18, 2025

Currently, we've not been doing that. When the next version of annotate-snippets is out, I'd like to look at switching all of our error reporting to that.

@Jujumba Jujumba linked a pull request Apr 18, 2025 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-feature-request Category: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted` Command-add S-accepted Status: Issue or feature is accepted, and has a team member available to help mentor or review
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants