Skip to content

Commit

Permalink
Warn for resolver = "2" and edition = "2015"
Browse files Browse the repository at this point in the history
  • Loading branch information
qryxip committed Aug 5, 2023
1 parent fdf40b8 commit 9e34a19
Show file tree
Hide file tree
Showing 5 changed files with 187 additions and 5 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
106 changes: 102 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
16 changes: 15 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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!()
Expand Down
56 changes: 56 additions & 0 deletions src/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ use indoc::indoc;
use itertools::Itertools as _;
use krates::PkgSpec;
use rand::Rng as _;
use serde::Deserialize;
use std::{
collections::{BTreeMap, HashMap, HashSet},
env,
io::Cursor,
path::{Path, PathBuf},
str,
};
use strum::EnumString;

pub(crate) fn locate_project(cwd: &Path) -> anyhow::Result<PathBuf> {
cwd.ancestors()
Expand All @@ -37,6 +39,28 @@ pub(crate) fn cargo_metadata(manifest_path: &Path, cwd: &Path) -> cm::Result<cm:
.exec()
}

pub(crate) fn resolve_behavior(
package: &cm::Package,
workspace_root: &Utf8Path,
) -> anyhow::Result<ResolveBehavior> {
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<ResolveBehavior>,
}
}

pub(crate) fn cargo_check_message_format_json(
toolchain: &str,
metadata: &cm::Metadata,
Expand Down Expand Up @@ -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<Option<String>>;
}

Expand All @@ -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<Option<String>> {
license::read_non_unlicense_license_file(self, mine, cache_dir)
}
Expand Down Expand Up @@ -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,
}

0 comments on commit 9e34a19

Please sign in to comment.