Skip to content

cargo add --optional overwrites previously declared feature #16164

@ginnyTheCat

Description

@ginnyTheCat

Problem

When having a feature called x and running cargo add x --optional the x feature gets overwritten with x = ["dep:x"]

Steps

  1. Have the following Cargo.toml:
[package]
name = "tmp"
version = "0.1.0"
edition = "2024"

[dependencies]
cookie_store = { version = "0.22", optional = true }

[features]
cookie = ["dep:cookie_store"]
  1. Run cargo add cookie --optional
  2. Now the Cargo.toml looks like this, with dep:cookie_store nowhere to be seen.
[package]
name = "tmp"
version = "0.1.0"
edition = "2024"

[dependencies]
cookie = { version = "0.18.1", optional = true }
cookie_store = { version = "0.22", optional = true }

[features]
cookie = ["dep:cookie"]

Possible Solution(s)

Theres three solutions I could think of:

  1. The safe option is to just not generate the feature in this case, so just leave cookie = ["dep:cookie_store"] alone.
  2. The other option would be to add it to the feature cookie = ["dep:cookie_store", "dep:cookie"]. In this case that's what I would want but I think it's quite dangerous as users would not expect it and in bigger Cargo.tomls seeing that change would be hard.
  3. Add it under another name _cookie = ["dep:cookie"]? But idk what to do when that name is taken already as well.

Personally I like option 1 the most.

Notes

No response

Version

cargo 1.92.0-nightly (81c3f77a4 2025-10-10)
release: 1.92.0-nightly
commit-hash: 81c3f77a467359c8be6bc747dc93ec66a6e4ce11
commit-date: 2025-10-10
host: x86_64-unknown-linux-gnu
libgit2: 1.9.1 (sys:0.20.2 vendored)
libcurl: 8.15.0-DEV (sys:0.4.83+curl-8.15.0 vendored ssl:OpenSSL/3.5.4)
ssl: OpenSSL 3.5.4 30 Sep 2025
os: Fedora 42.0.0 (Adams) [64-bit]

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: bugCommand-addS-needs-designStatus: Needs someone to work further on the design for the feature or fix. NOT YET accepted.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions