|
1 | 1 | use std::fmt; |
2 | 2 | use std::fmt::Write as _; |
3 | | -use std::path::Path; |
| 3 | +use std::fs::read_dir; |
| 4 | +use std::path::{Path, PathBuf}; |
4 | 5 | use std::task::Poll; |
5 | 6 |
|
6 | 7 | use crate::core::{Dependency, PackageId, Registry, SourceId, Summary}; |
@@ -387,30 +388,54 @@ pub(super) fn activation_error( |
387 | 388 | let _ = writeln!(&mut msg, "perhaps you meant: {suggestions}"); |
388 | 389 | } else { |
389 | 390 | 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(); |
391 | 397 |
|
392 | 398 | 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 | + ); |
396 | 409 |
|
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 | + ); |
403 | 415 |
|
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 | + } |
407 | 438 | } |
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 | | - ); |
414 | 439 |
|
415 | 440 | if let Some(gctx) = gctx { |
416 | 441 | if let Some(offline_flag) = gctx.offline_flag() { |
@@ -578,48 +603,94 @@ pub(crate) fn describe_path<'a>( |
578 | 603 | String::new() |
579 | 604 | } |
580 | 605 |
|
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> { |
582 | 623 | let mut ps = PathSource::new(path, sid, gctx); |
583 | 624 |
|
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"); |
589 | 626 |
|
590 | 627 | if let Err(e) = ps.load() { |
591 | 628 | msg.push_str(&e.to_string()); |
592 | 629 | msg.push('\n'); |
593 | | - return; |
594 | 630 | } |
595 | 631 |
|
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 | + }) |
600 | 639 | } |
601 | 640 |
|
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> { |
603 | 648 | let mut rps = RecursivePathSource::new(path, sid, gctx); |
604 | 649 |
|
605 | 650 | if let Err(e) = rps.load() { |
606 | 651 | msg.push_str(&e.to_string()); |
607 | 652 | msg.push('\n'); |
608 | | - return; |
| 653 | + return None; |
609 | 654 | } |
610 | 655 |
|
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); |
623 | 675 | } |
624 | 676 | } |
| 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); |
625 | 696 | } |
0 commit comments