diff --git a/src/cargo/core/compiler/compilation.rs b/src/cargo/core/compiler/compilation.rs index 8ec625d77a4..5ecf77d4c6a 100644 --- a/src/cargo/core/compiler/compilation.rs +++ b/src/cargo/core/compiler/compilation.rs @@ -323,7 +323,11 @@ impl<'gctx> Compilation<'gctx> { let dylib_path = paths::dylib_path(); let dylib_path_is_empty = dylib_path.is_empty(); - search_path.extend(dylib_path.into_iter()); + if dylib_path.starts_with(&search_path) { + search_path = dylib_path; + } else { + search_path.extend(dylib_path.into_iter()); + } if cfg!(target_os = "macos") && dylib_path_is_empty { // These are the defaults when DYLD_FALLBACK_LIBRARY_PATH isn't // set or set to an empty string. Since Cargo is explicitly setting diff --git a/tests/testsuite/build.rs b/tests/testsuite/build.rs index 3cc4aa8b8c5..b7c87615437 100644 --- a/tests/testsuite/build.rs +++ b/tests/testsuite/build.rs @@ -2178,7 +2178,7 @@ fn issue_14194_deduplicate_library_path_env_var() { let prev_lib_path = std::fs::read_to_string(txt).unwrap(); // Ensure no duplicate insertion to dylib search paths // when calling `cargo run` recursively. - assert_ne!(lib_path, prev_lib_path); + assert_eq!(lib_path, prev_lib_path); }} if level == 0 {{