Skip to content

Commit

Permalink
Lookup translated names in workspace deps
Browse files Browse the repository at this point in the history
  • Loading branch information
dohse committed Apr 17, 2024
1 parent 6fc8c25 commit 72af7aa
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 21 deletions.
45 changes: 29 additions & 16 deletions src/cargo/ops/cargo_add/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,24 +364,28 @@ fn resolve_dependency(
};
selected_dep = populate_dependency(selected_dep, arg);

let old_dep = get_existing_dependency(manifest, selected_dep.toml_key(), section)?;
let mut dependency = if let Some(mut old_dep) = old_dep.clone() {
if old_dep.name != selected_dep.name {
// Assuming most existing keys are not relevant when the package changes
if selected_dep.optional.is_none() {
selected_dep.optional = old_dep.optional;
let get_dependency = |mut selected_dep: Dependency| {
let old_dep = get_existing_dependency(manifest, selected_dep.toml_key(), section)?;
let dependency = if let Some(mut old_dep) = old_dep.clone() {
if old_dep.name != selected_dep.name {
// Assuming most existing keys are not relevant when the package changes
if selected_dep.optional.is_none() {
selected_dep.optional = old_dep.optional;
}
selected_dep
} else {
if selected_dep.source().is_some() {
// Overwrite with `crate_spec`
old_dep.source = selected_dep.source;
}
populate_dependency(old_dep, arg)
}
selected_dep
} else {
if selected_dep.source().is_some() {
// Overwrite with `crate_spec`
old_dep.source = selected_dep.source;
}
populate_dependency(old_dep, arg)
}
} else {
selected_dep
selected_dep
};
anyhow::Ok((old_dep, dependency))
};
let (mut old_dep, mut dependency) = get_dependency(selected_dep)?;

if dependency.source().is_none() {
// Checking for a workspace dependency happens first since a member could be specified
Expand All @@ -403,14 +407,23 @@ fn resolve_dependency(
let latest =
get_latest_dependency(spec, &dependency, honor_rust_version, gctx, registry)?;

dependency = dependency.set_source(latest.source.expect("latest always has a source"));

if dependency.name != latest.name {
gctx.shell().warn(format!(
"translating `{}` to `{}`",
dependency.name, latest.name,
))?;
dependency.name = latest.name; // Normalize the name
(old_dep, dependency) = get_dependency(dependency.clone())?;

// Re-check workspace dependencies table with normalized name
if let Some(_dep) =
find_workspace_dep(dependency.toml_key(), ws.root_manifest()).ok()
{
dependency = dependency.set_source(WorkspaceSource::new());
}
}
dependency = dependency.set_source(latest.source.expect("latest always has a source"));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ version = "0.0.0"
edition = "2015"

[dependencies]
fuzzy_dependency = "1.0.0"
fuzzy_dependency.workspace = true
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion tests/testsuite/cargo_add/features_fuzzy/out/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ version = "0.0.0"
edition = "2015"

[dependencies]
your_face = { version = "99999.0.0" }
your_face = { version = "99999.0.0", features = ["eyes"] }
4 changes: 2 additions & 2 deletions tests/testsuite/cargo_add/features_fuzzy/stderr.term.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 72af7aa

Please sign in to comment.