Skip to content

Commit 9801c50

Browse files
committed
Auto merge of #6167 - ehuss:fix-dylib-feature-flip, r=alexcrichton
Fix dylib reuse with uplift. Due to #5460, the files that are copied to the root of the target dir depend on whether or not it is a direct target. This causes a problem with dylibs if you build a dylib directly, and then separately run a target that depends on that dylib. It will run with the dylib path preferring the root dir, which picks up the old dylib. This PR swaps the preference of the dylib search path so that the dylib in the `deps` dir is preferred. This is maybe not the best solution for dynamic dependencies. I can imagine if you are trying to create a package, you'll want all of the shared libs along with the binary, and digging through the `deps` dir is not optimal. However, unconditionally linking dependencies into the root has issues (which #5460 fixed). There are other issues because shared libs do not include a hash in the filename. A grander solution might involve coming up with a better strategy for organizing the `target` directory to avoid conflicts between targets. Fixes #6162
2 parents b490af4 + b29d4d4 commit 9801c50

File tree

2 files changed

+62
-1
lines changed

2 files changed

+62
-1
lines changed

Diff for: src/cargo/core/compiler/compilation.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,8 @@ impl<'cfg> Compilation<'cfg> {
193193
} else {
194194
let mut search_path =
195195
super::filter_dynamic_search_path(self.native_dirs.iter(), &self.root_output);
196-
search_path.push(self.root_output.clone());
197196
search_path.push(self.deps_output.clone());
197+
search_path.push(self.root_output.clone());
198198
search_path.extend(self.target_dylib_path.clone());
199199
search_path
200200
};

Diff for: tests/testsuite/features.rs

+61
Original file line numberDiff line numberDiff line change
@@ -1669,3 +1669,64 @@ fn all_features_all_crates() {
16691669

16701670
p.cargo("build --all-features --all").run();
16711671
}
1672+
1673+
#[test]
1674+
fn feature_off_dylib() {
1675+
let p = project()
1676+
.file(
1677+
"Cargo.toml",
1678+
r#"
1679+
[workspace]
1680+
members = ["bar"]
1681+
1682+
[package]
1683+
name = "foo"
1684+
version = "0.0.1"
1685+
1686+
[lib]
1687+
crate-type = ["dylib"]
1688+
1689+
[features]
1690+
f1 = []
1691+
"#,
1692+
)
1693+
.file(
1694+
"src/lib.rs",
1695+
r#"
1696+
pub fn hello() -> &'static str {
1697+
if cfg!(feature = "f1") {
1698+
"f1"
1699+
} else {
1700+
"no f1"
1701+
}
1702+
}
1703+
"#,
1704+
)
1705+
.file(
1706+
"bar/Cargo.toml",
1707+
r#"
1708+
[package]
1709+
name = "bar"
1710+
version = "0.0.1"
1711+
1712+
[dependencies]
1713+
foo = { path = ".." }
1714+
"#,
1715+
)
1716+
.file(
1717+
"bar/src/main.rs",
1718+
r#"
1719+
extern crate foo;
1720+
1721+
fn main() {
1722+
assert_eq!(foo::hello(), "no f1");
1723+
}
1724+
"#,
1725+
)
1726+
.build();
1727+
1728+
// Build the dylib with `f1` feature.
1729+
p.cargo("build --features f1").run();
1730+
// Check that building without `f1` uses a dylib without `f1`.
1731+
p.cargo("run -p bar").run();
1732+
}

0 commit comments

Comments
 (0)