Skip to content

Commit

Permalink
Auto merge of #10565 - Muscraft:rfc2906-part7, r=epage
Browse files Browse the repository at this point in the history
Part 7 of RFC2906 - Add support for inheriting `exclude` and `include`

Tracking issue: #8415
RFC: rust-lang/rfcs#2906

Part 1: #10497
Part 2: #10517
Part 3: #10538
Part 4: #10548
Part 5: #10563
Part 6: #10564

This PR focuses on adding support for inheriting `include` and `exclude`. While they were not in the original RFC it was decided that they should be added per [epage's comment](#8415 (comment)).
- Changed `include` and `exclude` into `Option<MaybeWorkspace<Vec<String>>>` inside `TomlProject`
- Added `include` and `exclude` to `InheritbaleFields`
- Updated tests to verify `include` and `exclude` are inheriting correctly

Remaining implementation work for the RFC
- `cargo-add` support, see [epage's comment](#8415 (comment))
- Optimizations, as needed
  • Loading branch information
bors committed Apr 14, 2022
2 parents dba5baf + 114f1e5 commit ec83f8d
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 4 deletions.
50 changes: 46 additions & 4 deletions src/cargo/util/toml/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1056,8 +1056,8 @@ pub struct TomlProject {
#[serde(rename = "forced-target")]
forced_target: Option<String>,
links: Option<String>,
exclude: Option<Vec<String>>,
include: Option<Vec<String>>,
exclude: Option<MaybeWorkspace<Vec<String>>>,
include: Option<MaybeWorkspace<Vec<String>>>,
publish: Option<MaybeWorkspace<VecStringOrBool>>,
workspace: Option<String>,
im_a_teapot: Option<bool>,
Expand Down Expand Up @@ -1123,6 +1123,8 @@ pub struct InheritableFields {
publish: Option<VecStringOrBool>,
edition: Option<String>,
badges: Option<BTreeMap<String, BTreeMap<String, String>>>,
exclude: Option<Vec<String>>,
include: Option<Vec<String>>,
#[serde(rename = "rust-version")]
rust_version: Option<String>,
// We use skip here since it will never be present when deserializing
Expand Down Expand Up @@ -1271,6 +1273,20 @@ impl InheritableFields {
)
}

pub fn exclude(&self) -> CargoResult<Vec<String>> {
self.exclude.clone().map_or(
Err(anyhow!("`workspace.package.exclude` was not defined")),
|d| Ok(d),
)
}

pub fn include(&self) -> CargoResult<Vec<String>> {
self.include.clone().map_or(
Err(anyhow!("`workspace.package.include` was not defined")),
|d| Ok(d),
)
}

pub fn ws_root(&self) -> &PathBuf {
&self.ws_root
}
Expand Down Expand Up @@ -1824,8 +1840,26 @@ impl TomlManifest {
}
}

let exclude = project.exclude.clone().unwrap_or_default();
let include = project.include.clone().unwrap_or_default();
let exclude = project
.exclude
.clone()
.map(|mw| {
mw.resolve(&features, "exclude", || {
get_ws(config, resolved_path.clone(), workspace_config.clone())?.exclude()
})
})
.transpose()?
.unwrap_or_default();
let include = project
.include
.clone()
.map(|mw| {
mw.resolve(&features, "include", || {
get_ws(config, resolved_path.clone(), workspace_config.clone())?.include()
})
})
.transpose()?
.unwrap_or_default();
let empty_features = BTreeMap::new();

let summary = Summary::new(
Expand Down Expand Up @@ -1992,6 +2026,14 @@ impl TomlManifest {
.as_ref()
.map(|_| MaybeWorkspace::Defined(metadata.categories.clone()));
project.rust_version = rust_version.clone().map(|rv| MaybeWorkspace::Defined(rv));
project.exclude = project
.exclude
.as_ref()
.map(|_| MaybeWorkspace::Defined(exclude.clone()));
project.include = project
.include
.as_ref()
.map(|_| MaybeWorkspace::Defined(include.clone()));

let profiles = me.profile.clone();
if let Some(profiles) = &profiles {
Expand Down
30 changes: 30 additions & 0 deletions tests/testsuite/inheritable_workspace_fields.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ fn permit_additional_workspace_fields() {
publish = false
edition = "2018"
rust-version = "1.60"
exclude = ["foo.txt"]
include = ["bar.txt", "**/*.rs", "Cargo.toml", "LICENSE", "README.md"]
[workspace.package.badges]
gitlab = { repository = "https://gitlab.com/rust-lang/rust", branch = "master" }
Expand Down Expand Up @@ -133,6 +135,8 @@ fn inherit_own_workspace_fields() {
publish = { workspace = true }
edition = { workspace = true }
rust-version = { workspace = true }
exclude = { workspace = true }
include = { workspace = true }
[workspace]
members = []
Expand All @@ -149,11 +153,15 @@ fn inherit_own_workspace_fields() {
publish = true
edition = "2018"
rust-version = "1.60"
exclude = ["foo.txt"]
include = ["bar.txt", "**/*.rs", "Cargo.toml"]
[workspace.package.badges]
gitlab = { repository = "https://gitlab.com/rust-lang/rust", branch = "master" }
"#,
)
.file("src/main.rs", "fn main() {}")
.file("foo.txt", "") // should be ignored when packaging
.file("bar.txt", "") // should be included when packaging
.build();

p.cargo("publish --token sekrit")
Expand Down Expand Up @@ -190,6 +198,7 @@ fn inherit_own_workspace_fields() {
"Cargo.toml.orig",
"src/main.rs",
".cargo_vcs_info.json",
"bar.txt",
],
&[(
"Cargo.toml",
Expand All @@ -203,6 +212,12 @@ rust-version = "1.60"
name = "foo"
version = "1.2.3"
authors = ["Rustaceans"]
exclude = ["foo.txt"]
include = [
"bar.txt",
"**/*.rs",
"Cargo.toml",
]
publish = true
description = "This is a crate"
homepage = "https://www.rust-lang.org"
Expand Down Expand Up @@ -598,6 +613,8 @@ fn inherit_workspace_fields() {
publish = true
edition = "2018"
rust-version = "1.60"
exclude = ["foo.txt"]
include = ["bar.txt", "**/*.rs", "Cargo.toml", "LICENSE", "README.md"]
[workspace.package.badges]
gitlab = { repository = "https://gitlab.com/rust-lang/rust", branch = "master" }
"#,
Expand Down Expand Up @@ -625,11 +642,15 @@ fn inherit_workspace_fields() {
publish = { workspace = true }
edition = { workspace = true }
rust-version = { workspace = true }
exclude = { workspace = true }
include = { workspace = true }
"#,
)
.file("LICENSE", "license")
.file("README.md", "README.md")
.file("bar/src/main.rs", "fn main() {}")
.file("bar/foo.txt", "") // should be ignored when packaging
.file("bar/bar.txt", "") // should be included when packaging
.build();

p.cargo("publish --token sekrit")
Expand Down Expand Up @@ -669,6 +690,7 @@ fn inherit_workspace_fields() {
"README.md",
"LICENSE",
".cargo_vcs_info.json",
"bar.txt",
],
&[(
"Cargo.toml",
Expand All @@ -682,6 +704,14 @@ rust-version = "1.60"
name = "bar"
version = "1.2.3"
authors = ["Rustaceans"]
exclude = ["foo.txt"]
include = [
"bar.txt",
"**/*.rs",
"Cargo.toml",
"LICENSE",
"README.md",
]
publish = true
description = "This is a crate"
homepage = "https://www.rust-lang.org"
Expand Down

0 comments on commit ec83f8d

Please sign in to comment.