Skip to content

Commit 7571e92

Browse files
committed
Auto merge of #14715 - chentodd:error_message_fix, r=epage
fix: add source replacement info when no matching package found ### What does this PR try to resolve? Fixes #14697 I updated error message in `resolver/errors` with [Registry::describe_source](https://doc.rust-lang.org/nightly/nightly-rustc/cargo/core/registry/trait.Registry.html#tymethod.describe_source) - Use message from `Registry::describe_source` as default message - If the message is empty, use `SourceId` message and I also updated related test cases. ### How should we test and review this PR? 1. Run following test cases: - resolver-tests - source_replacement - build - directory - local_registry - offline - package - publish - registry - update 2. Run [cargo-bug.zip](https://github.com/user-attachments/files/17493272/cargo-bug.zip) - Unpack the zip archive - Run `cargo build` in the root following message should be seen: ``` error: no matching package named `ahasha` found location searched: directory source `/home/foo/Desktop/cargo-bug/vendor` (which is replacing registry `crates-io`) required by package `example v0.1.0 (/home/foo/Desktop/cargo-bug)` ```
2 parents 1a2df63 + 11f84a9 commit 7571e92

File tree

10 files changed

+90
-43
lines changed

10 files changed

+90
-43
lines changed

src/cargo/core/resolver/errors.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,13 @@ pub(super) fn activation_error(
353353
));
354354
msg.push('\n');
355355
}
356-
msg.push_str(&format!("location searched: {}\n", dep.source_id()));
356+
357+
let mut location_searched_msg = registry.describe_source(dep.source_id());
358+
if location_searched_msg.is_empty() {
359+
location_searched_msg = format!("{}", dep.source_id());
360+
}
361+
362+
msg.push_str(&format!("location searched: {}\n", location_searched_msg));
357363
msg.push_str("required by ");
358364
msg.push_str(&describe_path_in_context(
359365
resolver_ctx,

tests/testsuite/build.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6043,7 +6043,7 @@ fn avoid_dev_deps() {
60436043
.with_stderr_data(str![[r#"
60446044
[UPDATING] `dummy-registry` index
60456045
[ERROR] no matching package named `baz` found
6046-
location searched: registry `crates-io`
6046+
location searched: `dummy-registry` index (which is replacing registry `crates-io`)
60476047
required by package `bar v0.1.0 ([ROOT]/foo)`
60486048
60496049
"#]])

tests/testsuite/directory.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ Caused by:
199199
no matching package found
200200
searched package name: `baz`
201201
perhaps you meant: bar or foo
202-
location searched: registry `crates-io`
202+
location searched: directory source `[ROOT]/index` (which is replacing registry `crates-io`)
203203
required by package `bar v0.1.0`
204204
205205
"#]])
@@ -283,7 +283,7 @@ fn not_there() {
283283
.with_status(101)
284284
.with_stderr_data(str![[r#"
285285
[ERROR] no matching package named `bar` found
286-
location searched: registry `crates-io`
286+
location searched: directory source `[ROOT]/index` (which is replacing registry `crates-io`)
287287
required by package `foo v0.1.0 ([ROOT]/foo)`
288288
289289
"#]])

tests/testsuite/local_registry.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ fn not_found() {
101101
.with_status(101)
102102
.with_stderr_data(str![[r#"
103103
[ERROR] no matching package named `baz` found
104-
location searched: registry `crates-io`
104+
location searched: `[ROOT]/registry` index (which is replacing registry `crates-io`)
105105
required by package `foo v0.0.1 ([ROOT]/foo)`
106106
107107
"#]])

tests/testsuite/offline.rs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ fn cargo_compile_offline_not_try_update() {
184184
.with_status(101)
185185
.with_stderr_data(str![[r#"
186186
[ERROR] no matching package named `not_cached_dep` found
187-
location searched: registry `crates-io`
187+
location searched: crates.io index
188188
required by package `bar v0.1.0 ([ROOT]/bar)`
189189
As a reminder, you're using offline mode (--offline) which can sometimes cause surprising resolution failures, if this error is too confusing you may wish to retry without the offline flag.
190190
@@ -195,7 +195,7 @@ As a reminder, you're using offline mode (--offline) which can sometimes cause s
195195
p.change_file(".cargo/config.toml", "net.offline = true");
196196
p.cargo("check").with_status(101).with_stderr_data(str![[r#"
197197
[ERROR] no matching package named `not_cached_dep` found
198-
location searched: registry `crates-io`
198+
location searched: crates.io index
199199
required by package `bar v0.1.0 ([ROOT]/bar)`
200200
As a reminder, you're using offline mode (--offline) which can sometimes cause surprising resolution failures, if this error is too confusing you may wish to retry without the offline flag.
201201
@@ -383,18 +383,16 @@ fn update_offline_not_cached() {
383383
)
384384
.file("src/main.rs", "fn main() {}")
385385
.build();
386+
386387
p.cargo("update --offline")
387388
.with_status(101)
388-
.with_stderr_data(
389-
"\
389+
.with_stderr_data(str![["
390390
[ERROR] no matching package named `bar` found
391-
location searched: registry `crates-io`
391+
location searched: [..]
392392
required by package `foo v0.0.1 ([ROOT]/foo)`
393-
As a reminder, you're using offline mode (--offline) which can sometimes cause \
394-
surprising resolution failures, if this error is too confusing you may wish to \
395-
retry without the offline flag.
396-
",
397-
)
393+
As a reminder, you're using offline mode (--offline) which can sometimes cause surprising resolution failures, if this error is too confusing you may wish to retry without the offline flag.
394+
395+
"]])
398396
.run();
399397
}
400398

@@ -756,7 +754,7 @@ fn main(){
756754
.with_stderr_data(
757755
str![[r#"
758756
[ERROR] no matching package named `present_dep` found
759-
location searched: registry `crates-io`
757+
location searched: `dummy-registry` index (which is replacing registry `crates-io`)
760758
required by package `foo v0.1.0 ([ROOT]/foo)`
761759
As a reminder, you're using offline mode (--offline) which can sometimes cause surprising resolution failures, if this error is too confusing you may wish to retry without the offline flag.
762760

tests/testsuite/package.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5312,7 +5312,7 @@ fn workspace_with_local_deps() {
53125312
53135313
Caused by:
53145314
no matching package named `level2` found
5315-
location searched: registry `crates-io`
5315+
location searched: crates.io index
53165316
required by package `level1 v0.0.1 ([ROOT]/foo/level1)`
53175317
53185318
"#]])
@@ -5496,7 +5496,7 @@ fn workspace_with_local_deps_packaging_one_fails() {
54965496
54975497
Caused by:
54985498
no matching package named `level2` found
5499-
location searched: registry `crates-io`
5499+
location searched: crates.io index
55005500
required by package `level1 v0.0.1 ([ROOT]/foo/target/package/level1-0.0.1)`
55015501
55025502
"#]])
@@ -5523,7 +5523,7 @@ fn workspace_with_local_deps_packaging_one_fails_nightly() {
55235523
55245524
Caused by:
55255525
no matching package named `level2` found
5526-
location searched: registry `crates-io`
5526+
location searched: crates.io index
55275527
required by package `level1 v0.0.1 ([ROOT]/foo/target/package/level1-0.0.1)`
55285528
55295529
"#]])
@@ -5589,7 +5589,7 @@ fn workspace_with_local_deps_packaging_one_bin_fails() {
55895589
55905590
Caused by:
55915591
no matching package named `level2` found
5592-
location searched: registry `crates-io`
5592+
location searched: crates.io index
55935593
required by package `level1 v0.0.1 ([ROOT]/foo/level1)`
55945594
55955595
"#]])
@@ -5809,7 +5809,7 @@ fn workspace_with_local_deps_index_mismatch() {
58095809
58105810
Caused by:
58115811
no matching package named `level2` found
5812-
location searched: registry `crates-io`
5812+
location searched: crates.io index
58135813
required by package `level1 v0.0.1 ([ROOT]/foo/level1)`
58145814
58155815
"#]])
@@ -6620,7 +6620,7 @@ fn unpublishable_dependency() {
66206620
66216621
Caused by:
66226622
no matching package named `dep` found
6623-
location searched: registry `alternative`
6623+
location searched: `alternative` index
66246624
required by package `main v0.0.1 ([ROOT]/foo/main)`
66256625
66266626
"#]])

tests/testsuite/publish.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3932,7 +3932,7 @@ fn workspace_missing_dependency() {
39323932
39333933
Caused by:
39343934
no matching package named `a` found
3935-
location searched: registry `crates-io`
3935+
location searched: crates.io index
39363936
required by package `b v0.0.1 ([ROOT]/foo/target/package/b-0.0.1)`
39373937
39383938
"#]])

tests/testsuite/registry.rs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ fn nonexistent_http() {
167167
nonexistent(str![[r#"
168168
[UPDATING] `dummy-registry` index
169169
[ERROR] no matching package named `nonexistent` found
170-
location searched: registry `crates-io`
170+
location searched: `dummy-registry` index (which is replacing registry `crates-io`)
171171
required by package `foo v0.0.1 ([ROOT]/foo)`
172172
173173
"#]]);
@@ -178,7 +178,7 @@ fn nonexistent_git() {
178178
nonexistent(str![[r#"
179179
[UPDATING] `dummy-registry` index
180180
[ERROR] no matching package named `nonexistent` found
181-
location searched: registry `crates-io`
181+
location searched: `dummy-registry` index (which is replacing registry `crates-io`)
182182
required by package `foo v0.0.1 ([ROOT]/foo)`
183183
184184
"#]]);
@@ -218,7 +218,7 @@ fn wrong_case_http() {
218218
[ERROR] no matching package found
219219
searched package name: `Init`
220220
perhaps you meant: init
221-
location searched: registry `crates-io`
221+
location searched: `dummy-registry` index (which is replacing registry `crates-io`)
222222
required by package `foo v0.0.1 ([ROOT]/foo)`
223223
224224
"#]]);
@@ -231,7 +231,7 @@ fn wrong_case_git() {
231231
[ERROR] no matching package found
232232
searched package name: `Init`
233233
perhaps you meant: init
234-
location searched: registry `crates-io`
234+
location searched: `dummy-registry` index (which is replacing registry `crates-io`)
235235
required by package `foo v0.0.1 ([ROOT]/foo)`
236236
237237
"#]]);
@@ -272,7 +272,7 @@ fn mis_hyphenated_http() {
272272
[ERROR] no matching package found
273273
searched package name: `mis_hyphenated`
274274
perhaps you meant: mis-hyphenated
275-
location searched: registry `crates-io`
275+
location searched: `dummy-registry` index (which is replacing registry `crates-io`)
276276
required by package `foo v0.0.1 ([ROOT]/foo)`
277277
278278
"#]]);
@@ -285,7 +285,7 @@ fn mis_hyphenated_git() {
285285
[ERROR] no matching package found
286286
searched package name: `mis_hyphenated`
287287
perhaps you meant: mis-hyphenated
288-
location searched: registry `crates-io`
288+
location searched: `dummy-registry` index (which is replacing registry `crates-io`)
289289
required by package `foo v0.0.1 ([ROOT]/foo)`
290290
291291
"#]]);
@@ -468,7 +468,7 @@ fn update_registry_http() {
468468
str![[r#"
469469
[UPDATING] `dummy-registry` index
470470
[ERROR] no matching package named `notyet` found
471-
location searched: registry `crates-io`
471+
location searched: `dummy-registry` index (which is replacing registry `crates-io`)
472472
required by package `foo v0.0.1 ([ROOT]/foo)`
473473
474474
"#]],
@@ -491,7 +491,7 @@ fn update_registry_git() {
491491
str![[r#"
492492
[UPDATING] `dummy-registry` index
493493
[ERROR] no matching package named `notyet` found
494-
location searched: registry `crates-io`
494+
location searched: `dummy-registry` index (which is replacing registry `crates-io`)
495495
required by package `foo v0.0.1 ([ROOT]/foo)`
496496
497497
"#]],
@@ -551,7 +551,7 @@ fn package_with_path_deps_http() {
551551
552552
Caused by:
553553
no matching package named `notyet` found
554-
location searched: registry `crates-io`
554+
location searched: `dummy-registry` index (which is replacing registry `crates-io`)
555555
required by package `foo v0.0.1 ([ROOT]/foo)`
556556
557557
"#]],
@@ -580,7 +580,7 @@ fn package_with_path_deps_git() {
580580
581581
Caused by:
582582
no matching package named `notyet` found
583-
location searched: registry `crates-io`
583+
location searched: `dummy-registry` index (which is replacing registry `crates-io`)
584584
required by package `foo v0.0.1 ([ROOT]/foo)`
585585
586586
"#]],
@@ -925,7 +925,7 @@ fn yanks_in_lockfiles_are_ok_http() {
925925
str![[r#"
926926
[UPDATING] `dummy-registry` index
927927
[ERROR] no matching package named `bar` found
928-
location searched: registry `crates-io`
928+
location searched: `dummy-registry` index (which is replacing registry `crates-io`)
929929
required by package `foo v0.0.1 ([ROOT]/foo)`
930930
931931
"#]],
@@ -942,7 +942,7 @@ fn yanks_in_lockfiles_are_ok_git() {
942942
str![[r#"
943943
[UPDATING] `dummy-registry` index
944944
[ERROR] no matching package named `bar` found
945-
location searched: registry `crates-io`
945+
location searched: `dummy-registry` index (which is replacing registry `crates-io`)
946946
required by package `foo v0.0.1 ([ROOT]/foo)`
947947
948948
"#]],
@@ -994,7 +994,7 @@ fn yanks_in_lockfiles_are_ok_for_other_update_http() {
994994
str![[r#"
995995
[UPDATING] `dummy-registry` index
996996
[ERROR] no matching package named `bar` found
997-
location searched: registry `crates-io`
997+
location searched: `dummy-registry` index (which is replacing registry `crates-io`)
998998
required by package `foo v0.0.1 ([ROOT]/foo)`
999999
10001000
"#]],
@@ -1017,7 +1017,7 @@ fn yanks_in_lockfiles_are_ok_for_other_update_git() {
10171017
str![[r#"
10181018
[UPDATING] `dummy-registry` index
10191019
[ERROR] no matching package named `bar` found
1020-
location searched: registry `crates-io`
1020+
location searched: `dummy-registry` index (which is replacing registry `crates-io`)
10211021
required by package `foo v0.0.1 ([ROOT]/foo)`
10221022
10231023
"#]],
@@ -3226,7 +3226,7 @@ fn unknown_index_version_error() {
32263226
.with_stderr_data(str![[r#"
32273227
[UPDATING] `dummy-registry` index
32283228
[ERROR] no matching package named `bar` found
3229-
location searched: registry `crates-io`
3229+
location searched: `dummy-registry` index (which is replacing registry `crates-io`)
32303230
required by package `foo v0.1.0 ([ROOT]/foo)`
32313231
32323232
"#]])
@@ -3769,7 +3769,7 @@ foo v0.1.0 ([ROOT]/foo)
37693769
.with_stderr_data(str![[r#"
37703770
[UPDATING] `dummy-registry` index
37713771
[ERROR] no matching package named `bar` found
3772-
location searched: registry `crates-io`
3772+
location searched: `dummy-registry` index (which is replacing registry `crates-io`)
37733773
required by package `foo v0.1.0 ([ROOT]/foo)`
37743774
37753775
"#]])
@@ -3863,7 +3863,7 @@ fn not_found_permutations() {
38633863
.with_stderr_data(str![[r#"
38643864
[UPDATING] `dummy-registry` index
38653865
[ERROR] no matching package named `a-b_c` found
3866-
location searched: registry `crates-io`
3866+
location searched: `dummy-registry` index (which is replacing registry `crates-io`)
38673867
required by package `foo v0.0.1 ([ROOT]/foo)`
38683868
38693869
"#]])

tests/testsuite/source_replacement.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,3 +294,46 @@ fn source_replacement_with_registry_url() {
294294
"#]])
295295
.run();
296296
}
297+
298+
#[cargo_test]
299+
fn source_replacement_with_no_package_in_directoy() {
300+
let p = project()
301+
.file(
302+
"Cargo.toml",
303+
r#"
304+
[package]
305+
name = "foo"
306+
version = "0.1.0"
307+
edition = "2021"
308+
309+
[dependencies]
310+
bar = { version = "^0.8.9" }
311+
"#,
312+
)
313+
.file("src/lib.rs", "")
314+
.build();
315+
316+
let root = paths::root();
317+
t!(fs::create_dir(&root.join("vendor")));
318+
319+
let crates_io = setup_replacement(&format!(
320+
r#"
321+
[source.crates-io]
322+
replace-with = "vendored-sources"
323+
324+
[source.vendored-sources]
325+
directory = "vendor"
326+
"#
327+
));
328+
329+
p.cargo("build")
330+
.replace_crates_io(crates_io.index_url())
331+
.with_status(101)
332+
.with_stderr_data(str![[r#"
333+
[ERROR] no matching package named `bar` found
334+
location searched: directory source `[ROOT]/vendor` (which is replacing registry `crates-io`)
335+
required by package `foo v0.1.0 ([ROOT]/foo)`
336+
337+
"#]])
338+
.run();
339+
}

tests/testsuite/update.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,7 @@ perhaps a crate was updated and forgotten to be re-vendored?
456456
.with_stderr_data(str![[r#"
457457
[UPDATING] `dummy-registry` index
458458
[ERROR] no matching package named `serde` found
459-
location searched: registry `crates-io`
459+
location searched: `dummy-registry` index (which is replacing registry `crates-io`)
460460
required by package `bar v0.0.1 ([ROOT]/foo)`
461461
462462
"#]])
@@ -997,7 +997,7 @@ Caused by:
997997
.with_stderr_data(str![[r#"
998998
[UPDATING] `dummy-registry` index
999999
[ERROR] no matching package named `bar` found
1000-
location searched: registry `crates-io`
1000+
location searched: `dummy-registry` index (which is replacing registry `crates-io`)
10011001
required by package `foo v0.1.0 ([ROOT]/foo)`
10021002
10031003
"#]])
@@ -1017,7 +1017,7 @@ required by package `foo v0.1.0 ([ROOT]/foo)`
10171017
.with_stderr_data(str![[r#"
10181018
[UPDATING] `dummy-registry` index
10191019
[ERROR] no matching package named `bar` found
1020-
location searched: registry `crates-io`
1020+
location searched: `dummy-registry` index (which is replacing registry `crates-io`)
10211021
required by package `foo v0.1.0 ([ROOT]/foo)`
10221022
10231023
"#]])

0 commit comments

Comments
 (0)