Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BETA 1.64] Only override published resolver when the workspace is different #10970

Merged
merged 2 commits into from
Aug 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/cargo/core/features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down
16 changes: 6 additions & 10 deletions src/cargo/core/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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.
Expand Down
23 changes: 21 additions & 2 deletions src/cargo/util/toml/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -1342,7 +1342,26 @@ 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))
.unwrap_or(Ok(Edition::Edition2015))
.map(|e| e.default_resolve_behavior())
})?;
if ws.resolve_behavior() != current_resolver {
// This ensures the published crate if built as a root (e.g. `cargo install`) will
// use the same resolver behavior it was tested with in the workspace.
// To avoid forcing a higher MSRV we don't explicitly set this if it would implicitly
// result in the same thing.
package.resolver = Some(ws.resolve_behavior().to_manifest());
}
if let Some(license_file) = &package.license_file {
let license_file = license_file
.as_defined()
Expand Down
2 changes: 0 additions & 2 deletions tests/testsuite/features_namespaced.rs
Original file line number Diff line number Diff line change
Expand Up @@ -951,7 +951,6 @@ version = "0.1.0"
description = "foo"
homepage = "https://example.com/"
license = "MIT"
resolver = "1"

[dependencies.opt-dep1]
version = "1.0"
Expand Down Expand Up @@ -1059,7 +1058,6 @@ version = "0.1.0"
description = "foo"
homepage = "https://example.com/"
license = "MIT"
resolver = "1"

[dependencies.bar]
version = "1.0"
Expand Down
5 changes: 0 additions & 5 deletions tests/testsuite/inheritable_workspace_fields.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,6 @@ keywords = ["cli"]
categories = ["development-tools"]
license = "MIT"
repository = "https://github.com/example/example"
resolver = "1"

[badges.gitlab]
branch = "master"
Expand Down Expand Up @@ -349,7 +348,6 @@ fn inherit_own_dependencies() {
name = "bar"
version = "0.2.0"
authors = []
resolver = "1"

[dependencies.dep]
version = "0.1"
Expand Down Expand Up @@ -454,7 +452,6 @@ fn inherit_own_detailed_dependencies() {
name = "bar"
version = "0.2.0"
authors = []
resolver = "1"

[dependencies.dep]
version = "0.1.2"
Expand Down Expand Up @@ -691,7 +688,6 @@ categories = ["development-tools"]
license = "MIT"
license-file = "LICENSE"
repository = "https://github.com/example/example"
resolver = "1"

[badges.gitlab]
branch = "master"
Expand Down Expand Up @@ -823,7 +819,6 @@ fn inherit_dependencies() {
name = "bar"
version = "0.2.0"
authors = []
resolver = "1"

[dependencies.dep]
version = "0.1"
Expand Down
34 changes: 30 additions & 4 deletions tests/testsuite/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1119,7 +1119,6 @@ authors = []
exclude = ["*.txt"]
description = "foo"
license = "MIT"
resolver = "1"

[package.metadata]
foo = "bar"
Expand Down Expand Up @@ -1190,7 +1189,6 @@ fn ignore_workspace_specifier() {
name = "bar"
version = "0.1.0"
authors = []
resolver = "1"
"#,
cargo::core::package::MANIFEST_PREAMBLE
);
Expand Down Expand Up @@ -2335,7 +2333,7 @@ fn workspace_overrides_resolver() {
"Cargo.toml",
r#"
[workspace]
members = ["bar"]
members = ["bar", "baz"]
"#,
)
.file(
Expand All @@ -2348,9 +2346,19 @@ fn workspace_overrides_resolver() {
"#,
)
.file("bar/src/lib.rs", "")
.file(
"baz/Cargo.toml",
r#"
[package]
name = "baz"
version = "0.1.0"
edition = "2015"
"#,
)
.file("baz/src/lib.rs", "")
.build();

p.cargo("package --no-verify").cwd("bar").run();
p.cargo("package --no-verify -p bar -p baz").run();

let f = File::open(&p.root().join("target/package/bar-0.1.0.crate")).unwrap();
let rewritten_toml = format!(
Expand All @@ -2369,4 +2377,22 @@ resolver = "1"
&["Cargo.toml", "Cargo.toml.orig", "src/lib.rs"],
&[("Cargo.toml", &rewritten_toml)],
);

// When the crate has the same implicit resolver as the workspace it is not overridden
let f = File::open(&p.root().join("target/package/baz-0.1.0.crate")).unwrap();
let rewritten_toml = format!(
r#"{}
[package]
edition = "2015"
name = "baz"
version = "0.1.0"
"#,
cargo::core::package::MANIFEST_PREAMBLE
);
validate_crate_contents(
f,
"baz-0.1.0.crate",
&["Cargo.toml", "Cargo.toml.orig", "src/lib.rs"],
&[("Cargo.toml", &rewritten_toml)],
);
}
2 changes: 0 additions & 2 deletions tests/testsuite/publish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1178,7 +1178,6 @@ fn publish_git_with_version() {
authors = []\n\
description = \"foo\"\n\
license = \"MIT\"\n\
resolver = \"1\"\n\
\n\
[dependencies.dep1]\n\
version = \"1.0\"\n\
Expand Down Expand Up @@ -1285,7 +1284,6 @@ homepage = "foo"
documentation = "foo"
license = "MIT"
repository = "foo"
resolver = "1"

[dev-dependencies]
"#,
Expand Down
1 change: 0 additions & 1 deletion tests/testsuite/weak_dep_features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -608,7 +608,6 @@ version = "0.1.0"
description = "foo"
homepage = "https://example.com/"
license = "MIT"
resolver = "1"

[dependencies.bar]
version = "1.0"
Expand Down