Skip to content

Commit 61780a9

Browse files
committed
Separate warning and with root_id
Signed-off-by: hi-rustin <[email protected]>
1 parent f127831 commit 61780a9

File tree

3 files changed

+125
-24
lines changed

3 files changed

+125
-24
lines changed

src/cargo/core/package.rs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -567,26 +567,35 @@ impl<'cfg> PackageSet<'cfg> {
567567
requested_kinds: &[CompileKind],
568568
target_data: &RustcTargetData<'_>,
569569
force_all_targets: ForceAllTargets,
570-
) -> Vec<&Package> {
571-
let mut ret = vec![];
570+
) -> BTreeMap<PackageId, Vec<&Package>> {
571+
let mut ret = BTreeMap::new();
572572

573-
root_ids.iter().for_each(|root_id| {
574-
PackageSet::filter_deps(
575-
*root_id,
573+
root_ids.iter().for_each(|&root_id| {
574+
let pkgs: Vec<&Package> = PackageSet::filter_deps(
575+
root_id,
576576
resolve,
577577
has_dev_units,
578578
requested_kinds,
579579
target_data,
580580
force_all_targets,
581581
)
582582
.iter()
583-
.for_each(|&package_id| {
583+
.filter_map(|&package_id| {
584584
if let Ok(dep_pkg) = self.get_one(package_id) {
585585
if !dep_pkg.targets().iter().any(|t| t.is_lib()) {
586-
ret.push(dep_pkg);
586+
Some(dep_pkg)
587+
} else {
588+
None
587589
}
590+
} else {
591+
None
588592
}
589-
});
593+
})
594+
.collect();
595+
596+
if !pkgs.is_empty() {
597+
ret.insert(root_id, pkgs);
598+
}
590599
});
591600

592601
ret

src/cargo/ops/resolve.rs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -181,14 +181,20 @@ pub fn resolve_ws_with_opts<'cfg>(
181181
force_all_targets,
182182
)
183183
.iter()
184-
.map(|pkg| format!("No lib found in package `{}`.", pkg.name()))
184+
.flat_map(|(pkg_id, dep_pkgs)| {
185+
dep_pkgs.iter().map(move |dep_pkg| {
186+
format!(
187+
"{} has invalid dependency `{}`. `{}` has no lib package.",
188+
pkg_id,
189+
dep_pkg.name(),
190+
dep_pkg.name(),
191+
)
192+
})
193+
})
185194
.collect();
186-
if !no_lib_warnings.is_empty() {
187-
ws.config().shell().warn(format!(
188-
"{} The dependent package should have a lib, \
189-
otherwise it is an invalid dependency.",
190-
no_lib_warnings.join("\n")
191-
))?;
195+
196+
for warn in no_lib_warnings {
197+
ws.config().shell().warn(warn)?;
192198
}
193199

194200
Ok(WorkspaceResolve {

tests/testsuite/run.rs

Lines changed: 95 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -756,9 +756,7 @@ fn run_with_bin_dep() {
756756
p.cargo("run")
757757
.with_stderr(
758758
"\
759-
[WARNING] No lib found in package `bar`. \
760-
The dependent package should have a lib, \
761-
otherwise it is an invalid dependency.
759+
[WARNING] foo v0.0.1 ([CWD]) has invalid dependency `bar`. `bar` has no lib package.
762760
[COMPILING] foo v0.0.1 ([CWD])
763761
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
764762
[RUNNING] `target/debug/foo[EXE]`",
@@ -796,7 +794,7 @@ fn run_with_bin_deps() {
796794
name = "bar1"
797795
"#,
798796
)
799-
.file("bar1/src/main.rs", r#"fn main() { println!("bar"); }"#)
797+
.file("bar1/src/main.rs", r#"fn main() { println!("bar1"); }"#)
800798
.file(
801799
"bar2/Cargo.toml",
802800
r#"
@@ -809,16 +807,14 @@ fn run_with_bin_deps() {
809807
name = "bar2"
810808
"#,
811809
)
812-
.file("bar2/src/main.rs", r#"fn main() { println!("bar"); }"#)
810+
.file("bar2/src/main.rs", r#"fn main() { println!("bar2"); }"#)
813811
.build();
814812

815813
p.cargo("run")
816814
.with_stderr(
817815
"\
818-
[WARNING] No lib found in package `bar1`.
819-
No lib found in package `bar2`. \
820-
The dependent package should have a lib, \
821-
otherwise it is an invalid dependency.
816+
[WARNING] foo v0.0.1 ([CWD]) has invalid dependency `bar1`. `bar1` has no lib package.
817+
[WARNING] foo v0.0.1 ([CWD]) has invalid dependency `bar2`. `bar2` has no lib package.
822818
[COMPILING] foo v0.0.1 ([CWD])
823819
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
824820
[RUNNING] `target/debug/foo[EXE]`",
@@ -827,6 +823,96 @@ otherwise it is an invalid dependency.
827823
.run();
828824
}
829825

826+
#[cargo_test]
827+
fn run_with_bin_dep_in_workspace() {
828+
let p = project()
829+
.file(
830+
"Cargo.toml",
831+
r#"
832+
[workspace]
833+
members = ["foo1", "foo2"]
834+
"#,
835+
)
836+
.file(
837+
"foo1/Cargo.toml",
838+
r#"
839+
[package]
840+
name = "foo1"
841+
version = "0.0.1"
842+
843+
[dependencies.bar1]
844+
path = "bar1"
845+
"#,
846+
)
847+
.file("foo1/src/main.rs", r#"fn main() { println!("hello"); }"#)
848+
.file(
849+
"foo1/bar1/Cargo.toml",
850+
r#"
851+
[package]
852+
name = "bar1"
853+
version = "0.0.1"
854+
authors = []
855+
856+
[[bin]]
857+
name = "bar1"
858+
"#,
859+
)
860+
.file(
861+
"foo1/bar1/src/main.rs",
862+
r#"fn main() { println!("bar1"); }"#,
863+
)
864+
.file(
865+
"foo2/Cargo.toml",
866+
r#"
867+
[package]
868+
name = "foo2"
869+
version = "0.0.1"
870+
871+
[dependencies.bar2]
872+
path = "bar2"
873+
"#,
874+
)
875+
.file("foo2/src/main.rs", r#"fn main() { println!("hello"); }"#)
876+
.file(
877+
"foo2/bar2/Cargo.toml",
878+
r#"
879+
[package]
880+
name = "bar2"
881+
version = "0.0.1"
882+
authors = []
883+
884+
[[bin]]
885+
name = "bar2"
886+
"#,
887+
)
888+
.file(
889+
"foo2/bar2/src/main.rs",
890+
r#"fn main() { println!("bar2"); }"#,
891+
)
892+
.build();
893+
894+
p.cargo("run")
895+
.with_status(101)
896+
.with_stderr(
897+
"\
898+
[ERROR] `cargo run` could not determine which binary to run[..]
899+
available binaries: bar1, bar2, foo1, foo2",
900+
)
901+
.run();
902+
903+
p.cargo("run --bin foo1")
904+
.with_stderr(
905+
"\
906+
[WARNING] foo1 v0.0.1 ([CWD]/foo1) has invalid dependency `bar1`. `bar1` has no lib package.
907+
[WARNING] foo2 v0.0.1 ([CWD]/foo2) has invalid dependency `bar2`. `bar2` has no lib package.
908+
[COMPILING] foo1 v0.0.1 ([CWD]/foo1)
909+
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
910+
[RUNNING] `target/debug/foo1[EXE]`",
911+
)
912+
.with_stdout("hello")
913+
.run();
914+
}
915+
830916
#[cargo_test]
831917
fn release_works() {
832918
let p = project()

0 commit comments

Comments
 (0)