rust-lang/cargo · error · anyhow::Error
`cargo run` can run at most one executable, but multiple wer
Error message
`cargo run` can run at most one executable, but multiple were specified
What it means
`cargo run` can execute at most one executable. If the target filter resolves to multiple runnable targets while being specific (e.g. several --bin flags, or a combination that yields >1), cargo_run.rs:87 bails and lists the matched targets.
Source
Thrown at src/ops/cargo_run.rs:87
or the `default-run` manifest key.\n\
available binaries: {}",
names.join(", ")
)
} else {
let mut message = "`cargo run` can run at most one executable, but \
multiple were specified"
.to_owned();
write!(&mut message, "\nhelp: available targets:")?;
for (pkg, bin) in &bins {
write!(
&mut message,
"\n {} `{}` in package `{}`",
bin.kind().description(),
bin.name(),
pkg.name()
)?;
}
anyhow::bail!(message)
}
}
// `cargo run` is only compatible with one `--target` flag at most
options.build_config.single_requested_kind()?;
let compile = ops::compile(ws, options)?;
assert_eq!(compile.binaries.len(), 1);
let UnitOutput {
unit,
path,
script_metas,
env: _env,
} = &compile.binaries[0];
let exe = match path.strip_prefix(gctx.cwd()) {
Ok(path) if path.file_name() == Some(path.as_os_str()) => Path::new(".").join(path),
Ok(path) => path.to_path_buf(),
Err(_) => path.to_path_buf(),View on GitHub (pinned to 0e07a15537)
Solutions
- Pass exactly one target: `cargo run --bin a`.
- Run each binary in a separate `cargo run` invocation.
- If you scripted this, loop over the binaries calling `cargo run --bin $b` once each.
Example fix
# before $ cargo run --bin a --bin b error: `cargo run` can run at most one executable, but multiple were specified # after $ cargo run --bin a $ cargo run --bin b
Defensive patterns
Strategy: validation
Validate before calling
# Allow at most one runnable target on `cargo run`: n=$(printf '%s\n' "$@" | grep -c -- '--bin\|--example') if [ "$n" -gt 1 ]; then echo "cargo run accepts at most one executable" >&2; exit 1 fi cargo run "$@"
Prevention
- Pass a single --bin (or --example) to `cargo run`.
- Loop over binaries in a script, one `cargo run` each.
- Don't assume `cargo run` mirrors `cargo build` multi-target behavior.
When it happens
Trigger: `cargo run --bin a --bin b`, or combining flags such that more than one executable is selected.
Common situations: Assuming `cargo run` launches multiple binaries like `cargo build`; copy-pasting multiple --bin flags.
Related errors
- a bin target must be available for `cargo run`
- `cargo run` could not determine which binary to run. Use the
- example target `{}` is a library and cannot be executed
- `cargo run` does not support glob pattern `{}` on package se
- running the file `{cmd}` requires `-Zscript`
AI-assisted analysis of rust-lang/cargo@0e07a15537 (2026-08-06).
Data as JSON: /data/errors/12d13371fd171c95.json.
Report an issue: GitHub.