diff --git a/src/cargo/core/features.rs b/src/cargo/core/features.rs index 0400e89a1e46..e8de1dac6a8a 100644 --- a/src/cargo/core/features.rs +++ b/src/cargo/core/features.rs @@ -102,6 +102,7 @@ use anyhow::{bail, Error}; use cargo_util::ProcessBuilder; use serde::{Deserialize, Serialize}; +use crate::core::resolver::ResolveBehavior; use crate::util::errors::CargoResult; use crate::util::{indented_lines, iter_join}; use crate::Config; @@ -242,6 +243,14 @@ impl Edition { Edition2021 => false, } } + + pub(crate) fn default_resolve_behavior(&self) -> ResolveBehavior { + if *self >= Edition::Edition2021 { + ResolveBehavior::V2 + } else { + ResolveBehavior::V1 + } + } } impl fmt::Display for Edition { diff --git a/src/cargo/core/workspace.rs b/src/cargo/core/workspace.rs index 2adf13a5b1a9..0d07dafa3479 100644 --- a/src/cargo/core/workspace.rs +++ b/src/cargo/core/workspace.rs @@ -16,7 +16,7 @@ use crate::core::features::Features; use crate::core::registry::PackageRegistry; use crate::core::resolver::features::CliFeatures; use crate::core::resolver::ResolveBehavior; -use crate::core::{Dependency, Edition, FeatureValue, PackageId, PackageIdSpec}; +use crate::core::{Dependency, FeatureValue, PackageId, PackageIdSpec}; use crate::core::{EitherManifest, Package, SourceId, VirtualManifest}; use crate::ops; use crate::sources::{PathSource, CRATES_IO_INDEX, CRATES_IO_REGISTRY}; @@ -287,16 +287,12 @@ impl<'cfg> Workspace<'cfg> { // - If the root package specifies edition 2021, use v2. // - Otherwise, use the default v1. self.resolve_behavior = match self.root_maybe() { - MaybePackage::Package(p) => p.manifest().resolve_behavior().or_else(|| { - if p.manifest().edition() >= Edition::Edition2021 { - Some(ResolveBehavior::V2) - } else { - None - } - }), - MaybePackage::Virtual(vm) => vm.resolve_behavior(), + MaybePackage::Package(p) => p + .manifest() + .resolve_behavior() + .unwrap_or_else(|| p.manifest().edition().default_resolve_behavior()), + MaybePackage::Virtual(vm) => vm.resolve_behavior().unwrap_or(ResolveBehavior::V1), } - .unwrap_or(ResolveBehavior::V1); } /// Returns the current package of this workspace. diff --git a/src/cargo/util/toml/mod.rs b/src/cargo/util/toml/mod.rs index 58647e03f056..f7fd6c44e92d 100644 --- a/src/cargo/util/toml/mod.rs +++ b/src/cargo/util/toml/mod.rs @@ -3,7 +3,7 @@ use std::fmt; use std::marker::PhantomData; use std::path::{Path, PathBuf}; use std::rc::Rc; -use std::str; +use std::str::{self, FromStr}; use anyhow::{anyhow, bail, Context as _}; use cargo_platform::Platform; @@ -1342,7 +1342,22 @@ impl TomlManifest { .unwrap() .clone(); package.workspace = None; - package.resolver = Some(ws.resolve_behavior().to_manifest()); + let current_resolver = package + .resolver + .as_ref() + .map(|r| ResolveBehavior::from_manifest(r)) + .unwrap_or_else(|| { + package + .edition + .as_ref() + .and_then(|e| e.as_defined()) + .map(|e| Edition::from_str(e)) + .transpose() + .map(|e| e.unwrap_or(Edition::Edition2015).default_resolve_behavior()) + })?; + if ws.resolve_behavior() != current_resolver { + package.resolver = Some(ws.resolve_behavior().to_manifest()); + } if let Some(license_file) = &package.license_file { let license_file = license_file .as_defined()