{"record":{"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":null,"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/eb98b54bc9f3c74519f43d066cb3fd02ebc88df0/src/ops/cargo_pkgid.rs#L1-L16","documentation":"Thrown by `cargo pkgid` when no `Cargo.lock` file exists in the workspace. The `pkgid` command resolves a PackageIdSpec by querying the dependency graph from the lockfile, so a lockfile is a hard prerequisite.","triggerScenarios":"Running `cargo pkgid` (or `cargo pkgid <spec>`) in a workspace that has never been built or had its lockfile generated, or where `Cargo.lock` was deleted.","commonSituations":"Fresh checkout where `cargo build` has not been run; a library crate where `Cargo.lock` is gitignored and not present; running `cargo pkgid` before any dependency-resolving command.","solutions":["Run `cargo generate-lockfile` or `cargo fetch` to create Cargo.lock first.","Run `cargo build` which generates the lockfile as a side effect.","Ensure Cargo.lock is not deleted by CI or build scripts."],"exampleFix":"# Generate the lockfile, then run pkgid\ncargo generate-lockfile\ncargo pkgid","handlingStrategy":"validation","validationCode":"use std::path::Path;\nfn ensure_lockfile_exists(ws_root: &Path) -> Result<(), String> {\n    let lockfile = ws_root.join(\"Cargo.lock\");\n    if !lockfile.is_file() {\n        return Err(\"Cargo.lock not found; run `cargo generate-lockfile` first\".into());\n    }\n    Ok(())\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Run `cargo fetch` or `cargo build` before `cargo pkgid`.","Do not delete Cargo.lock in application projects.","Check for Cargo.lock existence in scripts before calling `cargo pkgid`."],"tags":["cargo-pkgid","lockfile","missing-file"],"backgroundTag":null,"analyzedSha":"eb98b54bc9f3c74519f43d066cb3fd02ebc88df0","analyzedAt":"2026-08-11T17:42:36.556Z","contentChangedAt":"2026-08-11T17:42:36.556Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}