Skip to content

Commit

Permalink
Auto merge of rust-lang#8853 - ehuss:fix-namespaced-publish, r=Eh2406
Browse files Browse the repository at this point in the history
Fix publishing with optional dependencies.

In rust-lang#8799, I neglected to update the `publish` code to use the correct features when generating the JSON to upload to the registry. The `Cargo.toml` file was correctly updated, but the JSON was not.  This caused Cargo to send the implicit `dep:` feature syntax in the JSON blob, which crates.io rejects.  The solution here is to use the original feature map before the implicit features have been added.
  • Loading branch information
bors authored and ehuss committed Nov 24, 2020
1 parent 8662ab4 commit 24438a4
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 12 deletions.
23 changes: 12 additions & 11 deletions src/cargo/ops/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,17 +267,18 @@ fn transmit(
return Ok(());
}

let summary = pkg.summary();
let string_features = summary
.features()
.iter()
.map(|(feat, values)| {
(
feat.to_string(),
values.iter().map(|fv| fv.to_string()).collect(),
)
})
.collect::<BTreeMap<String, Vec<String>>>();
let string_features = match manifest.original().features() {
Some(features) => features
.iter()
.map(|(feat, values)| {
(
feat.to_string(),
values.iter().map(|fv| fv.to_string()).collect(),
)
})
.collect::<BTreeMap<String, Vec<String>>>(),
None => BTreeMap::new(),
};

let publish = registry.publish(
&NewCrate {
Expand Down
111 changes: 110 additions & 1 deletion tests/testsuite/features_namespaced.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Tests for namespaced features.

use cargo_test_support::project;
use cargo_test_support::registry::{Dependency, Package};
use cargo_test_support::{project, publish};

#[cargo_test]
fn gated() {
Expand Down Expand Up @@ -997,3 +997,112 @@ bar v1.0.0
)
.run();
}

#[cargo_test]
fn publish_no_implicit() {
// Does not include implicit features or dep: syntax on publish.
Package::new("opt-dep1", "1.0.0").publish();
Package::new("opt-dep2", "1.0.0").publish();

let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.1.0"
description = "foo"
license = "MIT"
homepage = "https://example.com/"
[dependencies]
opt-dep1 = { version = "1.0", optional = true }
opt-dep2 = { version = "1.0", optional = true }
[features]
feat = ["opt-dep1"]
"#,
)
.file("src/lib.rs", "")
.build();

p.cargo("publish --no-verify --token sekrit")
.with_stderr(
"\
[UPDATING] [..]
[PACKAGING] foo v0.1.0 [..]
[UPLOADING] foo v0.1.0 [..]
",
)
.run();

publish::validate_upload_with_contents(
r#"
{
"authors": [],
"badges": {},
"categories": [],
"deps": [
{
"default_features": true,
"features": [],
"kind": "normal",
"name": "opt-dep1",
"optional": true,
"registry": "https://github.com/rust-lang/crates.io-index",
"target": null,
"version_req": "^1.0"
},
{
"default_features": true,
"features": [],
"kind": "normal",
"name": "opt-dep2",
"optional": true,
"registry": "https://github.com/rust-lang/crates.io-index",
"target": null,
"version_req": "^1.0"
}
],
"description": "foo",
"documentation": null,
"features": {
"feat": ["opt-dep1"]
},
"homepage": "https://example.com/",
"keywords": [],
"license": "MIT",
"license_file": null,
"links": null,
"name": "foo",
"readme": null,
"readme_file": null,
"repository": null,
"vers": "0.1.0"
}
"#,
"foo-0.1.0.crate",
&["Cargo.toml", "Cargo.toml.orig", "src/lib.rs"],
&[(
"Cargo.toml",
r#"[..]
[package]
name = "foo"
version = "0.1.0"
description = "foo"
homepage = "https://example.com/"
license = "MIT"
[dependencies.opt-dep1]
version = "1.0"
optional = true
[dependencies.opt-dep2]
version = "1.0"
optional = true
[features]
feat = ["opt-dep1"]
"#,
)],
);
}

0 comments on commit 24438a4

Please sign in to comment.