Skip to content

Commit ec83f8d

Browse files
committed
Auto merge of #10565 - Muscraft:rfc2906-part7, r=epage
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
2 parents dba5baf + 114f1e5 commit ec83f8d

File tree

2 files changed

+76
-4
lines changed

2 files changed

+76
-4
lines changed

src/cargo/util/toml/mod.rs

+46-4
Original file line numberDiff line numberDiff line change
@@ -1056,8 +1056,8 @@ pub struct TomlProject {
10561056
#[serde(rename = "forced-target")]
10571057
forced_target: Option<String>,
10581058
links: Option<String>,
1059-
exclude: Option<Vec<String>>,
1060-
include: Option<Vec<String>>,
1059+
exclude: Option<MaybeWorkspace<Vec<String>>>,
1060+
include: Option<MaybeWorkspace<Vec<String>>>,
10611061
publish: Option<MaybeWorkspace<VecStringOrBool>>,
10621062
workspace: Option<String>,
10631063
im_a_teapot: Option<bool>,
@@ -1123,6 +1123,8 @@ pub struct InheritableFields {
11231123
publish: Option<VecStringOrBool>,
11241124
edition: Option<String>,
11251125
badges: Option<BTreeMap<String, BTreeMap<String, String>>>,
1126+
exclude: Option<Vec<String>>,
1127+
include: Option<Vec<String>>,
11261128
#[serde(rename = "rust-version")]
11271129
rust_version: Option<String>,
11281130
// We use skip here since it will never be present when deserializing
@@ -1271,6 +1273,20 @@ impl InheritableFields {
12711273
)
12721274
}
12731275

1276+
pub fn exclude(&self) -> CargoResult<Vec<String>> {
1277+
self.exclude.clone().map_or(
1278+
Err(anyhow!("`workspace.package.exclude` was not defined")),
1279+
|d| Ok(d),
1280+
)
1281+
}
1282+
1283+
pub fn include(&self) -> CargoResult<Vec<String>> {
1284+
self.include.clone().map_or(
1285+
Err(anyhow!("`workspace.package.include` was not defined")),
1286+
|d| Ok(d),
1287+
)
1288+
}
1289+
12741290
pub fn ws_root(&self) -> &PathBuf {
12751291
&self.ws_root
12761292
}
@@ -1824,8 +1840,26 @@ impl TomlManifest {
18241840
}
18251841
}
18261842

1827-
let exclude = project.exclude.clone().unwrap_or_default();
1828-
let include = project.include.clone().unwrap_or_default();
1843+
let exclude = project
1844+
.exclude
1845+
.clone()
1846+
.map(|mw| {
1847+
mw.resolve(&features, "exclude", || {
1848+
get_ws(config, resolved_path.clone(), workspace_config.clone())?.exclude()
1849+
})
1850+
})
1851+
.transpose()?
1852+
.unwrap_or_default();
1853+
let include = project
1854+
.include
1855+
.clone()
1856+
.map(|mw| {
1857+
mw.resolve(&features, "include", || {
1858+
get_ws(config, resolved_path.clone(), workspace_config.clone())?.include()
1859+
})
1860+
})
1861+
.transpose()?
1862+
.unwrap_or_default();
18291863
let empty_features = BTreeMap::new();
18301864

