Skip to content

cargo add: Misleading error message when feature isn't available in semver #15524

Open
@ultrabear

Description

@ultrabear

Problem

When using cargo add foo --features bar when bar was added in 1.0.1 (and is locked to that version) but Cargo.toml already specifies foo = 1.0, cargo correctly errors because bar is not in 1.0, however, the error message does not very clearly state the version checked, nor does it attempt to parse the currently locked version and offer a hint to the developer that maybe they should change Cargo.toml to specify 1.0.1.

Steps

cargo new test_add
cd test_add
cargo add serde_json@1
#    Updating crates.io index
#      Adding serde_json v1 to dependencies
#             Features as of v1.0.0:
#             - linked-hash-map
#             - preserve_order
#    Updating crates.io index
#     Locking 10 packages to latest Rust 1.86.0 compatible versions

# above "Features as of v1.0.0" is noted

# but the following command was run at a later point in time such that it was forgotten

cargo add serde_json --features raw_value
#    Updating crates.io index
#      Adding serde_json v1 to dependencies
#error: unrecognized feature for crate serde_json: raw_value
#disabled features:
#    linked-hash-map, preserve_order

Possible Solution(s)

there are two avenues of improvement, ideally both can be implemented:

  • adding Features as of xxx to the second cargo add command to make it clearer that the feature is not available in the current semver
  • give a hint to the programmer that in a later version of serde_json (and indeed the one that was in the lockfile) the feature is available, so they should update that dependencies semver specification

Notes

This is clearly low priority, its a confused me once and never again type of issue, but it felt right to at least track an area of improvement.

Version

cargo 1.86.0 (adf9b6ad1 2025-02-28)
release: 1.86.0
commit-hash: adf9b6ad14cfa10ff680d5806741a144f7163698
commit-date: 2025-02-28
host: x86_64-unknown-linux-gnu
libgit2: 1.9.0 (sys:0.20.0 vendored)
libcurl: 8.12.0-DEV (sys:0.4.79+curl-8.12.0 vendored ssl:OpenSSL/1.1.1w)
ssl: OpenSSL 1.1.1w  11 Sep 2023
os: Manjaro 25.0.1 (Zetar) [64-bit]

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Error and warning messages generated by Cargo itself.C-bugCategory: bugCommand-addS-needs-mentorStatus: Issue or feature is accepted, but needs a team member to commit to helping and reviewing.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions