Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Warning for no lib dependencies #9771

Merged
merged 5 commits into from
Aug 23, 2021
Merged
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
89 changes: 68 additions & 21 deletions src/cargo/core/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -516,27 +516,14 @@ impl<'cfg> PackageSet<'cfg> {
if !used.insert(pkg_id) {
return Ok(());
}
let filtered_deps = resolve.deps(pkg_id).filter(|&(_id, deps)| {
deps.iter().any(|dep| {
if dep.kind() == DepKind::Development && has_dev_units == HasDevUnits::No {
return false;
}
// This is overly broad, since not all target-specific
// dependencies are used both for target and host. To tighten this
// up, this function would need to track "for_host" similar to how
// unit dependencies handles it.
if force_all_targets == ForceAllTargets::No {
let activated = requested_kinds
.iter()
.chain(Some(&CompileKind::Host))
.any(|kind| target_data.dep_platform_activated(dep, *kind));
if !activated {
return false;
}
}
true
})
});
let filtered_deps = PackageSet::filter_deps(
pkg_id,
resolve,
has_dev_units,
requested_kinds,
target_data,
force_all_targets,
);
for (dep_id, _deps) in filtered_deps {
collect_used_deps(
used,
Expand Down Expand Up @@ -571,6 +558,66 @@ impl<'cfg> PackageSet<'cfg> {
Ok(())
}

/// Check if there are any dependency packages that do not have any libs.
pub(crate) fn no_lib_pkgs(
&self,
resolve: &Resolve,
root_ids: &[PackageId],
has_dev_units: HasDevUnits,
requested_kinds: &[CompileKind],
target_data: &RustcTargetData<'_>,
force_all_targets: ForceAllTargets,
) -> CargoResult<Vec<&Package>> {
Rustin170506 marked this conversation as resolved.
Show resolved Hide resolved
let mut ret = vec![];

root_ids.iter().for_each(|pkg_id| {
PackageSet::filter_deps(
*pkg_id,
resolve,
has_dev_units,
requested_kinds,
target_data,
force_all_targets,
)
.for_each(|(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);
}
}
});
});

Ok(ret)
Rustin170506 marked this conversation as resolved.
Show resolved Hide resolved
}
Rustin170506 marked this conversation as resolved.
Show resolved Hide resolved

fn filter_deps<'a>(
pkg_id: PackageId,
resolve: &'a Resolve,
has_dev_units: HasDevUnits,
requested_kinds: &'a [CompileKind],
target_data: &'a RustcTargetData<'_>,
force_all_targets: ForceAllTargets,
) -> impl Iterator<Item = (PackageId, &'a HashSet<Dependency>)> {
Rustin170506 marked this conversation as resolved.
Show resolved Hide resolved
resolve.deps(pkg_id).filter(move |&(_id, deps)| {
deps.iter().any(|dep| {
if dep.kind() == DepKind::Development && has_dev_units == HasDevUnits::No {
return false;
}
if force_all_targets == ForceAllTargets::No {
let activated = requested_kinds
.iter()
.chain(Some(&CompileKind::Host))
.any(|kind| target_data.dep_platform_activated(dep, *kind));
if !activated {
return false;
}
}
true
})
})
}

pub fn sources(&self) -> Ref<'_, SourceMap<'cfg>> {
self.sources.borrow()
}
Expand Down
20 changes: 20 additions & 0 deletions src/cargo/ops/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,26 @@ pub fn resolve_ws_with_opts<'cfg>(
feature_opts,
)?;

let no_lib_warnings: Vec<String> = pkg_set
.no_lib_pkgs(
&resolved_with_overrides,
&member_ids,
has_dev_units,
requested_targets,
target_data,
force_all_targets,
)?
.iter()
.map(|pkg| format!("No lib found in package `{}`.", 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")
))?;
}

Ok(WorkspaceResolve {
pkg_set,
workspace_resolve: resolve,
Expand Down
104 changes: 104 additions & 0 deletions tests/testsuite/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -723,6 +723,110 @@ fn run_dylib_dep() {
p.cargo("run hello world").run();
}

#[cargo_test]
fn run_with_bin_dep() {
let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.0.1"

[dependencies.bar]
path = "bar"
"#,
)
.file("src/main.rs", r#"fn main() { println!("hello"); }"#)
.file(
"bar/Cargo.toml",
r#"
[package]
name = "bar"
version = "0.0.1"
authors = []

[[bin]]
name = "bar"
"#,
)
.file("bar/src/main.rs", r#"fn main() { println!("bar"); }"#)
.build();

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.
[COMPILING] foo v0.0.1 ([CWD])
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
[RUNNING] `target/debug/foo[EXE]`",
)
.with_stdout("hello")
.run();
}

#[cargo_test]
fn run_with_bin_deps() {
let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.0.1"

[dependencies.bar1]
path = "bar1"
[dependencies.bar2]
path = "bar2"
"#,
)
.file("src/main.rs", r#"fn main() { println!("hello"); }"#)
.file(
"bar1/Cargo.toml",
r#"
[package]
name = "bar1"
version = "0.0.1"
authors = []

[[bin]]
name = "bar1"
"#,
)
.file("bar1/src/main.rs", r#"fn main() { println!("bar"); }"#)
.file(
"bar2/Cargo.toml",
r#"
[package]
name = "bar2"
version = "0.0.1"
authors = []

[[bin]]
name = "bar2"
"#,
)
.file("bar2/src/main.rs", r#"fn main() { println!("bar"); }"#)
.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.
Rustin170506 marked this conversation as resolved.
Show resolved Hide resolved
[COMPILING] foo v0.0.1 ([CWD])
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
[RUNNING] `target/debug/foo[EXE]`",
)
.with_stdout("hello")
.run();
}

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