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

File tree

14 files changed

+164
-41
lines changed

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

0 commit comments

Comments
 (0)