rust-lang/cargo · error
invalid character `+` in dependency name: `+{toolchain}`
Error message
invalid character `+` in dependency name: `+{toolchain}`
Use `cargo +{toolchain} add` if you meant to use the `{toolchain}` toolchain. What it means
From parse_dependencies (src/bin/cargo/commands/add.rs:265-270). For each crate name passed to `cargo add`, if it begins with `+`, Cargo assumes the user confused a toolchain override with a dependency and bails, suggesting the correct `cargo +<toolchain> add` form. The `+` prefix is reserved by Cargo itself for toolchain selection.
Source
Thrown at src/bin/cargo/commands/add.rs:266
};
let default_features = default_features(matches);
let optional = optional(matches);
let public = public(matches);
let mut crates = matches
.get_many::<String>("crates")
.into_iter()
.flatten()
.map(|c| (Some(c.clone()), None))
.collect::<IndexMap<_, _>>();
let mut infer_crate_name = false;
for (crate_name, _) in crates.iter() {
let crate_name = crate_name.as_ref().unwrap();
if let Some(toolchain) = crate_name.strip_prefix("+") {
anyhow::bail!(
"invalid character `+` in dependency name: `+{toolchain}`
Use `cargo +{toolchain} add` if you meant to use the `{toolchain}` toolchain."
);
}
}
if crates.is_empty() {
if path.is_some() || git.is_some() {
crates.insert(None, None);
infer_crate_name = true;
} else {
unreachable!("clap should ensure we have some source selected");
}
}
for feature in matches
.get_many::<String>("features")
.into_iter()
.flatten()View on GitHub (pinned to 0e07a15537)
Solutions
- Move the toolchain before the subcommand: `cargo +<toolchain> add <crate>`.
- If you did not mean a toolchain, remove the leading `+` from the dependency name.
- Double-check shell quoting isn't prepending a stray `+`.
Example fix
// before cargo add +nightly serde // after cargo +nightly add serde
Defensive patterns
Strategy: validation
Validate before calling
// Normalize a toolchain+add invocation: ensure `+toolchain` precedes the subcommand
fn build_add_cmd(toolchain: Option<&str>, crate_name: &str) -> Vec<String> {
let mut v: Vec<String> = vec!["cargo".into()];
if let Some(tc) = toolchain.filter(|t| !t.is_empty()) {
v.push(format!("+{tc}"));
}
v.push("add".into());
assert!(!crate_name.starts_with('+'), "crate name must not start with +");
v.push(crate_name.into());
v
} Type guard
fn looks_like_toolchain_override(arg: &str) -> bool {
arg.starts_with('+') && arg.len() > 1
} Prevention
- Remember the order: `cargo +toolchain <subcommand>` — the `+` always comes first.
- In scripts, build the command vector with the toolchain slot before the subcommand.
When it happens
Trigger: Running `cargo add +nightly serde` instead of `cargo +nightly add serde`. Copying a toolchain-prefixed command and appending `add <crate>` in the wrong position.
Common situations: Misremembering argument order for toolchain overrides; shell scripts that concatenate `+toolchain` with the subcommand incorrectly; onboarding from a doc snippet with the wrong order.
Related errors
- invalid character `+` in package name: `+{toolchain}` Us
- argument for --color must be auto, always, or never, but fou
- feature `{feature}` must be qualified by the dependency it's
- feature `{feature}` is not allowed to use explicit `dep:` sy
- `{feature}` is unsupported when inferring the crate name, us
AI-assisted analysis of rust-lang/cargo@0e07a15537 (2026-08-06).
Data as JSON: /data/errors/bbea9adecf0f7465.json.
Report an issue: GitHub.