Skip to content

Commit a025762

Browse files
committed
test: add tests for invalid path dependency mismatch
fix: improve error message for path dependency name mismatch
1 parent 7204d98 commit a025762

File tree

3 files changed

+268
-3
lines changed

3 files changed

+268
-3
lines changed

src/cargo/core/resolver/errors.rs

Lines changed: 119 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
use std::fmt;
22
use std::fmt::Write as _;
3+
use std::fs::read_dir;
4+
use std::path::{Path, PathBuf};
35
use std::task::Poll;
46

5-
use crate::core::{Dependency, PackageId, Registry, Summary};
6-
use crate::sources::IndexSummary;
7+
use crate::core::{Dependency, PackageId, Registry, Shell, SourceId, Summary};
78
use crate::sources::source::QueryKind;
9+
use crate::sources::{IndexSummary, PathSource, RecursivePathSource};
810
use crate::util::edit_distance::{closest, edit_distance};
911
use crate::util::errors::CargoResult;
1012
use crate::util::{GlobalContext, OptVersionReq, VersionExt};
@@ -397,6 +399,31 @@ pub(super) fn activation_error(
397399
"no matching package named `{}` found",
398400
dep.package_name()
399401
);
402+
403+
if dep.source_id().is_path() {
404+
let path = dep
405+
.source_id()
406+
.url()
407+
.to_file_path()
408+
.expect("[ERROR]: failed to get the path");
409+
410+
let global_context = match gctx {
411+
Some(context) => context,
412+
None => &GlobalContext::new(Shell::new(), PathBuf::new(), PathBuf::new()),
413+
};
414+
let sid = dep.source_id();
415+
let requested = dep.package_name().as_str();
416+
417+
let _root_package =
418+
inspect_root_package(&mut msg, Path::new(&path), global_context, sid);
419+
let _recursive_packages = inspect_recursive_packages(
420+
&mut msg,
421+
Path::new(&path),
422+
global_context,
423+
sid,
424+
requested,
425+
);
426+
}
400427
}
401428

402429
let mut location_searched_msg = registry.describe_source(dep.source_id());
@@ -575,3 +602,93 @@ pub(crate) fn describe_path<'a>(
575602

576603
String::new()
577604
}
605+
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> {
623+
let mut ps = PathSource::new(path, sid, gctx);
624+
625+
ps.root_package().expect("failed to get the root");
626+
627+
if let Err(e) = ps.load() {
628+
msg.push_str(&e.to_string());
629+
msg.push('\n');
630+
}
631+
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+
})
639+
}
640+
641+
fn inspect_recursive_packages(
642+
msg: &mut String,
643+
path: &Path,
644+
gctx: &GlobalContext,
645+
sid: SourceId,
646+
requested: &str,
647+
) -> Option<RecursivePackageInfo> {
648+
let mut rps = RecursivePathSource::new(path, sid, gctx);
649+
650+
if let Err(e) = rps.load() {
651+
msg.push_str(&e.to_string());
652+
msg.push('\n');
653+
return None;
654+
}
655+
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);
675+
}
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+
// let walkdir = WalkDir::new(entry_path);
694+
}

src/cargo/sources/path.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ impl<'gctx> PathSource<'gctx> {
123123
Ok(())
124124
}
125125

