{"id":"64e7db0b30271f66","repo":"rust-lang/cargo","slug":"latest-always-has-a-source","errorCode":null,"errorMessage":"latest always has a source","messagePattern":"latest always has a source","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/ops/cargo_add/mod.rs","lineNumber":501,"sourceCode":"        } else if let Some((registry, public_source)) =\n            get_public_dependency(spec, manifest, ws, section, gctx, &dependency)?\n        {\n            if let Some(registry) = registry {\n                dependency = dependency.set_registry(registry);\n            }\n            dependency = dependency.set_source(public_source);\n        } else {\n            let latest =\n                get_latest_dependency(spec, &dependency, honor_rust_version, gctx, registry)?;\n\n            if dependency.name != latest.name {\n                gctx.shell().warn(format!(\n                    \"translating `{}` to `{}`\",\n                    dependency.name, latest.name,\n                ))?;\n                dependency.name = latest.name; // Normalize the name\n            }\n            dependency = dependency.set_source(latest.source.expect(\"latest always has a source\"));\n        }\n    }\n\n    if let Some(Source::Workspace(_)) = dependency.source() {\n        check_invalid_ws_keys(dependency.toml_key(), arg)?;\n    }\n\n    let version_required = dependency.source().and_then(|s| s.as_registry()).is_some();\n    let version_optional_in_section = section.kind() == DepKind::Development;\n    let preserve_existing_version = old_dep\n        .as_ref()\n        .map(|d| d.version().is_some())\n        .unwrap_or(false);\n    if !version_required && !preserve_existing_version && version_optional_in_section {\n        // dev-dependencies do not need the version populated\n        dependency = dependency.clear_version();\n    }\n","sourceCodeStart":483,"sourceCodeEnd":519,"githubUrl":"https://github.com/rust-lang/cargo/blob/0e07a155371a6ce88ae53a2c00df940280c09a67/src/ops/cargo_add/mod.rs#L483-L519","documentation":"This panic is in the cargo add dependency-resolution path. When a dependency has no explicit source (not path, not git, not workspace), cargo calls get_latest_dependency() to query the registry and then does dependency.set_source(latest.source.expect(\"latest always has a source\")). The invariant is that a registry-queried latest dependency always carries a source (the registry source it was found in).","triggerScenarios":"Querying a registry that returns a summary (IndexSummary::Candidate) where the resulting Dependency object has source = None. This would require a bug in how registry query results are converted to Dependency objects, or a registry source that doesn't populate the source field.","commonSituations":"Using a custom/private registry that returns malformed summary data; a cargo version with a bug in the registry-to-Dependency conversion; a sparse registry or vendored source that produces incomplete metadata.","solutions":["Switch to the default crates.io registry to see if the issue is registry-specific.","Specify the dependency source explicitly: cargo add <crate>@<version> or cargo add --path / --git.","Update cargo and the registry index (cargo update --dry-run) to refresh metadata.","Report the issue to the custom registry maintainer if using a private registry."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"// Before cargo add, verify the crate exists and has source metadata\n// Run: cargo search <crate-name> to confirm availability\n// Or check the registry index manually","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always specify the registry explicitly for private crates: cargo add --registry <name> <crate>.","Verify crate existence with cargo search before cargo add.","Keep registry indices up to date: cargo update --dry-run."],"tags":["rust","cargo","panic","invariant","cargo-add","registry","source-resolution"],"analyzedSha":"0e07a155371a6ce88ae53a2c00df940280c09a67","analyzedAt":"2026-08-06T01:46:58.334Z","schemaVersion":2}