diff --git a/src/cargo/core/compiler/compilation.rs b/src/cargo/core/compiler/compilation.rs index 0e768bfcb11..017c41b8afd 100644 --- a/src/cargo/core/compiler/compilation.rs +++ b/src/cargo/core/compiler/compilation.rs @@ -293,6 +293,18 @@ impl<'gctx> Compilation<'gctx> { ) -> CargoResult { let mut search_path = Vec::new(); if tool_kind.is_rustc_tool() { + if matches!(tool_kind, ToolKind::Rustdoc) { + // HACK: `rustdoc --test` not only compiles but executes doctests. + // Ideally only execution phase should have search paths appended, + // so the executions can find native libs just like other tests. + // However, there is no way to separate these two phase, so this + // hack is added for both phases. + // TODO: handle doctest-xcompile + 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()); } else { search_path.extend(super::filter_dynamic_search_path( diff --git a/tests/testsuite/test.rs b/tests/testsuite/test.rs index f20c707a435..44a830134f5 100644 --- a/tests/testsuite/test.rs +++ b/tests/testsuite/test.rs @@ -2801,8 +2801,8 @@ fn doctest_with_library_paths() { pub fn assert_search_path() {{ let search_path = std::env::var_os("{}").unwrap(); let paths = std::env::split_paths(&search_path).collect::>(); - assert!(!paths.contains(&r#"{}"#.into())); - assert!(!paths.contains(&r#"{}"#.into())); + assert!(paths.contains(&r#"{}"#.into())); + assert!(paths.contains(&r#"{}"#.into())); }} "##, dylib_path_envvar(),