Skip to content

Commit 259ea8c

Browse files
committed
Auto merge of #9803 - ehuss:fix-edition-resolve-differences-dev-deps, r=alexcrichton
Show feature resolver differences for dev-dependencies. During the crater run for 2021, there was a package that failed to update in a confusing way. The issue is that a feature was removed in the new resolver, but only for a dev-dependency. The report displayed with `cargo fix --edition` did not say anything about that, so it took me a bit to figure it out. This changes it so that the report also includes changes to features of dev-dependencies. I honestly don't remember my thought process behind the original code. For example, the offending package now says: ``` When building the following dependencies, the given features will no longer be used: log v0.4.8 removed features: std syn v0.15.44 (as host dependency) removed features: extra-traits, visit The following differences only apply when building with dev-dependencies: phf_shared v0.7.24 (as host dependency) removed features: unicase ``` And the error that happens after updating to 2021 is: ``` error[E0277]: the trait bound `UniCase<&str>: phf_shared::PhfHash` is not satisfied --> /Users/eric/.cargo/registry/src/github.com-1ecc6299db9ec823/mime_guess-1.8.7/build.rs:37:21 | 37 | forward_map.entry(UniCase(key), &format!("{:?}", val)); | ^^^^^ the trait `phf_shared::PhfHash` is not implemented for `UniCase<&str>` ``` Hopefully developers will be able to see the note about the feature `unicase` being removed from `phf_shared`, and the error message about `UniCase` not implementing `PhfHash`, and connect the two together. Previously, the upgrade report didn't mention anything about `phf_shared`, and thus no clues on what went wrong.
2 parents e96bdb0 + 5939e21 commit 259ea8c

File tree

2 files changed

+61
-36
lines changed

2 files changed

+61
-36
lines changed

src/cargo/ops/fix.rs

Lines changed: 51 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,10 @@ use rustfix::diagnostics::Diagnostic;
5252
use rustfix::{self, CodeFix};
5353

