From 34215b1b4a5db54ff09526aa8888d51bb458421c Mon Sep 17 00:00:00 2001 From: Weihang Lo Date: Thu, 22 Feb 2024 10:31:48 -0500 Subject: [PATCH] fix: doctest searches native libs in build script outputs 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. --- src/cargo/core/compiler/compilation.rs | 12 ++++++++++++ tests/testsuite/test.rs | 4 ++-- 2 files changed, 14 insertions(+), 2 deletions(-) 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(),