{"id":"961e175dab24b3ae","repo":"rust-lang/cargo","slug":"a-cargo-lock-must-exist-for-this-command","errorCode":null,"errorMessage":"a Cargo.lock must exist for this command","messagePattern":"a Cargo\\.lock must exist for this command","errorType":"exception","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"src/ops/cargo_pkgid.rs","lineNumber":7,"sourceCode":"use crate::ops;\nuse crate::util::CargoResult;\nuse crate::workspace::{PackageIdSpec, PackageIdSpecQuery, Workspace};\n\npub fn pkgid(ws: &Workspace<'_>, spec: Option<&str>) -> CargoResult<PackageIdSpec> {\n    let Some(resolve) = ops::load_pkg_lockfile(ws)? else {\n        anyhow::bail!(\"a Cargo.lock must exist for this command\")\n    };\n\n    let pkgid = match spec {\n        Some(spec) => PackageIdSpec::query_str(spec, resolve.iter())?,\n        None => ws.current()?.package_id(),\n    };\n    Ok(pkgid.to_spec())\n}\n","sourceCodeStart":1,"sourceCodeEnd":16,"githubUrl":"https://github.com/rust-lang/cargo/blob/0e07a155371a6ce88ae53a2c00df940280c09a67/src/ops/cargo_pkgid.rs#L1-L16","documentation":"`cargo pkgid` (and the underlying ops::pkgid) needs the resolved dependency graph stored in Cargo.lock to translate a spec into a canonical package ID. If load_pkg_lockfile returns None, there is nothing to query, so it bails immediately at cargo_pkgid.rs:7.","triggerScenarios":"Running `cargo pkgid [spec]` in a project that has no Cargo.lock yet (no prior build/generate-lockfile), or pointing cargo at a directory whose lockfile was deleted.","commonSituations":"Library crates that do not commit Cargo.lock; fresh clones before first build; running pkgid in a clean temp dir.","solutions":["Generate the lockfile first: `cargo generate-lockfile` (or run any `cargo build`/`cargo fetch`).","Ensure Cargo.lock is committed/restored in the project if reproducibility matters.","If scripting, run `cargo fetch` before invoking `cargo pkgid`."],"exampleFix":"# before\n$ cargo pkgid serde\nerror: a Cargo.lock must exist for this command\n\n# after\n$ cargo generate-lockfile\n$ cargo pkgid serde","handlingStrategy":"validation","validationCode":"# Ensure a lockfile exists before querying pkgid:\n[ -f Cargo.lock ] || cargo generate-lockfile\ncargo pkgid \"$@\"","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Commit Cargo.lock for binaries/tools so pkgid works on a fresh clone.","In scripts, run `cargo fetch` before any `cargo pkgid` call.","Check for Cargo.lock presence before invoking pkgid."],"tags":["lockfile","pkgid","resolve"],"analyzedSha":"0e07a155371a6ce88ae53a2c00df940280c09a67","analyzedAt":"2026-08-06T01:46:58.334Z","schemaVersion":2}