diff --git a/src/cargo/core/compiler/compilation.rs b/src/cargo/core/compiler/compilation.rs index 8ec625d77a46..5ecf77d4c6a0 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 662572c3c484..7095333c6b0c 100644 --- a/tests/testsuite/build.rs +++ b/tests/testsuite/build.rs @@ -2173,9 +2173,8 @@ fn issue_14194_deduplicate_library_path_env_var() { let lib_path = std::env::var("{}").unwrap(); let paths: Vec<_> = std::env::split_paths(&lib_path).collect(); if paths.len() > 1 {{ - // The first path will be prepended to the search path in the next run let first = &paths[0]; - assert!(paths[1..].contains(&first)); + assert!(!paths[1..].contains(&first)); }} return; }}