Skip to content

Part 7 of RFC2906 - Add support for inheriting exclude and include #10565

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

Merged
merged 1 commit into from
Apr 14, 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
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