Skip to content

Commit 1558944

Browse files
committedMar 13, 2025
rustdoc: Doctest merging: Remove superfluous library search path logic
1 parent aaa2d47 commit 1558944

File tree

7 files changed

+79
-12
lines changed

7 files changed

+79
-12
lines changed
 

‎src/librustdoc/doctest.rs

-12
Original file line numberDiff line numberDiff line change
@@ -672,18 +672,6 @@ fn run_test(
672672
"--extern=doctest_bundle_{edition}=",
673673
edition = doctest.edition
674674
));
675-
for extern_str in &rustdoc_options.extern_strs {
676-
if let Some((_cratename, path)) = extern_str.split_once('=') {
677-
// Direct dependencies of the tests themselves are
678-
// indirect dependencies of the test runner.
679-
// They need to be in the library search path.
680-
let dir = Path::new(path)
681-
.parent()
682-
.filter(|x| x.components().count() > 0)
683-
.unwrap_or(Path::new("."));
684-
runner_compiler.arg("-L").arg(dir);
685-
}
686-
}
687675
let output_bundle_file = doctest
688676
.test_opts
689677
.outdir
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub const STATUS: bool = dep_a_a::STATUS;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub const STATUS: bool = dep_b_b::STATUS;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub const STATUS: bool = true;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub const STATUS: bool = true;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
//! ```
2+
//! assert!(dep_a::STATUS);
3+
//! ```
4+
//!
5+
//! ```
6+
//! assert!(dep_b::STATUS);
7+
//! ```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
//! Since PR <https://github.com/rust-lang/rust/pull/137899>, merged doctests reside in so-called
2+
//! "bundle" crates which are separate from the actual "runner" crates that contain the necessary
3+
//! scaffolding / infrastructure for executing the tests by utilizing the unstable crate `test`
4+
//! and the internal lang feature `rustc_attrs`.
5+
//!
6+
//! In the light of this two-crate setup (per edition), this test ensures that rustdoc can handle
7+
//! mergeable doctests with direct and transitive dependencies (which would become ("first-degree")
8+
//! transitive and "second-degree" transitive dependencies of the runner crates, respectively).
9+
//!
10+
//! While this is about doctest merging which is only available in Rust 2024 and beyond,
11+
//! we also test Rust 2021 here to ensure that in this specific scenario rustdoc doesn't
12+
//! grossly diverge in observable behavior.
13+
14+
use run_make_support::{bare_rustc, rustdoc};
15+
16+
fn main() {
17+
// Re. `bare_rustc` over `rustc` (which would implicitly add `-L.`):
18+
// This test is all about verifying that rustdoc is able to find dependencies
19+
// and properly propagates library search paths etc.
20+
// Anything implicit like that would only obfuscate this test or even
21+
// accidentally break it.
22+
23+
//
24+
// Build crate `a_a` and its dependent `a` which is the direct dependency of
25+
// the doctests inside `doctest.rs` *and* of crate `doctest` itself!
26+
//
27+
28+
bare_rustc().current_dir("deps").input("dep_a_a.rs").crate_type("lib").run();
29+
30+
bare_rustc().input("dep_a.rs").crate_type("lib").args(["--extern=dep_a_a", "-Ldeps"]).run();
31+
32+
//
33+
// Build crate `b_b` and its dependent `b` which is the direct dependency of
34+
// the first doctest in `doctest.rs` *but not* of crate `doctest` itself!
35+
//
36+
37+
bare_rustc().current_dir("deps").input("dep_b_b.rs").crate_type("lib").run();
38+
39+
bare_rustc().input("dep_b.rs").crate_type("lib").args(["--extern=dep_b_b", "-Ldeps"]).run();
40+
41+
//
42+
// Collect and run the doctests inside `doctest.rs`.
43+
//
44+
45+
for edition in ["2021", "2024"] {
46+
// NB: `-Ldependency=<path>` only adds *transitive* dependencies to
47+
// the search path contrary to `-L<path>`.
48+
49+
rustdoc()
50+
.input("doctest.rs")
51+
.crate_type("lib")
52+
.edition(edition)
53+
// Adds crate `a` as a dep of `doctest` *and* its contained doctests.
54+
// Also registers transitive dependencies.
55+
.extern_("dep_a", "libdep_a.rlib")
56+
.arg("-Ldependency=deps")
57+
// Adds crate `b` as a dep of `doctest`'s contained doctests.
58+
// Also registers transitive dependencies.
59+
.args([
60+
"-Zunstable-options",
61+
"--doctest-compilation-args",
62+
"--extern=dep_b=libdep_b.rlib -Ldependency=deps",
63+
])
64+
.arg("--test")
65+
.arg("--test-args=--test-threads=1")
66+
.run();
67+
}
68+
}

0 commit comments

Comments
 (0)