Skip to content

Commit 88d15f9

Browse files
committed
Auto merge of #4340 - debris:fixed_4310, r=alexcrichton
Fixed #4310, print useful message when dependency contains a malformed Cargo.toml fixed #4310
2 parents 305bc25 + 0275ca0 commit 88d15f9

File tree

2 files changed

+74
-6
lines changed

2 files changed

+74
-6
lines changed

src/cargo/ops/cargo_read_manifest.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::path::{Path, PathBuf};
55

66
use core::{Package, SourceId, PackageId, EitherManifest};
77
use util::{self, Config};
8-
use util::errors::{CargoResult, CargoResultExt};
8+
use util::errors::{CargoResult, CargoResultExt, CargoError};
99
use util::important_paths::find_project_manifest_exact;
1010
use util::toml::read_manifest;
1111

@@ -28,6 +28,7 @@ pub fn read_packages(path: &Path, source_id: &SourceId, config: &Config)
2828
-> CargoResult<Vec<Package>> {
2929
let mut all_packages = HashMap::new();
3030
let mut visited = HashSet::<PathBuf>::new();
31+
let mut errors = Vec::<CargoError>::new();
3132

3233
trace!("looking for root package: {}, source_id={}", path.display(), source_id);
3334

@@ -55,13 +56,16 @@ pub fn read_packages(path: &Path, source_id: &SourceId, config: &Config)
5556

5657
if has_manifest(dir) {
5758
read_nested_packages(dir, &mut all_packages, source_id, config,
58-
&mut visited)?;
59+
&mut visited, &mut errors)?;
5960
}
6061
Ok(true)
6162
})?;
6263

6364
if all_packages.is_empty() {
64-
Err(format!("Could not find Cargo.toml in `{}`", path.display()).into())
65+
match errors.pop() {
66+
Some(err) => Err(err),
67+
None => Err(format!("Could not find Cargo.toml in `{}`", path.display()).into()),
68+
}
6569
} else {
6670
Ok(all_packages.into_iter().map(|(_, v)| v).collect())
6771
}
@@ -104,13 +108,14 @@ fn read_nested_packages(path: &Path,
104108
all_packages: &mut HashMap<PackageId, Package>,
105109
source_id: &SourceId,
106110
config: &Config,
107-
visited: &mut HashSet<PathBuf>) -> CargoResult<()> {
111+
visited: &mut HashSet<PathBuf>,
112+
errors: &mut Vec<CargoError>) -> CargoResult<()> {
108113
if !visited.insert(path.to_path_buf()) { return Ok(()) }
109114

110115
let manifest_path = find_project_manifest_exact(path, "Cargo.toml")?;
111116

112117
let (manifest, nested) = match read_manifest(&manifest_path, source_id, config) {
113-
Err(_) => {
118+
Err(err) => {
114119
// Ignore malformed manifests found on git repositories
115120
//
116121
// git source try to find and read all manifests from the repository
@@ -120,6 +125,7 @@ fn read_nested_packages(path: &Path,
120125
// TODO: Add a way to exclude folders?
121126
info!("skipping malformed package found at `{}`",
122127
path.to_string_lossy());
128+
errors.push(err);
123129
return Ok(());
124130
}
125131
Ok(tuple) => tuple
@@ -151,7 +157,7 @@ fn read_nested_packages(path: &Path,
151157
for p in nested.iter() {
152158
let path = util::normalize_path(&path.join(p));
153159
read_nested_packages(&path, all_packages, source_id,
154-
config, visited)?;
160+
config, visited, errors)?;
155161
}
156162
}
157163

tests/git.rs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2083,3 +2083,65 @@ fn include_overrides_gitignore() {
20832083
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
20842084
"));
20852085
}
2086+
2087+
#[test]
2088+
fn invalid_git_dependency_manifest() {
2089+
let project = project("foo");
2090+
let git_project = git::new("dep1", |project| {
2091+
project
2092+
.file("Cargo.toml", r#"
2093+
[project]
2094+
2095+
name = "dep1"
2096+
version = "0.5.0"
2097+
authors = ["[email protected]"]
2098+
categories = ["algorithms"]
2099+
categories = ["algorithms"]
2100+
2101+
[lib]
2102+
2103+
name = "dep1"
2104+
"#)
2105+
.file("src/dep1.rs", r#"
2106+
pub fn hello() -> &'static str {
2107+
"hello world"
2108+
}
2109+
"#)
2110+
}).unwrap();
2111+
2112+
let project = project
2113+
.file("Cargo.toml", &format!(r#"
2114+
[project]
2115+
2116+
name = "foo"
2117+
version = "0.5.0"
2118+
authors = ["[email protected]"]
2119+
2120+
[dependencies.dep1]
2121+
2122+
git = '{}'
2123+
"#, git_project.url()))
2124+
.file("src/main.rs", &main_file(r#""{}", dep1::hello()"#, &["dep1"]));
2125+
2126+
let git_root = git_project.root();
2127+
2128+
assert_that(project.cargo_process("build"),
2129+
execs()
2130+
.with_stderr(&format!("[UPDATING] git repository `{}`\n\
2131+
error: failed to load source for a dependency on `dep1`\n\
2132+
\n\
2133+
Caused by:\n \
2134+
Unable to update {}\n\
2135+
\n\
2136+
Caused by:\n \
2137+
failed to parse manifest at `[..]`\n\
2138+
\n\
2139+
Caused by:\n \
2140+
could not parse input as TOML\n\
2141+
\n\
2142+
Caused by:\n \
2143+
duplicate key: `categories` for key `project`",
2144+
path2url(git_root.clone()),
2145+
path2url(git_root),
2146+
)));
2147+
}

0 commit comments

Comments
 (0)