From 5c7f68fe5da5c7190c2209e197e7255f44accd4f Mon Sep 17 00:00:00 2001 From: Naman Garg Date: Fri, 20 Jun 2025 03:17:46 +0530 Subject: [PATCH] Add multi-build script support to `cargo vendor` --- src/cargo/ops/vendor.rs | 44 +++++++++++++---------- tests/testsuite/build_scripts_multiple.rs | 6 ++-- 2 files changed, 27 insertions(+), 23 deletions(-) diff --git a/src/cargo/ops/vendor.rs b/src/cargo/ops/vendor.rs index fde6cee40..a0067e79c 100644 --- a/src/cargo/ops/vendor.rs +++ b/src/cargo/ops/vendor.rs @@ -514,25 +514,31 @@ fn prepare_toml_for_vendor( .package .as_mut() .expect("venedored manifests must have packages"); - // Validates if build script file exists. If not, warn and ignore. - if let Some(TomlPackageBuild::SingleScript(path)) = &package.build { - let path = paths::normalize_path(Path::new(path)); - let included = packaged_files.contains(&path); - let build = if included { - let path = path - .into_os_string() - .into_string() - .map_err(|_err| anyhow::format_err!("non-UTF8 `package.build`"))?; - let path = crate::util::toml::normalize_path_string_sep(path); - TomlPackageBuild::SingleScript(path) - } else { - gctx.shell().warn(format!( - "ignoring `package.build` as `{}` is not included in the published package", - path.display() - ))?; - TomlPackageBuild::Auto(false) - }; - package.build = Some(build); + // Validates if build script file is included in package. If not, warn and ignore. + if let Some(custom_build_scripts) = package.normalized_build().expect("previously normalized") { + let mut included_scripts = Vec::new(); + for script in custom_build_scripts { + let path = paths::normalize_path(Path::new(script)); + let included = packaged_files.contains(&path); + if included { + let path = path + .into_os_string() + .into_string() + .map_err(|_err| anyhow::format_err!("non-UTF8 `package.build`"))?; + let path = crate::util::toml::normalize_path_string_sep(path); + included_scripts.push(path); + } else { + gctx.shell().warn(format!( + "ignoring `package.build` entry `{}` as it is not included in the published package", + path.display() + ))?; + } + } + package.build = Some(match included_scripts.len() { + 0 => TomlPackageBuild::Auto(false), + 1 => TomlPackageBuild::SingleScript(included_scripts[0].clone()), + _ => TomlPackageBuild::MultipleScript(included_scripts), + }); } let lib = if let Some(target) = &me.lib { diff --git a/tests/testsuite/build_scripts_multiple.rs b/tests/testsuite/build_scripts_multiple.rs index fe116d85c..4f81b8f42 100644 --- a/tests/testsuite/build_scripts_multiple.rs +++ b/tests/testsuite/build_scripts_multiple.rs @@ -353,6 +353,7 @@ fn verify_vendor_multiple_build_scripts() { [UPDATING] git repository `[ROOTURL]/dep` [LOCKING] 1 package to latest [..] compatible version Vendoring dep v0.1.0 ([ROOTURL]/dep#[..]) ([ROOT]/home/.cargo/git/checkouts/dep-[HASH]/[..]) to vendor/dep +[WARNING] ignoring `package.build` entry `build2.rs` as it is not included in the published package To use vendored sources, add this to your .cargo/config.toml for this project: @@ -381,10 +382,7 @@ edition = "2024" name = "dep" version = "0.1.0" authors = [] -build = [ - "build1.rs", - "build2.rs", -] +build = "build1.rs" include = [ "src/main.rs", "build1.rs",