Skip to content

Commit a9a8dc1

Browse files
committed
added the functionality to check the root and recursive packages
1 parent df56232 commit a9a8dc1

File tree

1 file changed

+116
-45
lines changed

1 file changed

+116
-45
lines changed

src/cargo/core/resolver/errors.rs

Lines changed: 116 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::fmt;
22
use std::fmt::Write as _;
3-
use std::path::Path;
3+
use std::fs::read_dir;
4+
use std::path::{Path, PathBuf};
45
use std::task::Poll;
56

67
use crate::core::{Dependency, PackageId, Registry, SourceId, Summary};
@@ -387,30 +388,54 @@ pub(super) fn activation_error(
387388
let _ = writeln!(&mut msg, "perhaps you meant: {suggestions}");
388389
} else {
389390
let sid = dep.source_id();
390-
let path = dep.source_id().url().to_file_path().unwrap();
391+
let path = dep
392+
.source_id()
393+
.url()
394+
.to_file_path()
395+
.expect("failed to get the path");
396+
let requested = dep.package_name().as_str();
391397

392398
if let Some(gctx) = gctx {
393-
debug_source_path(&mut msg, &path.as_path(), &gctx, sid);
394-
debug_recursive_source(&mut msg, &path.as_path(), &gctx, sid);
395-
}
399+
let root = inspect_root_package(&mut msg, &path.as_path(), &gctx, sid);
400+
let recursive =
401+
inspect_recursive_packages(&mut msg, &path.as_path(), &gctx, sid, requested);
402+
403+
let _ = writeln!(
404+
&mut msg,
405+
"no matching package named '{}' found at '{}/'",
406+
requested,
407+
path.display()
408+
);
396409

397-
let _ = writeln!(
398-
&mut msg,
399-
"no matching package named `{}` found",
400-
dep.package_name()
401-
);
402-
}
410+
let _ = writeln!(
411+
&mut msg,
412+
"required by {}",
413+
describe_path_in_context(resolver_ctx, &parent.package_id()),
414+
);
403415

404-
let mut location_searched_msg = registry.describe_source(dep.source_id());
405-
if location_searched_msg.is_empty() {
406-
location_searched_msg = format!("{}", dep.source_id());
416+
let mut help_name = None;
417+
let mut help_path = None;
418+
419+
if let Some(root_pkg) = &root {
420+
help_name = Some(root_pkg.name.clone());
421+
help_path = Some(path.clone());
422+
}
423+
424+
if let Some(recursive_pkg) = &recursive {
425+
help_name = Some(recursive_pkg.name.clone());
426+
help_path = Some(recursive_pkg.path.clone());
427+
}
428+
429+
if let (Some(name), Some(pkg_path)) = (help_name, help_path) {
430+
let _ = write!(
431+
&mut msg,
432+
"help: package `{}` exists at `{}`",
433+
name,
434+
pkg_path.display()
435+
);
436+
}
437+
}
407438
}
408-
let _ = writeln!(&mut msg, "location searched: {}", location_searched_msg);
409-
let _ = write!(
410-
&mut msg,
411-
"required by {}",
412-
describe_path_in_context(resolver_ctx, &parent.package_id()),
413-
);
414439

415440
if let Some(gctx) = gctx {
416441
if let Some(offline_flag) = gctx.offline_flag() {
@@ -578,48 +603,94 @@ pub(crate) fn describe_path<'a>(
578603
String::new()
579604
}
580605

581-
fn inspect_root_package(msg: &mut String, path: &Path, gctx: &GlobalContext, sid: SourceId) {
606+
#[derive(Debug)]
607+
struct RootPackageInfo {
608+
name: String,
609+
}
610+
611+
#[derive(Debug)]
612+
struct RecursivePackageInfo {
613+
name: String,
614+
path: PathBuf,
615+
}
616+
617+
fn inspect_root_package(
618+
msg: &mut String,
619+
path: &Path,
620+
gctx: &GlobalContext,
621+
sid: SourceId,
622+
) -> Option<RootPackageInfo> {
582623
let mut ps = PathSource::new(path, sid, gctx);
583624

584-
if let Err(e) = ps.root_package() {
585-
msg.push_str(&e.to_string());
586-
msg.push('\n');
587-
return;
588-
}
625+
ps.root_package().expect("failed to get the root");
589626

590627
if let Err(e) = ps.load() {
591628
msg.push_str(&e.to_string());
592629
msg.push('\n');
593-
return;
594630
}
595631

596-
if let Err(e) = ps.read_package() {
597-
msg.push_str(&e.to_string());
598-
msg.push('\n');
599-
}
632+
let pkg = ps
633+
.read_package()
634+
.expect("failed to read the package in root");
635+
636+
Some(RootPackageInfo {
637+
name: pkg.name().to_string(),
638+
})
600639
}
601640

602-
fn inspect_recursive_packages(msg: &mut String, path: &Path, gctx: &GlobalContext, sid: SourceId) {
641+
fn inspect_recursive_packages(
642+
msg: &mut String,
643+
path: &Path,
644+
gctx: &GlobalContext,
645+
sid: SourceId,
646+
requested: &str,
647+
) -> Option<RecursivePackageInfo> {
603648
let mut rps = RecursivePathSource::new(path, sid, gctx);
604649

605650
if let Err(e) = rps.load() {
606651
msg.push_str(&e.to_string());
607652
msg.push('\n');
608-
return;
653+
return None;
609654
}
610655

611-
match rps.read_packages() {
612-
Ok(pkgs) => {
613-
for p in pkgs {
614-
if let Err(e) = rps.list_files(&p) {
615-
msg.push_str(&e.to_string());
616-
msg.push('\n');
617-
}
618-
}
619-
}
620-
Err(e) => {
621-
msg.push_str(&e.to_string());
622-
msg.push('\n');
656+
let pkgs = rps
657+
.read_packages()
658+
.expect("failed to read the packages recursively");
659+
660+
for pkg in pkgs {
661+
if pkg.name() == requested {
662+
let manifest = pkg.manifest_path();
663+
let pkg_dir = manifest
664+
.parent()
665+
.expect("failed to take the parent path")
666+
.to_path_buf();
667+
668+
return Some(RecursivePackageInfo {
669+
name: pkg.name().to_string(),
670+
path: pkg_dir,
671+
});
672+
// } else {
673+
// let list = rps.list_files(&pkg).unwrap();
674+
// println!("{:?}", list);
623675
}
624676
}
677+
678+
None
679+
}
680+
681+
fn _inspect_else_packages(path: &Path) {
682+
let entry_path = read_dir(path);
683+
let entry_result = entry_path
684+
.expect("failed to get the path")
685+
.map(|f| {
686+
f.expect("failed to get the path")
687+
.path()
688+
.to_string_lossy()
689+
.to_string()
690+
})
691+
.collect::<Vec<_>>();
692+
693+
println!("{:?}", entry_result);
694+
695+
// let walkdir = WalkDir::new(entry_path);
625696
}

0 commit comments

Comments
 (0)