Skip to content

Commit 6dd2227

Browse files
committed
Add multi-build script support to cargo publish
1 parent 0febd42 commit 6dd2227

File tree

3 files changed

+32
-27
lines changed

3 files changed

+32
-27
lines changed

src/cargo/util/toml/mod.rs

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2890,25 +2890,32 @@ fn prepare_toml_for_publish(
28902890

28912891
let mut package = me.package().unwrap().clone();
28922892
package.workspace = None;
2893-
// Validates if build script file exists. If not, warn and ignore.
2894-
if let Some(TomlPackageBuild::SingleScript(path)) = &package.build {
2895-
let path = Path::new(path).to_path_buf();
2896-
let included = packaged_files.map(|i| i.contains(&path)).unwrap_or(true);
2897-
let build = if included {
2898-
let path = path
2899-
.into_os_string()
2900-
.into_string()
2901-
.map_err(|_err| anyhow::format_err!("non-UTF8 `package.build`"))?;
2902-
let path = normalize_path_string_sep(path);
2903-
TomlPackageBuild::SingleScript(path)
2904-
} else {
2905-
ws.gctx().shell().warn(format!(
2906-
"ignoring `package.build` as `{}` is not included in the published package",
2907-
path.display()
2908-
))?;
2909-
TomlPackageBuild::Auto(false)
2910-
};
2911-
package.build = Some(build);
2893+
// Validates if build script file is included in package. If not, warn and ignore.
2894+
if let Some(custom_build_scripts) = package.normalized_build().expect("previously normalized") {
2895+
let mut included_scripts = Vec::new();
2896+
for script in custom_build_scripts {
2897+
let path = Path::new(script).to_path_buf();
2898+
let included = packaged_files.map(|i| i.contains(&path)).unwrap_or(true);
2899+
if included {
2900+
let path = path
2901+
.into_os_string()
2902+
.into_string()
2903+
.map_err(|_err| anyhow::format_err!("non-UTF8 `package.build`"))?;
2904+
let path = normalize_path_string_sep(path);
2905+
included_scripts.push(path);
2906+
} else {
2907+
ws.gctx().shell().warn(format!(
2908+
"ignoring `package.build` entry `{}` as it is not included in the published package",
2909+
path.display()
2910+
))?;
2911+
}
2912+
}
2913+
2914+
package.build = Some(match included_scripts.len() {
2915+
0 => TomlPackageBuild::Auto(false),
2916+
1 => TomlPackageBuild::SingleScript(included_scripts[0].clone()),
2917+
_ => TomlPackageBuild::MultipleScript(included_scripts),
2918+
});
29122919
}
29132920
let current_resolver = package
29142921
.resolver

tests/testsuite/build_scripts_multiple.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ fn verify_package_multiple_build_scripts() {
216216
.with_status(0)
217217
.with_stderr_data(str![[r#"
218218
[PACKAGING] foo v0.1.0 ([ROOT]/foo)
219+
[WARNING] ignoring `package.build` entry `build2.rs` as it is not included in the published package
219220
[PACKAGED] 5 files, [FILE_SIZE]B ([FILE_SIZE]B compressed)
220221
[VERIFYING] foo v0.1.0 ([ROOT]/foo)
221222
[COMPILING] foo v0.1.0 ([ROOT]/foo/target/package/foo-0.1.0)
@@ -256,10 +257,7 @@ edition = "2024"
256257
name = "foo"
257258
version = "0.1.0"
258259
authors = []
259-
build = [
260-
"build1.rs",
261-
"build2.rs",
262-
]
260+
build = "build1.rs"
263261
include = [
264262
"src/main.rs",
265263
"build1.rs",

tests/testsuite/package.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3856,7 +3856,7 @@ fn normalize_case() {
38563856
[WARNING] manifest has no documentation, homepage or repository.
38573857
See https://doc.rust-lang.org/cargo/reference/manifest.html#package-metadata for more info.
38583858
[PACKAGING] foo v0.0.1 ([ROOT]/foo)
3859-
[WARNING] ignoring `package.build` as `build.rs` is not included in the published package
3859+
[WARNING] ignoring `package.build` entry `build.rs` as it is not included in the published package
38603860
[WARNING] ignoring binary `foo` as `src/main.rs` is not included in the published package
38613861
[WARNING] ignoring example `ExampleFoo` as `examples/ExampleFoo.rs` is not included in the published package
38623862
[WARNING] ignoring test `ExplicitPath` as `tests/ExplicitPath.rs` is not included in the published package
@@ -3885,7 +3885,7 @@ src/lib.rs
38853885
[WARNING] manifest has no documentation, homepage or repository.
38863886
See https://doc.rust-lang.org/cargo/reference/manifest.html#package-metadata for more info.
38873887
[PACKAGING] foo v0.0.1 ([ROOT]/foo)
3888-
[WARNING] ignoring `package.build` as `build.rs` is not included in the published package
3888+
[WARNING] ignoring `package.build` entry `build.rs` as it is not included in the published package
38893889
[WARNING] ignoring binary `foo` as `src/main.rs` is not included in the published package
38903890
[WARNING] ignoring example `ExampleFoo` as `examples/ExampleFoo.rs` is not included in the published package
38913891
[WARNING] ignoring test `ExplicitPath` as `tests/ExplicitPath.rs` is not included in the published package
@@ -4486,7 +4486,7 @@ fn discovery_inferred_build_rs_excluded() {
44864486
.with_stdout_data("")
44874487
.with_stderr_data(str![[r#"
44884488
[PACKAGING] foo v0.0.1 ([ROOT]/foo)
4489-
[WARNING] ignoring `package.build` as `build.rs` is not included in the published package
4489+
[WARNING] ignoring `package.build` entry `build.rs` as it is not included in the published package
44904490
[PACKAGED] 4 files, [FILE_SIZE]B ([FILE_SIZE]B compressed)
44914491
[VERIFYING] foo v0.0.1 ([ROOT]/foo)
44924492
[COMPILING] foo v0.0.1 ([ROOT]/foo/target/package/foo-0.0.1)
@@ -4654,7 +4654,7 @@ fn discovery_explicit_build_rs_excluded() {
46544654
.with_stdout_data("")
46554655
.with_stderr_data(str![[r#"
46564656
[PACKAGING] foo v0.0.1 ([ROOT]/foo)
4657-
[WARNING] ignoring `package.build` as `build.rs` is not included in the published package
4657+
[WARNING] ignoring `package.build` entry `build.rs` as it is not included in the published package
46584658
[PACKAGED] 4 files, [FILE_SIZE]B ([FILE_SIZE]B compressed)
46594659
[VERIFYING] foo v0.0.1 ([ROOT]/foo)
46604660
[COMPILING] foo v0.0.1 ([ROOT]/foo/target/package/foo-0.0.1)

0 commit comments

Comments
 (0)