Skip to content

Commit 7ba6e49

Browse files
committed
Auto merge of #7934 - ehuss:query-error-context, r=Eh2406
Provide extra context on a query failure. This adds error context when a query fails, primarily to tell you which parent package included the dependency that failed. For example, imagine deep within your dependency graph you have a `git` dependency, and it fails to download. The current error doesn't tell you where in the graph that `git` dependency was included. I also slightly tweaked the `failed to load source` error message. I felt like the existing wording could be misinterpreted that it was an error loading the dependency *for* the given package. I felt like there were multiple ways to interpret it, so I tried to simplify it to avoid that possibility.
2 parents ab2b2c0 + 1eca786 commit 7ba6e49

14 files changed

+164
-41
lines changed

src/cargo/core/registry.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -256,8 +256,7 @@ impl<'cfg> PackageRegistry<'cfg> {
256256
self.ensure_loaded(dep.source_id(), Kind::Normal)
257257
.chain_err(|| {
258258
anyhow::format_err!(
259-
"failed to load source for a dependency \
260-
on `{}`",
259+
"failed to load source for dependency `{}`",
261260
dep.package_name()
262261
)
263262
})?;
@@ -517,8 +516,7 @@ impl<'cfg> Registry for PackageRegistry<'cfg> {
517516
self.ensure_loaded(dep.source_id(), Kind::Normal)
518517
.chain_err(|| {
519518
anyhow::format_err!(
520-
"failed to load source for a dependency \
521-
on `{}`",
519+
"failed to load source for dependency `{}`",
522520
dep.package_name()
523521
)
524522
})?;

src/cargo/core/resolver/dep_cache.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@ use std::rc::Rc;
1616
use log::debug;
1717

1818
use crate::core::interning::InternedString;
19+
use crate::core::resolver::context::Context;
20+
use crate::core::resolver::errors::describe_path;
1921
use crate::core::{Dependency, FeatureValue, PackageId, PackageIdSpec, Registry, Summary};
20-
use crate::util::errors::CargoResult;
22+
use crate::util::errors::{CargoResult, CargoResultExt};
2123

2224
use crate::core::resolver::types::{ConflictReason, DepInfo, FeaturesSet};
2325
use crate::core::resolver::{ActivateResult, ResolveOpts};
@@ -197,6 +199,7 @@ impl<'a> RegistryQueryer<'a> {
197199
/// next obvious question.
198200
pub fn build_deps(
199201
&mut self,
202+
cx: &Context,
200203
parent: Option<PackageId>,
201204
candidate: &Summary,
202205
opts: &ResolveOpts,
@@ -220,7 +223,13 @@ impl<'a> RegistryQueryer<'a> {
220223
let mut deps = deps
221224
.into_iter()
222225
.map(|(dep, features)| {
223-
let candidates = self.query(&dep)?;
226+
let candidates = self.query(&dep).chain_err(|| {
227+
anyhow::format_err!(
228+
"failed to get `{}` as a dependency of {}",
229+
dep.package_name(),
230+
describe_path(&cx.parents.path_to_bottom(&candidate.package_id())),
231+
)
232+
})?;
224233
Ok((dep, candidates, features))
225234
})
226235
.collect::<CargoResult<Vec<DepInfo>>>()?;

src/cargo/core/resolver/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -656,7 +656,7 @@ fn activate(
656656

657657
let now = Instant::now();
658658
let (used_features, deps) =
659-
&*registry.build_deps(parent.map(|p| p.0.package_id()), &candidate, &opts)?;
659+
&*registry.build_deps(cx, parent.map(|p| p.0.package_id()), &candidate, &opts)?;
660660

661661
// Record what list of features is active for this package.
662662
if !used_features.is_empty() {

tests/testsuite/bad_config.rs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,10 @@ fn bad_git_dependency() {
368368
.with_stderr(
369369
"\
370370
[UPDATING] git repository `file:///`
371-
[ERROR] failed to load source for a dependency on `foo`
371+
[ERROR] failed to get `foo` as a dependency of package `foo v0.0.0 [..]`
372+
373+
Caused by:
374+
failed to load source for dependency `foo`
372375
373376
Caused by:
374377
Unable to update file:///
@@ -901,7 +904,10 @@ fn bad_source_config2() {
901904
.with_status(101)
902905
.with_stderr(
903906
"\
904-
error: failed to load source for a dependency on `bar`
907+
[ERROR] failed to get `bar` as a dependency of package `foo v0.0.0 [..]`
908+
909+
Caused by:
910+
failed to load source for dependency `bar`
905911
906912
Caused by:
907913
Unable to update registry `https://[..]`
@@ -944,7 +950,10 @@ fn bad_source_config3() {
944950
.with_status(101)
945951
.with_stderr(
946952
"\
947-
error: failed to load source for a dependency on `bar`
953+
[ERROR] failed to get `bar` as a dependency of package `foo v0.0.0 [..]`
954+
955+
Caused by:
956+
failed to load source for dependency `bar`
948957
949958
Caused by:
950959
Unable to update registry `https://[..]`
@@ -989,7 +998,10 @@ fn bad_source_config4() {
989998
.with_status(101)
990999
.with_stderr(
9911000
"\
992-
error: failed to load source for a dependency on `bar`
1001+
[ERROR] failed to get `bar` as a dependency of package `foo v0.0.0 ([..])`
1002+
1003+
Caused by:
1004+
failed to load source for dependency `bar`
9931005
9941006
Caused by:
9951007
Unable to update registry `https://[..]`

tests/testsuite/cargo_features.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,10 @@ fn nightly_feature_requires_nightly_in_dep() {
199199
.with_status(101)
200200
.with_stderr(
201201
"\
202-
error: failed to load source for a dependency on `a`
202+
[ERROR] failed to get `a` as a dependency of package `b v0.0.1 ([..])`
203+
204+
Caused by:
205+
failed to load source for dependency `a`
203206
204207
Caused by:
205208
Unable to update [..]

tests/testsuite/directory.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -652,7 +652,10 @@ fn git_override_requires_lockfile() {
652652
.with_status(101)
653653
.with_stderr(
654654
"\
655-
error: failed to load source for a dependency on `git`
655+
[ERROR] failed to get `git` as a dependency of package `foo v0.0.1 ([..])`
656+
657+
Caused by:
658+
failed to load source for dependency `git`
656659
657660
Caused by:
658661
Unable to update [..]

tests/testsuite/git.rs

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -931,7 +931,10 @@ fn dep_with_bad_submodule() {
931931
let expected = format!(
932932
"\
933933
[UPDATING] git repository [..]
934-
[ERROR] failed to load source for a dependency on `dep1`
934+
[ERROR] failed to get `dep1` as a dependency of package `foo v0.5.0 [..]`
935+
936+
Caused by:
937+
failed to load source for dependency `dep1`
935938
936939
Caused by:
937940
Unable to update {}
@@ -2382,20 +2385,24 @@ fn invalid_git_dependency_manifest() {
23822385
.cargo("build")
23832386
.with_status(101)
23842387
.with_stderr(&format!(
2385-
"[UPDATING] git repository `{}`\n\
2386-
error: failed to load source for a dependency on `dep1`\n\
2387-
\n\
2388-
Caused by:\n \
2389-
Unable to update {}\n\
2390-
\n\
2391-
Caused by:\n \
2392-
failed to parse manifest at `[..]`\n\
2393-
\n\
2394-
Caused by:\n \
2395-
could not parse input as TOML\n\
2396-
\n\
2397-
Caused by:\n \
2398-
duplicate key: `categories` for key `project` at line 10 column 17",
2388+
"\
2389+
[UPDATING] git repository `{}`
2390+
[ERROR] failed to get `dep1` as a dependency of package `foo v0.5.0 ([..])`
2391+
2392+
Caused by:
2393+
failed to load source for dependency `dep1`
2394+
2395+
Caused by:
2396+
Unable to update {}
2397+
2398+
Caused by:
2399+
failed to parse manifest at `[..]`
2400+
2401+
Caused by:
2402+
could not parse input as TOML
2403+
2404+
Caused by:
2405+
duplicate key: `categories` for key `project` at line 10 column 17",
23992406
path2url(&git_root),
24002407
path2url(&git_root),
24012408
))

tests/testsuite/git_auth.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,10 @@ fn http_auth_offered() {
133133
.with_stderr_contains(&format!(
134134
"\
135135
[UPDATING] git repository `http://{addr}/foo/bar`
136-
[ERROR] failed to load source for a dependency on `bar`
136+
[ERROR] failed to get `bar` as a dependency of package `foo v0.0.1 [..]`
137+
138+
Caused by:
139+
failed to load source for dependency `bar`
137140
138141
Caused by:
139142
Unable to update http://{addr}/foo/bar

tests/testsuite/local_registry.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,10 @@ fn invalid_dir_bad() {
360360
.with_status(101)
361361
.with_stderr(
362362
"\
363-
[ERROR] failed to load source for a dependency on `bar`
363+
[ERROR] failed to get `bar` as a dependency of package `foo v0.0.1 [..]`
364+
365+
Caused by:
366+
failed to load source for dependency `bar`
364367
365368
Caused by:
366369
Unable to update registry `https://[..]`

tests/testsuite/offline.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,10 @@ fn cargo_compile_forbird_git_httpsrepo_offline() {
270270
.build();
271271

272272
p.cargo("build --offline").with_status(101).with_stderr("\
273-
error: failed to load source for a dependency on `dep1`
273+
[ERROR] failed to get `dep1` as a dependency of package `foo v0.5.0 [..]`
274+
275+
Caused by:
276+
failed to load source for dependency `dep1`
274277
275278
Caused by:
276279
Unable to update https://github.com/some_user/dep1.git

tests/testsuite/path.rs

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -521,7 +521,10 @@ fn error_message_for_missing_manifest() {
521521
.with_status(101)
522522
.with_stderr(
523523
"\
524-
[ERROR] failed to load source for a dependency on `bar`
524+
[ERROR] failed to get `bar` as a dependency of package `foo v0.5.0 [..]`
525+
526+
Caused by:
527+
failed to load source for dependency `bar`
525528
526529
Caused by:
527530
Unable to update [CWD]/src/bar
@@ -1017,3 +1020,66 @@ fn workspace_produces_rlib() {
10171020
assert!(p.root().join("target/debug/libtop.rlib").is_file());
10181021
assert!(!p.root().join("target/debug/libfoo.rlib").is_file());
10191022
}
1023+
1024+
#[cargo_test]
1025+
fn deep_path_error() {
1026+
// Test for an error loading a path deep in the dependency graph.
1027+
let p = project()
1028+
.file(
1029+
"Cargo.toml",
1030+
r#"
1031+
[package]
1032+
name = "foo"
1033+
version = "0.1.0"
1034+
[dependencies]
1035+
a = {path="a"}
1036+
"#,
1037+
)
1038+
.file("src/lib.rs", "")
1039+
.file(
1040+
"a/Cargo.toml",
1041+
r#"
1042+
[package]
1043+
name = "a"
1044+
version = "0.1.0"
1045+
[dependencies]
1046+
b = {path="../b"}
1047+
"#,
1048+
)
1049+
.file("a/src/lib.rs", "")
1050+
.file(
1051+
"b/Cargo.toml",
1052+
r#"
1053+
[package]
1054+
name = "b"
1055+
version = "0.1.0"
1056+
[dependencies]
1057+
c = {path="../c"}
1058+
"#,
1059+
)
1060+
.file("b/src/lib.rs", "")
1061+
.build();
1062+
1063+
p.cargo("check")
1064+
.with_status(101)
1065+
.with_stderr(
1066+
"\
1067+
[ERROR] failed to get `c` as a dependency of package `b v0.1.0 [..]`
1068+
... which is depended on by `a v0.1.0 [..]`
1069+
... which is depended on by `foo v0.1.0 [..]`
1070+
1071+
Caused by:
1072+
failed to load source for dependency `c`
1073+
1074+
Caused by:
1075+
Unable to update [..]/foo/c
1076+
1077+
Caused by:
1078+
failed to read `[..]/foo/c/Cargo.toml`
1079+
1080+
Caused by:
1081+
[..]
1082+
",
1083+
)
1084+
.run();
1085+
}

tests/testsuite/registry.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1558,7 +1558,10 @@ fn disallow_network() {
15581558
.with_status(101)
15591559
.with_stderr(
15601560
"\
1561-
error: failed to load source for a dependency on `foo`
1561+
[ERROR] failed to get `foo` as a dependency of package `bar v0.5.0 [..]`
1562+
1563+
Caused by:
1564+
failed to load source for dependency `foo`
15621565
15631566
Caused by:
15641567
Unable to update registry [..]

tests/testsuite/replace.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -544,7 +544,10 @@ fn override_wrong_name() {
544544
"\
545545
[UPDATING] [..] index
546546
[UPDATING] git repository [..]
547-
error: no matching package for override `[..]baz:0.1.0` found
547+
[ERROR] failed to get `baz` as a dependency of package `foo v0.0.1 ([..])`
548+
549+
Caused by:
550+
no matching package for override `[..]baz:0.1.0` found
548551
location searched: file://[..]
549552
version required: = 0.1.0
550553
",
@@ -588,7 +591,10 @@ fn override_with_nothing() {
588591
"\
589592
[UPDATING] [..] index
590593
[UPDATING] git repository [..]
591-
[ERROR] failed to load source for a dependency on `bar`
594+
[ERROR] failed to get `bar` as a dependency of package `foo v0.0.1 ([..])`
595+
596+
Caused by:
597+
failed to load source for dependency `bar`
592598
593599
Caused by:
594600
Unable to update file://[..]
@@ -671,7 +677,10 @@ fn multiple_specs() {
671677
"\
672678
[UPDATING] [..] index
673679
[UPDATING] git repository [..]
674-
error: overlapping replacement specifications found:
680+
[ERROR] failed to get `bar` as a dependency of package `foo v0.0.1 ([..])`
681+
682+
Caused by:
683+
overlapping replacement specifications found:
675684
676685
* [..]
677686
* [..]

tests/testsuite/workspaces.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2203,7 +2203,7 @@ fn ws_warn_path() {
22032203

22042204
#[cargo_test]
22052205
fn invalid_missing() {
2206-
// Warnings include path to manifest.
2206+
// Make sure errors are not suppressed with -q.
22072207
let p = project()
22082208
.file(
22092209
"Cargo.toml",
@@ -2223,16 +2223,20 @@ fn invalid_missing() {
22232223
.with_status(101)
22242224
.with_stderr(
22252225
"\
2226-
error: [..]
2226+
[ERROR] failed to get `x` as a dependency of package `foo v0.1.0 [..]`
22272227
22282228
Caused by:
2229-
[..]
2229+
failed to load source for dependency `x`
22302230
22312231
Caused by:
2232-
[..]
2232+
Unable to update [..]/foo/x
22332233
22342234
Caused by:
2235-
[..]",
2235+
failed to read `[..]foo/x/Cargo.toml`
2236+
2237+
Caused by:
2238+
[..]
2239+
",
22362240
)
22372241
.run();
22382242
}

0 commit comments

Comments
 (0)