18311865
let summary = Summary::new(
@@ -1992,6 +2026,14 @@ impl TomlManifest {
19922026
.as_ref()
19932027
.map(|_| MaybeWorkspace::Defined(metadata.categories.clone()));
19942028
project.rust_version = rust_version.clone().map(|rv| MaybeWorkspace::Defined(rv));
2029+
project.exclude = project
2030+
.exclude
2031+
.as_ref()
2032+
.map(|_| MaybeWorkspace::Defined(exclude.clone()));
2033+
project.include = project
2034+
.include
2035+
.as_ref()
2036+
.map(|_| MaybeWorkspace::Defined(include.clone()));
19952037

19962038
let profiles = me.profile.clone();
19972039
if let Some(profiles) = &profiles {

tests/testsuite/inheritable_workspace_fields.rs

+30
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ fn permit_additional_workspace_fields() {
2727
publish = false
2828
edition = "2018"
2929
rust-version = "1.60"
30+
exclude = ["foo.txt"]
31+
include = ["bar.txt", "**/*.rs", "Cargo.toml", "LICENSE", "README.md"]
3032
3133
[workspace.package.badges]
3234
gitlab = { repository = "https://gitlab.com/rust-lang/rust", branch = "master" }
@@ -133,6 +135,8 @@ fn inherit_own_workspace_fields() {
133135
publish = { workspace = true }
134136
edition = { workspace = true }
135137
rust-version = { workspace = true }
138+
exclude = { workspace = true }
139+
include = { workspace = true }
136140
137141
[workspace]
138142
members = []
@@ -149,11 +153,15 @@ fn inherit_own_workspace_fields() {
149153
publish = true
150154
edition = "2018"
151155
rust-version = "1.60"
156+
exclude = ["foo.txt"]
157+
include = ["bar.txt", "**/*.rs", "Cargo.toml"]
152158
[workspace.package.badges]
153159
gitlab = { repository = "https://gitlab.com/rust-lang/rust", branch = "master" }
154160
"#,
155161
)
156162
.file("src/main.rs", "fn main() {}")
163+
.file("foo.txt", "") // should be ignored when packaging
164+
.file("bar.txt", "") // should be included when packaging
157165
.build();
158166

159167
p.cargo("publish --token sekrit")
@@ -190,6 +198,7 @@ fn inherit_own_workspace_fields() {
190198
"Cargo.toml.orig",
191199
"src/main.rs",
192200
".cargo_vcs_info.json",
201+
"bar.txt",
193202
],
194203
&[(
195204
"Cargo.toml",
@@ -203,6 +212,12 @@ rust-version = "1.60"
203212
name = "foo"
204213
version = "1.2.3"
205214
authors = ["Rustaceans"]
215+
exclude = ["foo.txt"]
216+
include = [
217+
"bar.txt",
218+
"**/*.rs",
219+
"Cargo.toml",
220+
]
206221
publish = true
207222
description = "This is a crate"
208223
homepage = "https://www.rust-lang.org"
@@ -598,6 +613,8 @@ fn inherit_workspace_fields() {
598613
publish = true
599614
edition = "2018"
600615
rust-version = "1.60"
616+
exclude = ["foo.txt"]
617+
include = ["bar.txt", "**/*.rs", "Cargo.toml", "LICENSE", "README.md"]
601618
[workspace.package.badges]
602619
gitlab = { repository = "https://gitlab.com/rust-lang/rust", branch = "master" }
603620
"#,
@@ -625,11 +642,15 @@ fn inherit_workspace_fields() {
625642
publish = { workspace = true }
626643
edition = { workspace = true }
627644
rust-version = { workspace = true }
645+
exclude = { workspace = true }
646+
include = { workspace = true }
628647
"#,
629648
)
630649
.file("LICENSE", "license")
631650
.file("README.md", "README.md")
632651
.file("bar/src/main.rs", "fn main() {}")
652+
.file("bar/foo.txt", "") // should be ignored when packaging
653+
.file("bar/bar.txt", "") // should be included when packaging
633654
.build();
634655

635656
p.cargo("publish --token sekrit")
@@ -669,6 +690,7 @@ fn inherit_workspace_fields() {
669690
"README.md",
670691
"LICENSE",
671692
".cargo_vcs_info.json",
693+
"bar.txt",
672694
],
673695
&[(
674696
"Cargo.toml",
@@ -682,6 +704,14 @@ rust-version = "1.60"
682704
name = "bar"
683705
version = "1.2.3"
684706
authors = ["Rustaceans"]
707+
exclude = ["foo.txt"]
708+
include = [
709+
"bar.txt",
710+
"**/*.rs",
711+
"Cargo.toml",
712+
"LICENSE",
713+
"README.md",
714+
]
685715
publish = true
686716
description = "This is a crate"
687717
homepage = "https://www.rust-lang.org"

0 commit comments

Comments
 (0)