diff --git a/src/cargo/core/compiler/compilation.rs b/src/cargo/core/compiler/compilation.rs index 13d5d96be57..99fab990ae8 100644 --- a/src/cargo/core/compiler/compilation.rs +++ b/src/cargo/core/compiler/compilation.rs @@ -178,16 +178,17 @@ impl<'cfg> Compilation<'cfg> { search_path }; - search_path.extend(util::dylib_path().into_iter()); - if cfg!(target_os = "macos") { + let dylib_path = util::dylib_path(); + let dylib_path_is_empty = dylib_path.is_empty(); + 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. Since Cargo is explicitly setting the value, make sure the - // defaults still work. - if let Ok(home) = env::var("HOME") { + // set or set to an empty string. Since Cargo is explicitly setting + // the value, make sure the defaults still work. + if let Some(home) = env::var_os("HOME") { search_path.push(PathBuf::from(home).join("lib")); } search_path.push(PathBuf::from("/usr/local/lib")); - search_path.push(PathBuf::from("/lib")); search_path.push(PathBuf::from("/usr/lib")); } let search_path = join_paths(&search_path, util::dylib_path_envvar())?; diff --git a/tests/testsuite/run.rs b/tests/testsuite/run.rs index e596dede15f..25494a51cc4 100644 --- a/tests/testsuite/run.rs +++ b/tests/testsuite/run.rs @@ -1223,6 +1223,13 @@ fn run_link_system_path_macos() { ) .unwrap(); p.root().rm_rf(); - p2.cargo("run").run(); - p2.cargo("test").run(); + const VAR: &str = "DYLD_FALLBACK_LIBRARY_PATH"; + // Reset DYLD_FALLBACK_LIBRARY_PATH so that we don't inherit anything that + // was set by the cargo that invoked the test. + p2.cargo("run").env_remove(VAR).run(); + p2.cargo("test").env_remove(VAR).run(); + // Ensure this still works when DYLD_FALLBACK_LIBRARY_PATH has + // a value set. + p2.cargo("run").env(VAR, &libdir).run(); + p2.cargo("test").env(VAR, &libdir).run(); }