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

Compute non custom build and non transitive deps for doc #10341

Merged
merged 2 commits into from
Feb 3, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
53 changes: 21 additions & 32 deletions src/cargo/core/compiler/unit_dependencies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
use crate::core::compiler::unit_graph::{UnitDep, UnitGraph};
use crate::core::compiler::UnitInterner;
use crate::core::compiler::{CompileKind, CompileMode, RustcTargetData, Unit};
use crate::core::dependency::DepKind;
use crate::core::profiles::{Profile, Profiles, UnitFor};
use crate::core::resolver::features::{FeaturesFor, ResolvedFeatures};
use crate::core::resolver::Resolve;
Expand Down Expand Up @@ -243,29 +242,7 @@ fn compute_deps(
}

let id = unit.pkg.package_id();
let filtered_deps = state.deps(unit, unit_for, &|dep| {
// If this target is a build command, then we only want build
// dependencies, otherwise we want everything *other than* build
// dependencies.
if unit.target.is_custom_build() != dep.is_build() {
return false;
}

// If this dependency is **not** a transitive dependency, then it
// only applies to test/example targets.
if !dep.is_transitive()
&& !unit.target.is_test()
&& !unit.target.is_example()
&& !unit.mode.is_doc_scrape()
&& !unit.mode.is_any_test()
{
return false;
}

// If we've gotten past all that, then this dependency is
// actually used!
true
});
let filtered_deps = state.deps(unit, unit_for);

let mut ret = Vec::new();
let mut dev_deps = Vec::new();
Expand Down Expand Up @@ -423,7 +400,7 @@ fn compute_deps_doc(
state: &mut State<'_, '_>,
unit_for: UnitFor,
) -> CargoResult<Vec<UnitDep>> {
let deps = state.deps(unit, unit_for, &|dep| dep.kind() == DepKind::Normal);
let deps = state.deps(unit, unit_for);

// To document a library, we depend on dependencies actually being
// built. If we're documenting *all* libraries, then we also depend on
Expand Down Expand Up @@ -831,22 +808,32 @@ impl<'a, 'cfg> State<'a, 'cfg> {
}

/// Returns a filtered set of dependencies for the given unit.
fn deps(
&self,
unit: &Unit,
unit_for: UnitFor,
filter: &dyn Fn(&Dependency) -> bool,
) -> Vec<(PackageId, &HashSet<Dependency>)> {
fn deps(&self, unit: &Unit, unit_for: UnitFor) -> Vec<(PackageId, &HashSet<Dependency>)> {
let pkg_id = unit.pkg.package_id();
let kind = unit.kind;
self.resolve()
.deps(pkg_id)
.filter(|&(_id, deps)| {
assert!(!deps.is_empty());
deps.iter().any(|dep| {
if !filter(dep) {
// If this target is a build command, then we only want build
// dependencies, otherwise we want everything *other than* build
// dependencies.
if unit.target.is_custom_build() != dep.is_build() {
return false;
}

// If this dependency is **not** a transitive dependency, then it
// only applies to test/example targets.
if !dep.is_transitive()
&& !unit.target.is_test()
&& !unit.target.is_example()
&& !unit.mode.is_doc_scrape()
&& !unit.mode.is_any_test()
{
return false;
}

// If this dependency is only available for certain platforms,
// make sure we're only enabling it for that platform.
if !self.target_data.dep_platform_activated(dep, kind) {
Expand All @@ -862,6 +849,8 @@ impl<'a, 'cfg> State<'a, 'cfg> {
}
}

// If we've gotten past all that, then this dependency is
// actually used!
true
})
})
Expand Down
64 changes: 64 additions & 0 deletions tests/testsuite/doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1815,6 +1815,70 @@ fn doc_example() {
.exists());
}

#[cargo_test]
fn doc_example_with_deps() {
let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.1.0"
edition = "2018"

[[example]]
crate-type = ["lib"]
name = "ex"
doc = true

[dev-dependencies]
a = {path = "a"}
b = {path = "b"}
"#,
)
.file("src/lib.rs", "")
.file(
"examples/ex.rs",
r#"
use a::fun;

/// Example
pub fn x() { fun(); }
"#,
)
.file(
"a/Cargo.toml",
r#"
[package]
name = "a"
version = "0.0.1"

[dependencies]
b = {path = "../b"}
"#,
)
.file("a/src/fun.rs", "pub fn fun() {}")
.file("a/src/lib.rs", "pub mod fun;")
.file(
"b/Cargo.toml",
r#"
[package]
name = "b"
version = "0.0.1"
"#,
)
.file("b/src/lib.rs", "")
.build();

p.cargo("doc --examples").run();
assert!(p
.build_dir()
.join("doc")
.join("ex")
.join("fn.x.html")
.exists());
}

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