Skip to content

Commit e08a813

Browse files
committed
Auto merge of #13281 - LuuuXXX:issue-10729, r=epage
fix(add): Improve error when adding registry packages while vendored ### **What does this PR try to resolve?** When a vendored directory is established, cargo add no longer adds new packages. Instead, it tries to translate a package name into a package that already exists in the vendored directory. [More details](#10729 (comment)) Since `@epage` has done most of the work, here I do the rest of the finishing work. Improves the error from #10729 ### **How should we test and review this PR?** The implementation procedure is as follows: #10729 (comment) Test steps: 1. Try to get an arbitrary crate and execute `cargo vendor` command. 2. Configure the vendor directory in .cargo/config.toml. 3. Add `alter-registry` to the config.toml file. ``` [registries] alter-registry= { index = "XXX" } ``` 4. run the same `cargo add` command. ``` cargo add another-crate --registry alter-registry ```
2 parents 08c173a + 640c077 commit e08a813

File tree

28 files changed

+134
-10
lines changed

28 files changed

+134
-10
lines changed

crates/resolver-tests/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,8 @@ pub fn resolve_with_global_context_raw(
112112
for summary in self.list.iter() {
113113
let matched = match kind {
114114
QueryKind::Exact => dep.matches(summary),
115-
QueryKind::Fuzzy => true,
115+
QueryKind::Alternatives => true,
116+
QueryKind::Normalized => true,
116117
};
117118
if matched {
118119
self.used.insert(summary.package_id());

src/cargo/core/resolver/errors.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ pub(super) fn activation_error(
312312
// Maybe the user mistyped the name? Like `dep-thing` when `Dep_Thing`
313313
// was meant. So we try asking the registry for a `fuzzy` search for suggestions.
314314
let candidates = loop {
315-
match registry.query_vec(&new_dep, QueryKind::Fuzzy) {
315+
match registry.query_vec(&new_dep, QueryKind::Alternatives) {
316316
Poll::Ready(Ok(candidates)) => break candidates,
317317
Poll::Ready(Err(e)) => return to_resolve_err(e),
318318
Poll::Pending => match registry.block_until_ready() {

src/cargo/ops/cargo_add/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -588,7 +588,7 @@ fn get_latest_dependency(
588588
}
589589
MaybeWorkspace::Other(query) => {
590590
let possibilities = loop {
591-
match registry.query_vec(&query, QueryKind::Fuzzy) {
591+
match registry.query_vec(&query, QueryKind::Normalized) {
592592
std::task::Poll::Ready(res) => {
593593
break res?;
594594
}
@@ -711,7 +711,7 @@ fn select_package(
711711
MaybeWorkspace::Other(query) => {
712712
let possibilities = loop {
713713
// Exact to avoid returning all for path/git
714-
match registry.query_vec(&query, QueryKind::Exact) {
714+
match registry.query_vec(&query, QueryKind::Normalized) {
715715
std::task::Poll::Ready(res) => {
716716
break res?;
717717
}
@@ -938,7 +938,7 @@ fn populate_available_features(
938938
}
939939

940940
let possibilities = loop {
941-
match registry.query_vec(&query, QueryKind::Exact) {
941+
match registry.query_vec(&query, QueryKind::Normalized) {
942942
std::task::Poll::Ready(res) => {
943943
break res?;
944944
}

src/cargo/sources/directory.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,8 @@ impl<'gctx> Source for DirectorySource<'gctx> {
108108
let packages = self.packages.values().map(|p| &p.0);
109109
let matches = packages.filter(|pkg| match kind {
110110
QueryKind::Exact => dep.matches(pkg.summary()),
111-
QueryKind::Fuzzy => true,
111+
QueryKind::Alternatives => true,
112+
QueryKind::Normalized => dep.matches(pkg.summary()),
112113
});
113114
for summary in matches.map(|pkg| pkg.summary().clone()) {
114115
f(IndexSummary::Candidate(summary));

src/cargo/sources/path.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -558,7 +558,8 @@ impl<'gctx> Source for PathSource<'gctx> {
558558
for s in self.packages.iter().map(|p| p.summary()) {
559559
let matched = match kind {
560560
QueryKind::Exact => dep.matches(s),
561-
QueryKind::Fuzzy => true,
561+
QueryKind::Alternatives => true,
562+
QueryKind::Normalized => dep.matches(s),
562563
};
563564
if matched {
564565
f(IndexSummary::Candidate(s.clone()))

src/cargo/sources/registry/mod.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -791,7 +791,8 @@ impl<'gctx> Source for RegistrySource<'gctx> {
791791
.query_inner(dep.package_name(), &req, &mut *self.ops, &mut |s| {
792792
let matched = match kind {
793793
QueryKind::Exact => dep.matches(s.as_summary()),
794-
QueryKind::Fuzzy => true,
794+
QueryKind::Alternatives => true,
795+
QueryKind::Normalized => true,
795796
};
796797
if !matched {
797798
return;
@@ -830,7 +831,7 @@ impl<'gctx> Source for RegistrySource<'gctx> {
830831
return Poll::Ready(Ok(()));
831832
}
832833
let mut any_pending = false;
833-
if kind == QueryKind::Fuzzy {
834+
if kind == QueryKind::Alternatives || kind == QueryKind::Normalized {
834835
// Attempt to handle misspellings by searching for a chain of related
835836
// names to the original name. The resolver will later
836837
// reject any candidates that have the wrong name, and with this it'll

src/cargo/sources/source.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,10 @@ pub enum QueryKind {
183183
/// Path/Git sources may return all dependencies that are at that URI,
184184
/// whereas an `Registry` source may return dependencies that have the same
185185
/// canonicalization.
186-
Fuzzy,
186+
Alternatives,
187+
/// Match a denpendency in all ways and will normalize the package name.
188+
/// Each source defines what normalizing means.
189+
Normalized,
187190
}
188191

189192
/// A download status that represents if a [`Package`] has already been
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[source.crates-io]
2+
replace-with = "vendored-sources"
3+
4+
[source.vendored-sources]
5+
directory = "./vendor"
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[workspace]
2+
3+
[package]
4+
name = "cargo-list-test-fixture"
5+
version = "0.0.0"

tests/testsuite/cargo_add/add_no_vendored_package_with_alter_registry/in/src/lib.rs

Whitespace-only changes.

0 commit comments

Comments
 (0)