Skip to content

Commit ed4b4b3

Browse files
committed
Properly memoize Docscrape units, update doc tests with scraping-by-default
Update to latest scrape-test flag
1 parent cd59ae5 commit ed4b4b3

File tree

15 files changed

+121
-31
lines changed

15 files changed

+121
-31
lines changed

crates/cargo-test-support/src/compare.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ fn substitute_macros(input: &str) -> String {
117117
("[NOTE]", "note:"),
118118
("[HELP]", "help:"),
119119
("[DOCUMENTING]", " Documenting"),
120+
("[SCRAPING]", " Scraping"),
120121
("[FRESH]", " Fresh"),
121122
("[UPDATING]", " Updating"),
122123
("[ADDING]", " Adding"),

src/cargo/core/compiler/fingerprint.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1276,7 +1276,7 @@ fn calculate_normal(cx: &mut Context<'_, '_>, unit: &Unit) -> CargoResult<Finger
12761276

12771277
// Afterwards calculate our own fingerprint information.
12781278
let target_root = target_root(cx);
1279-
let local = if unit.mode.is_doc() {
1279+
let local = if unit.mode.is_doc() || unit.mode.is_doc_scrape() {
12801280
// rustdoc does not have dep-info files.
12811281
let fingerprint = pkg_fingerprint(cx.bcx, &unit.pkg).with_context(|| {
12821282
format!(
@@ -1303,7 +1303,7 @@ fn calculate_normal(cx: &mut Context<'_, '_>, unit: &Unit) -> CargoResult<Finger
13031303
// Fill out a bunch more information that we'll be tracking typically
13041304
// hashed to take up less space on disk as we just need to know when things
13051305
// change.
1306-
let extra_flags = if unit.mode.is_doc() {
1306+
let extra_flags = if unit.mode.is_doc() || unit.mode.is_doc_scrape() {
13071307
cx.bcx.rustdocflags_args(unit)
13081308
} else {
13091309
cx.bcx.rustflags_args(unit)
@@ -1371,7 +1371,7 @@ fn calculate_run_custom_build(cx: &mut Context<'_, '_>, unit: &Unit) -> CargoRes
13711371
An I/O error happened. Please make sure you can access the file.
13721372
13731373
By default, if your project contains a build script, cargo scans all files in
1374-
it to determine whether a rebuild is needed. If you don't expect to access the
1374+
it to determine whether a rebuild is needed. If you don't expect to access the
13751375
file, specify `rerun-if-changed` in your build script.
13761376
See https://doc.rust-lang.org/cargo/reference/build-scripts.html#rerun-if-changed for more information.";
13771377
pkg_fingerprint(cx.bcx, &unit.pkg).map_err(|err| {

src/cargo/core/compiler/job_queue.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1204,6 +1204,8 @@ impl<'cfg> DrainState<'cfg> {
12041204
config.shell().status("Documenting", &unit.pkg)?;
12051205
} else if unit.mode.is_doc_test() {
12061206
// Skip doc test.
1207+
} else if unit.mode.is_doc_scrape() {
1208+
config.shell().status("Scraping", &unit.pkg)?;
12071209
} else {
12081210
self.compiled.insert(unit.pkg.package_id());
12091211
if unit.mode.is_check() {

src/cargo/core/compiler/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ pub use self::lto::Lto;
5252
use self::output_depinfo::output_depinfo;
5353
use self::unit_graph::UnitDep;
5454
use crate::core::compiler::future_incompat::FutureIncompatReport;
55-
pub use crate::core::compiler::unit::{Unit, UnitInterner};
55+
pub use crate::core::compiler::unit::{Unit, UnitInner, UnitInterner};
5656
use crate::core::manifest::TargetSourcePath;
5757
use crate::core::profiles::{PanicStrategy, Profile, Strip};
5858
use crate::core::{Feature, PackageId, Target};
@@ -659,15 +659,15 @@ fn rustdoc(cx: &mut Context<'_, '_>, unit: &Unit) -> CargoResult<Work> {
659659
rustdoc.arg("-C").arg(format!("metadata={}", metadata));
660660

661661
let scrape_output_path = |unit: &Unit| -> CargoResult<PathBuf> {
662-
let output_dir = cx.files().deps_dir(unit);
663-
Ok(output_dir.join(format!("{}.examples", unit.buildkey())))
662+
cx.outputs(unit)
663+
.map(|outputs| outputs[0].path.to_path_buf())
664664
};
665665

666666
if unit.mode.is_doc_scrape() {
667667
debug_assert!(cx.bcx.scrape_units.contains(unit));
668668

669669
if unit.target.is_test() {
670-
rustdoc.arg("--test");
670+
rustdoc.arg("--scrape-tests");
671671
}
672672

673673
rustdoc.arg("-Zunstable-options");

src/cargo/core/compiler/unit.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ impl UnitInner {
101101

102102
impl Unit {
103103
pub fn buildkey(&self) -> String {
104-
format!("{}-{}", self.pkg.name(), short_hash(self))
104+
format!("{}-{}", self.pkg.name(), short_hash(&*self.inner))
105105
}
106106
}
107107

src/cargo/core/compiler/unit_dependencies.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -712,14 +712,15 @@ fn compute_deps_doc(
712712
if state.ws.unit_needs_doc_scrape(unit) {
713713
for scrape_unit in state.scrape_units.iter() {
714714
deps_of(scrape_unit, state, unit_for)?;
715-
ret.push(new_unit_dep(
715+
ret.push(new_unit_dep_with_profile(
716716
state,
717717
scrape_unit,
718718
&scrape_unit.pkg,
719719
&scrape_unit.target,
720720
unit_for,
721721
scrape_unit.kind,
722722
scrape_unit.mode,
723+
scrape_unit.profile.clone(),
723724
IS_NO_ARTIFACT_DEP,
724725
)?);
725726
}

src/cargo/ops/cargo_compile.rs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ use crate::core::compiler::unit_dependencies::{build_unit_dependencies, IsArtifa
3030
use crate::core::compiler::unit_graph::{self, UnitDep, UnitGraph};
3131
use crate::core::compiler::{standard_lib, CrateType, TargetInfo};
3232
use crate::core::compiler::{BuildConfig, BuildContext, Compilation, Context};
33-
use crate::core::compiler::{CompileKind, CompileMode, CompileTarget, RustcTargetData, Unit};
33+
use crate::core::compiler::{
34+
CompileKind, CompileMode, CompileTarget, RustcTargetData, Unit, UnitInner,
35+
};
3436
use crate::core::compiler::{DefaultExecutor, Executor, UnitInterner};
3537
use crate::core::profiles::{Profiles, UnitFor};
3638
use crate::core::resolver::features::{self, CliFeatures, FeaturesFor};
@@ -510,7 +512,7 @@ pub fn create_bcx<'a, 'cfg>(
510512
override_rustc_crate_types(&mut units, args, interner)?;
511513
}
512514

513-
let mut scrape_units = {
515+
let mut scrape_units = if need_reverse_dependencies {
514516
let to_build_ids = resolve.specs_to_ids(&resolve_specs)?;
515517
let to_builds = pkg_set.get_many(to_build_ids)?;
516518

@@ -530,6 +532,8 @@ pub fn create_bcx<'a, 'cfg>(
530532
&profiles,
531533
interner,
532534
)?
535+
} else {
536+
Vec::new()
533537
};
534538

535539
let std_roots = if let Some(crates) = &config.cli_unstable().build_std {
@@ -1574,9 +1578,12 @@ fn rebuild_unit_graph_shared(
15741578
traverse_and_share(interner, &mut memo, &mut result, &unit_graph, root, to_host)
15751579
})
15761580
.collect();
1581+
// If no unit in the unit graph ended up having scrape units attached as dependencies,
1582+
// then they won't have been discovered in traverse_and_share and hence won't be in
1583+
// memo. So we filter out missing scrape units.
15771584
let new_scrape_units = scrape_units
15781585
.iter()
1579-
.map(|unit| memo.get(unit).unwrap().clone())
1586+
.filter_map(|unit| memo.get(unit).cloned())
15801587
.collect();
15811588
(new_roots, new_scrape_units, result)
15821589
}
@@ -1604,7 +1611,13 @@ fn traverse_and_share(
16041611
.map(|dep| {
16051612
let new_dep_unit =
16061613
traverse_and_share(interner, memo, new_graph, unit_graph, &dep.unit, to_host);
1607-
new_dep_unit.hash(&mut dep_hash);
1614+
1615+
// The default hash for a unit is the interned pointer, but we need dep_hash to
1616+
// be stable across Cargo sessions for Doscrape units to be memoized, so we
1617+
// take care to hash the contents of the unit here.
1618+
let inner: &UnitInner = &*new_dep_unit;
1619+
inner.hash(&mut dep_hash);
1620+
16081621
UnitDep {
16091622
unit: new_dep_unit,
16101623
..dep.clone()

tests/testsuite/build_script.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1317,6 +1317,8 @@ fn testing_and_such() {
13171317
p.cargo("doc -v")
13181318
.with_stderr(
13191319
"\
1320+
[SCRAPING] foo v0.5.0 ([CWD])
1321+
[RUNNING] `rustdoc [..]`
13201322
[DOCUMENTING] foo v0.5.0 ([CWD])
13211323
[RUNNING] `rustdoc [..]`
13221324
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]

tests/testsuite/collisions.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ fn collision_doc_multiple_versions() {
199199
[CHECKING] bar v1.0.0
200200
[DOCUMENTING] bar v2.0.0
201201
[FINISHED] [..]
202+
[SCRAPING] foo v0.1.0 [..]
202203
[DOCUMENTING] foo v0.1.0 [..]
203204
",
204205
)
@@ -378,6 +379,7 @@ fn collision_doc_profile_split() {
378379
[CHECKING] common v1.0.0
379380
[DOCUMENTING] common v1.0.0
380381
[DOCUMENTING] pm v0.1.0 [..]
382+
[SCRAPING] foo v0.1.0 [..]
381383
[DOCUMENTING] foo v0.1.0 [..]
382384
[FINISHED] [..]
383385
",
@@ -424,6 +426,7 @@ the same path; see <https://github.com/rust-lang/cargo/issues/6313>.
424426
[DOCUMENTING] bar v1.0.0 [..]
425427
[DOCUMENTING] bar v1.0.0
426428
[CHECKING] bar v1.0.0
429+
[SCRAPING] foo v0.1.0 [..]
427430
[DOCUMENTING] foo v0.1.0 [..]
428431
[FINISHED] [..]
429432
",
@@ -538,6 +541,7 @@ the same path; see <https://github.com/rust-lang/cargo/issues/6313>.
538541
[CHECKING] foo-macro v1.0.0
539542
[DOCUMENTING] foo-macro v1.0.0
540543
[CHECKING] abc v1.0.0 [..]
544+
[SCRAPING] foo-macro v1.0.0 [..]
541545
[DOCUMENTING] foo-macro v1.0.0 [..]
542546
[DOCUMENTING] abc v1.0.0 [..]
543547
[FINISHED] [..]

0 commit comments

Comments
 (0)