Skip to content

Commit 977254c

Browse files
authored
fix(doctest): Include all search paths with new build layout (#16348)
### What does this PR try to resolve? When compilation was updated for the new build layout, doctests were overlooked. This extracts the search path (`-L`) creation and switches doctests to reuse it. Part of #15010 ### How to test and review this PR?
2 parents f2729c0 + d88e51d commit 977254c

File tree

4 files changed

+109
-39
lines changed

4 files changed

+109
-39
lines changed

src/cargo/core/compiler/build_runner/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@ impl<'a, 'gctx> BuildRunner<'a, 'gctx> {
234234
if unit.mode.is_doc_test() {
235235
let mut unstable_opts = false;
236236
let mut args = compiler::extern_args(&self, unit, &mut unstable_opts)?;
237+
args.extend(compiler::lib_search_paths(&self, unit)?);
237238
args.extend(compiler::lto_args(&self, unit));
238239
args.extend(compiler::features_args(unit));
239240
args.extend(compiler::check_cfg_args(unit));

src/cargo/core/compiler/mod.rs

Lines changed: 38 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1700,37 +1700,9 @@ fn build_deps_args(
17001700
unit: &Unit,
17011701
) -> CargoResult<()> {
17021702
let bcx = build_runner.bcx;
1703-
if build_runner.bcx.gctx.cli_unstable().build_dir_new_layout {
1704-
let mut map = BTreeMap::new();
1705-
1706-
// Recursively add all dependency args to rustc process
1707-
add_dep_arg(&mut map, build_runner, unit);
1708-
1709-
let paths = map.into_iter().map(|(_, path)| path).sorted_unstable();
17101703

1711-
for path in paths {
1712-
cmd.arg("-L").arg(&{
1713-
let mut deps = OsString::from("dependency=");
1714-
deps.push(path);
1715-
deps
1716-
});
1717-
}
1718-
} else {
1719-
cmd.arg("-L").arg(&{
1720-
let mut deps = OsString::from("dependency=");
1721-
deps.push(build_runner.files().deps_dir(unit));
1722-
deps
1723-
});
1724-
}
1725-
1726-
// Be sure that the host path is also listed. This'll ensure that proc macro
1727-
// dependencies are correctly found (for reexported macros).
1728-
if !unit.kind.is_host() {
1729-
cmd.arg("-L").arg(&{
1730-
let mut deps = OsString::from("dependency=");
1731-
deps.push(build_runner.files().host_deps(unit));
1732-
deps
1733-
});
1704+
for arg in lib_search_paths(build_runner, unit)? {
1705+
cmd.arg(arg);
17341706
}
17351707

17361708
let deps = build_runner.unit_deps(unit);
@@ -1847,6 +1819,42 @@ fn add_custom_flags(
18471819
Ok(())
18481820
}
18491821

1822+
/// Generate a list of `-L` arguments
1823+
pub fn lib_search_paths(
1824+
build_runner: &BuildRunner<'_, '_>,
1825+
unit: &Unit,
1826+
) -> CargoResult<Vec<OsString>> {
1827+
let mut lib_search_paths = Vec::new();
1828+
if build_runner.bcx.gctx.cli_unstable().build_dir_new_layout {
1829+
let mut map = BTreeMap::new();
1830+
1831+
// Recursively add all dependency args to rustc process
1832+
add_dep_arg(&mut map, build_runner, unit);
1833+
1834+
let paths = map.into_iter().map(|(_, path)| path).sorted_unstable();
1835+
1836+
for path in paths {
1837+
let mut deps = OsString::from("dependency=");
1838+
deps.push(path);
1839+
lib_search_paths.extend(["-L".into(), deps]);
1840+
}
1841+
} else {
1842+
let mut deps = OsString::from("dependency=");
1843+
deps.push(build_runner.files().deps_dir(unit));
1844+
lib_search_paths.extend(["-L".into(), deps]);
1845+
}
1846+
1847+
// Be sure that the host path is also listed. This'll ensure that proc macro
1848+
// dependencies are correctly found (for reexported macros).
1849+
if !unit.kind.is_host() {
1850+
let mut deps = OsString::from("dependency=");
1851+
deps.push(build_runner.files().host_deps(unit));
1852+
lib_search_paths.extend(["-L".into(), deps]);
1853+
}
1854+
1855+
Ok(lib_search_paths)
1856+
}
1857+
18501858
/// Generates a list of `--extern` arguments.
18511859
pub fn extern_args(
18521860
build_runner: &BuildRunner<'_, '_>,

src/cargo/ops/cargo_test.rs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -229,15 +229,6 @@ fn run_doc_tests(
229229
p.arg("-C").arg(format!("panic={}", unit.profile.panic));
230230
}
231231

232-
for &rust_dep in &[
233-
&compilation.deps_output[&unit.kind],
234-
&compilation.deps_output[&CompileKind::Host],
235-
] {
236-
let mut arg = OsString::from("dependency=");
237-
arg.push(rust_dep);
238-
p.arg("-L").arg(arg);
239-
}
240-
241232
for native_dep in compilation.native_dirs.iter() {
242233
p.arg("-L").arg(native_dep);
243234
}

tests/testsuite/test.rs

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2562,6 +2562,76 @@ fn doctest_dev_dep() {
25622562
p.cargo("test -v").run();
25632563
}
25642564

2565+
#[cargo_test]
2566+
fn doctest_dep() {
2567+
let p = project()
2568+
.file(
2569+
"Cargo.toml",
2570+
r#"
2571+
[package]
2572+
name = "foo"
2573+
version = "0.0.1"
2574+
edition = "2015"
2575+
authors = []
2576+
2577+
[dependencies]
2578+
b = { path = "b" }
2579+
"#,
2580+
)
2581+
.file(
2582+
"src/lib.rs",
2583+
r#"
2584+
/// ```
2585+
/// foo::foo();
2586+
/// ```
2587+
pub fn foo() {
2588+
b::bar();
2589+
}
2590+
"#,
2591+
)
2592+
.file("b/Cargo.toml", &basic_manifest("b", "0.0.1"))
2593+
.file("b/src/lib.rs", "pub fn bar() {}")
2594+
.build();
2595+
2596+
p.cargo("test -v").run();
2597+
}
2598+
2599+
#[cargo_test]
2600+
fn doctest_dep_new_layout() {
2601+
let p = project()
2602+
.file(
2603+
"Cargo.toml",
2604+
r#"
2605+
[package]
2606+
name = "foo"
2607+
version = "0.0.1"
2608+
edition = "2015"
2609+
authors = []
2610+
2611+
[dependencies]
2612+
b = { path = "b" }
2613+
"#,
2614+
)
2615+
.file(
2616+
"src/lib.rs",
2617+
r#"
2618+
/// ```
2619+
/// foo::foo();
2620+
/// ```
2621+
pub fn foo() {
2622+
b::bar();
2623+
}
2624+
"#,
2625+
)
2626+
.file("b/Cargo.toml", &basic_manifest("b", "0.0.1"))
2627+
.file("b/src/lib.rs", "pub fn bar() {}")
2628+
.build();
2629+
2630+
p.cargo("-Zbuild-dir-new-layout test")
2631+
.masquerade_as_nightly_cargo(&["new build-dir layout"])
2632+
.run();
2633+
}
2634+
25652635
#[cargo_test]
25662636
fn filter_no_doc_tests() {
25672637
let p = project()

0 commit comments

Comments
 (0)