From 88b413ec895d5777422521d6d2015cb66996ab3e Mon Sep 17 00:00:00 2001 From: Sage Griffin Date: Wed, 9 Mar 2022 11:53:04 -0700 Subject: [PATCH] Properly set dylib search path for doctests Fixes #8531. This issue was introduced by 3fd28143de72d2d0e63186273d68aa890d8297e7. Prior to that commit, the `rustdoc_process` function took other branch of the if statement being modified by this commit. The flag was previously called `is_host`, and `rustdoc` was run reporting `false`. In that commit, the flag was changed to `is_rustc_tool`, and rustdoc began reporting `true`, which resulted in the native directories added by build scripts no longer being appended to LD_LIBRARY_PATH when running doctests. This commit changes the behavior so that we are always appending the same set of paths, and the only variance is if we are cross compiling or not. An alternative would be to change rustdoc to always pass `kind: CompileKind::Host, is_rustc_tool: false`, but as rustdoc is in fact a rustc tool, that feels wrong. The commit which introduced the bug did not include context on why the flag was changed to `is_rustc_tool`, so I don't have a sense for if we should just change that flag to mean something else. While the test suite previously had an explicit test for the library path behavior of `cargo run`, it did not test this for various test forms. This commit modifies the test to ensure the behavior is consistent across `run`, normal tests, and doctests. --- src/cargo/core/compiler/compilation.rs | 4 ++++ tests/testsuite/run.rs | 21 ++++++++++++++++++++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/cargo/core/compiler/compilation.rs b/src/cargo/core/compiler/compilation.rs index 38996222486..892aee0b26d 100644 --- a/src/cargo/core/compiler/compilation.rs +++ b/src/cargo/core/compiler/compilation.rs @@ -267,6 +267,10 @@ impl<'cfg> Compilation<'cfg> { ) -> CargoResult { let mut search_path = Vec::new(); if is_rustc_tool { + search_path.extend(super::filter_dynamic_search_path( + self.native_dirs.iter(), + &self.root_output[&CompileKind::Host], + )); search_path.push(self.deps_output[&CompileKind::Host].clone()); search_path.push(self.sysroot_host_libdir.clone()); } else { diff --git a/tests/testsuite/run.rs b/tests/testsuite/run.rs index 0ef9d49cdcf..56d17aafa89 100644 --- a/tests/testsuite/run.rs +++ b/tests/testsuite/run.rs @@ -1146,9 +1146,27 @@ fn run_with_library_paths() { ) .file( "src/main.rs", + r##" + extern crate foo; + + fn main() { + foo::assert_search_path(); + } + "##, + ) + .file( + "src/lib.rs", &format!( r##" - fn main() {{ + #[test] + fn test_search_path() {{ + assert_search_path(); + }} + + /// ``` + /// foo::assert_search_path(); + /// ``` + pub fn assert_search_path() {{ let search_path = std::env::var_os("{}").unwrap(); let paths = std::env::split_paths(&search_path).collect::>(); println!("{{:#?}}", paths); @@ -1164,6 +1182,7 @@ fn run_with_library_paths() { .build(); p.cargo("run").run(); + p.cargo("test").run(); } #[cargo_test]