From 9c538bb3c7c5dbe4688b0cf8953c711e26f4c84a Mon Sep 17 00:00:00 2001 From: Bilal Khan Date: Wed, 22 Oct 2025 18:36:59 +0500 Subject: [PATCH 1/3] wrote 3 tests about invalid_package_name_path, invalid_package_in_subdirectory and invalid_manifest_in_path made the tests pass by showing the current behaviour added generate-lockfile in the remaining two tests added the 7 spaces in the invalid_base to pass it and it got matched with the test before added 7 spaces to make the test pass --- tests/testsuite/path.rs | 148 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 148 insertions(+) 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(); +} From 84704a0b38be13f2142034a8707ebe41f3b1c02c Mon Sep 17 00:00:00 2001 From: Bilal Khan Date: Wed, 26 Nov 2025 20:12:46 +0500 Subject: [PATCH 2/3] added the logic for debugging the source path and recursive path --- src/cargo/core/resolver/errors.rs | 46 +++++++++++++++++++++++++++++-- src/cargo/sources/path.rs | 2 +- 2 files changed, 45 insertions(+), 3 deletions(-) diff --git a/src/cargo/core/resolver/errors.rs b/src/cargo/core/resolver/errors.rs index 15bb496dbc2..58d4ac50776 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,45 @@ use anyhow::Error; use super::context::ResolverContext; use super::types::{ConflictMap, ConflictReason}; +fn debug_source_path() { + let path = Path::new("/home/ibilalkayy/Documents/rust/testing/fourth"); + let gctx = GlobalContext::default().unwrap(); + let sid = SourceId::for_path(path).unwrap(); + + let mut ps = PathSource::new(path, sid, &gctx); + + match ps.root_package() { + Ok(pkg) => println!("Found package: {}", pkg.name()), + Err(e) => eprintln!("Err: {e:?}"), + } + + let loading = ps.load().expect("Err: failed to load"); + println!("Loading data: {:?}", loading); + + let read_package = ps.read_package().expect("Err: failed to read the package"); + println!("Read package: {:?}", read_package); +} + +fn debug_recursive_source() { + let path = Path::new("/home/ibilalkayy/Documents/rust/testing/fourth"); + let gctx = GlobalContext::default().unwrap(); + let sid = SourceId::for_path(path).unwrap(); + + let mut rps = RecursivePathSource::new(path, sid, &gctx); + + rps.load().expect("Err: failed to load"); + match rps.read_packages() { + Ok(pkgs) => { + for p in pkgs { + println!("found pkg: {}", p.name()); + let files = rps.list_files(&p); + println!("files: {:?}", files.unwrap()); + } + } + Err(e) => eprintln!("err: {e:?}"), + }; +} + /// Error during resolution providing a path of `PackageId`s. pub struct ResolveError { cause: Error, @@ -385,6 +425,8 @@ pub(super) fn activation_error( }); let _ = writeln!(&mut msg, "perhaps you meant: {suggestions}"); } else { + debug_source_path(); + debug_recursive_source(); 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) From 88c8b3fbbe45d50f80ef8fa029a465fa3829dc32 Mon Sep 17 00:00:00 2001 From: Bilal Khan Date: Wed, 3 Dec 2025 19:52:04 +0500 Subject: [PATCH 3/3] didn't use the print statement and not used the manual path --- src/cargo/core/resolver/errors.rs | 99 +++++++++++++++++++++++-------- 1 file changed, 75 insertions(+), 24 deletions(-) diff --git a/src/cargo/core/resolver/errors.rs b/src/cargo/core/resolver/errors.rs index 58d4ac50776..f7da257cb47 100644 --- a/src/cargo/core/resolver/errors.rs +++ b/src/cargo/core/resolver/errors.rs @@ -14,43 +14,88 @@ use anyhow::Error; use super::context::ResolverContext; use super::types::{ConflictMap, ConflictReason}; -fn debug_source_path() { - let path = Path::new("/home/ibilalkayy/Documents/rust/testing/fourth"); - let gctx = GlobalContext::default().unwrap(); - let sid = SourceId::for_path(path).unwrap(); - +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) => println!("Found package: {}", pkg.name()), - Err(e) => eprintln!("Err: {e:?}"), + 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'); + } } - let loading = ps.load().expect("Err: failed to load"); - println!("Loading data: {:?}", loading); + 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'); + } + } - let read_package = ps.read_package().expect("Err: failed to read the package"); - println!("Read package: {:?}", read_package); + 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() { - let path = Path::new("/home/ibilalkayy/Documents/rust/testing/fourth"); - let gctx = GlobalContext::default().unwrap(); - let sid = SourceId::for_path(path).unwrap(); - +fn debug_recursive_source(msg: &mut String, path: &Path, gctx: &GlobalContext, sid: SourceId) { let mut rps = RecursivePathSource::new(path, sid, &gctx); - rps.load().expect("Err: failed to load"); + 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 { - println!("found pkg: {}", p.name()); - let files = rps.list_files(&p); - println!("files: {:?}", files.unwrap()); + 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) => eprintln!("err: {e:?}"), - }; + Err(e) => { + msg.push_str("Err: "); + msg.push_str(&e.to_string()); + msg.push('\n'); + } + } } /// Error during resolution providing a path of `PackageId`s. @@ -425,8 +470,14 @@ pub(super) fn activation_error( }); let _ = writeln!(&mut msg, "perhaps you meant: {suggestions}"); } else { - debug_source_path(); - debug_recursive_source(); + 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",