From 5a1862cd36548cf68355562705dc80799b5a62df Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Sun, 15 Mar 2020 10:10:25 -0700 Subject: [PATCH] Add proc-macro to the index. --- Cargo.toml | 2 +- crates/crates-io/Cargo.toml | 2 +- crates/crates-io/lib.rs | 2 +- src/cargo/core/summary.rs | 7 +++ src/cargo/ops/registry.rs | 2 + src/cargo/sources/registry/index.rs | 11 ++++- src/cargo/sources/registry/mod.rs | 14 ++++++ src/cargo/util/toml/mod.rs | 20 +++++---- tests/testsuite/alt_registry.rs | 3 ++ tests/testsuite/publish.rs | 68 +++++++++++++++++++++++++++++ 10 files changed, 119 insertions(+), 12 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 0f8341a03ee..d5a15a223ae 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,7 +22,7 @@ path = "src/cargo/lib.rs" atty = "0.2" bytesize = "1.0" cargo-platform = { path = "crates/cargo-platform", version = "0.1.1" } -crates-io = { path = "crates/crates-io", version = "0.31" } +crates-io = { path = "crates/crates-io", version = "0.32" } crossbeam-utils = "0.7" crypto-hash = "0.3.1" curl = { version = "0.4.23", features = ["http2"] } diff --git a/crates/crates-io/Cargo.toml b/crates/crates-io/Cargo.toml index a4f93c8c0ff..4f56d92d8f9 100644 --- a/crates/crates-io/Cargo.toml +++ b/crates/crates-io/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "crates-io" -version = "0.31.0" +version = "0.32.0" edition = "2018" authors = ["Alex Crichton "] license = "MIT OR Apache-2.0" diff --git a/crates/crates-io/lib.rs b/crates/crates-io/lib.rs index 68e5a3b9b0d..b31aeb2039b 100644 --- a/crates/crates-io/lib.rs +++ b/crates/crates-io/lib.rs @@ -54,8 +54,8 @@ pub struct NewCrate { pub license_file: Option, pub repository: Option, pub badges: BTreeMap>, - #[serde(default)] pub links: Option, + pub proc_macro: bool, } #[derive(Serialize)] diff --git a/src/cargo/core/summary.rs b/src/cargo/core/summary.rs index 365c9e29fba..6248d5a6c87 100644 --- a/src/cargo/core/summary.rs +++ b/src/cargo/core/summary.rs @@ -30,6 +30,11 @@ struct Inner { checksum: Option, links: Option, namespaced_features: bool, + /// Whether or not this package is a proc-macro library. + /// + /// This was added in 2020. Packages published before this will always be + /// `false`. + proc_macro: bool, } impl Summary { @@ -39,6 +44,7 @@ impl Summary { features: &BTreeMap>>, links: Option>, namespaced_features: bool, + proc_macro: bool, ) -> CargoResult where K: Borrow + Ord + Display, @@ -68,6 +74,7 @@ impl Summary { checksum: None, links: links.map(|l| l.into()), namespaced_features, + proc_macro, }), }) } diff --git a/src/cargo/ops/registry.rs b/src/cargo/ops/registry.rs index 18c24a5df2b..cd1c3a0f829 100644 --- a/src/cargo/ops/registry.rs +++ b/src/cargo/ops/registry.rs @@ -255,6 +255,7 @@ fn transmit( ) }) .collect::>>(); + let proc_macro = pkg.targets().iter().any(|target| target.proc_macro()); let publish = registry.publish( &NewCrate { @@ -275,6 +276,7 @@ fn transmit( license_file: license_file.clone(), badges: badges.clone(), links: links.clone(), + proc_macro, }, tarball, ); diff --git a/src/cargo/sources/registry/index.rs b/src/cargo/sources/registry/index.rs index 9bfb15d7e42..d4b87787b60 100644 --- a/src/cargo/sources/registry/index.rs +++ b/src/cargo/sources/registry/index.rs @@ -715,6 +715,7 @@ impl IndexSummary { features, yanked, links, + pm, } = serde_json::from_slice(line)?; log::trace!("json parsed registry {}/{}", name, vers); let pkgid = PackageId::new(name, &vers, source_id)?; @@ -722,7 +723,15 @@ impl IndexSummary { .into_iter() .map(|dep| dep.into_dep(source_id)) .collect::>>()?; - let mut summary = Summary::new(pkgid, deps, &features, links, false)?; + let namespaced_features = false; + let mut summary = Summary::new( + pkgid, + deps, + &features, + links, + namespaced_features, + pm.unwrap_or(false), + )?; summary.set_checksum(cksum); Ok(IndexSummary { summary, diff --git a/src/cargo/sources/registry/mod.rs b/src/cargo/sources/registry/mod.rs index 4e0c1d12d1e..b97b5b9e565 100644 --- a/src/cargo/sources/registry/mod.rs +++ b/src/cargo/sources/registry/mod.rs @@ -217,6 +217,7 @@ pub struct RegistryConfig { pub api: Option, } +/// A single line in the index representing a single version of a package. #[derive(Deserialize)] pub struct RegistryPackage<'a> { name: InternedString, @@ -225,8 +226,21 @@ pub struct RegistryPackage<'a> { deps: Vec>, features: BTreeMap>, cksum: String, + /// If `true`, Cargo will skip this version when resolving. + /// + /// This was added in 2014. Everything in the crates.io index has this set + /// now, so this probably doesn't need to be an option anymore. yanked: Option, + /// Native library name this package links to. + /// + /// Added early 2018 (see https://github.com/rust-lang/cargo/pull/4978), + /// can be `None` if published before then. links: Option, + /// Whether or not this package is a proc-macro library. + /// + /// If `None`, then the status is unknown (crate was published before this + /// field was added), and generally should be treated as `false.` + pm: Option, } #[test] diff --git a/src/cargo/util/toml/mod.rs b/src/cargo/util/toml/mod.rs index e8ea62c36c2..ee258699119 100644 --- a/src/cargo/util/toml/mod.rs +++ b/src/cargo/util/toml/mod.rs @@ -1146,19 +1146,23 @@ impl TomlManifest { features.require(Feature::namespaced_features())?; } + let summary_features = me + .features + .as_ref() + .map(|x| { + x.iter() + .map(|(k, v)| (k.as_str(), v.iter().collect())) + .collect() + }) + .unwrap_or_else(BTreeMap::new); + let proc_macro = targets.iter().any(|target| target.proc_macro()); let summary = Summary::new( pkgid, deps, - &me.features - .as_ref() - .map(|x| { - x.iter() - .map(|(k, v)| (k.as_str(), v.iter().collect())) - .collect() - }) - .unwrap_or_else(BTreeMap::new), + &summary_features, project.links.as_deref(), project.namespaced_features.unwrap_or(false), + proc_macro, )?; let metadata = ManifestMetadata { description: project.description.clone(), diff --git a/tests/testsuite/alt_registry.rs b/tests/testsuite/alt_registry.rs index cf45400b21a..972f81ce7bf 100644 --- a/tests/testsuite/alt_registry.rs +++ b/tests/testsuite/alt_registry.rs @@ -357,6 +357,7 @@ fn publish_with_registry_dependency() { "license_file": null, "links": null, "name": "foo", + "proc_macro": false, "readme": null, "readme_file": null, "repository": null, @@ -456,6 +457,7 @@ fn publish_to_alt_registry() { "license_file": null, "links": null, "name": "foo", + "proc_macro": false, "readme": null, "readme_file": null, "repository": null, @@ -519,6 +521,7 @@ fn publish_with_crates_io_dep() { "license_file": null, "links": null, "name": "foo", + "proc_macro": false, "readme": null, "readme_file": null, "repository": null, diff --git a/tests/testsuite/publish.rs b/tests/testsuite/publish.rs index 345f269764d..05c82df27a6 100644 --- a/tests/testsuite/publish.rs +++ b/tests/testsuite/publish.rs @@ -23,6 +23,7 @@ const CLEAN_FOO_JSON: &str = r#" "license_file": null, "links": null, "name": "foo", + "proc_macro": false, "readme": null, "readme_file": null, "repository": "foo", @@ -47,6 +48,7 @@ fn validate_upload_foo() { "license_file": null, "links": null, "name": "foo", + "proc_macro": false, "readme": null, "readme_file": null, "repository": null, @@ -979,6 +981,7 @@ fn publish_with_patch() { "license_file": null, "links": null, "name": "foo", + "proc_macro": false, "readme": null, "readme_file": null, "repository": null, @@ -1148,6 +1151,7 @@ fn publish_git_with_version() { "license_file": null, "links": null, "name": "foo", + "proc_macro": false, "readme": null, "readme_file": null, "repository": null, @@ -1235,6 +1239,7 @@ fn publish_dev_dep_no_version() { "license_file": null, "links": null, "name": "foo", + "proc_macro": false, "readme": null, "readme_file": null, "repository": "foo", @@ -1295,3 +1300,66 @@ fn credentials_ambiguous_filename() { validate_upload_foo(); } + +#[cargo_test] +fn publish_proc_macro() { + registry::init(); + + let p = project() + .file( + "Cargo.toml", + r#" + [project] + name = "foo" + version = "0.0.1" + authors = [] + license = "MIT" + description = "foo" + edition = "2018" + homepage = "https://example.com" + + [lib] + proc-macro = true + "#, + ) + .file("src/lib.rs", "") + .build(); + + p.cargo("publish --no-verify --index") + .arg(registry_url().to_string()) + .with_stderr( + "\ +[UPDATING] [..] +[PACKAGING] foo v0.0.1 ([CWD]) +[UPLOADING] foo v0.0.1 ([CWD]) +", + ) + .run(); + + publish::validate_upload( + r#" + { + "authors": [], + "badges": {}, + "categories": [], + "deps": [], + "description": "foo", + "documentation": null, + "features": {}, + "homepage": "https://example.com", + "keywords": [], + "license": "MIT", + "license_file": null, + "links": null, + "name": "foo", + "proc_macro": true, + "readme": null, + "readme_file": null, + "repository": null, + "vers": "0.0.1" + } + "#, + "foo-0.0.1.crate", + &["Cargo.toml", "Cargo.toml.orig", "src/lib.rs"], + ); +}