Skip to content

Commit 027bd44

Browse files
committed
Fix dependency renaming in published crates
Turns out that when a crate uses a dependency with a `package` key, the meaning of `name` and `rename` gets inverted. This is a bit confusing so let's look at an example. We have a package that depends on hyper through a `package` but that has a local name that doesn't exist in the registry. ``` [package] name = "test_dep_resolver" ... [dependencies] thisdoesntexist = { package = "hyper", version = "0.14" } ``` When published to crates.io, it will generate the following: (visible here: https://github.com/rust-lang/crates.io-index/blob/master/te/st/test_dep_resolver) ``` { "name": "test_dep_resolver", "vers": "0.1.1", "deps": [ { "name": "thisdoesntexist", "req": "^0.14", "features": [], "optional": false, "default_features": true, "target": null, "kind": "normal", "package": "hyper" } ], "cksum": "1484b56f82049380557a5799807afd981e7d443d053710ff49494db34f151b5a", "features": {}, "yanked": false, "links": null } ``` As we can see, the hyper dependency has the name `thisdoesntexist` and `package` hyper. Now let's have a look at the `cargo metadata` output: ``` > cat local | jq '.packages[] | select(.name == "test_dep_resolver") | .dependencies[] | select (.name == "hyper")' { "name": "hyper", "source": "registry+https://github.com/rust-lang/crates.io-index", "req": "^0.14", "kind": null, "rename": "thisdoesntexist", "optional": false, "uses_default_features": true, "features": [], "target": null, "registry": null } ``` Here we can see `name` is `hyper` and `rename` is `thisdoesntexist`. With the previous code, they were mapped 1-1 with `name` and `package` but need to be inverted to match what cargo is expecting.
1 parent 19aafa0 commit 027bd44

File tree

1 file changed

+21
-13
lines changed

1 file changed

+21
-13
lines changed

src/metadata.rs

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,27 @@ pub fn transform(
2626
deps: package
2727
.dependencies
2828
.into_iter()
29-
.map(|v| CargoIndexCrateMetadataDependency {
30-
name: v.name,
31-
req: v.req,
32-
features: v.features,
33-
optional: v.optional,
34-
default_features: v.uses_default_features,
35-
target: v.target,
36-
kind: v.kind,
37-
registry: Some(v.registry.map_or(
38-
Cow::Borrowed("https://github.com/rust-lang/crates.io-index.git"),
39-
Cow::Owned,
40-
)),
41-
package: v.rename,
29+
.map(|v| {
30+
let (name, package) = if let Some(rename) = v.rename {
31+
(rename, Some(v.name))
32+
} else {
33+
(v.name, None)
34+
};
35+
36+
CargoIndexCrateMetadataDependency {
37+
name,
38+
req: v.req,
39+
features: v.features,
40+
optional: v.optional,
41+
default_features: v.uses_default_features,
42+
target: v.target,
43+
kind: v.kind,
44+
registry: Some(v.registry.map_or(
45+
Cow::Borrowed("https://github.com/rust-lang/crates.io-index.git"),
46+
Cow::Owned,
47+
)),
48+
package,
49+
}
4250
})
4351
.collect(),
4452
cksum,

0 commit comments

Comments
 (0)