Skip to content

Commit 2a4a9b4

Browse files
committed
Auto merge of #11503 - willcrichton:scrape-dev-deps-diagnostic, r=weihanglo
Add warning if potentially-scrapable examples are skipped due to dev-dependencies ### What does this PR try to resolve? Another point of feedback I've received on the scrape-examples feature is that the dev-dependency situation is quite confusing and subtle. To make users more aware of the issue, I added a warning where Cargo will alert users when examples are skipped due to a dev-dependency requirement, along with proposing a fix. ### How should we test and review this PR? The test `docscrape::no_scrape_with_dev_deps` has been updated to reflect this new warning. r? `@weihanglo` (PS thank you for the reviews Weihang. I know I'm doing lots of little patches right now to get this feature finalized. If you want to share the reviewing burden on scrape-examples with anyone else, let me know!)
2 parents 74c7aab + 1c4065c commit 2a4a9b4

File tree

2 files changed

+29
-10
lines changed

2 files changed

+29
-10
lines changed

src/cargo/ops/cargo_compile/unit_generator.rs

Lines changed: 22 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::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,14 @@ 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
474+
.borrow_mut()
475+
.push(target.name().to_string());
476+
}
477+
safe_to_scrape_example_targets
478+
}
470479
// All other targets are ignored for now. This may change in the future!
471480
(RustdocScrapeExamples::Unset, false) => false,
472481
}
@@ -475,6 +484,18 @@ impl<'a> UnitGenerator<'a, '_> {
475484
let mut scrape_proposals = self.filter_targets(can_scrape, false, CompileMode::Docscrape);
476485
scrape_proposals.retain(|proposal| pkgs_to_scrape.contains(proposal.pkg));
477486

487+
let skipped_examples = skipped_examples.into_inner();
488+
if !skipped_examples.is_empty() {
489+
let mut shell = self.ws.config().shell();
490+
let example_str = skipped_examples.join(", ");
491+
shell.warn(format!(
492+
"\
493+
Rustdoc did not scrape the following examples because they require dev-dependencies: {example_str}
494+
If you want Rustdoc to scrape these examples, then add `doc-scrape-examples = true`
495+
to the [[example]] target configuration of at least one example."
496+
))?;
497+
}
498+
478499
Ok(scrape_proposals)
479500
}
480501

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: 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)