{"record":{"id":"b4f4683b8d981fc3","repo":"rust-lang/cargo","slug":"package-spec-requires-a-spec-format-value-w","errorCode":null,"errorMessage":"\"--package <SPEC>\" requires a SPEC format value, which can be any package ID specifier in the dependency graph.\nRun `cargo help pkgid` for more information about SPEC format.","messagePattern":"\"--package <SPEC>\" requires a SPEC format value, which can be any package ID specifier in the dependency graph\\.\nRun `cargo help pkgid` for more information about SPEC format\\.","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/util/command_prelude.rs","lineNumber":865,"sourceCode":"                self._values_of(\"example\"),\n                self.flag(\"examples\"),\n                self._values_of(\"bench\"),\n                self.flag(\"benches\"),\n                self.flag(\"all-targets\"),\n            ),\n            target_rustdoc_args: None,\n            target_rustc_args: None,\n            target_rustc_crate_types: None,\n            rustdoc_document_private_items: false,\n            honor_rust_version: self.honor_rust_version(),\n        };\n\n        if let Some(ws) = workspace {\n            self.check_optional_opts(ws, &opts)?;\n        } else if self.is_present_with_zero_values(\"package\") {\n            // As for cargo 0.50.0, this won't occur but if someone sneaks in\n            // we can still provide this informative message for them.\n            anyhow::bail!(\n                \"\\\"--package <SPEC>\\\" requires a SPEC format value, \\\n                which can be any package ID specifier in the dependency graph.\\n\\\n                Run `cargo help pkgid` for more information about SPEC format.\"\n            )\n        }\n\n        Ok(opts)\n    }\n\n    fn cli_features(&self) -> CargoResult<CliFeatures> {\n        CliFeatures::from_command_line(\n            &self._values_of(\"features\"),\n            self.flag(\"all-features\"),\n            !self.flag(\"no-default-features\"),\n        )\n    }\n\n    fn compile_options_for_single_package(","sourceCodeStart":847,"sourceCodeEnd":883,"githubUrl":"https://github.com/rust-lang/cargo/blob/98a09e7e7d62850f14e5b6132101fc1edd19a16f/src/util/command_prelude.rs#L847-L883","documentation":"When no Workspace is bound and the `--package` / `-p` flag is present with a zero/empty value, Cargo cannot resolve a SPEC and bails with guidance to consult `cargo help pkgid`. The guard covers a degenerate CLI state that newer clap prevents but older callers could still hit.","triggerScenarios":"Invoking a subcommand with `--package` passed an empty string or no positional value, in a code path where workspace is None (is_present_with_zero_values(\"package\") is true).","commonSituations":"Shell script interpolating an unset variable: `cargo build -p \"$PKG\"` with PKG empty; a wrapper tool forwarding a malformed argv; a clap value_parser regression letting empty through.","solutions":["Pass a concrete package name or SPEC: `cargo build -p my-crate`.","Fix the shell to guard against empty variables: `${PKG:?--package value required}`.","Run from inside the workspace so workspace-based resolution applies instead of the zero-value branch."],"exampleFix":"# before\n# cargo build -p \"$PKG\"   # PKG unset -> empty value\n\n# after\n# PKG=\"${PKG:?missing package name}\"\n# cargo build -p \"$PKG\"","handlingStrategy":"validation","validationCode":"fn require_package_spec(pkg: Option<&str>) -> Result<&str, String> {\n    match pkg {\n        Some(s) if !s.is_empty() => Ok(s),\n        _ => Err(\"--package requires a non-empty SPEC; see `cargo help pkgid`\".into()),\n    }\n}\n\n// before building the cargo invocation:\n// let spec = require_package_spec(pkg_opt)?;","typeGuard":"fn is_non_empty_spec(s: Option<&str>) -> bool {\n    matches!(s, Some(v) if !v.is_empty())\n}","tryCatchPattern":null,"preventionTips":["In shell scripts, default-expand package vars with `${PKG:?--package value required}`.","Validate CLI argv in wrappers before forwarding to cargo.","Prefer running cargo from inside the workspace so workspace-based resolution is used."],"tags":["cli","arguments","package-selection"],"backgroundTag":null,"analyzedSha":"98a09e7e7d62850f14e5b6132101fc1edd19a16f","analyzedAt":"2026-08-11T17:42:36.556Z","contentChangedAt":"2026-08-11T17:42:36.556Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}