Skip to content

Commit

Permalink
Use Cargo.lock from workspace root
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Elarnon committed Jul 26, 2018
1 parent 52a187c commit 00b3371
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
13 changes: 7 additions & 6 deletions src/bindgen/cargo/cargo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,13 @@ impl Cargo {
clean: bool,
) -> Result<Cargo, Error> {
let toml_path = crate_dir.join("Cargo.toml");
let lock_path = match lock_file {
Some(v) => PathBuf::from(v),
None => crate_dir.join("Cargo.lock"),
};
let metadata = cargo_metadata::metadata(&toml_path)
.map_err(|x| Error::CargoMetadata(toml_path.to_str().unwrap().to_owned(), x))?;
let lock_path = lock_file
.map(PathBuf::from)
.unwrap_or_else(|| {
Path::new(&metadata.workspace_root).join("Cargo.lock")
});

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

// Use the specified binding crate name or infer it from the manifest
let manifest = cargo_toml::manifest(&toml_path)
Expand Down
2 changes: 2 additions & 0 deletions src/bindgen/cargo/cargo_metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ pub struct Metadata {
/// A list of all crates referenced by this crate (and the crate itself)
pub packages: Vec<Package>,
version: usize,
/// path to the workspace containing the `Cargo.lock`
pub workspace_root: String,
}

#[derive(Clone, Deserialize, Debug)]
Expand Down

0 comments on commit 00b3371

Please sign in to comment.