Skip to content

Commit 543a138

Browse files
committed
(Unify workspace and package fuzzy dependency lookup)
1 parent c7890fa commit 543a138

File tree

1 file changed

+38
-37
lines changed

1 file changed

+38
-37
lines changed

src/cargo/ops/cargo_add/mod.rs

Lines changed: 38 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -364,23 +364,8 @@ fn resolve_dependency(
364364
};
365365
selected_dep = populate_dependency(selected_dep, arg);
366366

367-
let mut old_dep = get_existing_dependency(manifest, selected_dep.toml_key(), section)?;
368-
if old_dep.is_none() && selected_dep.source().is_none() && selected_dep.rename().is_none() {
369-
for name_permutation in [
370-
selected_dep.name.replace('-', "_"),
371-
selected_dep.name.replace('_', "-"),
372-
] {
373-
old_dep = get_existing_dependency(manifest, &name_permutation, section)?;
374-
if old_dep.is_some() {
375-
gctx.shell().warn(format!(
376-
"translating `{}` to `{}`",
377-
selected_dep.name, &name_permutation,
378-
))?;
379-
selected_dep.name = name_permutation;
380-
break;
381-
}
382-
}
383-
}
367+
let lookup = |dep_key: &_| get_existing_dependency(manifest, dep_key, section);
368+
let old_dep = fuzzy_lookup(&mut selected_dep, lookup, gctx)?;
384369

385370
let mut dependency = if let Some(mut old_dep) = old_dep.clone() {
386371
if old_dep.name != selected_dep.name {
@@ -403,26 +388,8 @@ fn resolve_dependency(
403388
if dependency.source().is_none() {
404389
// Checking for a workspace dependency happens first since a member could be specified
405390
// in the workspace dependencies table as a dependency
406-
let mut workspace_dep = find_workspace_dep(dependency.toml_key(), ws.root_manifest()).ok();
407-
if workspace_dep.is_none() && dependency.rename.is_none() {
408-
for name_permutation in [
409-
dependency.name.replace('-', "_"),
410-
dependency.name.replace('_', "-"),
411-
] {
412-
workspace_dep = find_workspace_dep(&name_permutation, ws.root_manifest()).ok();
413-
if let Some(workspace_dep) = &workspace_dep {
414-
if let Some(Source::Registry(_source)) = &workspace_dep.source {
415-
gctx.shell().warn(format!(
416-
"translating `{}` to `{}`",
417-
dependency.name, &name_permutation,
418-
))?;
419-
dependency.name = name_permutation;
420-
break;
421-
}
422-
}
423-
}
424-
}
425-
if let Some(_dep) = workspace_dep {
391+
let lookup = |toml_key: &_| Ok(find_workspace_dep(toml_key, ws.root_manifest()).ok());
392+
if let Some(_dep) = fuzzy_lookup(&mut dependency, lookup, gctx)? {
426393
dependency = dependency.set_source(WorkspaceSource::new());
427394
} else if let Some(package) = ws.members().find(|p| p.name().as_str() == dependency.name) {
428395
// Only special-case workspaces when the user doesn't provide any extra
@@ -488,6 +455,40 @@ fn resolve_dependency(
488455
Ok(dependency)
489456
}
490457

458+
fn fuzzy_lookup(
459+
dependency: &mut Dependency,
460+
lookup: impl Fn(&str) -> CargoResult<Option<Dependency>>,
461+
gctx: &GlobalContext,
462+
) -> CargoResult<Option<Dependency>> {
463+
if let Some(rename) = dependency.rename() {
464+
return lookup(rename);
465+
}
466+
467+
for name_permutation in [
468+
dependency.name.clone(),
469+
dependency.name.replace('-', "_"),
470+
dependency.name.replace('_', "-"),
471+
] {
472+
let Some(dep) = lookup(&name_permutation)? else {
473+
continue;
474+
};
475+
476+
if dependency.name != name_permutation {
477+
if !matches!(dep.source, Some(Source::Registry(_))) {
478+
continue;
479+
}
480+
gctx.shell().warn(format!(
481+
"translating `{}` to `{}`",
482+
dependency.name, &name_permutation,
483+
))?;
484+
dependency.name = name_permutation;
485+
}
486+
return Ok(Some(dep));
487+
}
488+
489+
Ok(None)
490+
}
491+
491492
/// When { workspace = true } you cannot define other keys that configure
492493
/// the source of the dependency such as `version`, `registry`, `registry-index`,
493494
/// `path`, `git`, `branch`, `tag`, `rev`, or `package`. You can also not define

0 commit comments

Comments
 (0)