Skip to content

Commit 6b9a28e

Browse files
committed
Add warning if potentially-scrapable examples are skipped due to dev-dependencies
1 parent eb829cf commit 6b9a28e

File tree

2 files changed

+31
-10
lines changed

2 files changed

+31
-10
lines changed

src/cargo/ops/cargo_compile/unit_generator.rs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::cell::RefCell;
12
use std::collections::{HashMap, HashSet};
23
use std::fmt::Write;
34

@@ -458,6 +459,7 @@ impl<'a> UnitGenerator<'a, '_> {
458459
.map(|u| &u.pkg)
459460
.collect::<HashSet<_>>();
460461

462+
let skipped_examples: RefCell<Vec<Target>> = RefCell::new(Vec::new());
461463
let can_scrape = |target: &Target| {
462464
match (target.doc_scrape_examples(), target.is_example()) {
463465
// Targets configured by the user to not be scraped should never be scraped
@@ -466,7 +468,12 @@ impl<'a> UnitGenerator<'a, '_> {
466468
(RustdocScrapeExamples::Enabled, _) => true,
467469
// Example targets with no configuration should be conditionally scraped if
468470
// it's guaranteed not to break the build
469-
(RustdocScrapeExamples::Unset, true) => safe_to_scrape_example_targets,
471+
(RustdocScrapeExamples::Unset, true) => {
472+
if !safe_to_scrape_example_targets {
473+
skipped_examples.borrow_mut().push(target.clone());
474+
}
475+
safe_to_scrape_example_targets
476+
}
470477
// All other targets are ignored for now. This may change in the future!
471478
(RustdocScrapeExamples::Unset, false) => false,
472479
}
@@ -475,6 +482,22 @@ impl<'a> UnitGenerator<'a, '_> {
475482
let mut scrape_proposals = self.filter_targets(can_scrape, false, CompileMode::Docscrape);
476483
scrape_proposals.retain(|proposal| pkgs_to_scrape.contains(proposal.pkg));
477484

485+
let skipped_examples = skipped_examples.into_inner();
486+
if !skipped_examples.is_empty() {
487+
let mut shell = self.ws.config().shell();
488+
let example_str = skipped_examples
489+
.into_iter()
490+
.map(|t| t.description_named())
491+
.collect::<Vec<_>>()
492+
.join(", ");
493+
shell.warn(format!(
494+
"\
495+
Rustdoc did not scrape the following examples because they require dev-dependencies: {example_str}
496+
If you want Rustdoc to scrape these examples, then add `doc-scrape-examples = true`
497+
to the [[example]] target configuration of at least one example."
498+
))?;
499+
}
500+
478501
Ok(scrape_proposals)
479502
}
480503

tests/testsuite/docscrape.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,7 @@ error: expected one of `!` or `::`, found `NOT`
429429
fn no_scrape_with_dev_deps() {
430430
// Tests that a crate with dev-dependencies does not have its examples
431431
// scraped unless explicitly prompted to check them. See
432-
// `CompileFilter::refine_for_docscrape` for details on why.
432+
// `UnitGenerator::create_docscrape_proposals` for details on why.
433433

434434
let p = project()
435435
.file(
@@ -440,9 +440,6 @@ fn no_scrape_with_dev_deps() {
440440
version = "0.0.1"
441441
authors = []
442442
443-
[lib]
444-
doc-scrape-examples = false
445-
446443
[dev-dependencies]
447444
a = {path = "a"}
448445
"#,
@@ -461,17 +458,21 @@ fn no_scrape_with_dev_deps() {
461458
.file("a/src/lib.rs", "pub fn f() {}")
462459
.build();
463460

464-
// If --examples is not provided, then the example is never scanned.
461+
// If --examples is not provided, then the example is not scanned, and a warning
462+
// should be raised.
465463
p.cargo("doc -Zunstable-options -Z rustdoc-scrape-examples")
466464
.masquerade_as_nightly_cargo(&["rustdoc-scrape-examples"])
467465
.with_stderr(
468466
"\
467+
warning: Rustdoc did not scrape the following examples because they require dev-dependencies: example \"ex\"
468+
If you want Rustdoc to scrape these examples, then add `doc-scrape-examples = true`
469+
to the [[example]] target configuration of at least one example.
469470
[DOCUMENTING] foo v0.0.1 ([CWD])
470471
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]",
471472
)
472473
.run();
473474

474-
// If --examples is provided, then the bad example is scanned and ignored.
475+
// If --examples is provided, then the example is scanned.
475476
p.cargo("doc --examples -Zunstable-options -Z rustdoc-scrape-examples")
476477
.masquerade_as_nightly_cargo(&["rustdoc-scrape-examples"])
477478
.with_stderr_unordered(
@@ -497,9 +498,6 @@ fn use_dev_deps_if_explicitly_enabled() {
497498
version = "0.0.1"
498499
authors = []
499500
500-
[lib]
501-
doc-scrape-examples = false
502-
503501
[[example]]
504502
name = "ex"
505503
doc-scrape-examples = true

0 commit comments

Comments
 (0)