rust-lang/cargo · error · anyhow::Error
glob patterns on package selection are not supported.
Error message
glob patterns on package selection are not supported.
What it means
compile_options_for_single_package reads the `-p`/`--package` values and checks each with restricted_names::is_glob_pattern. Cargo's package selection by SPEC does not support glob/wildcard characters (`*`, `?`, `[`), so any spec containing them is rejected.
Source
Thrown at src/util/command_prelude.rs:889
fn cli_features(&self) -> CargoResult<CliFeatures> {
CliFeatures::from_command_line(
&self._values_of("features"),
self.flag("all-features"),
!self.flag("no-default-features"),
)
}
fn compile_options_for_single_package(
&self,
gctx: &GlobalContext,
intent: UserIntent,
workspace: Option<&Workspace<'_>>,
profile_checking: ProfileChecking,
) -> CargoResult<CompileOptions> {
let mut compile_opts = self.compile_options(gctx, intent, workspace, profile_checking)?;
let spec = self._values_of("package");
if spec.iter().any(restricted_names::is_glob_pattern) {
anyhow::bail!("glob patterns on package selection are not supported.")
}
compile_opts.spec = Packages::Packages(spec);
Ok(compile_opts)
}
fn new_options(&self, gctx: &GlobalContext) -> CargoResult<NewOptions> {
let vcs = self._value_of("vcs").map(|vcs| match vcs {
"git" => VersionControl::Git,
"hg" => VersionControl::Hg,
"pijul" => VersionControl::Pijul,
"fossil" => VersionControl::Fossil,
"none" => VersionControl::NoVcs,
vcs => panic!("Impossible vcs: {:?}", vcs),
});
NewOptions::new(
vcs,
self.flag("bin"),
self.flag("lib"),View on GitHub (pinned to 0e07a15537)
Solutions
- List each package explicitly: `cargo build -p crate-a -p crate-b`.
- Use `--workspace` to build all packages, or `-p` per crate for a prefix group.
- Quote/escape to avoid the shell leaving literal glob chars, and remove any `*`/`?`/`[` from the SPEC.
Example fix
# before cargo build -p 'my-*' # after cargo build -p my-a -p my-b # or all cargo build --workspace
Defensive patterns
Strategy: validation
Validate before calling
fn clean_package_specs(specs: &[String]) -> Result<Vec<String>, anyhow::Error> {
for s in specs {
if restricted_names::is_glob_pattern(s) {
anyhow::bail!("package spec {s} contains glob characters; list packages explicitly");
}
}
Ok(specs.to_vec())
} Type guard
fn is_valid_package_spec(s: &str) -> bool {
!restricted_names::is_glob_pattern(s) && !s.is_empty()
} Try / catch
match cli.compile_options_for_single_package(...) {
Err(e) if e.to_string().contains("glob patterns") => {
eprintln!("list packages explicitly instead of using globs");
return Err(e);
}
r => r,
} Prevention
- Never put `*`, `?`, or `[` in a `-p` value.
- Quote shell arguments so unmatched globs don't leak literal `*`.
- Use `--workspace` or explicit `-p` repeats for groups.
When it happens
Trigger: Passing a package specifier containing glob metacharacters to `-p`, e.g. `cargo build -p 'my-*'` or `cargo test -p 'crate?[abc]'`.
Common situations: Trying to select multiple crates by a common prefix using a shell glob; copying a glob from another tool (npm, make) into cargo; shell expansion that left a literal `*` because no files matched.
Related errors
- {}package pattern(s) `{}` not found in workspace `{}`
- crate name is empty
- `cargo run` does not support glob pattern `{}` on package se
- --exclude can only be used together with --workspace
- {}package(s) `{}` not found in workspace `{}`
AI-assisted analysis of rust-lang/cargo@0e07a15537 (2026-08-06).
Data as JSON: /data/errors/7b6b71446542939b.json.
Report an issue: GitHub.