Skip to content

Commit

Permalink
Merge pull request #168 from dhovart/with-cargo-0.57
Browse files Browse the repository at this point in the history
Omit by default dependencies of namespaced features
  • Loading branch information
ChrisGreenaway authored Nov 29, 2021
2 parents bedbbb4 + db24bb3 commit c76aefe
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 20 deletions.
41 changes: 31 additions & 10 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
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};
use std::io;
use std::io::prelude::*;
use std::path::{self, Path, PathBuf};
use tar::{Builder, Header};
use url::Url;

#[derive(Deserialize)]
struct Options {
Expand Down Expand Up @@ -49,7 +49,7 @@ struct RegistryDependency {
default_features: bool,
target: Option<String>,
kind: Option<String>,
package: Option<String>
package: Option<String>,
}

fn main() {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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(&registry_pkg(&pkg, &resolve)).unwrap();
let line = serde_json::to_string(&registry_pkg(&pkg, &resolve, &config)).unwrap();

let prev = if no_delete || added_index.contains(&dst) {
read(&dst).unwrap_or(String::new())
Expand Down Expand Up @@ -297,7 +297,7 @@ fn build_ar(ar: &mut Builder<GzEncoder<File>>, 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()
Expand Down Expand Up @@ -329,17 +329,38 @@ fn registry_pkg(pkg: &Package, resolve: &Resolve) -> RegistryPackage {
.collect::<Vec<_>>();
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::<Vec<_>>();
v.sort();
(k.to_string(), v)
Some((k.to_string(), v))
})
.collect();

Expand Down
20 changes: 10 additions & 10 deletions tests/all.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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#"
Expand Down Expand Up @@ -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(&registry).arg("--sync").arg(&lock));
Expand All @@ -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 {
Expand Down

0 comments on commit c76aefe

Please sign in to comment.