Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 95 additions & 2 deletions src/cargo/core/resolver/errors.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These should go below where they are used so that the first item in the file is the starting point for reading it and going further down just adds more context

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,
Expand Down Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion src/cargo/sources/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ impl<'gctx> PathSource<'gctx> {
Ok(())
}

fn read_package(&self) -> CargoResult<Package> {
pub fn read_package(&self) -> CargoResult<Package> {
let path = self.path.join("Cargo.toml");
let pkg = ops::read_package(&path, self.source_id, self.gctx)?;
Ok(pkg)
Expand Down
148 changes: 148 additions & 0 deletions tests/testsuite/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is [workspace] here essential? If not maybe we should remove.


[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();
}
Loading