Skip to content
Draft
Show file tree
Hide file tree
Changes from 2 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
46 changes: 44 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,45 @@ use anyhow::Error;
use super::context::ResolverContext;
use super::types::{ConflictMap, ConflictReason};

fn debug_source_path() {
Copy link
Member

Choose a reason for hiding this comment

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

If these are for local debugging purpose, please remove.

Copy link
Contributor

Choose a reason for hiding this comment

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

This was showing where they are at in experimenting while they then work to change this to fit in

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,
Expand Down Expand Up @@ -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",
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