rust-lang/cargo · error · anyhow::Error

`cargo run` does not support glob patterns on target selecti

Error message

`cargo run` does not support glob patterns on target selection

What it means

`cargo run` can launch exactly one executable, so it refuses glob target selectors (e.g. `--bin 'foo*'`). The guard at cargo_run.rs:20 short-circuits before any target resolution when options.filter.contains_glob_patterns() is true.

Source

Thrown at src/ops/cargo_run.rs:20

use std::fmt::Write as _;
use std::iter;
use std::path::Path;

use crate::compiler::UnitOutput;
use crate::ops;
use crate::util::CargoResult;
use crate::workspace::MaybePackage;
use crate::workspace::{TargetKind, Workspace};

pub fn run(
    ws: &Workspace<'_>,
    options: &ops::CompileOptions,
    args: &[OsString],
) -> CargoResult<()> {
    let gctx = ws.gctx();

    if options.filter.contains_glob_patterns() {
        anyhow::bail!("`cargo run` does not support glob patterns on target selection")
    }

    // We compute the `bins` here *just for diagnosis*. The actual set of
    // packages to be run is determined by the `ops::compile` call below.
    let packages = options.spec.get_packages(ws)?;
    let bins: Vec<_> = packages
        .into_iter()
        .flat_map(|pkg| {
            iter::repeat(pkg).zip(pkg.manifest().targets().iter().filter(|target| {
                !target.is_lib()
                    && !target.is_custom_build()
                    && if !options.filter.is_specific() {
                        target.is_bin()
                    } else {
                        options.filter.target_run(target)
                    }
            }))
        })

View on GitHub (pinned to 0e07a15537)

Solutions

  1. Replace the glob with a concrete target name: `cargo run --bin app_cli`.
  2. If you need to run several binaries, invoke `cargo run` once per binary with an explicit --bin.
  3. Set `default-run` in Cargo.toml so you can omit --bin entirely.

Example fix

# before
$ cargo run --bin 'app_*'
error: `cargo run` does not support glob patterns on target selection

# after
$ cargo run --bin app_cli
Defensive patterns

Strategy: validation

Validate before calling

# Reject glob metacharacters in run target args:
case "$*" in
  *[*?\[\]]*) echo "cargo run does not support glob patterns" >&2; exit 1;;
esac
cargo run "$@"

Prevention

When it happens

Trigger: Passing a glob pattern to `cargo run`, such as `cargo run --bin 'app_*'` or `cargo run --example 't_*'`.

Common situations: Assuming `cargo run` supports the same glob syntax as `cargo build`/`cargo test`; shell expansions producing a pattern.

Related errors


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