Skip to content

Conversation

sadmac7000
Copy link

@sadmac7000 sadmac7000 commented Sep 15, 2025

What does this PR try to resolve?

This change considers public dependencies when adding a new dependency. For example, if you depend on foo, which depends publicly on bar 1.0, and you run cargo add bar, you will now get bar 1.0 even if bar 2.0 is available.

Fixes #13038

How to test and review this PR?

The test suite has been updated with an example scenario. More tests might be warrented to see how this interacts with other features though.

@rustbot rustbot added A-manifest Area: Cargo.toml issues Command-add S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Sep 15, 2025
@rustbot
Copy link
Collaborator

rustbot commented Sep 15, 2025

r? @epage

rustbot has assigned @epage.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Comment on lines 477 to 497
} else {
let latest =
get_latest_dependency(spec, &dependency, honor_rust_version, gctx, registry)?;

if dependency.name != latest.name {
gctx.shell().warn(format!(
"translating `{}` to `{}`",
dependency.name, latest.name,
))?;
dependency.name = latest.name; // Normalize the name
let (package_set, resolve) = resolve_ws(ws, true)?;
let public_source = if spec
.manifest()
.unstable_features()
.require(Feature::public_dependency())
.is_ok()
{
get_public_dependency(
manifest,
ws,
section,
gctx,
&dependency,
package_set,
resolve,
)
} else {
None
};
if let Some((registry, public_source)) = public_source {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this belong here or in get_existing_dependency?

Copy link
Author

@sadmac7000 sadmac7000 Sep 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since fuzzy_lookup calls that several times in a loop it's hard to know where to put the resolve_ws call. Could introduce some state outside the callback but it'll be a bit messy.

The new method get_dependencies doesn't take a dep key but instead
returns all deps. We'll sometimes need this in coming patches, and when
we don't need it it's easy to have the caller filter.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By resolving before Adding, the output becomes unclear where the locking ends and the manifest editing begins

Since this is unstable, we could possibly punt on this

Comment on lines +562 to +570
let pkg_ids_and_reqs = resolve.deps(dep_pkgid).filter_map(|(id, deps)| {
deps.iter()
.find(|dep| {
dep.is_public()
&& dep.kind() == DepKind::Normal
&& dep.package_name() == dependency.name.as_str()
})
.map(|dep| (id, dep.version_req().clone()))
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This appear to only go 1 deep for public dependencies but they can be recursive

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-manifest Area: Cargo.toml issues Command-add S-waiting-on-review Status: Awaiting review from the assignee but also interested parties.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

cargo add should check public transitive dependencies when auto-picking a version
3 participants