From 9e34a19f41464afdf838d07b4059aae3317b13dc Mon Sep 17 00:00:00 2001 From: Ryo Yamashita Date: Sun, 6 Aug 2023 01:54:04 +0900 Subject: [PATCH] Warn for `resolver = "2"` and `edition = "2015"` --- CHANGELOG.md | 12 ++++++ Cargo.lock | 106 +++++++++++++++++++++++++++++++++++++++++++++-- Cargo.toml | 2 + src/lib.rs | 16 ++++++- src/workspace.rs | 56 +++++++++++++++++++++++++ 5 files changed, 187 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3238dde..e2607ee 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -30,6 +30,18 @@ warning: `--toolchain` was renamed to `--toolchain-for-udeps` ``` +- cargo-equip now warns for `resolver = "2"` and `edition = "2015"`. + + Note that both has been unsupported. + + ```console + warning: Currently cargo-equip only support Feature Resovler v1, and may go search more crates than real Cargo does. Please watch https://github.com/qryxip/cargo-equip/issues/94 + ``` + + ```console + warning: Rust 2015 is unsupported + ``` + ### Fixed - Updated [prettytable](https://docs.rs/crate/prettytable). ([#193](https://github.com/qryxip/cargo-equip/pull/193) by [@ichyo](https://github.com/ichyo)) diff --git a/Cargo.lock b/Cargo.lock index 17795a7..021d331 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -139,11 +139,13 @@ dependencies = [ "serde_json", "spdx", "structopt", + "strum", "syn 1.0.109", "tap", "tempfile", "termcolor", - "toml_edit", + "toml", + "toml_edit 0.5.0", "which", ] @@ -447,6 +449,12 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "34aa73646ffb006b8f5147f3dc182bd4bcb190227ce861fc4a4844bf8e3cb2c0" +[[package]] +name = "equivalent" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5" + [[package]] name = "errno" version = "0.3.2" @@ -539,6 +547,12 @@ dependencies = [ "unicode-segmentation", ] +[[package]] +name = "heck" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" + [[package]] name = "hermit-abi" version = "0.1.19" @@ -582,6 +596,16 @@ dependencies = [ "hashbrown 0.12.3", ] +[[package]] +name = "indexmap" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d5477fe2230a79769d8dc68e0eabf5437907c0457a5614a9e8dddb67f65eb65d" +dependencies = [ + "equivalent", + "hashbrown 0.14.0", +] + [[package]] name = "indoc" version = "1.0.9" @@ -846,7 +870,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4dd7d28ee937e54fe3080c91faa1c3a46c06de6252988a7f4592ba2310ef22a4" dependencies = [ "fixedbitset", - "indexmap", + "indexmap 1.9.3", ] [[package]] @@ -1196,6 +1220,15 @@ dependencies = [ "serde", ] +[[package]] +name = "serde_spanned" +version = "0.6.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "96426c9936fd7a0124915f9185ea1d20aa9445cc9821142f0a73bc9207a2e186" +dependencies = [ + "serde", +] + [[package]] name = "shell-escape" version = "0.1.5" @@ -1261,13 +1294,35 @@ version = "0.4.18" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dcb5ae327f9cc13b68763b5749770cb9e048a99bd9dfdfa58d0cf05d5f64afe0" dependencies = [ - "heck", + "heck 0.3.3", "proc-macro-error", "proc-macro2", "quote", "syn 1.0.109", ] +[[package]] +name = "strum" +version = "0.25.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "290d54ea6f91c969195bdbcd7442c8c2a2ba87da8bf60a7ee86a235d4bc1e125" +dependencies = [ + "strum_macros", +] + +[[package]] +name = "strum_macros" +version = "0.25.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6069ca09d878a33f883cc06aaa9718ede171841d3832450354410b718b097232" +dependencies = [ + "heck 0.4.1", + "proc-macro2", + "quote", + "rustversion", + "syn 2.0.28", +] + [[package]] name = "syn" version = "1.0.109" @@ -1364,6 +1419,27 @@ dependencies = [ "syn 2.0.28", ] +[[package]] +name = "toml" +version = "0.7.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c17e963a819c331dcacd7ab957d80bc2b9a9c1e71c804826d2f283dd65306542" +dependencies = [ + "serde", + "serde_spanned", + "toml_datetime", + "toml_edit 0.19.14", +] + +[[package]] +name = "toml_datetime" +version = "0.6.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7cda73e2f1397b1262d6dfdcef8aafae14d1de7748d66822d3bfeeb6d03e5e4b" +dependencies = [ + "serde", +] + [[package]] name = "toml_edit" version = "0.5.0" @@ -1371,11 +1447,24 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e0a0ddb4919e3b49fe9e08e7921d69445bb00ce95a2ea807bb8826cde6610455" dependencies = [ "combine", - "indexmap", + "indexmap 1.9.3", "itertools", "kstring", ] +[[package]] +name = "toml_edit" +version = "0.19.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8123f27e969974a3dfba720fdb560be359f57b44302d280ba72e76a74480e8a" +dependencies = [ + "indexmap 2.0.0", + "serde", + "serde_spanned", + "toml_datetime", + "winnow", +] + [[package]] name = "tracing" version = "0.1.37" @@ -1664,6 +1753,15 @@ version = "0.48.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1a515f5799fe4961cb532f983ce2b23082366b898e52ffbce459c86f67c8378a" +[[package]] +name = "winnow" +version = "0.5.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "acaaa1190073b2b101e15083c38ee8ec891b5e05cbee516521e94ec008f61e64" +dependencies = [ + "memchr", +] + [[package]] name = "yaml-rust" version = "0.4.5" diff --git a/Cargo.toml b/Cargo.toml index e99b28f..10b36eb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -37,10 +37,12 @@ serde = { version = "1.0.181", features = ["derive"] } serde_json = "1.0.104" spdx = "0.6.2" structopt = "0.3.26" +strum = { version = "0.25.0", features = ["derive"] } syn = { version = "1.0.109", features = ["extra-traits", "full", "parsing", "visit"] } tap = "1.0.1" tempfile = "3.7.0" termcolor = "1.2.0" +toml = "0.7.6" toml_edit = "0.5.0" which = "4.4.0" diff --git a/src/lib.rs b/src/lib.rs index 89ce55e..bf88f61 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -15,7 +15,10 @@ use crate::{ ra_proc_macro::ProcMacroExpander, rust::CodeEdit, shell::Shell, - workspace::{MetadataExt as _, PackageExt as _, PackageIdExt as _, TargetExt as _}, + workspace::{ + Edition, MetadataExt as _, PackageExt as _, PackageIdExt as _, ResolveBehavior, + TargetExt as _, + }, }; use anyhow::Context as _; use cargo_metadata as cm; @@ -500,6 +503,17 @@ pub fn run(opt: Opt, ctx: Context<'_>) -> anyhow::Result<()> { metadata.exactly_one_target() }?; + if root_package.edition() == Edition::Edition2015 { + shell.warn("Rust 2015 is unsupported")?; + } + let resolve_behavior = workspace::resolve_behavior(root_package, &metadata.workspace_root)?; + if resolve_behavior >= ResolveBehavior::V2 { + shell.warn( + "Currently cargo-equip only support Feature Resovler v1, and may go search more crates \ + than real Cargo does. Please watch https://github.com/qryxip/cargo-equip/issues/94", + )?; + } + let libs_to_bundle = { let unused_deps = &if root.is_lib() { hashset!() diff --git a/src/workspace.rs b/src/workspace.rs index 0e9e957..b12a098 100644 --- a/src/workspace.rs +++ b/src/workspace.rs @@ -10,6 +10,7 @@ use indoc::indoc; use itertools::Itertools as _; use krates::PkgSpec; use rand::Rng as _; +use serde::Deserialize; use std::{ collections::{BTreeMap, HashMap, HashSet}, env, @@ -17,6 +18,7 @@ use std::{ path::{Path, PathBuf}, str, }; +use strum::EnumString; pub(crate) fn locate_project(cwd: &Path) -> anyhow::Result { cwd.ancestors() @@ -37,6 +39,28 @@ pub(crate) fn cargo_metadata(manifest_path: &Path, cwd: &Path) -> cm::Result anyhow::Result { + let cargo_toml = &cargo_util::paths::read(workspace_root.join("Cargo.toml").as_ref())?; + let CargoToml { workspace } = toml::from_str(cargo_toml)?; + return Ok(workspace + .resolver + .unwrap_or_else(|| package.edition().default_resolver_behavior())); + + #[derive(Deserialize)] + struct CargoToml { + #[serde(default)] + workspace: Workspace, + } + + #[derive(Default, Deserialize)] + struct Workspace { + resolver: Option, + } +} + pub(crate) fn cargo_check_message_format_json( toolchain: &str, metadata: &cm::Metadata, @@ -640,6 +664,7 @@ pub(crate) trait PackageExt { fn has_proc_macro(&self) -> bool; fn lib_like_target(&self) -> Option<&cm::Target>; fn manifest_dir(&self) -> &Utf8Path; + fn edition(&self) -> Edition; fn read_license_text(&self, mine: &[User], cache_dir: &Path) -> anyhow::Result>; } @@ -666,6 +691,10 @@ impl PackageExt for cm::Package { self.manifest_path.parent().expect("should not be empty") } + fn edition(&self) -> Edition { + self.edition.parse().expect("`edition` modified invalidly") + } + fn read_license_text(&self, mine: &[User], cache_dir: &Path) -> anyhow::Result> { license::read_non_unlicense_license_file(self, mine, cache_dir) } @@ -752,3 +781,30 @@ impl SourceExt for cm::Source { } } } + +#[derive(Clone, Copy, PartialEq, EnumString)] +pub(crate) enum Edition { + #[strum(serialize = "2015")] + Edition2015, + #[strum(serialize = "2018")] + Edition2018, + #[strum(serialize = "2021")] + Edition2021, +} + +impl Edition { + fn default_resolver_behavior(self) -> ResolveBehavior { + match self { + Self::Edition2015 | Self::Edition2018 => ResolveBehavior::V1, + Self::Edition2021 => ResolveBehavior::V2, + } + } +} + +#[derive(Clone, Copy, PartialEq, PartialOrd, Deserialize)] +pub(crate) enum ResolveBehavior { + #[serde(rename = "1")] + V1, + #[serde(rename = "2")] + V2, +}