diff --git a/src/cargo/ops/cargo_add/mod.rs b/src/cargo/ops/cargo_add/mod.rs index a46c61daa892..3637f9bc30c3 100644 --- a/src/cargo/ops/cargo_add/mod.rs +++ b/src/cargo/ops/cargo_add/mod.rs @@ -403,7 +403,26 @@ fn resolve_dependency( if dependency.source().is_none() { // Checking for a workspace dependency happens first since a member could be specified // in the workspace dependencies table as a dependency - if let Some(_dep) = find_workspace_dep(dependency.toml_key(), ws.root_manifest()).ok() { + let mut workspace_dep = find_workspace_dep(dependency.toml_key(), ws.root_manifest()).ok(); + if workspace_dep.is_none() && dependency.rename.is_none() { + for name_permutation in [ + dependency.name.replace('-', "_"), + dependency.name.replace('_', "-"), + ] { + workspace_dep = find_workspace_dep(&name_permutation, ws.root_manifest()).ok(); + if let Some(workspace_dep) = &workspace_dep { + if let Some(Source::Registry(_source)) = &workspace_dep.source { + gctx.shell().warn(format!( + "translating `{}` to `{}`", + dependency.name, &name_permutation, + ))?; + dependency.name = name_permutation; + break; + } + } + } + } + if let Some(_dep) = workspace_dep { dependency = dependency.set_source(WorkspaceSource::new()); } else if let Some(package) = ws.members().find(|p| p.name().as_str() == dependency.name) { // Only special-case workspaces when the user doesn't provide any extra diff --git a/tests/testsuite/cargo_add/detect_workspace_inherit_fuzzy/out/primary/Cargo.toml b/tests/testsuite/cargo_add/detect_workspace_inherit_fuzzy/out/primary/Cargo.toml index 1ae5f7b63618..22737f576822 100644 --- a/tests/testsuite/cargo_add/detect_workspace_inherit_fuzzy/out/primary/Cargo.toml +++ b/tests/testsuite/cargo_add/detect_workspace_inherit_fuzzy/out/primary/Cargo.toml @@ -4,4 +4,4 @@ version = "0.0.0" edition = "2015" [dependencies] -fuzzy_dependency = "1.0.0" +fuzzy_dependency.workspace = true diff --git a/tests/testsuite/cargo_add/detect_workspace_inherit_fuzzy/stderr.term.svg b/tests/testsuite/cargo_add/detect_workspace_inherit_fuzzy/stderr.term.svg index e5e452f3b051..b9cd1a406608 100644 --- a/tests/testsuite/cargo_add/detect_workspace_inherit_fuzzy/stderr.term.svg +++ b/tests/testsuite/cargo_add/detect_workspace_inherit_fuzzy/stderr.term.svg @@ -19,11 +19,11 @@ - Updating `dummy-registry` index + warning: translating `fuzzy-dependency` to `fuzzy_dependency` - warning: translating `fuzzy-dependency` to `fuzzy_dependency` + Updating `dummy-registry` index - Adding fuzzy_dependency v1.0.0 to dependencies + Adding fuzzy_dependency (workspace) to dependencies Locking 2 packages to latest compatible versions