5454
use crate::core::compiler::RustcTargetData;
55-
use crate::core::resolver::features::{FeatureOpts, FeatureResolver};
55+
use crate::core::resolver::features::{DiffMap, FeatureOpts, FeatureResolver};
5656
use crate::core::resolver::{HasDevUnits, Resolve, ResolveBehavior};
5757
use crate::core::{Edition, MaybePackage, Workspace};
58+
use crate::ops::resolve::WorkspaceResolve;
5859
use crate::ops::{self, CompileOptions};
5960
use crate::util::diagnostic_server::{Message, RustfixDiagnosticServer};
6061
use crate::util::errors::CargoResult;
@@ -229,36 +230,40 @@ fn check_resolver_change(ws: &Workspace<'_>, opts: &FixOptions) -> CargoResult<(
229230
assert_eq!(ws.resolve_behavior(), ResolveBehavior::V1);
230231
let specs = opts.compile_opts.spec.to_package_id_specs(ws)?;
231232
let target_data = RustcTargetData::new(ws, &opts.compile_opts.build_config.requested_kinds)?;
232-
// HasDevUnits::No because that may uncover more differences.
233-
// This is not the same as what `cargo fix` is doing, since it is doing
234-
// `--all-targets` which includes dev dependencies.
235-
let ws_resolve = ops::resolve_ws_with_opts(
236-
ws,
237-
&target_data,
238-
&opts.compile_opts.build_config.requested_kinds,
239-
&opts.compile_opts.cli_features,
240-
&specs,
241-
HasDevUnits::No,
242-
crate::core::resolver::features::ForceAllTargets::No,
243-
)?;
233+
let resolve_differences = |has_dev_units| -> CargoResult<(WorkspaceResolve<'_>, DiffMap)> {
234+
let ws_resolve = ops::resolve_ws_with_opts(
235+
ws,
236+
&target_data,
237+
&opts.compile_opts.build_config.requested_kinds,
238+
&opts.compile_opts.cli_features,
239+
&specs,
240+
has_dev_units,
241+
crate::core::resolver::features::ForceAllTargets::No,
242+
)?;
244243

245-
let feature_opts = FeatureOpts::new_behavior(ResolveBehavior::V2, HasDevUnits::No);
246-
let v2_features = FeatureResolver::resolve(
247-
ws,
248-
&target_data,
249-
&ws_resolve.targeted_resolve,
250-
&ws_resolve.pkg_set,
251-
&opts.compile_opts.cli_features,
252-
&specs,
253-
&opts.compile_opts.build_config.requested_kinds,
254-
feature_opts,
255-
)?;
244+
let feature_opts = FeatureOpts::new_behavior(ResolveBehavior::V2, has_dev_units);
245+
let v2_features = FeatureResolver::resolve(
246+
ws,
247+
&target_data,
248+
&ws_resolve.targeted_resolve,
249+
&ws_resolve.pkg_set,
250+
&opts.compile_opts.cli_features,
251+
&specs,
252+
&opts.compile_opts.build_config.requested_kinds,
253+
feature_opts,
254+
)?;
256255

257-
let differences = v2_features.compare_legacy(&ws_resolve.resolved_features);
258-
if differences.is_empty() {
256+
let diffs = v2_features.compare_legacy(&ws_resolve.resolved_features);
257+
Ok((ws_resolve, diffs))
258+
};
259+
let (_, without_dev_diffs) = resolve_differences(HasDevUnits::No)?;
260+
let (ws_resolve, mut with_dev_diffs) = resolve_differences(HasDevUnits::Yes)?;
261+
if without_dev_diffs.is_empty() && with_dev_diffs.is_empty() {
259262
// Nothing is different, nothing to report.
260263
return Ok(());
261264
}
265+
// Only display unique changes with dev-dependencies.
266+
with_dev_diffs.retain(|k, vals| without_dev_diffs.get(k) != Some(vals));
262267
let config = ws.config();
263268
config.shell().note(
264269
"Switching to Edition 2021 will enable the use of the version 2 feature resolver in Cargo.",
@@ -277,16 +282,28 @@ fn check_resolver_change(ws: &Workspace<'_>, opts: &FixOptions) -> CargoResult<(
277282
"When building the following dependencies, \
278283
the given features will no longer be used:\n"
279284
);
280-
for ((pkg_id, for_host), removed) in differences {
281-
drop_eprint!(config, " {}", pkg_id);
282-
if for_host {
283-
drop_eprint!(config, " (as host dependency)");
285+
let show_diffs = |differences: DiffMap| {
286+
for ((pkg_id, for_host), removed) in differences {
287+
drop_eprint!(config, " {}", pkg_id);
288+
if for_host {
289+
drop_eprint!(config, " (as host dependency)");
290+
}
291+
drop_eprint!(config, " removed features: ");
292+
let joined: Vec<_> = removed.iter().map(|s| s.as_str()).collect();
293+
drop_eprintln!(config, "{}", joined.join(", "));
284294
}
285-
drop_eprint!(config, ": ");
286-
let joined: Vec<_> = removed.iter().map(|s| s.as_str()).collect();
287-
drop_eprintln!(config, "{}", joined.join(", "));
295+
drop_eprint!(config, "\n");
296+
};
297+
if !without_dev_diffs.is_empty() {
298+
show_diffs(without_dev_diffs);
299+
}
300+
if !with_dev_diffs.is_empty() {
301+
drop_eprintln!(
302+
config,
303+
"The following differences only apply when building with dev-dependencies:\n"
304+
);
305+
show_diffs(with_dev_diffs);
288306
}
289-
drop_eprint!(config, "\n");
290307
report_maybe_diesel(config, &ws_resolve.targeted_resolve)?;
291308
Ok(())
292309
}

tests/testsuite/fix.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1428,6 +1428,7 @@ fn edition_v2_resolver_report() {
14281428
}
14291429
Package::new("common", "1.0.0")
14301430
.feature("f1", &[])
1431+
.feature("dev-feat", &[])
14311432
.add_dep(Dependency::new("opt_dep", "1.0").optional(true))
14321433
.publish();
14331434
Package::new("opt_dep", "1.0.0").publish();
@@ -1455,6 +1456,9 @@ fn edition_v2_resolver_report() {
14551456
14561457
[build-dependencies]
14571458
common = { version = "1.0", features = ["opt_dep"] }
1459+
1460+
[dev-dependencies]
1461+
common = { version="1.0", features=["dev-feat"] }
14581462
"#,
14591463
)
14601464
.file("src/lib.rs", "")
@@ -1473,8 +1477,12 @@ This may cause some dependencies to be built with fewer features enabled than pr
14731477
More information about the resolver changes may be found at https://doc.rust-lang.org/nightly/edition-guide/rust-2021/default-cargo-resolver.html
14741478
When building the following dependencies, the given features will no longer be used:
14751479
1476-
common v1.0.0: f1, opt_dep
1477-
common v1.0.0 (as host dependency): f1
1480+
common v1.0.0 removed features: dev-feat, f1, opt_dep
1481+
common v1.0.0 (as host dependency) removed features: dev-feat, f1
1482+
1483+
The following differences only apply when building with dev-dependencies:
1484+
1485+
common v1.0.0 removed features: f1, opt_dep
14781486
14791487
[CHECKING] opt_dep v1.0.0
14801488
[CHECKING] common v1.0.0

0 commit comments

Comments
 (0)