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

  1. Pass exactly one target: `cargo run --bin a`.
  2. Run each binary in a separate `cargo run` invocation.
  3. 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

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


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