Skip to content

Commit 125c6b4

Browse files
committed
Move scrape-examples target filtering from CompileFilter into generate_targets
1 parent e027c4b commit 125c6b4

File tree

2 files changed

+60
-74
lines changed

2 files changed

+60
-74
lines changed

src/cargo/ops/cargo_compile/compile_filter.rs

Lines changed: 2 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
//! Filters and their rules to select which Cargo targets will be built.
22
33
use crate::core::compiler::CompileMode;
4-
use crate::core::dependency::DepKind;
5-
use crate::core::resolver::HasDevUnits;
6-
use crate::core::{Package, Target, TargetKind};
4+
5+
use crate::core::{Target, TargetKind};
76
use crate::util::restricted_names::is_glob_pattern;
87

98
#[derive(Debug, PartialEq, Eq, Clone)]
@@ -301,67 +300,4 @@ impl CompileFilter {
301300
}
302301
}
303302
}
304-
305-
/// Generate a CompileFilter that represents the maximal set of targets
306-
/// that should be considered for scraped examples.
307-
pub(super) fn refine_for_docscrape(
308-
&self,
309-
to_builds: &[&Package],
310-
has_dev_units: HasDevUnits,
311-
) -> CompileFilter {
312-
// In general, the goal is to scrape examples from (a) whatever targets
313-
// the user is documenting, and (b) Example targets. However, if the user
314-
// is documenting a library with dev-dependencies, those dev-deps are not
315-
// needed for the library, while dev-deps are needed for the examples.
316-
//
317-
// If scrape-examples caused `cargo doc` to start requiring dev-deps, this
318-
// would be a breaking change to crates whose dev-deps don't compile.
319-
// Therefore we ONLY want to scrape Example targets if either:
320-
// (1) No package has dev-dependencies, so this is a moot issue, OR
321-
// (2) The provided CompileFilter requires dev-dependencies anyway.
322-
//
323-
// The next two variables represent these two conditions.
324-
325-
let no_pkg_has_dev_deps = to_builds.iter().all(|pkg| {
326-
pkg.summary()
327-
.dependencies()
328-
.iter()
329-
.all(|dep| !matches!(dep.kind(), DepKind::Development))
330-
});
331-
332-
let reqs_dev_deps = matches!(has_dev_units, HasDevUnits::Yes);
333-
334-
let example_filter = if no_pkg_has_dev_deps || reqs_dev_deps {
335-
FilterRule::All
336-
} else {
337-
FilterRule::none()
338-
};
339-
340-
match self {
341-
CompileFilter::Only {
342-
all_targets,
343-
lib,
344-
bins,
345-
tests,
346-
benches,
347-
..
348-
} => CompileFilter::Only {
349-
all_targets: *all_targets,
350-
lib: lib.clone(),
351-
bins: bins.clone(),
352-
examples: example_filter,
353-
tests: tests.clone(),
354-
benches: benches.clone(),
355-
},
356-
357-
CompileFilter::Default { .. } => CompileFilter::Only {
358-
all_targets: false,
359-
lib: LibRule::Default,
360-
bins: FilterRule::none(),
361-
examples: example_filter,
362-
tests: FilterRule::none(),
363-
benches: FilterRule::none(),
364-
},
365-
}
366-
}
367303
}

src/cargo/ops/cargo_compile/mod.rs

Lines changed: 58 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ use crate::core::compiler::{standard_lib, CrateType, TargetInfo};
4141
use crate::core::compiler::{BuildConfig, BuildContext, Compilation, Context};
4242
use crate::core::compiler::{CompileKind, CompileMode, CompileTarget, RustcTargetData, Unit};
4343
use crate::core::compiler::{DefaultExecutor, Executor, UnitInterner};
44+
use crate::core::dependency::DepKind;
4445
use crate::core::profiles::{Profiles, UnitFor};
4546
use crate::core::resolver::features::{self, CliFeatures, FeaturesFor};
4647
use crate::core::resolver::{HasDevUnits, Resolve};
@@ -361,6 +362,7 @@ pub fn create_bcx<'a, 'cfg>(
361362
&pkg_set,
362363
&profiles,
363364
interner,
365+
has_dev_units,
364366
)?;
365367

