Skip to content

Commit 7e5a250

Browse files
committed
fix(recursive packages): Inspect the recursive packages for path dependencies
1 parent 7c7cfeb commit 7e5a250

File tree

3 files changed

+135
-4
lines changed

3 files changed

+135
-4
lines changed

src/cargo/core/resolver/errors.rs

Lines changed: 84 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::task::Poll;
55

66
use crate::core::{Dependency, PackageId, Registry, Shell, SourceId, Summary};
77
use crate::sources::source::QueryKind;
8-
use crate::sources::{IndexSummary, PathSource};
8+
use crate::sources::{IndexSummary, PathSource, RecursivePathSource};
99
use crate::util::edit_distance::{closest, edit_distance};
1010
use crate::util::errors::CargoResult;
1111
use crate::util::{GlobalContext, OptVersionReq, VersionExt};
@@ -420,7 +420,7 @@ pub(super) fn activation_error(
420420

421421
let _ = writeln!(
422422
&mut msg,
423-
"required by {}",
423+
"note: required by {}",
424424
describe_path_in_context(resolver_ctx, &parent.package_id()),
425425
);
426426

@@ -434,6 +434,41 @@ pub(super) fn activation_error(
434434
let _ = writeln!(&mut msg, "no package name is provided");
435435
}
436436
}
437+
438+
let requested = dep.package_name().as_str();
439+
let recursive_package = inspect_recursive_packages(
440+
&mut msg,
441+
Path::new(&path),
442+
global_context,
443+
sid,
444+
requested,
445+
);
446+
447+
match recursive_package {
448+
Some(name) => {
449+
let _ = writeln!(
450+
&mut msg,
451+
"no matching package named `{}` found at `{}`",
452+
name.name,
453+
dep.package_name()
454+
);
455+
456+
let _ = writeln!(
457+
&mut msg,
458+
"note: required by {}",
459+
describe_path_in_context(resolver_ctx, &parent.package_id()),
460+
);
461+
462+
let _ = writeln!(
463+
&mut msg,
464+
"help: package `{}` exists at `{}`",
465+
name.name, name.name
466+
);
467+
}
468+
None => {
469+
let _ = writeln!(&mut msg, "no recursive package name is found");
470+
}
471+
}
437472
} else {
438473
let _ = writeln!(
439474
&mut msg,
@@ -650,3 +685,50 @@ fn inspect_root_package(
650685

651686
Some(package_info)
652687
}
688+
689+
#[derive(Debug)]
690+
struct RecursivePackageInfo {
691+
name: String,
692+
path: PathBuf,
693+
}
694+
695+
fn inspect_recursive_packages(
696+
msg: &mut String,
697+
path: &Path,
698+
gctx: &GlobalContext,
699+
sid: SourceId,
700+
requested: &str,
701+
) -> Option<RecursivePackageInfo> {
702+
let mut rps = RecursivePathSource::new(path, sid, gctx);
703+
704+
if let Err(e) = rps.load() {
705+
msg.push_str(&e.to_string());
706+
msg.push('\n');
707+
}
708+
709+
let pkgs = rps
710+
.read_packages()
711+
.expect("failed to read the packages recursively");
712+
713+
for pkg in pkgs {
714+
if pkg.name() == requested {
715+
let manifest = pkg.manifest_path();
716+
let pkg_dir = manifest
717+
.parent()
718+
.expect("failed to take the parent path")
719+
.to_path_buf();
720+
721+
let package_info = RecursivePackageInfo {
722+
name: pkg.name().to_string(),
723+
path: pkg_dir,
724+
};
725+
726+
return Some(package_info);
727+
// } else {
728+
// let list = rps.list_files(&pkg).unwrap();
729+
// println!("{:?}", list);
730+
}
731+
}
732+
733+
None
734+
}

tests/testsuite/build.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1278,8 +1278,9 @@ fn cargo_compile_with_dep_name_mismatch() {
12781278
.with_status(101)
12791279
.with_stderr_data(str![[r#"
12801280
[ERROR] no matching package named `bar` found at `notquitebar`
1281-
required by package `foo v0.0.1 ([ROOT]/foo)`
1281+
[NOTE] required by package `foo v0.0.1 ([ROOT]/foo)`
12821282
[HELP] package `bar` exists at `bar`
1283+
no recursive package name is found
12831284
location searched: [ROOT]/foo/bar
12841285
required by package `foo v0.0.1 ([ROOT]/foo)`
12851286

tests/testsuite/path.rs

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1956,11 +1956,59 @@ fn invalid_package_name_in_path() {
19561956
.with_status(101)
19571957
.with_stderr_data(str![[r#"
19581958
[ERROR] no matching package named `bar` found at `definitely_not_bar`
1959-
required by package `foo v0.5.0 ([ROOT]/foo)`
1959+
[NOTE] required by package `foo v0.5.0 ([ROOT]/foo)`
19601960
[HELP] package `bar` exists at `bar`
1961+
no recursive package name is found
19611962
location searched: [ROOT]/foo/crates/bar
19621963
required by package `foo v0.5.0 ([ROOT]/foo)`
19631964
19641965
"#]])
19651966
.run();
19661967
}
1968+
1969+
#[cargo_test]
1970+
fn invalid_package_in_subdirectory() {
1971+
let p = project()
1972+
.file(
1973+
"Cargo.toml",
1974+
r#"
1975+
[package]
1976+
name = "foo"
1977+
version = "0.5.0"
1978+
edition = "2015"
1979+
authors = []
1980+
1981+
[workspace]
1982+
1983+
[dependencies]
1984+
definitely_not_bar = { path = "crates/bar" }
1985+
"#,
1986+
)
1987+
.file("src/lib.rs", "")
1988+
.file(
1989+
"crates/bar/definitely_not_bar/Cargo.toml",
1990+
r#"
1991+
[package]
1992+
name = "definitely_not_bar"
1993+
version = "0.5.0"
1994+
edition = "2015"
1995+
authors = []
1996+
"#,
1997+
)
1998+
.file("crates/bar/definitely_not_bar/src/lib.rs", "")
1999+
.build();
2000+
2001+
p.cargo("generate-lockfile")
2002+
.with_status(101)
2003+
.with_stderr_data(str![[r#"
2004+
[ERROR] failed to load manifest for dependency `definitely_not_bar`
2005+
2006+
Caused by:
2007+
failed to read `[ROOT]/foo/crates/bar/Cargo.toml`
2008+
2009+
Caused by:
2010+
[NOT_FOUND]
2011+
2012+
"#]])
2013+
.run();
2014+
}

0 commit comments

Comments
 (0)