126-
fn read_package(&self) -> CargoResult<Package> {
126+
pub fn read_package(&self) -> CargoResult<Package> {
127127
let path = self.path.join("Cargo.toml");
128128
let pkg = ops::read_package(&path, self.source_id, self.gctx)?;
129129
Ok(pkg)

tests/testsuite/path.rs

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1919,3 +1919,151 @@ foo v1.0.0 ([ROOT]/foo)
19191919
"#]])
19201920
.run();
19211921
}
1922+
1923+
#[cargo_test]
1924+
fn invalid_package_name_in_path() {
1925+
let p = project()
1926+
.file(
1927+
"Cargo.toml",
1928+
r#"
1929+
[package]
1930+
name = "foo"
1931+
version = "0.5.0"
1932+
edition = "2015"
1933+
authors = []
1934+
1935+
[workspace]
1936+
1937+
[dependencies]
1938+
definitely_not_bar = { path = "crates/bar" }
1939+
"#,
1940+
)
1941+
.file("src/lib.rs", "")
1942+
.file(
1943+
"crates/bar/Cargo.toml",
1944+
r#"
1945+
[package]
1946+
name = "bar"
1947+
version = "0.5.0"
1948+
edition = "2015"
1949+
authors = []
1950+
"#,
1951+
)
1952+
.file("crates/bar/src/lib.rs", "")
1953+
.build();
1954+
1955+
p.cargo("generate-lockfile")
1956+
.with_status(101)
1957+
.with_stderr_data(str![[r#"
1958+
[ERROR] no matching package named `definitely_not_bar` found
1959+
location searched: [ROOT]/foo/crates/bar
1960+
required by package `foo v0.5.0 ([ROOT]/foo)`
1961+
1962+
"#]])
1963+
.run();
1964+
}
1965+
1966+
#[cargo_test]
1967+
fn invalid_package_in_subdirectory() {
1968+
let p = project()
1969+
.file(
1970+
"Cargo.toml",
1971+
r#"
1972+
[package]
1973+
name = "foo"
1974+
version = "0.5.0"
1975+
edition = "2015"
1976+
authors = []
1977+
1978+
[workspace]
1979+
1980+
[dependencies]
1981+
definitely_not_bar = { path = "crates/bar" }
1982+
"#,
1983+
)
1984+
.file("src/lib.rs", "")
1985+
.file(
1986+
"crates/bar/definitely_not_bar/Cargo.toml",
1987+
r#"
1988+
[package]
1989+
name = "definitely_not_bar"
1990+
version = "0.5.0"
1991+
edition = "2015"
1992+
authors = []
1993+
"#,
1994+
)
1995+
.file("crates/bar/definitely_not_bar/src/lib.rs", "")
1996+
.build();
1997+
1998+
p.cargo("generate-lockfile")
1999+
.with_status(101)
2000+
.with_stderr_data(str![[r#"
2001+
[ERROR] failed to load manifest for dependency `definitely_not_bar`
2002+
2003+
Caused by:
2004+
failed to read `[ROOT]/foo/crates/bar/Cargo.toml`
2005+
2006+
Caused by:
2007+
[NOT_FOUND]
2008+
2009+
"#]])
2010+
.run();
2011+
}
2012+
2013+
#[cargo_test]
2014+
fn invalid_manifest_in_path() {
2015+
let p = project()
2016+
.file(
2017+
"Cargo.toml",
2018+
r#"
2019+
[package]
2020+
name = "foo"
2021+
version = "0.5.0"
2022+
edition = "2015"
2023+
authors = []
2024+
2025+
[workspace]
2026+
2027+
[dependencies]
2028+
definitely_not_bar = { path = "crates/bar" }
2029+
"#,
2030+
)
2031+
.file("src/lib.rs", "")
2032+
.file(
2033+
"crates/bar/alice/Cargo.toml",
2034+
r#"
2035+
[package]
2036+
name = "alice"
2037+
version = "0.5.0"
2038+
edition = "2015"
2039+
authors = []
2040+
"#,
2041+
)
2042+
.file("crates/bar/alice/src/lib.rs", "")
2043+
.file(
2044+
"crates/bar/bob/Cargo.toml",
2045+
r#"
2046+
[package]
2047+
name = "bob"
2048+
version = "0.5.0"
2049+
edition = "2015"
2050+
authors = []
2051+
"#,
2052+
)
2053+
.file("crates/bar/bob/src/lib.rs", "")
2054+
.build();
2055+
2056+
p.cargo("generate-lockfile")
2057+
.with_status(101)
2058+
.with_stderr_data(str![[r#"
2059+
[ERROR] failed to load manifest for dependency `definitely_not_bar`
2060+
2061+
Caused by:
2062+
failed to read `[ROOT]/foo/crates/bar/Cargo.toml`
2063+
2064+
Caused by:
2065+
[NOT_FOUND]
2066+
2067+
"#]])
2068+
.run();
2069+
}

0 commit comments

Comments
 (0)