366368
if let Some(args) = target_rustc_crate_types {
@@ -369,11 +371,10 @@ pub fn create_bcx<'a, 'cfg>(
369371

370372
let should_scrape = build_config.mode.is_doc() && config.cli_unstable().rustdoc_scrape_examples;
371373
let mut scrape_units = if should_scrape {
372-
let scrape_filter = filter.refine_for_docscrape(&to_builds, has_dev_units);
373374
let all_units = generate_targets(
374375
ws,
375376
&to_builds,
376-
&scrape_filter,
377+
&filter,
377378
&build_config.requested_kinds,
378379
explicit_host_kind,
379380
CompileMode::Docscrape,
@@ -383,6 +384,7 @@ pub fn create_bcx<'a, 'cfg>(
383384
&pkg_set,
384385
&profiles,
385386
interner,
387+
has_dev_units,
386388
)?;
387389

388390
// The set of scraped targets should be a strict subset of the set of documented targets,
@@ -397,10 +399,11 @@ pub fn create_bcx<'a, 'cfg>(
397399
let valid_units = all_units
398400
.into_iter()
399401
.filter(|unit| {
400-
!matches!(
401-
unit.target.doc_scrape_examples(),
402-
RustdocScrapeExamples::Disabled
403-
)
402+
ws.unit_needs_doc_scrape(unit)
403+
&& !matches!(
404+
unit.target.doc_scrape_examples(),
405+
RustdocScrapeExamples::Disabled
406+
)
404407
})
405408
.collect::<Vec<_>>();
406409
valid_units
@@ -597,6 +600,7 @@ fn generate_targets(
597600
package_set: &PackageSet<'_>,
598601
profiles: &Profiles,
599602
interner: &UnitInterner,
603+
has_dev_units: HasDevUnits,
600604
) -> CargoResult<Vec<Unit>> {
601605
let config = ws.config();
602606
// Helper for creating a list of `Unit` structures
@@ -828,6 +832,52 @@ fn generate_targets(
828832
}
829833
}
830834

835+
if mode.is_doc_scrape() {
836+
// In general, the goal is to scrape examples from (a) whatever targets
837+
// the user is documenting, and (b) Example targets. However, if the user
838+
// is documenting a library with dev-dependencies, those dev-deps are not
839+
// needed for the library, while dev-deps are needed for the examples.
840+
//
841+
// If scrape-examples caused `cargo doc` to start requiring dev-deps, this
842+
// would be a breaking change to crates whose dev-deps don't compile.
843+
// Therefore we ONLY want to scrape Example targets if either:
844+
// (1) No package has dev-dependencies, so this is a moot issue, OR
845+
// (2) The provided CompileFilter requires dev-dependencies anyway.
846+
//
847+
// The next two variables represent these two conditions.
848+
let no_pkg_has_dev_deps = packages.iter().all(|pkg| {
849+
pkg.summary()
850+
.dependencies()
851+
.iter()
852+
.all(|dep| !matches!(dep.kind(), DepKind::Development))
853+
});
854+
let reqs_dev_deps = matches!(has_dev_units, HasDevUnits::Yes);
855+
let safe_to_scrape_example_targets = no_pkg_has_dev_deps || reqs_dev_deps;
856+
857+
let proposed_targets: HashSet<&Target> = proposals.iter().map(|p| p.target).collect();
858+
let can_scrape = |target: &Target| {
859+
let not_redundant = !proposed_targets.contains(target);
860+
not_redundant
861+
&& match (target.doc_scrape_examples(), target.is_example()) {
862+
// Targets configured by the user to not be scraped should never be scraped
863+
(RustdocScrapeExamples::Disabled, _) => false,
864+
// Targets configured by the user to be scraped should always be scraped
865+
(RustdocScrapeExamples::Enabled, _) => true,
866+
// Example targets with no configuration should be conditionally scraped if
867+
// it's guaranteed not to break the build
868+
(RustdocScrapeExamples::Unset, true) => safe_to_scrape_example_targets,
869+
// All other targets are ignored for now. This may change in the future!
870+
(RustdocScrapeExamples::Unset, false) => false,
871+
}
872+
};
873+
proposals.extend(filter_targets(
874+
packages,
875+
can_scrape,
876+
false,
877+
CompileMode::Docscrape,
878+
));
879+
}
880+
831881
// Only include targets that are libraries or have all required
832882
// features available.
833883
//
@@ -1074,7 +1124,7 @@ fn filter_default_targets(targets: &[Target], mode: CompileMode) -> Vec<&Target>
10741124
.iter()
10751125
.filter(|t| t.is_bin() || t.is_lib())
10761126
.collect(),
1077-
CompileMode::Doc { .. } => {
1127+
CompileMode::Doc { .. } | CompileMode::Docscrape => {
10781128
// `doc` does lib and bins (bin with same name as lib is skipped).
10791129
targets
10801130
.iter()
@@ -1085,7 +1135,7 @@ fn filter_default_targets(targets: &[Target], mode: CompileMode) -> Vec<&Target>
10851135
})
10861136
.collect()
10871137
}
1088-
CompileMode::Doctest | CompileMode::Docscrape | CompileMode::RunCustomBuild => {
1138+
CompileMode::Doctest | CompileMode::RunCustomBuild => {
10891139
panic!("Invalid mode {:?}", mode)
10901140
}
10911141
}

0 commit comments

Comments
 (0)