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
59 changes: 57 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 Down Expand Up @@ -385,6 +386,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 Expand Up @@ -568,3 +577,49 @@ pub(crate) fn describe_path<'a>(

String::new()
}

fn inspect_root_package(msg: &mut String, path: &Path, gctx: &GlobalContext, sid: SourceId) {
let mut ps = PathSource::new(path, sid, gctx);

if let Err(e) = ps.root_package() {
msg.push_str(&e.to_string());
msg.push('\n');
return;
}

if let Err(e) = ps.load() {
msg.push_str(&e.to_string());
msg.push('\n');
return;
}

if let Err(e) = ps.read_package() {
msg.push_str(&e.to_string());
msg.push('\n');
}
}

fn inspect_recursive_packages(msg: &mut String, path: &Path, gctx: &GlobalContext, sid: SourceId) {
let mut rps = RecursivePathSource::new(path, sid, gctx);

if let Err(e) = rps.load() {
msg.push_str(&e.to_string());
msg.push('\n');
return;
}

match rps.read_packages() {
Ok(pkgs) => {
for p in pkgs {
if let Err(e) = rps.list_files(&p) {
msg.push_str(&e.to_string());
msg.push('\n');
}
}
}
Err(e) => {
msg.push_str(&e.to_string());
msg.push('\n');
}
}
}
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