build(router_env): obtain workspace member package names from cargo_metadata more deterministically (#4139)

Co-authored-by: Sampras Lopes <lsampras@pm.me>
This commit is contained in:
Sanchith Hegde
2024-03-20 16:35:28 +05:30
committed by GitHub
parent 3653c2c108
commit 8f7d9fbc3a
3 changed files with 15 additions and 25 deletions

View File

@ -14,17 +14,11 @@ pub fn set_cargo_workspace_members_env() {
let metadata = cargo_metadata::MetadataCommand::new()
.exec()
.expect("Failed to obtain cargo metadata");
let workspace_members = metadata.workspace_members;
let workspace_members = workspace_members
let workspace_members = metadata
.workspace_packages()
.iter()
.map(|package_id| {
package_id
.repr
.split_once(' ')
.expect("Unknown cargo metadata package ID format")
.0
})
.map(|package| package.name.as_str())
.collect::<Vec<_>>()
.join(",");
@ -35,7 +29,7 @@ pub fn set_cargo_workspace_members_env() {
.expect("Failed to set `CARGO_WORKSPACE_MEMBERS` environment variable");
}
/// Verify that the cargo metadata workspace members format matches that expected by
/// Verify that the cargo metadata workspace packages format matches that expected by
/// [`set_cargo_workspace_members_env`] to set the `CARGO_WORKSPACE_MEMBERS` environment variable.
///
/// This function should be typically called within build scripts, before the
@ -43,24 +37,20 @@ pub fn set_cargo_workspace_members_env() {
///
/// # Panics
///
/// Panics if running the `cargo metadata` command fails, or if the workspace members package ID
/// format cannot be determined.
/// Panics if running the `cargo metadata` command fails, or if the workspace member package names
/// cannot be determined.
pub fn verify_cargo_metadata_format() {
#[allow(clippy::expect_used)]
let metadata = cargo_metadata::MetadataCommand::new()
.exec()
.expect("Failed to obtain cargo metadata");
let workspace_members = metadata.workspace_members;
let package_id_entry_prefix =
format!("{} {}", env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION"));
assert!(
workspace_members
metadata
.workspace_packages()
.iter()
.any(|package_id| package_id.repr.starts_with(&package_id_entry_prefix)),
"Unknown workspace members package ID format. \
Please run `cargo metadata --format-version=1 | jq '.workspace_members'` and update this \
build script to match the updated package ID format."
.any(|package| package.name == env!("CARGO_PKG_NAME")),
"Unable to determine workspace member package names from `cargo metadata`"
);
}