Skip to content

Commit

Permalink
Separate warning and with root_id
Browse files Browse the repository at this point in the history
Signed-off-by: hi-rustin <[email protected]>
  • Loading branch information
Rustin170506 committed Aug 10, 2021
1 parent f127831 commit 61780a9
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 24 deletions.
25 changes: 17 additions & 8 deletions src/cargo/core/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -567,26 +567,35 @@ impl<'cfg> PackageSet<'cfg> {
requested_kinds: &[CompileKind],
target_data: &RustcTargetData<'_>,
force_all_targets: ForceAllTargets,
) -> Vec<&Package> {
let mut ret = vec![];
) -> BTreeMap<PackageId, Vec<&Package>> {
let mut ret = BTreeMap::new();

root_ids.iter().for_each(|root_id| {
PackageSet::filter_deps(
*root_id,
root_ids.iter().for_each(|&root_id| {
let pkgs: Vec<&Package> = PackageSet::filter_deps(
root_id,
resolve,
has_dev_units,
requested_kinds,
target_data,
force_all_targets,
)
.iter()
.for_each(|&package_id| {
.filter_map(|&package_id| {
if let Ok(dep_pkg) = self.get_one(package_id) {
if !dep_pkg.targets().iter().any(|t| t.is_lib()) {
ret.push(dep_pkg);
Some(dep_pkg)
} else {
None
}
} else {
None
}
});
})
.collect();

if !pkgs.is_empty() {
ret.insert(root_id, pkgs);
}
});

ret
Expand Down
20 changes: 13 additions & 7 deletions src/cargo/ops/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,14 +181,20 @@ pub fn resolve_ws_with_opts<'cfg>(
force_all_targets,
)
.iter()
.map(|pkg| format!("No lib found in package `{}`.", pkg.name()))
.flat_map(|(pkg_id, dep_pkgs)| {
dep_pkgs.iter().map(move |dep_pkg| {
format!(
"{} has invalid dependency `{}`. `{}` has no lib package.",
pkg_id,
dep_pkg.name(),
dep_pkg.name(),
)
})
})
.collect();
if !no_lib_warnings.is_empty() {
ws.config().shell().warn(format!(
"{} The dependent package should have a lib, \
otherwise it is an invalid dependency.",
no_lib_warnings.join("\n")
))?;

for warn in no_lib_warnings {
ws.config().shell().warn(warn)?;
}

Ok(WorkspaceResolve {
Expand Down
104 changes: 95 additions & 9 deletions tests/testsuite/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -756,9 +756,7 @@ fn run_with_bin_dep() {
p.cargo("run")
.with_stderr(
"\
[WARNING] No lib found in package `bar`. \
The dependent package should have a lib, \
otherwise it is an invalid dependency.
[WARNING] foo v0.0.1 ([CWD]) has invalid dependency `bar`. `bar` has no lib package.
[COMPILING] foo v0.0.1 ([CWD])
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
[RUNNING] `target/debug/foo[EXE]`",
Expand Down Expand Up @@ -796,7 +794,7 @@ fn run_with_bin_deps() {
name = "bar1"
"#,
)
.file("bar1/src/main.rs", r#"fn main() { println!("bar"); }"#)
.file("bar1/src/main.rs", r#"fn main() { println!("bar1"); }"#)
.file(
"bar2/Cargo.toml",
r#"
Expand All @@ -809,16 +807,14 @@ fn run_with_bin_deps() {
name = "bar2"
"#,
)
.file("bar2/src/main.rs", r#"fn main() { println!("bar"); }"#)
.file("bar2/src/main.rs", r#"fn main() { println!("bar2"); }"#)
.build();

p.cargo("run")
.with_stderr(
"\
[WARNING] No lib found in package `bar1`.
No lib found in package `bar2`. \
The dependent package should have a lib, \
otherwise it is an invalid dependency.
[WARNING] foo v0.0.1 ([CWD]) has invalid dependency `bar1`. `bar1` has no lib package.
[WARNING] foo v0.0.1 ([CWD]) has invalid dependency `bar2`. `bar2` has no lib package.
[COMPILING] foo v0.0.1 ([CWD])
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
[RUNNING] `target/debug/foo[EXE]`",
Expand All @@ -827,6 +823,96 @@ otherwise it is an invalid dependency.
.run();
}

#[cargo_test]
fn run_with_bin_dep_in_workspace() {
let p = project()
.file(
"Cargo.toml",
r#"
[workspace]
members = ["foo1", "foo2"]
"#,
)
.file(
"foo1/Cargo.toml",
r#"
[package]
name = "foo1"
version = "0.0.1"
[dependencies.bar1]
path = "bar1"
"#,
)
.file("foo1/src/main.rs", r#"fn main() { println!("hello"); }"#)
.file(
"foo1/bar1/Cargo.toml",
r#"
[package]
name = "bar1"
version = "0.0.1"
authors = []
[[bin]]
name = "bar1"
"#,
)
.file(
"foo1/bar1/src/main.rs",
r#"fn main() { println!("bar1"); }"#,
)
.file(
"foo2/Cargo.toml",
r#"
[package]
name = "foo2"
version = "0.0.1"
[dependencies.bar2]
path = "bar2"
"#,
)
.file("foo2/src/main.rs", r#"fn main() { println!("hello"); }"#)
.file(
"foo2/bar2/Cargo.toml",
r#"
[package]
name = "bar2"
version = "0.0.1"
authors = []
[[bin]]
name = "bar2"
"#,
)
.file(
"foo2/bar2/src/main.rs",
r#"fn main() { println!("bar2"); }"#,
)
.build();

p.cargo("run")
.with_status(101)
.with_stderr(
"\
[ERROR] `cargo run` could not determine which binary to run[..]
available binaries: bar1, bar2, foo1, foo2",
)
.run();

p.cargo("run --bin foo1")
.with_stderr(
"\
[WARNING] foo1 v0.0.1 ([CWD]/foo1) has invalid dependency `bar1`. `bar1` has no lib package.
[WARNING] foo2 v0.0.1 ([CWD]/foo2) has invalid dependency `bar2`. `bar2` has no lib package.
[COMPILING] foo1 v0.0.1 ([CWD]/foo1)
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
[RUNNING] `target/debug/foo1[EXE]`",
)
.with_stdout("hello")
.run();
}

#[cargo_test]
fn release_works() {
let p = project()
Expand Down

0 comments on commit 61780a9

Please sign in to comment.