diff --git a/src/main.rs b/src/main.rs index b78235e..7b561a4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,15 +1,14 @@ +use anyhow::Context as _; use cargo::core::dependency::DepKind; use cargo::core::resolver::Resolve; -use cargo::core::{Package, SourceId, Workspace}; +use cargo::core::{FeatureValue, Package, SourceId, Workspace}; use cargo::sources::PathSource; use cargo::util::errors::*; -use anyhow::{Context as _}; use cargo::util::Config; use cargo_platform::Platform; use docopt::Docopt; use flate2::write::GzEncoder; use serde::{Deserialize, Serialize}; -use url::Url; use std::collections::{BTreeMap, HashSet}; use std::env; use std::fs::{self, File}; @@ -17,6 +16,7 @@ use std::io; use std::io::prelude::*; use std::path::{self, Path, PathBuf}; use tar::{Builder, Header}; +use url::Url; #[derive(Deserialize)] struct Options { @@ -49,7 +49,7 @@ struct RegistryDependency { default_features: bool, target: Option, kind: Option, - package: Option + package: Option, } fn main() { @@ -104,7 +104,7 @@ fn real_main(options: Options, config: &mut Config) -> CargoResult<()> { /* offline = */ false, /* target dir = */ &None, /* unstable flags = */ &[], - /* cli_config = */ &[] + /* cli_config = */ &[], )?; let path = Path::new(&options.arg_path); @@ -203,7 +203,7 @@ fn sync( _ => index_dir.join(&name[..2]).join(&name[2..4]).join(name), }; fs::create_dir_all(&dst.parent().unwrap())?; - let line = serde_json::to_string(®istry_pkg(&pkg, &resolve)).unwrap(); + let line = serde_json::to_string(®istry_pkg(&pkg, &resolve, &config)).unwrap(); let prev = if no_delete || added_index.contains(&dst) { read(&dst).unwrap_or(String::new()) @@ -297,7 +297,7 @@ fn build_ar(ar: &mut Builder>, pkg: &Package, config: &Config) { } } -fn registry_pkg(pkg: &Package, resolve: &Resolve) -> RegistryPackage { +fn registry_pkg(pkg: &Package, resolve: &Resolve, config: &Config) -> RegistryPackage { let id = pkg.package_id(); let mut deps = pkg .dependencies() @@ -329,17 +329,38 @@ fn registry_pkg(pkg: &Package, resolve: &Resolve) -> RegistryPackage { .collect::>(); deps.sort(); + let allow_namespaced_features = + config.nightly_features_allowed && config.cli_unstable().namespaced_features; let features = pkg .summary() .features() .into_iter() - .map(|(k, v)| { + .filter_map(|(k, v)| { + if !allow_namespaced_features + && !v.is_empty() + && v.iter().all(|fv| { + if let FeatureValue::Dep { .. } = fv { + true + } else { + false + } + }) + { + return None; + } let mut v = v .iter() - .map(|x| x.to_string()) + .filter_map(|fv| { + if !allow_namespaced_features { + if let FeatureValue::Dep { .. } = fv { + return None; + } + } + Some(fv.to_string()) + }) .collect::>(); v.sort(); - (k.to_string(), v) + Some((k.to_string(), v)) }) .collect(); diff --git a/tests/all.rs b/tests/all.rs index 625bb63..414df8b 100644 --- a/tests/all.rs +++ b/tests/all.rs @@ -317,7 +317,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" File::open(registry.join("index/in/fl/inflector")).unwrap() .read_to_string(&mut contents).unwrap(); - assert_eq!(contents, r#"{"name":"Inflector","vers":"0.11.3","deps":[{"name":"lazy_static","req":"^1.0.0","features":[],"optional":true,"default_features":true,"target":null,"kind":null,"package":null},{"name":"regex","req":"^1.0","features":[],"optional":true,"default_features":true,"target":null,"kind":null,"package":null}],"cksum":"4467f98bb61f615f8273359bf1c989453dfc1ea4a45ae9298f1dcd0672febe5d","features":{"default":["heavyweight"],"heavyweight":["lazy_static","regex"],"lazy_static":["dep:lazy_static"],"regex":["dep:regex"],"unstable":[]},"yanked":false}"#); + assert_eq!(contents, r#"{"name":"Inflector","vers":"0.11.3","deps":[{"name":"lazy_static","req":"^1.0.0","features":[],"optional":true,"default_features":true,"target":null,"kind":null,"package":null},{"name":"regex","req":"^1.0","features":[],"optional":true,"default_features":true,"target":null,"kind":null,"package":null}],"cksum":"4467f98bb61f615f8273359bf1c989453dfc1ea4a45ae9298f1dcd0672febe5d","features":{"default":["heavyweight"],"heavyweight":["lazy_static","regex"],"unstable":[]},"yanked":false}"#); } #[test] @@ -359,7 +359,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" File::open(registry.join("index/ru/st/rustc-demangle")).unwrap() .read_to_string(&mut contents).unwrap(); - assert_eq!(contents, r#"{"name":"rustc-demangle","vers":"0.1.14","deps":[{"name":"compiler_builtins","req":"^0.1.2","features":[],"optional":true,"default_features":true,"target":null,"kind":null,"package":null},{"name":"core","req":"^1.0.0","features":[],"optional":true,"default_features":true,"target":null,"kind":null,"package":"rustc-std-workspace-core"}],"cksum":"ccc78bfd5acd7bf3e89cffcf899e5cb1a52d6fafa8dec2739ad70c9577a57288","features":{"compiler_builtins":["dep:compiler_builtins"],"core":["dep:core"],"rustc-dep-of-std":["compiler_builtins","core"]},"yanked":false}"#); + assert_eq!(contents, r#"{"name":"rustc-demangle","vers":"0.1.14","deps":[{"name":"compiler_builtins","req":"^0.1.2","features":[],"optional":true,"default_features":true,"target":null,"kind":null,"package":null},{"name":"core","req":"^1.0.0","features":[],"optional":true,"default_features":true,"target":null,"kind":null,"package":"rustc-std-workspace-core"}],"cksum":"ccc78bfd5acd7bf3e89cffcf899e5cb1a52d6fafa8dec2739ad70c9577a57288","features":{"rustc-dep-of-std":["compiler_builtins","core"]},"yanked":false}"#); } #[test] @@ -410,12 +410,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" let mut contents = String::new(); File::open(registry.join("index/la/zy/lazy_static")).unwrap() .read_to_string(&mut contents).unwrap(); - assert_eq!(contents, r#"{"name":"lazy_static","vers":"0.2.11","deps":[{"name":"compiletest_rs","req":"^0.3","features":[],"optional":true,"default_features":true,"target":null,"kind":null,"package":null},{"name":"spin","req":"^0.4.6","features":[],"optional":true,"default_features":true,"target":null,"kind":null,"package":null}],"cksum":"76f033c7ad61445c5b347c7382dd1237847eb1bce590fe50365dcb33d546be73","features":{"compiletest":["compiletest_rs"],"compiletest_rs":["dep:compiletest_rs"],"nightly":[],"spin":["dep:spin"],"spin_no_std":["nightly","spin"]},"yanked":false}"#); + assert_eq!(contents, r#"{"name":"lazy_static","vers":"0.2.11","deps":[{"name":"compiletest_rs","req":"^0.3","features":[],"optional":true,"default_features":true,"target":null,"kind":null,"package":null},{"name":"spin","req":"^0.4.6","features":[],"optional":true,"default_features":true,"target":null,"kind":null,"package":null}],"cksum":"76f033c7ad61445c5b347c7382dd1237847eb1bce590fe50365dcb33d546be73","features":{"compiletest":["compiletest_rs"],"nightly":[],"spin_no_std":["nightly","spin"]},"yanked":false}"#); contents.clear(); File::open(registry.join("index/la/ng/language-tags")).unwrap() .read_to_string(&mut contents).unwrap(); - assert_eq!(contents, r#"{"name":"language-tags","vers":"0.2.2","deps":[{"name":"heapsize","req":">=0.2.2, <0.4","features":[],"optional":true,"default_features":true,"target":null,"kind":null,"package":null},{"name":"heapsize_plugin","req":"^0.1.2","features":[],"optional":true,"default_features":true,"target":null,"kind":null,"package":null}],"cksum":"a91d884b6667cd606bb5a69aa0c99ba811a115fc68915e7056ec08a46e93199a","features":{"heap_size":["heapsize","heapsize_plugin"],"heapsize":["dep:heapsize"],"heapsize_plugin":["dep:heapsize_plugin"]},"yanked":false}"#); + assert_eq!(contents, r#"{"name":"language-tags","vers":"0.2.2","deps":[{"name":"heapsize","req":">=0.2.2, <0.4","features":[],"optional":true,"default_features":true,"target":null,"kind":null,"package":null},{"name":"heapsize_plugin","req":"^0.1.2","features":[],"optional":true,"default_features":true,"target":null,"kind":null,"package":null}],"cksum":"a91d884b6667cd606bb5a69aa0c99ba811a115fc68915e7056ec08a46e93199a","features":{"heap_size":["heapsize","heapsize_plugin"]},"yanked":false}"#); // Modify the Cargo.toml to swap an existing library, add a new one and delete another File::create(&td.path().join("Cargo.toml")).unwrap().write_all(br#" @@ -464,18 +464,18 @@ source = "registry+https://github.com/rust-lang/crates.io-index" contents.clear(); File::open(registry.join("index/la/zy/lazy_static")).unwrap() .read_to_string(&mut contents).unwrap(); - assert_eq!(contents, r#"{"name":"lazy_static","vers":"0.2.11","deps":[{"name":"compiletest_rs","req":"^0.3","features":[],"optional":true,"default_features":true,"target":null,"kind":null,"package":null},{"name":"spin","req":"^0.4.6","features":[],"optional":true,"default_features":true,"target":null,"kind":null,"package":null}],"cksum":"76f033c7ad61445c5b347c7382dd1237847eb1bce590fe50365dcb33d546be73","features":{"compiletest":["compiletest_rs"],"compiletest_rs":["dep:compiletest_rs"],"nightly":[],"spin":["dep:spin"],"spin_no_std":["nightly","spin"]},"yanked":false} -{"name":"lazy_static","vers":"1.2.0","deps":[{"name":"spin","req":"^0.4.10","features":["once"],"optional":true,"default_features":false,"target":null,"kind":null,"package":null}],"cksum":"a374c89b9db55895453a74c1e38861d9deec0b01b405a82516e9d5de4820dea1","features":{"nightly":[],"spin":["dep:spin"],"spin_no_std":["spin"]},"yanked":false}"#); + assert_eq!(contents, r#"{"name":"lazy_static","vers":"0.2.11","deps":[{"name":"compiletest_rs","req":"^0.3","features":[],"optional":true,"default_features":true,"target":null,"kind":null,"package":null},{"name":"spin","req":"^0.4.6","features":[],"optional":true,"default_features":true,"target":null,"kind":null,"package":null}],"cksum":"76f033c7ad61445c5b347c7382dd1237847eb1bce590fe50365dcb33d546be73","features":{"compiletest":["compiletest_rs"],"nightly":[],"spin_no_std":["nightly","spin"]},"yanked":false} +{"name":"lazy_static","vers":"1.2.0","deps":[{"name":"spin","req":"^0.4.10","features":["once"],"optional":true,"default_features":false,"target":null,"kind":null,"package":null}],"cksum":"a374c89b9db55895453a74c1e38861d9deec0b01b405a82516e9d5de4820dea1","features":{"nightly":[],"spin_no_std":["spin"]},"yanked":false}"#); contents.clear(); File::open(registry.join("index/la/zy/lazycell")).unwrap() .read_to_string(&mut contents).unwrap(); - assert_eq!(contents, r#"{"name":"lazycell","vers":"1.2.1","deps":[{"name":"clippy","req":"^0.0","features":[],"optional":true,"default_features":true,"target":null,"kind":null,"package":null}],"cksum":"b294d6fa9ee409a054354afc4352b0b9ef7ca222c69b8812cbea9e7d2bf3783f","features":{"clippy":["dep:clippy"],"nightly":[],"nightly-testing":["clippy","nightly"]},"yanked":false}"#); + assert_eq!(contents, r#"{"name":"lazycell","vers":"1.2.1","deps":[{"name":"clippy","req":"^0.0","features":[],"optional":true,"default_features":true,"target":null,"kind":null,"package":null}],"cksum":"b294d6fa9ee409a054354afc4352b0b9ef7ca222c69b8812cbea9e7d2bf3783f","features":{"nightly":[],"nightly-testing":["clippy","nightly"]},"yanked":false}"#); contents.clear(); File::open(registry.join("index/la/ng/language-tags")).unwrap() .read_to_string(&mut contents).unwrap(); - assert_eq!(contents, r#"{"name":"language-tags","vers":"0.2.2","deps":[{"name":"heapsize","req":">=0.2.2, <0.4","features":[],"optional":true,"default_features":true,"target":null,"kind":null,"package":null},{"name":"heapsize_plugin","req":"^0.1.2","features":[],"optional":true,"default_features":true,"target":null,"kind":null,"package":null}],"cksum":"a91d884b6667cd606bb5a69aa0c99ba811a115fc68915e7056ec08a46e93199a","features":{"heap_size":["heapsize","heapsize_plugin"],"heapsize":["dep:heapsize"],"heapsize_plugin":["dep:heapsize_plugin"]},"yanked":false}"#); + assert_eq!(contents, r#"{"name":"language-tags","vers":"0.2.2","deps":[{"name":"heapsize","req":">=0.2.2, <0.4","features":[],"optional":true,"default_features":true,"target":null,"kind":null,"package":null},{"name":"heapsize_plugin","req":"^0.1.2","features":[],"optional":true,"default_features":true,"target":null,"kind":null,"package":null}],"cksum":"a91d884b6667cd606bb5a69aa0c99ba811a115fc68915e7056ec08a46e93199a","features":{"heap_size":["heapsize","heapsize_plugin"]},"yanked":false}"#); // Run for the third time -- delete unused (default) run(cmd().arg(®istry).arg("--sync").arg(&lock)); @@ -493,12 +493,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" contents.clear(); File::open(registry.join("index/la/zy/lazy_static")).unwrap() .read_to_string(&mut contents).unwrap(); - assert_eq!(contents, r#"{"name":"lazy_static","vers":"1.2.0","deps":[{"name":"spin","req":"^0.4.10","features":["once"],"optional":true,"default_features":false,"target":null,"kind":null,"package":null}],"cksum":"a374c89b9db55895453a74c1e38861d9deec0b01b405a82516e9d5de4820dea1","features":{"nightly":[],"spin":["dep:spin"],"spin_no_std":["spin"]},"yanked":false}"#); + assert_eq!(contents, r#"{"name":"lazy_static","vers":"1.2.0","deps":[{"name":"spin","req":"^0.4.10","features":["once"],"optional":true,"default_features":false,"target":null,"kind":null,"package":null}],"cksum":"a374c89b9db55895453a74c1e38861d9deec0b01b405a82516e9d5de4820dea1","features":{"nightly":[],"spin_no_std":["spin"]},"yanked":false}"#); contents.clear(); File::open(registry.join("index/la/zy/lazycell")).unwrap() .read_to_string(&mut contents).unwrap(); - assert_eq!(contents, r#"{"name":"lazycell","vers":"1.2.1","deps":[{"name":"clippy","req":"^0.0","features":[],"optional":true,"default_features":true,"target":null,"kind":null,"package":null}],"cksum":"b294d6fa9ee409a054354afc4352b0b9ef7ca222c69b8812cbea9e7d2bf3783f","features":{"clippy":["dep:clippy"],"nightly":[],"nightly-testing":["clippy","nightly"]},"yanked":false}"#); + assert_eq!(contents, r#"{"name":"lazycell","vers":"1.2.1","deps":[{"name":"clippy","req":"^0.0","features":[],"optional":true,"default_features":true,"target":null,"kind":null,"package":null}],"cksum":"b294d6fa9ee409a054354afc4352b0b9ef7ca222c69b8812cbea9e7d2bf3783f","features":{"nightly":[],"nightly-testing":["clippy","nightly"]},"yanked":false}"#); } fn run(cmd: &mut Command) -> String {