diff --git a/src/cargo/core/compiler/build_runner/mod.rs b/src/cargo/core/compiler/build_runner/mod.rs index 1768dccaf35..02f3c62267a 100644 --- a/src/cargo/core/compiler/build_runner/mod.rs +++ b/src/cargo/core/compiler/build_runner/mod.rs @@ -234,6 +234,7 @@ impl<'a, 'gctx> BuildRunner<'a, 'gctx> { if unit.mode.is_doc_test() { let mut unstable_opts = false; let mut args = compiler::extern_args(&self, unit, &mut unstable_opts)?; + args.extend(compiler::lib_search_paths(&self, unit)?); args.extend(compiler::lto_args(&self, unit)); args.extend(compiler::features_args(unit)); args.extend(compiler::check_cfg_args(unit)); diff --git a/src/cargo/core/compiler/mod.rs b/src/cargo/core/compiler/mod.rs index 974c58abc7f..4ad36afdbb8 100644 --- a/src/cargo/core/compiler/mod.rs +++ b/src/cargo/core/compiler/mod.rs @@ -1700,37 +1700,9 @@ fn build_deps_args( unit: &Unit, ) -> CargoResult<()> { let bcx = build_runner.bcx; - if build_runner.bcx.gctx.cli_unstable().build_dir_new_layout { - let mut map = BTreeMap::new(); - - // Recursively add all dependency args to rustc process - add_dep_arg(&mut map, build_runner, unit); - - let paths = map.into_iter().map(|(_, path)| path).sorted_unstable(); - for path in paths { - cmd.arg("-L").arg(&{ - let mut deps = OsString::from("dependency="); - deps.push(path); - deps - }); - } - } else { - cmd.arg("-L").arg(&{ - let mut deps = OsString::from("dependency="); - deps.push(build_runner.files().deps_dir(unit)); - deps - }); - } - - // Be sure that the host path is also listed. This'll ensure that proc macro - // dependencies are correctly found (for reexported macros). - if !unit.kind.is_host() { - cmd.arg("-L").arg(&{ - let mut deps = OsString::from("dependency="); - deps.push(build_runner.files().host_deps(unit)); - deps - }); + for arg in lib_search_paths(build_runner, unit)? { + cmd.arg(arg); } let deps = build_runner.unit_deps(unit); @@ -1847,6 +1819,42 @@ fn add_custom_flags( Ok(()) } +/// Generate a list of `-L` arguments +pub fn lib_search_paths( + build_runner: &BuildRunner<'_, '_>, + unit: &Unit, +) -> CargoResult> { + let mut lib_search_paths = Vec::new(); + if build_runner.bcx.gctx.cli_unstable().build_dir_new_layout { + let mut map = BTreeMap::new(); + + // Recursively add all dependency args to rustc process + add_dep_arg(&mut map, build_runner, unit); + + let paths = map.into_iter().map(|(_, path)| path).sorted_unstable(); + + for path in paths { + let mut deps = OsString::from("dependency="); + deps.push(path); + lib_search_paths.extend(["-L".into(), deps]); + } + } else { + let mut deps = OsString::from("dependency="); + deps.push(build_runner.files().deps_dir(unit)); + lib_search_paths.extend(["-L".into(), deps]); + } + + // Be sure that the host path is also listed. This'll ensure that proc macro + // dependencies are correctly found (for reexported macros). + if !unit.kind.is_host() { + let mut deps = OsString::from("dependency="); + deps.push(build_runner.files().host_deps(unit)); + lib_search_paths.extend(["-L".into(), deps]); + } + + Ok(lib_search_paths) +} + /// Generates a list of `--extern` arguments. pub fn extern_args( build_runner: &BuildRunner<'_, '_>, diff --git a/src/cargo/ops/cargo_test.rs b/src/cargo/ops/cargo_test.rs index 8a52b14ed6c..0e3e309c3b6 100644 --- a/src/cargo/ops/cargo_test.rs +++ b/src/cargo/ops/cargo_test.rs @@ -229,15 +229,6 @@ fn run_doc_tests( p.arg("-C").arg(format!("panic={}", unit.profile.panic)); } - for &rust_dep in &[ - &compilation.deps_output[&unit.kind], - &compilation.deps_output[&CompileKind::Host], - ] { - let mut arg = OsString::from("dependency="); - arg.push(rust_dep); - p.arg("-L").arg(arg); - } - for native_dep in compilation.native_dirs.iter() { p.arg("-L").arg(native_dep); } diff --git a/tests/testsuite/test.rs b/tests/testsuite/test.rs index 6478e767ee1..dacff7631b7 100644 --- a/tests/testsuite/test.rs +++ b/tests/testsuite/test.rs @@ -2562,6 +2562,76 @@ fn doctest_dev_dep() { p.cargo("test -v").run(); } +#[cargo_test] +fn doctest_dep() { + let p = project() + .file( + "Cargo.toml", + r#" + [package] + name = "foo" + version = "0.0.1" + edition = "2015" + authors = [] + + [dependencies] + b = { path = "b" } + "#, + ) + .file( + "src/lib.rs", + r#" + /// ``` + /// foo::foo(); + /// ``` + pub fn foo() { + b::bar(); + } + "#, + ) + .file("b/Cargo.toml", &basic_manifest("b", "0.0.1")) + .file("b/src/lib.rs", "pub fn bar() {}") + .build(); + + p.cargo("test -v").run(); +} + +#[cargo_test] +fn doctest_dep_new_layout() { + let p = project() + .file( + "Cargo.toml", + r#" + [package] + name = "foo" + version = "0.0.1" + edition = "2015" + authors = [] + + [dependencies] + b = { path = "b" } + "#, + ) + .file( + "src/lib.rs", + r#" + /// ``` + /// foo::foo(); + /// ``` + pub fn foo() { + b::bar(); + } + "#, + ) + .file("b/Cargo.toml", &basic_manifest("b", "0.0.1")) + .file("b/src/lib.rs", "pub fn bar() {}") + .build(); + + p.cargo("-Zbuild-dir-new-layout test") + .masquerade_as_nightly_cargo(&["new build-dir layout"]) + .run(); +} + #[cargo_test] fn filter_no_doc_tests() { let p = project()