Skip to content

Commit 81c2cd5

Browse files
committed
Compute non custom build and non transitive deps for doc
Signed-off-by: hi-rustin <[email protected]>
1 parent 8906474 commit 81c2cd5

File tree

2 files changed

+97
-25
lines changed

2 files changed

+97
-25
lines changed

src/cargo/core/compiler/unit_dependencies.rs

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
use crate::core::compiler::unit_graph::{UnitDep, UnitGraph};
1919
use crate::core::compiler::UnitInterner;
2020
use crate::core::compiler::{CompileKind, CompileMode, RustcTargetData, Unit};
21-
use crate::core::dependency::DepKind;
2221
use crate::core::profiles::{Profile, Profiles, UnitFor};
2322
use crate::core::resolver::features::{FeaturesFor, ResolvedFeatures};
2423
use crate::core::resolver::Resolve;
@@ -243,29 +242,7 @@ fn compute_deps(
243242
}
244243

245244
let id = unit.pkg.package_id();
246-
let filtered_deps = state.deps(unit, unit_for, &|dep| {
247-
// If this target is a build command, then we only want build
248-
// dependencies, otherwise we want everything *other than* build
249-
// dependencies.
250-
if unit.target.is_custom_build() != dep.is_build() {
251-
return false;
252-
}
253-
254-
// If this dependency is **not** a transitive dependency, then it
255-
// only applies to test/example targets.
256-
if !dep.is_transitive()
257-
&& !unit.target.is_test()
258-
&& !unit.target.is_example()
259-
&& !unit.mode.is_doc_scrape()
260-
&& !unit.mode.is_any_test()
261-
{
262-
return false;
263-
}
264-
265-
// If we've gotten past all that, then this dependency is
266-
// actually used!
267-
true
268-
});
245+
let filtered_deps = non_custom_build_and_non_transitive_deps(unit, state, unit_for);
269246

270247
let mut ret = Vec::new();
271248
let mut dev_deps = Vec::new();
@@ -372,6 +349,37 @@ fn compute_deps(
372349
Ok(ret)
373350
}
374351

352+
/// Returns non-custom and non-transitive dependencies.
353+
fn non_custom_build_and_non_transitive_deps<'a>(
354+
unit: &Unit,
355+
state: &'a State<'_, '_>,
356+
unit_for: UnitFor,
357+
) -> Vec<(PackageId, &'a HashSet<Dependency>)> {
358+
state.deps(unit, unit_for, &|dep| {
359+
// If this target is a build command, then we only want build
360+
// dependencies, otherwise we want everything *other than* build
361+
// dependencies.
362+
if unit.target.is_custom_build() != dep.is_build() {
363+
return false;
364+
}
365+
366+
// If this dependency is **not** a transitive dependency, then it
367+
// only applies to test/example targets.
368+
if !dep.is_transitive()
369+
&& !unit.target.is_test()
370+
&& !unit.target.is_example()
371+
&& !unit.mode.is_doc_scrape()
372+
&& !unit.mode.is_any_test()
373+
{
374+
return false;
375+
}
376+
377+
// If we've gotten past all that, then this dependency is
378+
// actually used!
379+
true
380+
})
381+
}
382+
375383
/// Returns the dependencies needed to run a build script.
376384
///
377385
/// The `unit` provided must represent an execution of a build script, and
@@ -423,7 +431,7 @@ fn compute_deps_doc(
423431
state: &mut State<'_, '_>,
424432
unit_for: UnitFor,
425433
) -> CargoResult<Vec<UnitDep>> {
426-
let deps = state.deps(unit, unit_for, &|dep| dep.kind() == DepKind::Normal);
434+
let deps = non_custom_build_and_non_transitive_deps(unit, state, unit_for);
427435

428436
// To document a library, we depend on dependencies actually being
429437
// built. If we're documenting *all* libraries, then we also depend on

tests/testsuite/doc.rs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1815,6 +1815,70 @@ fn doc_example() {
18151815
.exists());
18161816
}
18171817

1818+
#[cargo_test]
1819+
fn doc_example_with_deps() {
1820+
let p = project()
1821+
.file(
1822+
"Cargo.toml",
1823+
r#"
1824+
[package]
1825+
name = "foo"
1826+
version = "0.1.0"
1827+
edition = "2018"
1828+
1829+
[[example]]
1830+
crate-type = ["lib"]
1831+
name = "ex"
1832+
doc = true
1833+
1834+
[dev-dependencies]
1835+
a = {path = "a"}
1836+
b = {path = "b"}
1837+
"#,
1838+
)
1839+
.file("src/lib.rs", "")
1840+
.file(
1841+
"examples/ex.rs",
1842+
r#"
1843+
use a::fun;
1844+
1845+
/// Example
1846+
pub fn x() { fun(); }
1847+
"#,
1848+
)
1849+
.file(
1850+
"a/Cargo.toml",
1851+
r#"
1852+
[package]
1853+
name = "a"
1854+
version = "0.0.1"
1855+
1856+
[dependencies]
1857+
b = {path = "../b"}
1858+
"#,
1859+
)
1860+
.file("a/src/fun.rs", "pub fn fun() {}")
1861+
.file("a/src/lib.rs", "pub mod fun;")
1862+
.file(
1863+
"b/Cargo.toml",
1864+
r#"
1865+
[package]
1866+
name = "b"
1867+
version = "0.0.1"
1868+
"#,
1869+
)
1870+
.file("b/src/lib.rs", "")
1871+
.build();
1872+
1873+
p.cargo("doc --examples").run();
1874+
assert!(p
1875+
.build_dir()
1876+
.join("doc")
1877+
.join("ex")
1878+
.join("fn.x.html")
1879+
.exists());
1880+
}
1881+
18181882
#[cargo_test]
18191883
fn bin_private_items() {
18201884
let p = project()

0 commit comments

Comments
 (0)