-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Scrape code examples from examples/ directory for Rustdoc #9525
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
Changes from 28 commits
8acf0e8
3f9a4f2
a0122df
63c4346
49a3a54
4ac6835
b14a778
711539f
6772991
5ed35a4
d19cfd2
48056e5
ff13eb5
0c8e1f8
0b2e293
b9b39a6
dbcabc7
0792cde
82d937e
70f3821
d29ac15
8331d7d
223adac
19c8f05
4705566
17c6df7
8b06a0f
e52a9d9
b948fc8
e4a65b9
0deeea8
1120957
0a2382b
33718c7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,7 @@ mod unit; | |
pub mod unit_dependencies; | ||
pub mod unit_graph; | ||
|
||
use std::collections::HashSet; | ||
use std::env; | ||
use std::ffi::{OsStr, OsString}; | ||
use std::fs::{self, File}; | ||
|
@@ -165,7 +166,7 @@ fn compile<'cfg>( | |
let force = exec.force_rebuild(unit) || force_rebuild; | ||
let mut job = fingerprint::prepare_target(cx, unit, force)?; | ||
job.before(if job.freshness() == Freshness::Dirty { | ||
let work = if unit.mode.is_doc() { | ||
let work = if unit.mode.is_doc() || unit.mode.is_doc_scrape() { | ||
rustdoc(cx, unit)? | ||
} else { | ||
rustc(cx, unit, exec)? | ||
|
@@ -647,6 +648,66 @@ fn rustdoc(cx: &mut Context<'_, '_>, unit: &Unit) -> CargoResult<Work> { | |
rustdoc.args(args); | ||
} | ||
|
||
// rustdoc needs a -Cmetadata flag in order to recognize StableCrateIds that refer to | ||
// items in the crate being documented. The -Cmetadata flag used by reverse-dependencies | ||
// will be the metadata of the Cargo unit that generated the current library's rmeta file, | ||
// which should be a Check unit. | ||
// | ||
// If the current crate has reverse-dependencies, such a Check unit should exist, and so | ||
// we use that crate's metadata. If not, we use the crate's Doc unit so at least examples | ||
// scraped from the current crate can be used when documenting the current crate. | ||
let matching_units = cx | ||
.bcx | ||
.unit_graph | ||
.keys() | ||
.filter(|other| { | ||
unit.pkg == other.pkg && unit.target == other.target && !other.mode.is_doc_scrape() | ||
}) | ||
.collect::<Vec<_>>(); | ||
let metadata_unit = matching_units | ||
.iter() | ||
.find(|other| other.mode.is_check()) | ||
.or_else(|| matching_units.iter().find(|other| other.mode.is_doc())) | ||
.unwrap_or(&unit); | ||
let metadata = cx.files().metadata(metadata_unit); | ||
rustdoc.arg("-C").arg(format!("metadata={}", metadata)); | ||
alexcrichton marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
let scrape_output_path = |unit: &Unit| -> CargoResult<PathBuf> { | ||
let layout = cx.files().layout(unit.kind); | ||
let output_dir = layout.prepare_tmp()?; | ||
Ok(output_dir.join(format!("{}.examples", unit.buildkey()))) | ||
}; | ||
|
||
if unit.mode.is_doc_scrape() { | ||
rustdoc.arg("-Zunstable-options"); | ||
|
||
rustdoc | ||
.arg("--scrape-examples-output-path") | ||
.arg(scrape_output_path(unit)?); | ||
|
||
// Limit the scraped examples to just crates in the root set | ||
let root_packages = cx | ||
.bcx | ||
.roots | ||
.iter() | ||
.map(|root| root.pkg.name()) | ||
.collect::<HashSet<_>>(); | ||
for pkg in root_packages { | ||
rustdoc.arg("--scrape-examples-target-crate").arg(pkg); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How does this interact if you do something like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the other way around. If you have a file fn main() {
foo::f();
bar::f();
baz::f();
Vec::new();
} Then the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What if There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure what your concern is. This flag is being passed to reverse-dependencies being scraped, not to packages being documented. So this isn't saying "documentation for crate There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm still slightly concerned about this but I don't know enough about these new rustdoc flags to really bottom our my concern so "if it works it works" |
||
} else if cx.bcx.scrape_units.len() > 0 && cx.bcx.roots.contains(unit) { | ||
willcrichton marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// We only pass scraped examples to packages in the workspace (bcx.roots) | ||
// since examples are only coming from reverse-dependencies of workspace packages | ||
|
||
rustdoc.arg("-Zunstable-options"); | ||
|
||
for scrape_unit in &cx.bcx.scrape_units { | ||
rustdoc | ||
.arg("--with-examples") | ||
.arg(scrape_output_path(scrape_unit)?); | ||
} | ||
} | ||
|
||
build_deps_args(&mut rustdoc, cx, unit)?; | ||
rustdoc::add_root_urls(cx, unit, &mut rustdoc)?; | ||
|
||
|
Uh oh!
There was an error while loading. Please reload this page.