Skip to content

Commit

Permalink
Add proc-macro to the index.
Browse files Browse the repository at this point in the history
  • Loading branch information
ehuss committed Mar 15, 2020
1 parent 94093c2 commit 5a1862c
Show file tree
Hide file tree
Showing 10 changed files with 119 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down
2 changes: 1 addition & 1 deletion crates/crates-io/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "crates-io"
version = "0.31.0"
version = "0.32.0"
edition = "2018"
authors = ["Alex Crichton <alex@alexcrichton.com>"]
license = "MIT OR Apache-2.0"
Expand Down
2 changes: 1 addition & 1 deletion crates/crates-io/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ pub struct NewCrate {
pub license_file: Option<String>,
pub repository: Option<String>,
pub badges: BTreeMap<String, BTreeMap<String, String>>,
#[serde(default)]
pub links: Option<String>,
pub proc_macro: bool,
}

#[derive(Serialize)]
Expand Down
7 changes: 7 additions & 0 deletions src/cargo/core/summary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ struct Inner {
checksum: Option<String>,
links: Option<InternedString>,
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 {
Expand All @@ -39,6 +44,7 @@ impl Summary {
features: &BTreeMap<K, Vec<impl AsRef<str>>>,
links: Option<impl Into<InternedString>>,
namespaced_features: bool,
proc_macro: bool,
) -> CargoResult<Summary>
where
K: Borrow<str> + Ord + Display,
Expand Down Expand Up @@ -68,6 +74,7 @@ impl Summary {
checksum: None,
links: links.map(|l| l.into()),
namespaced_features,
proc_macro,
}),
})
}
Expand Down
2 changes: 2 additions & 0 deletions src/cargo/ops/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ fn transmit(
)
})
.collect::<BTreeMap<String, Vec<String>>>();
let proc_macro = pkg.targets().iter().any(|target| target.proc_macro());

let publish = registry.publish(
&NewCrate {
Expand All @@ -275,6 +276,7 @@ fn transmit(
license_file: license_file.clone(),
badges: badges.clone(),
links: links.clone(),
proc_macro,
},
tarball,
);
Expand Down
11 changes: 10 additions & 1 deletion src/cargo/sources/registry/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -715,14 +715,23 @@ 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)?;
let deps = deps
.into_iter()
.map(|dep| dep.into_dep(source_id))
.collect::<CargoResult<Vec<_>>>()?;
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,
Expand Down
14 changes: 14 additions & 0 deletions src/cargo/sources/registry/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ pub struct RegistryConfig {
pub api: Option<String>,
}

/// A single line in the index representing a single version of a package.
#[derive(Deserialize)]
pub struct RegistryPackage<'a> {
name: InternedString,
Expand All @@ -225,8 +226,21 @@ pub struct RegistryPackage<'a> {
deps: Vec<RegistryDependency<'a>>,
features: BTreeMap<InternedString, Vec<InternedString>>,
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<bool>,
/// 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<InternedString>,
/// 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<bool>,
}

#[test]
Expand Down
20 changes: 12 additions & 8 deletions src/cargo/util/toml/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
3 changes: 3 additions & 0 deletions tests/testsuite/alt_registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
68 changes: 68 additions & 0 deletions tests/testsuite/publish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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"],
);
}

0 comments on commit 5a1862c

Please sign in to comment.