Skip to content

Commit 416ed03

Browse files
committed
Avoid adding DYLD_FALLBACK_LIBRARY_PATH defaults when it is set
The macos dynamic linker behavior wrt DYLD_FALLBACK_LIBRARY_PATH is to use the value it is set with, and if there is no such value (the environment variable is either not set or set but empty), it uses a default value of $HOME/lib:/usr/local/lib:/usr/lib. Currently, cargo takes the value of DYLD_FALLBACK_LIBRARY_PATH, prepends its paths to it, and then unconditionally adds $HOME/lib:/usr/local/lib:/usr/lib, which in principle, shouldn't happen if DYLD_FALLBACK_LIBRARY_PATH was set originally.
1 parent 165be97 commit 416ed03

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

src/cargo/core/compiler/compilation.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -178,11 +178,13 @@ impl<'cfg> Compilation<'cfg> {
178178
search_path
179179
};
180180

181-
search_path.extend(util::dylib_path().into_iter());
182-
if cfg!(target_os = "macos") {
181+
let dylib_path = util::dylib_path();
182+
let dylib_path_is_empty = dylib_path.is_empty();
183+
search_path.extend(dylib_path.into_iter());
184+
if cfg!(target_os = "macos") && dylib_path_is_empty {
183185
// These are the defaults when DYLD_FALLBACK_LIBRARY_PATH isn't
184-
// set. Since Cargo is explicitly setting the value, make sure the
185-
// defaults still work.
186+
// set or set to an empty string. Since Cargo is explicitly setting
187+
// the value, make sure the defaults still work.
186188
if let Some(home) = env::var_os("HOME") {
187189
search_path.push(PathBuf::from(home).join("lib"));
188190
}

tests/testsuite/run.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1228,4 +1228,8 @@ fn run_link_system_path_macos() {
12281228
// was set by the cargo that invoked the test.
12291229
p2.cargo("run").env_remove(VAR).run();
12301230
p2.cargo("test").env_remove(VAR).run();
1231+
// Ensure this still works when DYLD_FALLBACK_LIBRARY_PATH has
1232+
// a value set.
1233+
p2.cargo("run").env(VAR, &libdir).run();
1234+
p2.cargo("test").env(VAR, &libdir).run();
12311235
}

0 commit comments

Comments
 (0)