{"id":"36dc5c8fedd937e5","repo":"rust-lang/cargo","slug":"cannot-specify-a-git-url-url-with-a-version","errorCode":null,"errorMessage":"cannot specify a git URL (`{url}`) with a version (`{v}`).","messagePattern":"cannot specify a git URL \\(`(.+?)`\\) with a version \\(`(.+?)`\\)\\.","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/ops/cargo_add/mod.rs","lineNumber":371,"sourceCode":"        .as_deref()\n        .map(CrateSpec::resolve)\n        .transpose()?;\n    let mut selected_dep = if let Some(url) = &arg.git {\n        let mut src = GitSource::new(url);\n        if let Some(branch) = &arg.branch {\n            src = src.set_branch(branch);\n        }\n        if let Some(tag) = &arg.tag {\n            src = src.set_tag(tag);\n        }\n        if let Some(rev) = &arg.rev {\n            src = src.set_rev(rev);\n        }\n\n        let selected = if let Some(crate_spec) = &crate_spec {\n            if let Some(v) = crate_spec.version_req() {\n                // crate specifier includes a version (e.g. `docopt@0.8`)\n                anyhow::bail!(\"cannot specify a git URL (`{url}`) with a version (`{v}`).\");\n            }\n            let dependency = crate_spec.to_dependency()?.set_source(src);\n            let selected = select_package(&dependency, gctx, registry)?;\n            if dependency.name != selected.name {\n                gctx.shell().warn(format!(\n                    \"translating `{}` to `{}`\",\n                    dependency.name, selected.name,\n                ))?;\n            }\n            selected\n        } else {\n            let source = crate::sources::GitSource::new(src.source_id()?, gctx)?;\n            let packages = source.read_packages()?;\n            let package = infer_package_for_git_source(packages, &src)?;\n            Dependency::from(package.summary())\n        };\n        selected\n    } else if let Some(raw_path) = &arg.path {","sourceCodeStart":353,"sourceCodeEnd":389,"githubUrl":"https://github.com/rust-lang/cargo/blob/0e07a155371a6ce88ae53a2c00df940280c09a67/src/ops/cargo_add/mod.rs#L353-L389","documentation":"`cargo add` does not allow combining a git source with an inline version requirement. The crate specifier (e.g. `docopt@0.8`) cannot carry a version when `--git <url>` is also given, because for a git source the version is determined by the repository checkout, not by a registry version requirement. The bail fires at mod.rs:371 when `crate_spec.version_req()` is `Some` while `arg.git` is set.","triggerScenarios":"`cargo add --git https://example/repo.git mycrate@1.2` — the `@1.2` portion becomes a `version_req` and conflicts with the git source. Also reachable via programmatic `DepOp { crate_spec: Some(\"x@1.0\"), git: Some(url), .. }`.","commonSituations":"Users assuming version pins work with git sources like they do with registry crates, or copy-pasting a registry-style spec next to `--git`.","solutions":["Remove the `@version` from the crate spec: `cargo add --git <url> <crate>`.","If you need a specific revision, pin with `--rev`, `--tag`, or `--branch` instead of a version: `cargo add --git <url> --tag v1.2 <crate>`.","If you actually need a registry version requirement, drop `--git` entirely and use `cargo add <crate>@<version>`."],"exampleFix":"# before\ncargo add --git https://example/repo.git mycrate@1.2\n\n# after\ncargo add --git https://example/repo.git --tag v1.2 mycrate","handlingStrategy":"validation","validationCode":"// A git source and a version req are mutually exclusive; enforce before building DepOp.\nfn build_dep_op(name: &str, git: Option<&str>, ver: Option<&str>) -> Result<DepOp, String> {\n    match (git, ver) {\n        (Some(_), Some(_)) => Err(\"cannot combine --git with a version requirement\".into()),\n        (Some(g), None) => Ok(DepOp { crate_spec: Some(name.into()), git: Some(g.into()), ..Default::default() }),\n        (None, Some(v)) => Ok(DepOp { crate_spec: Some(format!(\"{name}@{v}\")), ..Default::default() }),\n        (None, None) => Ok(DepOp { crate_spec: Some(name.into()), ..Default::default() }),\n    }\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["When pinning a git revision, always use `--rev`/`--tag`/`--branch`, never `@version`.","Centralize DepOp construction in one helper that rejects git+version combos.","Document the source-kind matrix (registry/git/path) in your add wrapper."],"tags":["cargo-add","git","version","conflict"],"analyzedSha":"0e07a155371a6ce88ae53a2c00df940280c09a67","analyzedAt":"2026-08-06T01:46:58.334Z","schemaVersion":2}