diff --git a/src/cargo/core/resolver/errors.rs b/src/cargo/core/resolver/errors.rs index 15bb496dbc2..f7da257cb47 100644 --- a/src/cargo/core/resolver/errors.rs +++ b/src/cargo/core/resolver/errors.rs @@ -1,10 +1,11 @@ use std::fmt; use std::fmt::Write as _; +use std::path::Path; use std::task::Poll; -use crate::core::{Dependency, PackageId, Registry, Summary}; -use crate::sources::IndexSummary; +use crate::core::{Dependency, PackageId, Registry, SourceId, Summary}; use crate::sources::source::QueryKind; +use crate::sources::{IndexSummary, PathSource, RecursivePathSource}; use crate::util::edit_distance::{closest, edit_distance}; use crate::util::errors::CargoResult; use crate::util::{GlobalContext, OptVersionReq, VersionExt}; @@ -13,6 +14,90 @@ use anyhow::Error; use super::context::ResolverContext; use super::types::{ConflictMap, ConflictReason}; +fn debug_source_path(msg: &mut String, path: &Path, gctx: &GlobalContext, sid: SourceId) { + let mut ps = PathSource::new(path, sid, &gctx); + + match ps.root_package() { + Ok(pkg) => { + msg.push_str("Found package: "); + msg.push_str(pkg.name().as_str()); + msg.push('\n'); + } + Err(e) => { + msg.push_str("Err: "); + msg.push_str(&e.to_string()); + msg.push('\n'); + } + } + + match ps.load() { + Ok(_) => { + msg.push_str("Loaded package information\n"); + } + Err(e) => { + msg.push_str("Err: "); + msg.push_str(&e.to_string()); + msg.push('\n'); + } + } + + match ps.read_package() { + Ok(pkg) => { + msg.push_str("Read package: "); + msg.push_str(pkg.name().as_str()); + msg.push('\n'); + } + Err(e) => { + msg.push_str("Err: "); + msg.push_str(&e.to_string()); + msg.push('\n'); + } + } +} + +fn debug_recursive_source(msg: &mut String, path: &Path, gctx: &GlobalContext, sid: SourceId) { + let mut rps = RecursivePathSource::new(path, sid, &gctx); + + match rps.load() { + Ok(_) => msg.push_str("Loaded package information\n"), + Err(e) => { + msg.push_str("Err: "); + msg.push_str(&e.to_string()); + msg.push('\n'); + } + } + + match rps.read_packages() { + Ok(pkgs) => { + for p in pkgs { + msg.push_str("found package: "); + msg.push_str(&p.name()); + msg.push('\n'); + + match rps.list_files(&p) { + Ok(files) => { + for f in files { + msg.push_str(" "); + msg.push_str(&f.to_string_lossy()); + msg.push('\n'); + } + } + Err(e) => { + msg.push_str("Err: "); + msg.push_str(&e.to_string()); + msg.push('\n'); + } + } + } + } + Err(e) => { + msg.push_str("Err: "); + msg.push_str(&e.to_string()); + msg.push('\n'); + } + } +} + /// Error during resolution providing a path of `PackageId`s. pub struct ResolveError { cause: Error, @@ -385,6 +470,14 @@ pub(super) fn activation_error( }); let _ = writeln!(&mut msg, "perhaps you meant: {suggestions}"); } else { + let sid = dep.source_id(); + let path = dep.source_id().url().to_file_path().unwrap(); + + if let Some(gctx) = gctx { + debug_source_path(&mut msg, &path.as_path(), &gctx, sid); + debug_recursive_source(&mut msg, &path.as_path(), &gctx, sid); + } + let _ = writeln!( &mut msg, "no matching package named `{}` found", diff --git a/src/cargo/sources/path.rs b/src/cargo/sources/path.rs index 8e754e03fa9..bd90debaad0 100644 --- a/src/cargo/sources/path.rs +++ b/src/cargo/sources/path.rs @@ -123,7 +123,7 @@ impl<'gctx> PathSource<'gctx> { Ok(()) } - fn read_package(&self) -> CargoResult { + pub fn read_package(&self) -> CargoResult { let path = self.path.join("Cargo.toml"); let pkg = ops::read_package(&path, self.source_id, self.gctx)?; Ok(pkg) diff --git a/tests/testsuite/path.rs b/tests/testsuite/path.rs index 118c55409fc..3ace5bfd3e3 100644 --- a/tests/testsuite/path.rs +++ b/tests/testsuite/path.rs @@ -1919,3 +1919,151 @@ foo v1.0.0 ([ROOT]/foo) "#]]) .run(); } + +#[cargo_test] +fn invalid_package_name_in_path() { + let p = project() + .file( + "Cargo.toml", + r#" + [package] + name = "foo" + version = "0.5.0" + edition = "2015" + authors = [] + + [workspace] + + [dependencies] + definitely_not_bar = { path = "crates/bar" } + "#, + ) + .file("src/lib.rs", "") + .file( + "crates/bar/Cargo.toml", + r#" + [package] + name = "bar" + version = "0.5.0" + edition = "2015" + authors = [] + "#, + ) + .file("crates/bar/src/lib.rs", "") + .build(); + + p.cargo("generate-lockfile") + .with_status(101) + .with_stderr_data(str![[r#" +[ERROR] no matching package named `definitely_not_bar` found +location searched: [ROOT]/foo/crates/bar +required by package `foo v0.5.0 ([ROOT]/foo)` + +"#]]) + .run(); +} + +#[cargo_test] +fn invalid_package_in_subdirectory() { + let p = project() + .file( + "Cargo.toml", + r#" + [package] + name = "foo" + version = "0.5.0" + edition = "2015" + authors = [] + + [workspace] + + [dependencies] + definitely_not_bar = { path = "crates/bar" } + "#, + ) + .file("src/lib.rs", "") + .file( + "crates/bar/definitely_not_bar/Cargo.toml", + r#" + [package] + name = "definitely_not_bar" + version = "0.5.0" + edition = "2015" + authors = [] + "#, + ) + .file("crates/bar/definitely_not_bar/src/lib.rs", "") + .build(); + + p.cargo("generate-lockfile") + .with_status(101) + .with_stderr_data(str![[r#" +[ERROR] failed to load manifest for dependency `definitely_not_bar` + +Caused by: + failed to read `[ROOT]/foo/crates/bar/Cargo.toml` + +Caused by: + [NOT_FOUND] + +"#]]) + .run(); +} + +#[cargo_test] +fn invalid_manifest_in_path() { + let p = project() + .file( + "Cargo.toml", + r#" + [package] + name = "foo" + version = "0.5.0" + edition = "2015" + authors = [] + + [workspace] + + [dependencies] + definitely_not_bar = { path = "crates/bar" } + "#, + ) + .file("src/lib.rs", "") + .file( + "crates/bar/alice/Cargo.toml", + r#" + [package] + name = "alice" + version = "0.5.0" + edition = "2015" + authors = [] + "#, + ) + .file("crates/bar/alice/src/lib.rs", "") + .file( + "crates/bar/bob/Cargo.toml", + r#" + [package] + name = "bob" + version = "0.5.0" + edition = "2015" + authors = [] + "#, + ) + .file("crates/bar/bob/src/lib.rs", "") + .build(); + + p.cargo("generate-lockfile") + .with_status(101) + .with_stderr_data(str![[r#" +[ERROR] failed to load manifest for dependency `definitely_not_bar` + +Caused by: + failed to read `[ROOT]/foo/crates/bar/Cargo.toml` + +Caused by: + [NOT_FOUND] + +"#]]) + .run(); +}