rust-lang/cargo · error

cannot specify both `@{v}` and `--version`

Error message

cannot specify both `@{v}` and `--version`

What it means

In `cargo yank`'s resolve_crate (yank.rs:53-56), a crate spec written as `crate@version` is split on `@`. If the user ALSO passed `--version <ver>`, the version is now specified twice (once inline, once via flag), which is ambiguous, so Cargo bails.

Source

Thrown at src/bin/cargo/commands/yank.rs:55

    ops::yank(
        gctx,
        krate.map(|s| s.to_string()),
        version.map(|s| s.to_string()),
        args.get_one::<String>("token").cloned().map(Secret::from),
        args.registry_or_index(gctx)?,
        args.flag("undo"),
    )?;
    Ok(())
}

fn resolve_crate<'k>(
    mut krate: Option<&'k str>,
    mut version: Option<&'k str>,
) -> crate::CargoResult<(Option<&'k str>, Option<&'k str>)> {
    if let Some((k, v)) = krate.and_then(|k| k.split_once('@')) {
        if version.is_some() {
            anyhow::bail!("cannot specify both `@{v}` and `--version`");
        }
        if k.is_empty() {
            // by convention, arguments starting with `@` are response files
            anyhow::bail!("missing crate name for `@{v}`");
        }
        krate = Some(k);
        version = Some(v);
    }

    if let Some(version) = version {
        semver::Version::parse(version).with_context(|| {
            if let Some(stripped) = version.strip_prefix("v") {
                return format!(
                    "the version provided, `{version}` is not a \
                    valid SemVer version\n\n\
                    help: try changing the version to `{stripped}`",
                );
            }

View on GitHub (pinned to 0e07a15537)

Solutions

  1. Use only ONE version source: `cargo yank mycrate@1.2.3` OR `cargo yank mycrate --version 1.2.3`.
  2. Remove the redundant `--version` flag (the `@` form is usually more concise).

Example fix

// before
$ cargo yank mycrate@1.2.3 --version 1.2.3
error: cannot specify both `@1.2.3` and `--version`

// after
$ cargo yank mycrate@1.2.3
Defensive patterns

Strategy: validation

Validate before calling

let has_at = crate_spec.as_deref().is_some_and(|s| s.contains('@'));
let has_version_flag = version_flag.is_some();
if has_at && has_version_flag {
    return Err("specify version via @ OR --version, not both");
}

Type guard

fn version_specified_twice(spec: Option<&str>, ver: Option<&str>) -> bool {
    spec.map(|s| s.contains('@')).unwrap_or(false) && ver.is_some()
}

Prevention

When it happens

Trigger: Running `cargo yank mycrate@1.2.3 --version 1.2.3` (or any combination where both the `@` form and `--version` are present).

Common situations: Copy-pasting a version in two places; transitioning between the two syntaxes and forgetting to remove one; shell aliases that inject `--version`.

Related errors


AI-assisted analysis of rust-lang/cargo@0e07a15537 (2026-08-06). Data as JSON: /data/errors/8bca4112e614bbcd.json. Report an issue: GitHub.