Skip to content

Commit

Permalink
feat(fix): Migrate inherited deps with ignored default-features
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed May 1, 2024
1 parent 10012ce commit 627b1d1
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 12 deletions.
7 changes: 7 additions & 0 deletions crates/cargo-util-schemas/src/manifest/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -683,6 +683,13 @@ impl TomlDependency {
}
}

pub fn default_features(&self) -> Option<bool> {
match self {
TomlDependency::Detailed(d) => d.default_features(),
TomlDependency::Simple(..) => None,
}
}

pub fn unused_keys(&self) -> Vec<String> {
match self {
TomlDependency::Simple(_) => vec![],
Expand Down
63 changes: 63 additions & 0 deletions src/cargo/ops/fix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ use std::{env, fs, str};

use anyhow::{bail, Context as _};
use cargo_util::{exit_status_to_string, is_simple_exit_code, paths, ProcessBuilder};
use cargo_util_schemas::manifest::TomlManifest;
use rustfix::diagnostics::Diagnostic;
use rustfix::CodeFix;
use semver::Version;
Expand Down Expand Up @@ -265,6 +266,10 @@ fn migrate_manifests(ws: &Workspace<'_>, pkgs: &[&Package]) -> CargoResult<()> {
format!("{file} from {existing_edition} edition to {prepare_for_edition}"),
)?;

let ws_original_toml = match ws.root_maybe() {
MaybePackage::Package(package) => package.manifest().original_toml(),
MaybePackage::Virtual(manifest) => manifest.original_toml(),
};
if Edition::Edition2024 <= prepare_for_edition {
let mut document = pkg.manifest().document().clone().into_mut();
let mut fixes = 0;
Expand All @@ -290,10 +295,15 @@ fn migrate_manifests(ws: &Workspace<'_>, pkgs: &[&Package]) -> CargoResult<()> {
fixes += rename_array_of_target_fields_2024(root, "test");
fixes += rename_array_of_target_fields_2024(root, "bench");
fixes += rename_dep_fields_2024(root, "dependencies");
fixes += remove_ignored_default_features_2024(root, "dependencies", ws_original_toml);
fixes += rename_table(root, "dev_dependencies", "dev-dependencies");
fixes += rename_dep_fields_2024(root, "dev-dependencies");
fixes +=
remove_ignored_default_features_2024(root, "dev-dependencies", ws_original_toml);
fixes += rename_table(root, "build_dependencies", "build-dependencies");
fixes += rename_dep_fields_2024(root, "build-dependencies");
fixes +=
remove_ignored_default_features_2024(root, "build-dependencies", ws_original_toml);
for target in root
.get_mut("target")
.and_then(|t| t.as_table_like_mut())
Expand All @@ -302,10 +312,22 @@ fn migrate_manifests(ws: &Workspace<'_>, pkgs: &[&Package]) -> CargoResult<()> {
.filter_map(|(_k, t)| t.as_table_like_mut())
{
fixes += rename_dep_fields_2024(target, "dependencies");
fixes +=
remove_ignored_default_features_2024(target, "dependencies", ws_original_toml);
fixes += rename_table(target, "dev_dependencies", "dev-dependencies");
fixes += rename_dep_fields_2024(target, "dev-dependencies");
fixes += remove_ignored_default_features_2024(
target,
"dev-dependencies",
ws_original_toml,
);
fixes += rename_table(target, "build_dependencies", "build-dependencies");
fixes += rename_dep_fields_2024(target, "build-dependencies");
fixes += remove_ignored_default_features_2024(
target,
"build-dependencies",
ws_original_toml,
);
}

if 0 < fixes {
Expand Down Expand Up @@ -337,6 +359,47 @@ fn rename_dep_fields_2024(parent: &mut dyn toml_edit::TableLike, dep_kind: &str)
fixes
}

fn remove_ignored_default_features_2024(
parent: &mut dyn toml_edit::TableLike,
dep_kind: &str,
ws_original_toml: &TomlManifest,
) -> usize {
let mut fixes = 0;
for (name_in_toml, target) in parent
.get_mut(dep_kind)
.and_then(|t| t.as_table_like_mut())
.iter_mut()
.flat_map(|t| t.iter_mut())
.filter_map(|(k, t)| t.as_table_like_mut().map(|t| (k, t)))
{
let name_in_toml: &str = &name_in_toml;
let ws_deps = ws_original_toml
.workspace
.as_ref()
.and_then(|ws| ws.dependencies.as_ref());
if let Some(ws_dep) = ws_deps.and_then(|ws_deps| ws_deps.get(name_in_toml)) {
if ws_dep.default_features() == Some(false) {
continue;
}
}
if target
.get("workspace")
.and_then(|i| i.as_value())
.and_then(|i| i.as_bool())
== Some(true)
&& target
.get("default-features")
.and_then(|i| i.as_value())
.and_then(|i| i.as_bool())
== Some(false)
{
target.remove("default-features");
fixes += 1;
}
}
fixes
}

fn rename_array_of_target_fields_2024(root: &mut dyn toml_edit::TableLike, kind: &str) -> usize {
let mut fixes = 0;
for target in root
Expand Down
19 changes: 7 additions & 12 deletions tests/testsuite/fix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2753,12 +2753,7 @@ dep_df_false = { version = "0.1.0", default-features = false }
[MIGRATING] pkg_default/Cargo.toml from 2021 edition to 2024
[MIGRATING] pkg_df_true/Cargo.toml from 2021 edition to 2024
[MIGRATING] pkg_df_false/Cargo.toml from 2021 edition to 2024
[WARNING] [CWD]/pkg_df_false/Cargo.toml: `default-features` is ignored for dep_df_true, since `default-features` was true for `workspace.dependencies.dep_df_true`, this could become a hard error in the future
[WARNING] [CWD]/pkg_df_false/Cargo.toml: `default-features` is ignored for dep_simple, since `default-features` was not specified for `workspace.dependencies.dep_simple`, this could become a hard error in the future
[WARNING] [CWD]/pkg_df_false/Cargo.toml: `default-features` is ignored for dep_df_true, since `default-features` was true for `workspace.dependencies.dep_df_true`, this could become a hard error in the future
[WARNING] [CWD]/pkg_df_false/Cargo.toml: `default-features` is ignored for dep_simple, since `default-features` was not specified for `workspace.dependencies.dep_simple`, this could become a hard error in the future
[WARNING] [CWD]/pkg_df_false/Cargo.toml: `default-features` is ignored for dep_df_true, since `default-features` was true for `workspace.dependencies.dep_df_true`, this could become a hard error in the future
[WARNING] [CWD]/pkg_df_false/Cargo.toml: `default-features` is ignored for dep_simple, since `default-features` was not specified for `workspace.dependencies.dep_simple`, this could become a hard error in the future
[FIXED] pkg_df_false/Cargo.toml (6 fixes)
[UPDATING] `dummy-registry` index
[LOCKING] 6 packages to latest compatible versions
[DOWNLOADING] crates ...
Expand Down Expand Up @@ -2790,18 +2785,18 @@ version = "0.1.0"
edition = "2021"
[dependencies]
dep_simple = { workspace = true, default-features = false }
dep_df_true = { workspace = true, default-features = false }
dep_simple = { workspace = true}
dep_df_true = { workspace = true}
dep_df_false = { workspace = true, default-features = false }
[build-dependencies]
dep_simple = { workspace = true, default-features = false }
dep_df_true = { workspace = true, default-features = false }
dep_simple = { workspace = true}
dep_df_true = { workspace = true}
dep_df_false = { workspace = true, default-features = false }
[target.'cfg(target_os = "linux")'.dependencies]
dep_simple = { workspace = true, default-features = false }
dep_df_true = { workspace = true, default-features = false }
dep_simple = { workspace = true}
dep_df_true = { workspace = true}
dep_df_false = { workspace = true, default-features = false }
"#
);
Expand Down

0 comments on commit 627b1d1

Please sign in to comment.