Skip to content

Commit c9afed1

Browse files
committed
Fix dependency resolution with lockfile v2.
Lockfile v2 only includes version numbers if they're conflicting. Fixes mozilla#436
1 parent ae5c1b1 commit c9afed1

File tree

5 files changed

+27
-11
lines changed

5 files changed

+27
-11
lines changed

src/bindgen/cargo/cargo.rs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ use bindgen::error::Error;
1313
use bindgen::ir::Cfg;
1414

1515
/// Parse a dependency string used in Cargo.lock
16-
fn parse_dep_string(dep_string: &str) -> (&str, &str) {
16+
fn parse_dep_string(dep_string: &str) -> (&str, Option<&str>) {
1717
let split: Vec<&str> = dep_string.split_whitespace().collect();
1818

19-
(split[0], split[1])
19+
(split[0], split.get(1).cloned())
2020
}
2121

2222
/// A collection of metadata for a library from cargo.
@@ -99,14 +99,25 @@ impl Cargo {
9999

100100
// Find the dependencies listing in the lockfile
101101
if let Some(ref root) = lock.root {
102-
if root.name == package.name && root.version == package.version {
102+
// If the version is not on the lockfile then it shouldn't be
103+
// ambiguous.
104+
if root.name == package.name
105+
&& package
106+
.version
107+
.as_ref()
108+
.map_or(true, |v| *v == root.version)
109+
{
103110
dependencies = root.dependencies.as_ref();
104111
}
105112
}
106113
if dependencies.is_none() {
107114
if let Some(ref lock_packages) = lock.package {
108115
for lock_package in lock_packages {
109-
if lock_package.name == package.name && lock_package.version == package.version
116+
if lock_package.name == package.name
117+
&& package
118+
.version
119+
.as_ref()
120+
.map_or(true, |v| *v == lock_package.version)
110121
{
111122
dependencies = lock_package.dependencies.as_ref();
112123
break;
@@ -134,7 +145,7 @@ impl Cargo {
134145

135146
let package_ref = PackageRef {
136147
name: dep_name.to_owned(),
137-
version: dep_version.to_owned(),
148+
version: dep_version.map(|v| v.to_owned()),
138149
};
139150

140151
(package_ref, cfg)
@@ -202,7 +213,7 @@ impl Cargo {
202213
cargo_expand::expand(
203214
&self.manifest_path,
204215
&package.name,
205-
&package.version,
216+
package.version.as_ref().map(|v| &**v),
206217
self.clean,
207218
expand_all_features,
208219
expand_default_features,

src/bindgen/cargo/cargo_expand.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ impl error::Error for Error {
6060
pub fn expand(
6161
manifest_path: &Path,
6262
crate_name: &str,
63-
version: &str,
63+
version: Option<&str>,
6464
use_tempdir: bool,
6565
expand_all_features: bool,
6666
expand_default_features: bool,
@@ -109,7 +109,12 @@ pub fn expand(
109109
cmd.arg("--no-default-features");
110110
}
111111
cmd.arg("-p");
112-
cmd.arg(&format!("{}:{}", crate_name, version));
112+
let mut package = crate_name.to_owned();
113+
if let Some(version) = version {
114+
package.push_str(":");
115+
package.push_str(version);
116+
}
117+
cmd.arg(&package);
113118
cmd.arg("--verbose");
114119
cmd.arg("--");
115120
cmd.arg("-Z");

src/bindgen/cargo/cargo_lock.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ pub struct Lock {
3939
pub struct Package {
4040
pub name: String,
4141
pub version: String,
42-
/// A list of dependencies formatted like "NAME VERSION REGISTRY-OPT"
42+
/// A list of dependencies formatted like "NAME VERSION-OPT REGISTRY-OPT"
4343
pub dependencies: Option<Vec<String>>,
4444
}
4545

src/bindgen/cargo/cargo_metadata.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ pub struct Metadata {
3636
#[derive(Clone, Debug, Hash, Eq, PartialEq, Serialize, Deserialize)]
3737
pub struct PackageRef {
3838
pub name: String,
39-
pub version: String,
39+
pub version: Option<String>,
4040
}
4141

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

src/bindgen/parser.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ pub fn parse_src(src_file: &FilePath, config: &Config) -> ParseResult {
5252

5353
let pkg_ref = PackageRef {
5454
name: mod_name.to_owned(),
55-
version: "0.0.0".to_owned(),
55+
version: None,
5656
};
5757

5858
context.parse_mod(&pkg_ref, src_file)?;

0 commit comments

Comments
 (0)