Skip to content

Commit 00b3371

Browse files
committed
Use Cargo.lock from workspace root
cbindgen currently assumes that the `Cargo.lock` is in directly in the crate directory as a sibling to `Cargo.toml`; however that is usually not the case inside a workspace. Instead, this PR extracts the workspace root from the output of `cargo metadata` [1] (already used to get a list of packages) and uses the `Cargo.lock` from there. 1. This is available since Rust 1.24; see rust-lang/cargo#4940
1 parent 52a187c commit 00b3371

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

src/bindgen/cargo/cargo.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,13 @@ impl Cargo {
4646
clean: bool,
4747
) -> Result<Cargo, Error> {
4848
let toml_path = crate_dir.join("Cargo.toml");
49-
let lock_path = match lock_file {
50-
Some(v) => PathBuf::from(v),
51-
None => crate_dir.join("Cargo.lock"),
52-
};
49+
let metadata = cargo_metadata::metadata(&toml_path)
50+
.map_err(|x| Error::CargoMetadata(toml_path.to_str().unwrap().to_owned(), x))?;
51+
let lock_path = lock_file
52+
.map(PathBuf::from)
53+
.unwrap_or_else(|| {
54+
Path::new(&metadata.workspace_root).join("Cargo.lock")
55+
});
5356

5457
let lock = if use_cargo_lock {
5558
match cargo_lock::lock(&lock_path) {
@@ -62,8 +65,6 @@ impl Cargo {
6265
} else {
6366
None
6467
};
65-
let metadata = cargo_metadata::metadata(&toml_path)
66-
.map_err(|x| Error::CargoMetadata(toml_path.to_str().unwrap().to_owned(), x))?;
6768

6869
// Use the specified binding crate name or infer it from the manifest
6970
let manifest = cargo_toml::manifest(&toml_path)

src/bindgen/cargo/cargo_metadata.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ pub struct Metadata {
2424
/// A list of all crates referenced by this crate (and the crate itself)
2525
pub packages: Vec<Package>,
2626
version: usize,
27+
/// path to the workspace containing the `Cargo.lock`
28+
pub workspace_root: String,
2729
}
2830

2931
#[derive(Clone, Deserialize, Debug)]

0 commit comments

Comments
 (0)